You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
3.3 KiB
Go
160 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func main() {
|
|
mux := http.NewServeMux()
|
|
|
|
setupRoutes(mux)
|
|
|
|
server := http.Server{
|
|
Addr: ":4000",
|
|
Handler: mux,
|
|
}
|
|
|
|
log.Printf("Starting server on port 4000...")
|
|
log.Fatal(server.ListenAndServe())
|
|
}
|
|
|
|
type Store interface {
|
|
Count() (int, error)
|
|
Increment() (int, error)
|
|
Decrement() (int, error)
|
|
}
|
|
|
|
const StoreFilename = "store.local.json"
|
|
|
|
type fileStore struct {
|
|
Counter int `json:"counter"`
|
|
}
|
|
|
|
func (db *fileStore) Count() (int, error) {
|
|
if err := readOrCreateJson(StoreFilename, db); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return db.Counter, nil
|
|
}
|
|
|
|
func (db *fileStore) Increment() (int, error) {
|
|
if err := readOrCreateJson(StoreFilename, db); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
db.Counter++
|
|
|
|
if err := writeJson(StoreFilename, db); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return db.Counter, nil
|
|
}
|
|
|
|
func (db *fileStore) Decrement() (int, error) {
|
|
if err := readOrCreateJson(StoreFilename, db); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
db.Counter--
|
|
|
|
if err := writeJson(StoreFilename, db); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return db.Counter, nil
|
|
}
|
|
|
|
func setupRoutes(mux *http.ServeMux) {
|
|
var db Store = &fileStore{Counter: 0}
|
|
{
|
|
initialCount, err := db.Count()
|
|
if err != nil {
|
|
log.Fatalf(`Unable to create store: %v`, err)
|
|
return
|
|
}
|
|
|
|
log.Printf(`Store correctly initialized, count: %v`, initialCount)
|
|
}
|
|
|
|
// API Routes
|
|
|
|
mux.HandleFunc("/api/status", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
err := json.NewEncoder(w).Encode("ok")
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
|
|
mux.HandleFunc("/api/value", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
count, err := db.Count()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(count); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
|
|
mux.HandleFunc("/api/increment", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
newCount, err := db.Increment()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(newCount); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
|
|
mux.HandleFunc("/api/decrement", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
newCount, err := db.Decrement()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(newCount); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
|
|
// Static Files
|
|
mux.Handle("/", http.FileServer(http.Dir("./dist/")))
|
|
}
|