From 23690649087a0f21daaf74a9ffb9351c2eba162c Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sat, 17 Oct 2020 20:33:43 +0200 Subject: [PATCH] Make app configurable via SPACEAPI env variable --- README.md | 8 ++++- main.go | 94 +++++++++++++++++++++---------------------------------- 2 files changed, 43 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 6649087..2880a6e 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,10 @@ Simple project in Go retrieving opening status from [presence button API](https://wiki.fuz.re/doku.php?id=projets:fuz:presence_button) and serving JSON according to [SpaceAPI specs](https://spaceapi.io/docs/). -Read about it on [Fuz wiki](https://wiki.fuz.re/doku.php?id=projets:fuz:spaceapi). \ No newline at end of file +Read about it on [Fuz wiki](https://wiki.fuz.re/doku.php?id=projets:fuz:spaceapi). + +## Configuration + +The app gets its configuration from these environment variables: +* `PRESENCEAPI`: URL where the app should get the space opening status (e.g. `PRESENCEAPI=https://presence.fuz.re/api`) +* `SPACEAPI`: JSON string, static information to be served on the endpoint (e.g. `SPACEAPI='{"api":"0.13","space":"FUZ","logo":"https://fuz.re/WWW.FUZ.RE_fichiers/5c02b2a84373a.png","url":"https://fuz.re/","location":{"address":"11-15 rue dela RĂ©union, Paris 75020, FRANCE","lon":2.40308,"lat":48.85343},"contact":{"email":"","irc":"","ml":"fuz@fuz.re","twitter":"@fuz_re","matrix":"https://matrix.to/#/#fuz_general:matrix.fuz.re"},"issue_report_channels":["ml","twitter"],"state":{"icon":{"open":"https://presence.fuz.re/img","closed":"https://presence.fuz.re/img"},"message":"open under conditions: https://wiki.fuz.re/doku.php?id=map"},"projects":["https://wiki.fuz.re/doku.php?id=projets:fuz:start"]}'`) diff --git a/main.go b/main.go index 1ee3d07..a4f9fc2 100644 --- a/main.go +++ b/main.go @@ -13,40 +13,37 @@ import ( type Config struct { PORT string PRESENCEAPI string + SPACEAPI string } type SpaceAPI struct { - API string `json:"api"` - Space string `json:"space"` - Logo string `json:"logo"` - URL string `json:"url"` - Location Location `json:"location"` - Contact Contact `json:"contact"` + API string `json:"api"` + Space string `json:"space"` + Logo string `json:"logo"` + URL string `json:"url"` + Location struct { + Address string `json:"address,omitempty"` + Lon float64 `json:"lon"` + Lat float64 `json:"lat"` + } `json:"location"` + Contact struct { + Email string `json:"email,omitempty"` + Irc string `json:"irc,omitempty"` + Ml string `json:"ml,omitempty"` + Twitter string `json:"twitter,omitempty"` + Matrix string `json:"matrix,omitempty"` + } `json:"contact"` IssueReportChannels []string `json:"issue_report_channels"` - State State `json:"state"` - Projects []string `json:"projects"` -} -type Location struct { - Address string `json:"address"` - Lon float64 `json:"lon"` - Lat float64 `json:"lat"` -} -type Contact struct { - Email string `json:"email"` - IRC string `json:"irc"` - ML string `json:"ml"` - Twitter string `json:"twitter"` - Matrix string `json:"matrix"` -} -type Icon struct { - Open string `json:"open"` - Closed string `json:"closed"` -} -type State struct { - Icon Icon `json:"icon"` - Open bool `json:"open"` - Message string `json:"message"` - LastChange int64 `json:"lastchange"` + State struct { + Icon struct { + Open string `json:"open"` + Closed string `json:"closed"` + } `json:"icon,omitempty"` + Open bool `json:"open"` + Message string `json:"message,omitempty"` + LastChange int64 `json:"lastchange,omitempty"` + } `json:"state"` + Projects []string `json:"projects,omitempty"` } type Status struct { @@ -62,34 +59,7 @@ var ( PORT: "8080", } spaceAPI = SpaceAPI{ - API: "0.13", - Space: "FUZ", - Logo: "https://fuz.re/WWW.FUZ.RE_fichiers/5c02b2a84373a.png", - URL: "https://fuz.re/", - Location: Location{ - Address: "11-15 rue de la RĂ©union, Paris 75020, FRANCE", - Lat: 48.85343, - Lon: 2.40308, - }, - Contact: Contact{ - ML: "fuz@fuz.re", - Twitter: "@fuz_re", - Matrix: "https://matrix.to/#/#fuz_general:matrix.fuz.re", - }, - IssueReportChannels: []string{ - "ml", - "twitter", - }, - State: State{ - Icon: Icon{ - Open: "https://presence.fuz.re/img", - Closed: "https://presence.fuz.re/img", - }, - Message: "open under conditions: https://wiki.fuz.re/doku.php?id=map", - }, - Projects: []string{ - "https://wiki.fuz.re/doku.php?id=projets:fuz:start", - }, + API: "0.13", } ) @@ -102,6 +72,14 @@ func init() { if config.PRESENCEAPI == "" { panic("PRESENCEAPI is empty") } + config.SPACEAPI = os.Getenv("SPACEAPI") + if config.SPACEAPI == "" { + panic("SPACEAPI is empty") + } + err := json.Unmarshal([]byte(config.SPACEAPI), &spaceAPI) + if err != nil { + panic(fmt.Sprintf("SPACEAPI is not valid JSON: %v", err)) + } } func rootHandler(w http.ResponseWriter, r *http.Request) {