2023-11-04 20:10:08 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-14 04:20:03 +01:00
|
|
|
"flag"
|
2023-11-04 20:10:08 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-11-10 03:24:12 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2023-11-04 20:10:08 +01:00
|
|
|
|
2024-01-31 17:11:15 +01:00
|
|
|
"git.hamburg.ccc.de/ccchh/spaceapid/config"
|
|
|
|
"git.hamburg.ccc.de/ccchh/spaceapid/handlers"
|
2024-01-31 18:25:34 +01:00
|
|
|
"git.hamburg.ccc.de/ccchh/spaceapid/persistence"
|
2024-01-31 17:11:15 +01:00
|
|
|
"git.hamburg.ccc.de/ccchh/spaceapid/types"
|
2024-02-13 19:42:37 +01:00
|
|
|
"git.hamburg.ccc.de/ccchh/spaceapid/util"
|
2023-11-04 20:10:08 +01:00
|
|
|
)
|
|
|
|
|
2024-01-14 04:20:03 +01:00
|
|
|
var (
|
|
|
|
flagHelp bool
|
2024-01-16 02:20:45 +01:00
|
|
|
version string
|
2024-01-14 04:20:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&flagHelp, "h", false, "Print help output")
|
|
|
|
}
|
|
|
|
|
2023-11-04 20:10:08 +01:00
|
|
|
func main() {
|
2024-01-16 02:20:45 +01:00
|
|
|
log.Println("SpaceAPId version", version)
|
|
|
|
|
2024-01-14 04:20:03 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if flagHelp {
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-01-13 23:26:04 +01:00
|
|
|
// Get spaceapid configuration
|
|
|
|
conf := config.ParseConfiguration()
|
2023-11-04 20:10:08 +01:00
|
|
|
|
2023-11-10 03:24:12 +01:00
|
|
|
// Merge old state if present
|
2024-01-31 18:25:34 +01:00
|
|
|
persistence.MergeOldState(&conf.Response)
|
2023-11-10 03:24:12 +01:00
|
|
|
|
2023-11-10 03:49:05 +01:00
|
|
|
// Register signal handler
|
|
|
|
sc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
|
2024-01-06 19:43:51 +01:00
|
|
|
go func(resp *types.SpaceAPIResponseV14) {
|
|
|
|
<-sc
|
2023-11-10 03:49:05 +01:00
|
|
|
log.Println("Saving state and shutting down...")
|
2024-01-31 18:25:34 +01:00
|
|
|
persistence.SaveCurrentState(*resp)
|
2023-11-10 03:49:05 +01:00
|
|
|
os.Exit(0)
|
2024-01-13 23:26:04 +01:00
|
|
|
}(&conf.Response)
|
2023-11-10 03:49:05 +01:00
|
|
|
|
2023-11-05 20:23:31 +01:00
|
|
|
// Register HTTP handlers
|
2024-08-03 17:55:28 +02:00
|
|
|
http.HandleFunc("GET /{$}",
|
2024-01-13 23:26:04 +01:00
|
|
|
handlers.Root(&conf.Response),
|
|
|
|
)
|
2024-08-03 17:55:28 +02:00
|
|
|
http.HandleFunc("PUT /state/open",
|
2024-01-14 01:53:36 +01:00
|
|
|
handlers.StateOpen(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response.State),
|
2024-01-13 23:26:04 +01:00
|
|
|
)
|
2024-08-03 17:55:28 +02:00
|
|
|
// Register handler for environmental sensors
|
2024-02-13 19:42:37 +01:00
|
|
|
for sensorType, envSensorConfigs := range conf.Dynamic.Sensors {
|
2024-01-14 01:04:01 +01:00
|
|
|
for i, envSensorConfig := range envSensorConfigs {
|
2024-08-03 17:55:28 +02:00
|
|
|
urlPattern := "PUT " + util.GetSensorURLPath(
|
2024-02-13 19:42:37 +01:00
|
|
|
sensorType, envSensorConfig.SensorData.Location, envSensorConfig.SensorData.Name,
|
|
|
|
)
|
|
|
|
http.HandleFunc(
|
2024-08-03 17:55:28 +02:00
|
|
|
urlPattern,
|
2024-01-14 01:04:01 +01:00
|
|
|
handlers.EnvironmentSensor(
|
2024-02-13 19:42:37 +01:00
|
|
|
conf.Credentials, envSensorConfig.AllowedCredentials, &conf.Response.Sensors[sensorType][i],
|
2024-01-14 01:04:01 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-11-04 20:10:08 +01:00
|
|
|
|
2023-11-10 03:24:12 +01:00
|
|
|
// Start webserver
|
2023-11-04 20:10:08 +01:00
|
|
|
log.Println("Starting HTTP server...")
|
2023-11-10 03:49:05 +01:00
|
|
|
log.Fatalln(http.ListenAndServe(":8080", nil))
|
2023-11-04 20:10:08 +01:00
|
|
|
}
|