Skip to content

Commit

Permalink
Make sure we log all requests
Browse files Browse the repository at this point in the history
Before this there would be no logs printed for requests not having
explicit handlers, like when responding "method not allowed" (405) or
"not found" (404).
  • Loading branch information
eest committed Feb 20, 2024
1 parent e9ee620 commit 5900b74
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,18 @@ func staleLocksFunc(cc *configCache, timeout time.Duration) http.HandlerFunc {
}
}

func methodNotAllowedFunc() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
}

func notFoundFunc() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
}
}

func writeNewlineJSON(w http.ResponseWriter, b []byte, statusCode int) error {
w.Header().Set("content-type", contentTypeString)
w.WriteHeader(statusCode)
Expand Down Expand Up @@ -1597,6 +1609,9 @@ func Run(configPath string) {
// Create middleware chain used for swagger docs endpoint
swaggerMiddlewares := newSwaggerMiddlewareChain(hlogMiddlewares)

// Create middleware chain used for unhandled requests
unhandledMiddlewares := newUnhandledChain(hlogMiddlewares)

preRebootChain := alice.New(fleetLockMiddlewares...).Then(preRebootFunc(cc, timeout))
steadyStateChain := alice.New(fleetLockMiddlewares...).Then(steadyStateFunc(cc, timeout))

Expand All @@ -1617,7 +1632,10 @@ func Run(configPath string) {

swaggerChain := alice.New(swaggerMiddlewares...).Then(httpSwagger.Handler(httpSwagger.URL(swaggerURL.String())))

router := newRouter(preRebootChain, steadyStateChain, lockStatusChain, staleLocksChain, apiChain, swaggerChain)
methodNotAllowedChain := alice.New(unhandledMiddlewares...).Then(methodNotAllowedFunc())
notFoundChain := alice.New(unhandledMiddlewares...).Then(notFoundFunc())

router := newRouter(methodNotAllowedChain, notFoundChain, preRebootChain, steadyStateChain, lockStatusChain, staleLocksChain, apiChain, swaggerChain)

srv := &http.Server{
Addr: conf.Server.Listen,
Expand Down Expand Up @@ -1723,6 +1741,13 @@ func newSwaggerMiddlewareChain(hlogMiddlewares []alice.Constructor) []alice.Cons
return swaggerMiddlewares
}

func newUnhandledChain(hlogMiddlewares []alice.Constructor) []alice.Constructor {
unhandledMiddlewares := []alice.Constructor{}
unhandledMiddlewares = append(unhandledMiddlewares, hlogMiddlewares...)

return unhandledMiddlewares
}

func newEtcd3Client(conf etcd3config, tlsConfig *tls.Config) (*clientv3.Client, error) {
etcd3Config := clientv3.Config{
Endpoints: conf.Endpoints,
Expand Down Expand Up @@ -1753,8 +1778,10 @@ func newEtcd3Client(conf etcd3config, tlsConfig *tls.Config) (*clientv3.Client,
return etcd3Client, nil
}

func newRouter(preRebootChain, steadyStateChain, lockStatusChain, staleLocksChain, apiChain http.Handler, swaggerChain http.Handler) *httprouter.Router {
func newRouter(methodNotAllowedChain, notFoundChain, preRebootChain, steadyStateChain, lockStatusChain, staleLocksChain, apiChain http.Handler, swaggerChain http.Handler) *httprouter.Router {
router := httprouter.New()
router.MethodNotAllowed = methodNotAllowedChain
router.NotFound = notFoundChain

router.Handler("POST", "/v1/pre-reboot", preRebootChain)
router.Handler("POST", "/v1/steady-state", steadyStateChain)
Expand Down

0 comments on commit 5900b74

Please sign in to comment.