parent
17327c23a1
commit
cefb4a6bb0
@ -0,0 +1,82 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"flag" |
||||
"fmt" |
||||
"log" |
||||
"net/http" |
||||
"os" |
||||
|
||||
"git.hacknology.de/projekte/spaceapi.git" |
||||
) |
||||
|
||||
var ( |
||||
addr = ":8080" |
||||
statusFile = "./status.json" |
||||
) |
||||
|
||||
func main() { |
||||
log.SetFlags(0) |
||||
|
||||
flag.StringVar(&addr, "addr", addr, "Network address to listen on.") |
||||
flag.StringVar(&statusFile, "status-file", statusFile, "Status file to modify.") |
||||
flag.Parse() |
||||
|
||||
status, err := readCurrentState(statusFile) |
||||
if err != nil { |
||||
log.Fatalf("Error reading state: %s", err) |
||||
} |
||||
|
||||
http.Handle("/status.json", handleReadStatus(status)) |
||||
http.Handle("/status/state", handleSetState(status)) |
||||
http.Handle("/", http.RedirectHandler("/status.json", http.StatusFound)) |
||||
|
||||
log.Printf("Listening on %s...", addr) |
||||
log.Fatal(http.ListenAndServe(addr, nil)) |
||||
} |
||||
|
||||
func readCurrentState(fileName string) (*spaceapi.SpaceStatus, error) { |
||||
file, err := os.Open(fileName) |
||||
if err != nil { |
||||
return nil, fmt.Errorf("error opening file: %s", err) |
||||
} |
||||
defer file.Close() |
||||
|
||||
var status spaceapi.SpaceStatus |
||||
if err := json.NewDecoder(file).Decode(&status); err != nil { |
||||
return nil, fmt.Errorf("error decoding state: %s", err) |
||||
} |
||||
|
||||
return &status, nil |
||||
} |
||||
|
||||
func handleReadStatus(status *spaceapi.SpaceStatus) http.Handler { |
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
||||
if err := json.NewEncoder(w).Encode(status); err != nil { |
||||
http.Error(w, fmt.Sprintf("Error encoding JSON: %s", err), http.StatusInternalServerError) |
||||
} |
||||
}) |
||||
} |
||||
|
||||
func handleSetState(status *spaceapi.SpaceStatus) http.Handler { |
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
||||
if r.Method != http.MethodPost { |
||||
http.Error(w, fmt.Sprintf("Invalid method: %s", r.Method), http.StatusMethodNotAllowed) |
||||
return |
||||
} |
||||
defer r.Body.Close() |
||||
|
||||
var newState spaceapi.State |
||||
if err := json.NewDecoder(r.Body).Decode(&newState); err != nil { |
||||
http.Error(w, fmt.Sprintf("Error decoding body: %s", err), http.StatusBadRequest) |
||||
return |
||||
} |
||||
|
||||
status.State = newState |
||||
|
||||
if err := json.NewEncoder(w).Encode(status); err != nil { |
||||
http.Error(w, fmt.Sprintf("Error encoding JSON: %s", err), http.StatusInternalServerError) |
||||
} |
||||
}) |
||||
} |
@ -0,0 +1,37 @@ |
||||
package spaceapi |
||||
|
||||
type SpaceStatus struct { |
||||
APIVersion string `json:"api,omitempty"` |
||||
Space string `json:"space,omitempty"` |
||||
LogoURL string `json:"logo,omitempty"` |
||||
URL string `json:"url,omitempty"` |
||||
Location Location `json:"location"` |
||||
Contact Contact `json:"contact"` |
||||
IssueReportChannels []string `json:"issue_report_channels,omitempty"` |
||||
State State `json:"state"` |
||||
Feeds map[string]Feed `json:"feeds,omitempty"` |
||||
ProjectURLs []string `json:"projects,omitempty"` |
||||
} |
||||
|
||||
type Location struct { |
||||
Address string `json:"address,omitempty"` |
||||
Longitude float64 `json:"lon,omitempty"` |
||||
Latitude float64 `json:"lat,omitempty"` |
||||
} |
||||
|
||||
type Contact struct { |
||||
Jabber string `json:"jabber,omitempty"` |
||||
IRC string `json:"irc,omitempty"` |
||||
Email string `json:"email,omitempty"` |
||||
MailingList string `json:"ml,omitempty"` |
||||
IssueMail string `json:"issue_mail,omitempty"` |
||||
} |
||||
|
||||
type State struct { |
||||
Open *bool `json:"open"` |
||||
} |
||||
|
||||
type Feed struct { |
||||
Type string `json:"type,omitempty"` |
||||
URL string `json:"url,omitempty"` |
||||
} |
Loading…
Reference in new issue