9
0
mirror of https://github.com/Lomanic/fuz-spaceapi synced 2024-11-22 00:07:30 +00:00

Make app configurable via SPACEAPI env variable

This commit is contained in:
Lomanic 2020-10-17 20:33:43 +02:00
parent 7d44134f12
commit 2369064908
2 changed files with 43 additions and 59 deletions

View File

@ -3,3 +3,9 @@
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/). 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). 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"]}'`)

76
main.go
View File

@ -13,6 +13,7 @@ import (
type Config struct { type Config struct {
PORT string PORT string
PRESENCEAPI string PRESENCEAPI string
SPACEAPI string
} }
type SpaceAPI struct { type SpaceAPI struct {
@ -20,33 +21,29 @@ type SpaceAPI struct {
Space string `json:"space"` Space string `json:"space"`
Logo string `json:"logo"` Logo string `json:"logo"`
URL string `json:"url"` URL string `json:"url"`
Location Location `json:"location"` Location struct {
Contact Contact `json:"contact"` Address string `json:"address,omitempty"`
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"` Lon float64 `json:"lon"`
Lat float64 `json:"lat"` Lat float64 `json:"lat"`
} } `json:"location"`
type Contact struct { Contact struct {
Email string `json:"email"` Email string `json:"email,omitempty"`
IRC string `json:"irc"` Irc string `json:"irc,omitempty"`
ML string `json:"ml"` Ml string `json:"ml,omitempty"`
Twitter string `json:"twitter"` Twitter string `json:"twitter,omitempty"`
Matrix string `json:"matrix"` Matrix string `json:"matrix,omitempty"`
} } `json:"contact"`
type Icon struct { IssueReportChannels []string `json:"issue_report_channels"`
State struct {
Icon struct {
Open string `json:"open"` Open string `json:"open"`
Closed string `json:"closed"` Closed string `json:"closed"`
} } `json:"icon,omitempty"`
type State struct {
Icon Icon `json:"icon"`
Open bool `json:"open"` Open bool `json:"open"`
Message string `json:"message"` Message string `json:"message,omitempty"`
LastChange int64 `json:"lastchange"` LastChange int64 `json:"lastchange,omitempty"`
} `json:"state"`
Projects []string `json:"projects,omitempty"`
} }
type Status struct { type Status struct {
@ -63,33 +60,6 @@ var (
} }
spaceAPI = SpaceAPI{ spaceAPI = SpaceAPI{
API: "0.13", 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",
},
} }
) )
@ -102,6 +72,14 @@ func init() {
if config.PRESENCEAPI == "" { if config.PRESENCEAPI == "" {
panic("PRESENCEAPI is empty") 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) { func rootHandler(w http.ResponseWriter, r *http.Request) {