diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b4079a6 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module goweb + +go 1.22 diff --git a/main.go b/main.go new file mode 100644 index 0000000..34cf6ef --- /dev/null +++ b/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + "os" + "time" +) + +func main() { + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]interface{}{ + "status": "online", + "app": "Cosmos Orbit — Go Lightweight Web Server", + "timestamp": time.Now().Format(time.RFC3339), + }) + }) + + http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("OK")) + }) + + log.Printf("Go server listening on port %s", port) + if err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil); err != nil { + log.Fatal(err) + } +}