diff --git a/logtest_api.go b/logtest_api.go new file mode 100644 index 0000000..e50c7e7 --- /dev/null +++ b/logtest_api.go @@ -0,0 +1,33 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" + + slog "github.com/sinmetal/slog/v2" +) + +func HandleLogTestAPI(w http.ResponseWriter, r *http.Request) { + ctx := slog.WithValue(r.Context()) + defer slog.Flush(ctx) + + for k, v := range r.Header { + slog.Info(ctx, slog.KV{k, v}) + } + + lc, ok := slog.Value(ctx) + if !ok { + slog.Info(ctx, slog.KV{"MSG", "slog.Value is ng"}) + } + j, err := json.Marshal(lc) + if err != nil { + slog.Info(ctx, slog.KV{"MSG", fmt.Sprintf("failed json.Marshal", err)}) + } + + w.Header().Set("Content-type", "application/json;charset=utf-8") + _, err = w.Write(j) + if err != nil { + slog.Info(ctx, slog.KV{"MSG", fmt.Sprintf("failed json.Marshal", err)}) + } +} diff --git a/main.go b/main.go index c57b6d7..a0121ab 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ func main() { http.HandleFunc("/api/v1/datastore-export-job-check/", HandleDatastoreExportJobCheckAPI) http.HandleFunc("/api/v1/datastore-export/", HandleDatastoreExportAPI) http.HandleFunc("/api/v1/storage-change-notify/", HandleStorageChangeNotifyAPI) + http.HandleFunc("/api/v1/logtest/", HandleLogTestAPI) http.HandleFunc("/", HandleHealthCheck) port := os.Getenv("PORT")