-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (68 loc) · 1.57 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/google/uuid"
)
type writerWrapper struct {
http.ResponseWriter
statusCode int
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "80"
}
addr := fmt.Sprintf("localhost:%s", port)
server := newServer(addr, middleware(setupRoute()))
fmt.Printf("Listening on http://%s\n", addr)
err := server.ListenAndServe()
if err != nil {
log.Fatalln("Error starting server:", err)
return
}
}
func newServer(addr string, handler http.Handler) *http.Server {
return &http.Server{
Addr: addr,
Handler: handler,
}
}
func setupRoute() *http.ServeMux {
handler := http.NewServeMux()
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := map[any]any{
"Request ID": uuid.New().String(),
"Timestamp": time.Now(),
}
err := sendJSON(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
return handler
}
func sendJSON(w http.ResponseWriter, data map[any]any) error {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(data)
if err != nil {
return err
}
return nil
}
func (w writerWrapper) WriteHeader(code int) {
w.ResponseWriter.WriteHeader(code)
w.statusCode = code
}
func middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wrapped := writerWrapper{w, http.StatusOK}
log.Printf(fmt.Sprintf("%s %s %v \n", r.Method, r.RequestURI, wrapped.statusCode))
next.ServeHTTP(wrapped, r)
})
}