Compare commits

..

No commits in common. "74a8db7d2b1316ebba4fb6075cd7666ec8a547f1" and "b98174c7da4dd5734b60969464e26ad4e2d5be2f" have entirely different histories.

4 changed files with 19 additions and 10 deletions

View file

@ -38,15 +38,16 @@ curl -X PUT -u user:password -d true http://[::1]:8080/state/open
The same is true for the endpoints for sensors configured under `"dynamic"`. The same is true for the endpoints for sensors configured under `"dynamic"`.
Currently only the sensors with the `value/unit/location/name/description` schema are implemented. Currently only the sensors with the `value/unit/location/name/description` schema are implemented.
At the time of writing this includes `temperature`, `barometer`, `humidity`, `beverage_supply`, `power_consumption`, and `account_balance`. At the time of writing this includes `temperature`, `barometer`, `humidity`, `beverage_supply`, `power_consumption`,
Out-of-spec sensors may be used as well, as long as they share the same schema. and `account_balance`.
Out-of-spec sensors may also be used as long as they share the same schema.
```shell ```shell
curl -X PUT -u user:password -d 23.42 http://[::1]:8080/sensors/{temperature,humidity,...}[/location[/name]] curl -X PUT -u user:password -d 23.42 http://[::1]:8080/sensors/{temperature,humidity,...}[/location[/name]]
``` ```
As can be seen in the example, the http urls are generated from sensor type and optionally `location` and `name`. As can be seen in the example, the http urls are generated from sensor type and optionally `location` and `name`.
Depending on sensor type, `location` might be required for your sensors, see the schema for details. Depending on sensor type `location` might be required for your sensors, see the schema for details.
## Building ## Building

2
go.mod
View file

@ -1,3 +1,3 @@
module git.hamburg.ccc.de/ccchh/spaceapid module git.hamburg.ccc.de/ccchh/spaceapid
go 1.22 go 1.21

View file

@ -10,7 +10,8 @@ import (
"git.hamburg.ccc.de/ccchh/spaceapid/util" "git.hamburg.ccc.de/ccchh/spaceapid/util"
) )
// updateEndpointValidator checks BasicAuth credentials and then returns the request body // updateEndpointValidator checks BasicAuth credentials,
// checks for correct HTTP method and then returns the request body
func updateEndpointValidator( func updateEndpointValidator(
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID, authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
w http.ResponseWriter, r *http.Request, w http.ResponseWriter, r *http.Request,
@ -23,6 +24,13 @@ func updateEndpointValidator(
return []byte{}, errors.New(fmt.Sprintf("Unauthorized request from %s Username: %s Password: %s", r.RemoteAddr, username, password)) return []byte{}, errors.New(fmt.Sprintf("Unauthorized request from %s Username: %s Password: %s", r.RemoteAddr, username, password))
} }
// Check if PUT method
if r.Method != http.MethodPut {
w.Header().Set("Allow", http.MethodPut)
http.Error(w, "", http.StatusMethodNotAllowed)
return []byte{}, errors.New(fmt.Sprintf("Wrong Method: %s from %s at %s", r.Method, r.RemoteAddr, r.RequestURI))
}
// Read request body // Read request body
body, err := io.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {

10
main.go
View file

@ -51,20 +51,20 @@ func main() {
}(&conf.Response) }(&conf.Response)
// Register HTTP handlers // Register HTTP handlers
http.HandleFunc("GET /{$}", http.HandleFunc("/",
handlers.Root(&conf.Response), handlers.Root(&conf.Response),
) )
http.HandleFunc("PUT /state/open", http.HandleFunc("/state/open",
handlers.StateOpen(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response.State), handlers.StateOpen(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response.State),
) )
// Register handler for environmental sensors // Register handlers for Environmental Sensors
for sensorType, envSensorConfigs := range conf.Dynamic.Sensors { for sensorType, envSensorConfigs := range conf.Dynamic.Sensors {
for i, envSensorConfig := range envSensorConfigs { for i, envSensorConfig := range envSensorConfigs {
urlPattern := "PUT " + util.GetSensorURLPath( urlPath := util.GetSensorURLPath(
sensorType, envSensorConfig.SensorData.Location, envSensorConfig.SensorData.Name, sensorType, envSensorConfig.SensorData.Location, envSensorConfig.SensorData.Name,
) )
http.HandleFunc( http.HandleFunc(
urlPattern, urlPath,
handlers.EnvironmentSensor( handlers.EnvironmentSensor(
conf.Credentials, envSensorConfig.AllowedCredentials, &conf.Response.Sensors[sensorType][i], conf.Credentials, envSensorConfig.AllowedCredentials, &conf.Response.Sensors[sensorType][i],
), ),