2023-11-04 20:10:08 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-11-10 03:24:12 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2023-11-04 20:10:08 +01:00
|
|
|
|
2023-11-05 20:23:31 +01:00
|
|
|
"gitlab.hamburg.ccc.de/ccchh/spaceapid/handlers"
|
2023-11-10 03:49:05 +01:00
|
|
|
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
|
2023-11-05 20:23:31 +01:00
|
|
|
"gitlab.hamburg.ccc.de/ccchh/spaceapid/util"
|
2023-11-04 20:10:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-11-05 20:23:31 +01:00
|
|
|
log.Println("Reading configuration values")
|
|
|
|
config := util.GetConfiguration()
|
2023-11-04 23:30:26 +01:00
|
|
|
|
2023-11-05 20:23:31 +01:00
|
|
|
log.Println("Reading initial SpaceAPI response from", config.TemplatePath)
|
|
|
|
spaceApiResponse := util.ParseTemplate(config.TemplatePath)
|
2023-11-04 20:10:08 +01:00
|
|
|
|
2023-11-10 03:24:12 +01:00
|
|
|
// Merge old state if present
|
|
|
|
util.MergeOldState(&spaceApiResponse)
|
|
|
|
|
2023-11-10 03:49:05 +01:00
|
|
|
// Register signal handler
|
|
|
|
sc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
go func(ch chan os.Signal, resp *types.SpaceAPIResponseV14) {
|
|
|
|
<-ch
|
|
|
|
log.Println("Saving state and shutting down...")
|
|
|
|
util.SaveCurrentState(*resp)
|
|
|
|
os.Exit(0)
|
|
|
|
}(sc, &spaceApiResponse)
|
|
|
|
|
2023-11-05 20:23:31 +01:00
|
|
|
// Register HTTP handlers
|
|
|
|
http.HandleFunc("/", handlers.Root(&spaceApiResponse))
|
|
|
|
http.HandleFunc("/state/open", handlers.StateOpen(config.BAUsername, config.BAPassword, &spaceApiResponse))
|
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
|
|
|
}
|