From 6e98443bb9ee90ebcdb94ce1d80b6b1c51c81b49 Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Wed, 1 Mar 2023 15:56:54 +0100 Subject: [PATCH] finished project --- main.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ vite.config.js | 3 +++ 2 files changed, 53 insertions(+) diff --git a/main.go b/main.go index 62fdc3b..ccebd2a 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,10 @@ func main() { } func setupRoutes(mux *http.ServeMux) { + counter := 0 + + // 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 +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 mux.Handle("/", http.FileServer(http.Dir("./dist/"))) } diff --git a/vite.config.js b/vite.config.js index 98fba07..a309308 100644 --- a/vite.config.js +++ b/vite.config.js @@ -3,5 +3,8 @@ import { defineConfig } from 'vite' export default defineConfig({ server: { port: 3000, + proxy: { + '/api': 'http://localhost:4000/', + }, }, })