Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
362d958a34 | ||
|
|
6e98443bb9 |
@@ -1,2 +1,4 @@
|
||||
dist/
|
||||
node_modules/
|
||||
|
||||
*.local*
|
||||
|
||||
@@ -20,7 +20,68 @@ func main() {
|
||||
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)
|
||||
@@ -36,6 +97,63 @@ func setupRoutes(mux *http.ServeMux) {
|
||||
}
|
||||
})
|
||||
|
||||
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/")))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
)
|
||||
|
||||
func readOrCreateJson(filename string, value any) error {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return writeJson(filename, value)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if json.NewDecoder(f).Decode(value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeJson(filename string, value any) error {
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
enc := json.NewEncoder(f)
|
||||
enc.SetIndent("", " ")
|
||||
if err := enc.Encode(value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -3,5 +3,8 @@ import { defineConfig } from 'vite'
|
||||
export default defineConfig({
|
||||
server: {
|
||||
port: 3000,
|
||||
proxy: {
|
||||
'/api': 'http://localhost:4000/',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user