spaceapid/main.go
2024-01-06 19:43:51 +01:00

43 lines
1.1 KiB
Go

package main
import (
"log"
"net/http"
"os"
"os/signal"
"syscall"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/handlers"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/util"
)
func main() {
log.Println("Reading configuration values")
config := util.GetConfiguration()
log.Println("Reading initial SpaceAPI response from", config.TemplatePath)
spaceApiResponse := util.ParseTemplate(config.TemplatePath)
// Merge old state if present
util.MergeOldState(&spaceApiResponse)
// Register signal handler
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
go func(resp *types.SpaceAPIResponseV14) {
<-sc
log.Println("Saving state and shutting down...")
util.SaveCurrentState(*resp)
os.Exit(0)
}(&spaceApiResponse)
// Register HTTP handlers
http.HandleFunc("/", handlers.Root(&spaceApiResponse))
http.HandleFunc("/state/open", handlers.StateOpen(config.BAUsername, config.BAPassword, &spaceApiResponse))
// Start webserver
log.Println("Starting HTTP server...")
log.Fatalln(http.ListenAndServe(":8080", nil))
}