43 lines
1.1 KiB
Go
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(ch chan os.Signal, resp *types.SpaceAPIResponseV14) {
|
|
<-ch
|
|
log.Println("Saving state and shutting down...")
|
|
util.SaveCurrentState(*resp)
|
|
os.Exit(0)
|
|
}(sc, &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))
|
|
}
|