Abstract storage.

change-name
Robert Jacob 5 years ago
parent 8e99d53687
commit 02646057cc
  1. 38
      cmd/spaceapi-server/main.go
  2. 2
      spaceapi.go
  3. 53
      storage.go

@ -6,9 +6,8 @@ import (
"fmt"
"log"
"net/http"
"os"
"git.hacknology.de/projekte/spaceapi.git"
"git.hacknology.de/projekte/spaceapi"
)
var (
@ -23,43 +22,30 @@ func main() {
flag.StringVar(&statusFile, "status-file", statusFile, "Status file to modify.")
flag.Parse()
status, err := readCurrentState(statusFile)
storage, err := spaceapi.Open(statusFile, false)
if err != nil {
log.Fatalf("Error reading state: %s", err)
log.Fatalf("Error opening data file: %s", err)
}
http.Handle("/spaceapi/status.json", handleReadStatus(status))
http.Handle("/spaceapi/status/state", handleSetState(status))
http.Handle("/spaceapi/status.json", handleReadStatus(storage.Status))
http.Handle("/spaceapi/status/state", handleSetState(storage))
http.Handle("/", http.RedirectHandler("/spaceapi/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 {
func handleReadStatus(statusFunc func() spaceapi.SpaceStatus) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status := statusFunc()
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 {
func handleSetState(storage *spaceapi.Storage) 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)
@ -73,7 +59,11 @@ func handleSetState(status *spaceapi.SpaceStatus) http.Handler {
return
}
status.State = newState
storage.Modify(func(status *spaceapi.SpaceStatus) {
status.State = newState
})
status := storage.Status()
if err := json.NewEncoder(w).Encode(status); err != nil {
http.Error(w, fmt.Sprintf("Error encoding JSON: %s", err), http.StatusInternalServerError)

@ -1,4 +1,4 @@
package spaceapi
package spaceapi // import "git.hacknology.de/projekte/spaceapi"
type SpaceStatus struct {
APIVersion string `json:"api,omitempty"`

@ -0,0 +1,53 @@
package spaceapi
import (
"encoding/json"
"fmt"
"os"
)
type Listener func(status SpaceStatus)
type Storage struct {
filePath string
persistent bool
status SpaceStatus
}
func Open(filePath string, persistent bool) (*Storage, error) {
status, err := readFile(filePath)
if err != nil {
return nil, fmt.Errorf("error reading status: %s", err)
}
return &Storage{
filePath: filePath,
persistent: persistent,
status: *status,
}, nil
}
func (s *Storage) Status() SpaceStatus {
return s.status
}
func (s *Storage) Modify(modifiers ...func(*SpaceStatus)) {
for _, m := range modifiers {
m(&s.status)
}
}
func readFile(fileName string) (*SpaceStatus, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("error opening file: %s", err)
}
defer file.Close()
var status SpaceStatus
if err := json.NewDecoder(file).Decode(&status); err != nil {
return nil, fmt.Errorf("error decoding state: %s", err)
}
return &status, nil
}
Loading…
Cancel
Save