finished project

demo-finished
Antonio De Lucreziis 2 years ago
parent f6575b32b1
commit 6e98443bb9

@ -21,6 +21,10 @@ func main() {
} }
func setupRoutes(mux *http.ServeMux) { func setupRoutes(mux *http.ServeMux) {
counter := 0
// API Routes
mux.HandleFunc("/api/status", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/status", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet { if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
@ -36,6 +40,52 @@ 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
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(counter)
if 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
}
counter++
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(counter)
if 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
}
counter--
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(counter)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
// Static Files // Static Files
mux.Handle("/", http.FileServer(http.Dir("./dist/"))) mux.Handle("/", http.FileServer(http.Dir("./dist/")))
} }

@ -3,5 +3,8 @@ import { defineConfig } from 'vite'
export default defineConfig({ export default defineConfig({
server: { server: {
port: 3000, port: 3000,
proxy: {
'/api': 'http://localhost:4000/',
},
}, },
}) })

Loading…
Cancel
Save