First implementation of new config format
- Functionally equivalent to old version at present - Temperature and Humidity sensors not handled yet - Moved HTTP handlers around
This commit is contained in:
parent
7bb676887f
commit
b2f62c7bb0
7 changed files with 125 additions and 121 deletions
|
@ -1,48 +0,0 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gitlab.hamburg.ccc.de/ccchh/spaceapid/config"
|
||||
)
|
||||
|
||||
const (
|
||||
envBAUsername = "BA_USERNAME"
|
||||
envBAPassword = "BA_PASSWORD"
|
||||
envJSONTemplatePath = "JSON_TEMPLATE_PATH"
|
||||
)
|
||||
|
||||
// GetConfiguration gets the spaceapid configuration from the respective environment variables
|
||||
func GetConfiguration() (c config.Configuration) {
|
||||
var (
|
||||
success bool
|
||||
err error
|
||||
)
|
||||
|
||||
// HTTP BasicAuth username
|
||||
c.BAUsername, success = os.LookupEnv(envBAUsername)
|
||||
if !success || c.BAUsername == "" {
|
||||
log.Fatalln("Could not retrieve env variable", envBAUsername, "or variable is empty")
|
||||
}
|
||||
|
||||
// HTTP BasicAuth password
|
||||
c.BAPassword, success = os.LookupEnv(envBAPassword)
|
||||
if !success || c.BAPassword == "" {
|
||||
log.Fatalln("Could not retrieve", envBAPassword, "env variable or variable is empty")
|
||||
}
|
||||
|
||||
// JSON template path
|
||||
templatePath, success := os.LookupEnv(envJSONTemplatePath)
|
||||
if !success || templatePath == "" {
|
||||
log.Fatalln("Could not retrieve", envJSONTemplatePath, "env variable or variable is empty")
|
||||
}
|
||||
// Save as absolute path
|
||||
c.TemplatePath, err = filepath.Abs(templatePath)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed converting", templatePath, "to absolute path:", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
20
util/credentials.go
Normal file
20
util/credentials.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"gitlab.hamburg.ccc.de/ccchh/spaceapid/config"
|
||||
)
|
||||
|
||||
// CheckCredentials validates whether a given username/password pair matches an
|
||||
// entry in the authDB whose id is present in the validCredentials list
|
||||
func CheckCredentials(
|
||||
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID, username, password string,
|
||||
) bool {
|
||||
for _, id := range validCredentials {
|
||||
if cred, present := authDB[id]; present {
|
||||
if cred.Username == username && cred.Password == password {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
30
util/util.go
30
util/util.go
|
@ -1,42 +1,18 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"slices"
|
||||
|
||||
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
|
||||
)
|
||||
|
||||
const savedStateJSONPath = "/var/lib/spaceapid/spaceapid-state.json"
|
||||
|
||||
// ParseTemplate parses the given file and
|
||||
func ParseTemplate(file string) (resp types.SpaceAPIResponseV14) {
|
||||
// Read template file
|
||||
template, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed reading file:", err)
|
||||
}
|
||||
|
||||
// Parse JSON
|
||||
dec := json.NewDecoder(bytes.NewReader(template))
|
||||
dec.DisallowUnknownFields()
|
||||
err = dec.Decode(&resp)
|
||||
if err != nil {
|
||||
log.Fatalln("Could not parse SpaceAPI response template:", err)
|
||||
}
|
||||
|
||||
// Check if compatible with v14
|
||||
if !slices.Contains(resp.APICompatibility, "14") {
|
||||
log.Fatalln("Provided template doesn't specify compatibility with API version 14")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
const (
|
||||
savedStateJSONPath = "/var/lib/spaceapid/spaceapid-state.json"
|
||||
)
|
||||
|
||||
// MergeOldState merges a given SpaceAPIResponse with the state saved at the time of last program exit and then deletes
|
||||
// the file containing the old state.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue