Skip to content

Commit

Permalink
LoggerGas
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed Nov 7, 2023
1 parent 1df8e79 commit 1529889
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"errors"
"github.com/aofei/air"
"go.uber.org/zap"
"net/http"
"time"
)

func SslCheckHandler(app *App) air.Gas {
func SslCheckHandlerGas(app *App) air.Gas {
return func(next air.Handler) air.Handler {
return func(req *air.Request, res *air.Response) error {
if h := req.HTTPRequest(); h != nil {
Expand All @@ -25,3 +27,20 @@ func SslCheckHandler(app *App) air.Gas {
}
}
}

func LoggerGas(log *zap.SugaredLogger) air.Gas {
return func(next air.Handler) air.Handler {
return func(req *air.Request, res *air.Response) (err error) {
startTime := time.Now()
res.Defer(func() {
endTime := time.Now()
user := getUsernameFromReq(req)

log.With(zap.String("user", user), zap.Int("status", res.Status)).Infof("%s %s, client: %s, time :%s",
req.Method, req.Path, req.ClientAddress(), endTime.Sub(startTime))
})

return next(req, res)
}
}
}
4 changes: 2 additions & 2 deletions cmd/goatak_server/cert_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ func getCertApi(app *App, addr string) *air.Air {
},
})

certApi.Gases = []air.Gas{auth}
certApi.Gases = []air.Gas{auth, LoggerGas(app.Logger.Named("cert_api"))}

certApi.GET("/Marti/api/tls/config", getTlsConfigHandler(app))
certApi.POST("/Marti/api/tls/signClient", getSignHandler(app))
certApi.POST("/Marti/api/tls/signClient/v2", getSignHandlerV2(app))
certApi.GET("/Marti/api/tls/profile/enrollment", getProfileEnrollmentHandler(app))

certApi.NotFoundHandler = getNotFoundHandler(app, "cert")
certApi.NotFoundHandler = getNotFoundHandler()

return certApi
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/goatak_server/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"github.com/kdudkov/goatak/pkg/cot"
"go.uber.org/zap"
"net/http"
"path/filepath"
"runtime/pprof"
Expand Down Expand Up @@ -64,7 +63,8 @@ func NewHttp(app *App) *HttpServer {
func getAdminApi(app *App, addr string, renderer *staticfiles.Renderer, webtakRoot string) *air.Air {
adminApi := air.New()
adminApi.Address = addr
adminApi.NotFoundHandler = getNotFoundHandler(app, "admin")
adminApi.NotFoundHandler = getNotFoundHandler()
adminApi.Gases = []air.Gas{LoggerGas(app.Logger.Named("admin_api"))}

staticfiles.EmbedFiles(adminApi, "/static")
adminApi.GET("/", getIndexHandler(app, renderer))
Expand All @@ -82,7 +82,7 @@ func getAdminApi(app *App, addr string, renderer *staticfiles.Renderer, webtakRo
if webtakRoot != "" {
adminApi.FILE("/webtak/", filepath.Join(webtakRoot, "index.html"))
adminApi.FILES("/webtak", webtakRoot)
addMartiRoutes(app, adminApi, "admin")
addMartiRoutes(app, adminApi)
}

adminApi.GET("/stack", getStackHandler())
Expand Down Expand Up @@ -134,9 +134,8 @@ func getMapHandler(app *App, r *staticfiles.Renderer) func(req *air.Request, res
}
}

func getNotFoundHandler(app *App, name string) func(req *air.Request, res *air.Response) error {
func getNotFoundHandler() func(req *air.Request, res *air.Response) error {
return func(req *air.Request, res *air.Response) error {
app.Logger.With(zap.String("api", name)).Infof("404 - %s %s", req.Method, req.Path)
res.Status = http.StatusNotFound
return errors.New(http.StatusText(res.Status))
}
Expand Down
Loading

0 comments on commit 1529889

Please sign in to comment.