* feat(server): moved an http server object into a pkg subdir to be reusable for http, spots, and integrations * feat(web): isolated web module (server, router, middleware, utils) used in spots and new integrations * feat(web): removed possible panic * feat(web): split all handlers from http service into different packages for better management. * feat(web): changed router's method signature * feat(web): added missing handlers interface * feat(web): added health middleware to remove unnecessary checks * feat(web): customizable middleware set for web servers * feat(web): simplified the handler's structure * feat(web): created an unified server.Run method for all web services (http, spot, integrations) * feat(web): fixed a json size limit issue * feat(web): removed Keys and PG connection from router * feat(web): simplified integration's main file * feat(web): simplified spot's main file * feat(web): simplified http's main file (builder) * feat(web): refactored audit trail functionality * feat(web): added ee version of audit trail * feat(web): added ee version of conditions module * feat(web): moved ee version of some web session structs * feat(web): new format of web metrics * feat(web): added new web metrics to all handlers * feat(web): added justExpired feature to web ingest handler * feat(web): added small integrations improvements
61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"openreplay/backend/pkg/logger"
|
|
"openreplay/backend/pkg/metrics/web"
|
|
)
|
|
|
|
type Responser struct {
|
|
metrics web.Web
|
|
}
|
|
|
|
func NewResponser(webMetrics web.Web) *Responser {
|
|
return &Responser{
|
|
metrics: webMetrics,
|
|
}
|
|
}
|
|
|
|
type response struct {
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
func (r *Responser) ResponseOK(log logger.Logger, ctx context.Context, w http.ResponseWriter, requestStart time.Time, url string, bodySize int) {
|
|
w.WriteHeader(http.StatusOK)
|
|
log.Info(ctx, "response ok")
|
|
r.recordMetrics(requestStart, url, http.StatusOK, bodySize)
|
|
}
|
|
|
|
func (r *Responser) ResponseWithJSON(log logger.Logger, ctx context.Context, w http.ResponseWriter, res interface{}, requestStart time.Time, url string, bodySize int) {
|
|
log.Info(ctx, "response ok")
|
|
body, err := json.Marshal(res)
|
|
if err != nil {
|
|
log.Error(ctx, "can't marshal response: %s", err)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(body)
|
|
r.recordMetrics(requestStart, url, http.StatusOK, bodySize)
|
|
}
|
|
|
|
func (r *Responser) ResponseWithError(log logger.Logger, ctx context.Context, w http.ResponseWriter, code int, err error, requestStart time.Time, url string, bodySize int) {
|
|
log.Error(ctx, "response error, code: %d, error: %s", code, err)
|
|
body, err := json.Marshal(&response{err.Error()})
|
|
if err != nil {
|
|
log.Error(ctx, "can't marshal response: %s", err)
|
|
}
|
|
w.WriteHeader(code)
|
|
w.Write(body)
|
|
r.recordMetrics(requestStart, url, code, bodySize)
|
|
}
|
|
|
|
func (r *Responser) recordMetrics(requestStart time.Time, url string, code, bodySize int) {
|
|
if bodySize > 0 {
|
|
r.metrics.RecordRequestSize(float64(bodySize), url, code)
|
|
}
|
|
r.metrics.IncreaseTotalRequests()
|
|
r.metrics.RecordRequestDuration(float64(time.Now().Sub(requestStart).Milliseconds()), url, code)
|
|
}
|