Initial commit: wb-mock skeleton
This commit is contained in:
38
cmd/server/main.go
Normal file
38
cmd/server/main.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/rinat/wb-mock/internal/config"
|
||||
"github.com/rinat/wb-mock/internal/middleware"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := config.Load()
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
auth := func(h http.HandlerFunc) http.HandlerFunc {
|
||||
return middleware.Auth(cfg.APIKey, h)
|
||||
}
|
||||
|
||||
// Health
|
||||
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"status":"ok"}`))
|
||||
})
|
||||
|
||||
_ = auth
|
||||
|
||||
// 404 handler
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte(`{"code":404,"message":"not found"}`))
|
||||
})
|
||||
|
||||
addr := ":" + cfg.Port
|
||||
log.Printf("Starting wb-mock on %s", addr)
|
||||
log.Fatal(http.ListenAndServe(addr, mux))
|
||||
}
|
||||
Reference in New Issue
Block a user