From f6575b32b13546fe91f61f1ae7f00dde9961638a Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Wed, 1 Mar 2023 18:03:17 +0100 Subject: [PATCH] first go demo with only /api/status --- main.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..62fdc3b --- /dev/null +++ b/main.go @@ -0,0 +1,41 @@ +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()) +} + +func setupRoutes(mux *http.ServeMux) { + 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 + } + }) + + // Static Files + mux.Handle("/", http.FileServer(http.Dir("./dist/"))) +}