Skip to content

Commit

Permalink
ログをテストするハンドラを追加した refs #10
Browse files Browse the repository at this point in the history
  • Loading branch information
sinmetal committed Jul 11, 2019
1 parent 7fff845 commit 8428d5d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions logtest_api.go
Original file line number Diff line number Diff line change
@@ -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)})
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 8428d5d

Please sign in to comment.