mirror of
https://github.com/shouptech/tempgopher.git
synced 2026-02-03 08:39:43 +00:00
Add auth to /api
This commit is contained in:
parent
c817f27eb2
commit
89657c381b
6 changed files with 31 additions and 1 deletions
|
|
@ -36,9 +36,16 @@ type Sensor struct {
|
||||||
Verbose bool `json:"verbose" yaml:"verbose"`
|
Verbose bool `json:"verbose" yaml:"verbose"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// User defines a user's configuration
|
||||||
|
type User struct {
|
||||||
|
Name string `json:"name" yaml:"name"`
|
||||||
|
Password string `json:"password" yaml:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
// Config contains the applications configuration
|
// Config contains the applications configuration
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Sensors []Sensor `yaml:"sensors"`
|
Sensors []Sensor `yaml:"sensors"`
|
||||||
|
Users []User `yaml:"users"`
|
||||||
BaseURL string `yaml:"baseurl"`
|
BaseURL string `yaml:"baseurl"`
|
||||||
ListenAddr string `yaml:"listenaddr"`
|
ListenAddr string `yaml:"listenaddr"`
|
||||||
DisplayFahrenheit bool `yaml:"displayfahrenheit"`
|
DisplayFahrenheit bool `yaml:"displayfahrenheit"`
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,11 @@ import (
|
||||||
|
|
||||||
func Test_LoadConfig(t *testing.T) {
|
func Test_LoadConfig(t *testing.T) {
|
||||||
|
|
||||||
|
testUser := User{
|
||||||
|
Name: "foo",
|
||||||
|
Password: "bar",
|
||||||
|
}
|
||||||
|
|
||||||
testConfig := Config{
|
testConfig := Config{
|
||||||
Sensors: []Sensor{
|
Sensors: []Sensor{
|
||||||
Sensor{
|
Sensor{
|
||||||
|
|
@ -24,6 +29,7 @@ func Test_LoadConfig(t *testing.T) {
|
||||||
Verbose: true,
|
Verbose: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Users: []User{testUser},
|
||||||
BaseURL: "https://foo.bar",
|
BaseURL: "https://foo.bar",
|
||||||
ListenAddr: "127.0.0.1:8080",
|
ListenAddr: "127.0.0.1:8080",
|
||||||
DisplayFahrenheit: true,
|
DisplayFahrenheit: true,
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@ sensors:
|
||||||
coolinvert: false
|
coolinvert: false
|
||||||
coolminutes: 10
|
coolminutes: 10
|
||||||
verbose: true
|
verbose: true
|
||||||
|
users:
|
||||||
|
- foo: bar
|
||||||
baseurl: https://foo.bar
|
baseurl: https://foo.bar
|
||||||
listenaddr: 127.0.0.1:8080
|
listenaddr: 127.0.0.1:8080
|
||||||
displayfahrenheit: true
|
displayfahrenheit: true
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@ sensors:
|
||||||
coolinvert: false
|
coolinvert: false
|
||||||
coolminutes: 10
|
coolminutes: 10
|
||||||
verbose: true
|
verbose: true
|
||||||
|
users:
|
||||||
|
- foo: bar
|
||||||
baseurl: https://foo.bar
|
baseurl: https://foo.bar
|
||||||
listenaddr: 127.0.0.1:8080
|
listenaddr: 127.0.0.1:8080
|
||||||
displayfahrenheit: true
|
displayfahrenheit: true
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,9 @@ sensors:
|
||||||
coolinvert: false
|
coolinvert: false
|
||||||
coolminutes: 10
|
coolminutes: 10
|
||||||
verbose: true
|
verbose: true
|
||||||
|
users:
|
||||||
|
- name: foo
|
||||||
|
password: bar
|
||||||
baseurl: https://foo.bar
|
baseurl: https://foo.bar
|
||||||
listenaddr: 127.0.0.1:8080
|
listenaddr: 127.0.0.1:8080
|
||||||
displayfahrenheit: true
|
displayfahrenheit: true
|
||||||
|
|
|
||||||
12
web.go
12
web.go
|
|
@ -40,6 +40,7 @@ func ConfigHandler(config *Config) gin.HandlerFunc {
|
||||||
} else if c.Param("alias") == "/" {
|
} else if c.Param("alias") == "/" {
|
||||||
c.JSON(http.StatusOK, config.Sensors)
|
c.JSON(http.StatusOK, config.Sensors)
|
||||||
} else {
|
} else {
|
||||||
|
config.Users = nil // Never return the users in GET requests
|
||||||
c.JSON(http.StatusOK, config)
|
c.JSON(http.StatusOK, config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -125,7 +126,7 @@ func SetupRouter(config *Config, states *map[string]State) *gin.Engine {
|
||||||
r.GET("/ping", PingHandler)
|
r.GET("/ping", PingHandler)
|
||||||
|
|
||||||
// API Endpoints
|
// API Endpoints
|
||||||
api := r.Group("/api")
|
api := r.Group("/api", gin.BasicAuth(GetGinAccounts(config)))
|
||||||
{
|
{
|
||||||
api.GET("/status", StatusHandler(states))
|
api.GET("/status", StatusHandler(states))
|
||||||
api.GET("/status/*alias", StatusHandler(states))
|
api.GET("/status/*alias", StatusHandler(states))
|
||||||
|
|
@ -159,6 +160,15 @@ func reloadWebConfig(c *Config, p string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetGinAccounts returns a gin.Accounts struct with values pulled from a Config struct
|
||||||
|
func GetGinAccounts(config *Config) gin.Accounts {
|
||||||
|
var a gin.Accounts
|
||||||
|
for _, user := range config.Users {
|
||||||
|
a[user.Name] = user.Password
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
// RunWeb launches a web server. sc is used to update the states from the Thermostats.
|
// RunWeb launches a web server. sc is used to update the states from the Thermostats.
|
||||||
func RunWeb(configpath string, sc <-chan State, wg *sync.WaitGroup) {
|
func RunWeb(configpath string, sc <-chan State, wg *sync.WaitGroup) {
|
||||||
// Update sensor states when a new state comes back from the thermostat.
|
// Update sensor states when a new state comes back from the thermostat.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue