mirror of
https://github.com/idanoo/go-http-log
synced 2025-07-01 22:02:16 +00:00
Initial Commit
This commit is contained in:
parent
ddd219c723
commit
32e5408498
1 changed files with 44 additions and 0 deletions
44
main.go
Normal file
44
main.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
// main
|
||||
func main() {
|
||||
// Init logged
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
|
||||
slog.SetDefault(logger)
|
||||
|
||||
// Check for port override
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
slog.Info("Starting server", "port", port)
|
||||
|
||||
// Start HTTP server
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
slog.Error("Error reading request body", "error", err)
|
||||
body = []byte("")
|
||||
}
|
||||
|
||||
slog.Info("Request",
|
||||
"method", r.Method,
|
||||
"path", r.URL.Path,
|
||||
"query", r.URL.RawQuery,
|
||||
"headers", r.Header,
|
||||
"body", string(body),
|
||||
)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
http.ListenAndServe(":"+port, mux)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue