69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.dwysokinski.me/Kichiyaki/chizap"
|
|
"github.com/go-chi/chi/v5"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func main() {
|
|
logger, err := zap.NewDevelopment()
|
|
if err != nil {
|
|
log.Fatalln("zap.NewDevelopment", err)
|
|
}
|
|
|
|
http.ListenAndServe(":8080", newRouter(logger))
|
|
}
|
|
|
|
func newRouter(logger *zap.Logger) *chi.Mux {
|
|
router := chi.NewRouter()
|
|
|
|
router.Route("/", func(r chi.Router) {
|
|
r.Use(chizap.Logger(
|
|
logger,
|
|
chizap.WithFilter(func(r *http.Request) bool {
|
|
return r.URL.Path != "/excluded"
|
|
}),
|
|
chizap.WithFilter(func(r *http.Request) bool {
|
|
return r.URL.Path != "/excluded2"
|
|
}),
|
|
chizap.WithAdditionalFieldExtractor(func(r *http.Request) []zap.Field {
|
|
if !strings.HasPrefix(r.URL.Path, "/delete") {
|
|
return nil
|
|
}
|
|
|
|
return []zap.Field{
|
|
zap.String("id", chi.URLParam(r, "id")),
|
|
}
|
|
}),
|
|
))
|
|
r.Get("/info", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
r.Get("/warn", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
})
|
|
r.Get("/error", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
})
|
|
r.Get("/excluded", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
r.Delete("/delete/{id}", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
})
|
|
|
|
router.With(chizap.Logger(logger, chizap.WithTimeFormat(time.RFC1123Z))).
|
|
Get("/custom-time-format", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
|
|
return router
|
|
}
|