diff --git a/go.mod b/go.mod index 0a5ec84..ef6003c 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/digitalocean/sample-golang -go 1.13 +go 1.22.1 -require github.com/gofrs/uuid v3.3.0+incompatible // indirect +require github.com/gofrs/uuid v4.4.0+incompatible diff --git a/go.sum b/go.sum index 4b1328f..c0ad687 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,2 @@ -github.com/gofrs/uuid v1.2.0 h1:coDhrjgyJaglxSjxuJdqQSSdUpG3w6p1OwN2od6frBU= -github.com/gofrs/uuid v3.3.0+incompatible h1:8K4tyRfvU1CYPgJsveYFQMhpFd/wXNM7iK6rR7UHz84= -github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= +github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= diff --git a/main.go b/main.go index fc3cb3d..3a85353 100644 --- a/main.go +++ b/main.go @@ -2,45 +2,32 @@ package main import ( "fmt" + "log/slog" "net/http" "os" "strconv" "strings" + "time" "github.com/gofrs/uuid" ) -const startupMessage = `                                                                                 -                                                   �                             -                                                                                 -                                                                                 -                                               ��������                          -                                              �������          �                 -  ��        �� ��   ���  ��                  �������          � ����             -  �          ����       ����          ����          �         �  ���             -  �      �    ��    ���               ����                     �����             -                     �         �����������            �           ���������      -                                            �����   ����        ����������       -                                              �      �������                     -             � �  �                                 �����������       �          -                  �  �        ��  ��  �         � ����������������   ��          -                           ��� �     �� �           �����������������            -` - -func logRequest(r *http.Request) { +func logRequest(r *http.Request, log *slog.Logger) { uri := r.RequestURI method := r.Method - fmt.Println("Got request!", method, uri) + log.Info("Received Request!", "method", method, "uri", uri) } func main() { + + log := slog.New(slog.NewJSONHandler(os.Stdout, nil)) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - logRequest(r) - fmt.Fprintf(w, "Hello! you've requested %s\n", r.URL.Path) + fmt.Fprintf(w, "received request at path %s with method %s", r.URL.Path, r.Method) + logRequest(r, log) }) http.HandleFunc("/cached", func(w http.ResponseWriter, r *http.Request) { - logRequest(r) + logRequest(r, log) maxAgeParams, ok := r.URL.Query()["max-age"] if ok && len(maxAgeParams) > 0 { maxAge, _ := strconv.Atoi(maxAgeParams[0]) @@ -63,13 +50,13 @@ func main() { }) http.HandleFunc("/headers", func(w http.ResponseWriter, r *http.Request) { - logRequest(r) + logRequest(r, log) keys, ok := r.URL.Query()["key"] if ok && len(keys) > 0 { fmt.Fprint(w, r.Header.Get(keys[0])) return } - headers := []string{} + var headers []string headers = append(headers, fmt.Sprintf("host=%s", r.Host)) for key, values := range r.Header { headers = append(headers, fmt.Sprintf("%s=%s", key, strings.Join(values, ","))) @@ -78,19 +65,19 @@ func main() { }) http.HandleFunc("/env", func(w http.ResponseWriter, r *http.Request) { - logRequest(r) + logRequest(r, log) keys, ok := r.URL.Query()["key"] if ok && len(keys) > 0 { fmt.Fprint(w, os.Getenv(keys[0])) return } - envs := []string{} + var envs []string envs = append(envs, os.Environ()...) fmt.Fprint(w, strings.Join(envs, "\n")) }) http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { - logRequest(r) + logRequest(r, log) codeParams, ok := r.URL.Query()["code"] if ok && len(codeParams) > 0 { statusCode, _ := strconv.Atoi(codeParams[0]) @@ -118,15 +105,7 @@ func main() { }) } - bindAddr := fmt.Sprintf(":%s", port) - lines := strings.Split(startupMessage, "\n") - fmt.Println() - for _, line := range lines { - fmt.Println(line) - } - fmt.Println() - fmt.Printf("==> Server listening at %s 🚀\n", bindAddr) - + time.Sleep(60 * time.Second) err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil) if err != nil { panic(err)