add http server

This commit is contained in:
2024-08-10 02:15:00 +03:00
parent 6a2489a65c
commit ba6ac26bac
9 changed files with 380 additions and 14 deletions

188
internal/api/http/server.go Normal file
View File

@ -0,0 +1,188 @@
package http
import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
"net/http/pprof"
"time"
"git.loyso.art/frx/devsim/internal/entities"
"git.loyso.art/frx/devsim/internal/store"
)
type handlersBuilder struct {
mux *http.ServeMux
}
func NewHandlersBuilder() *handlersBuilder {
return &handlersBuilder{
mux: http.NewServeMux(),
}
}
// MountStatsHandlers mounts stats related handlers.
func (h *handlersBuilder) MountStatsHandlers(sr store.Stats, log *slog.Logger) {
log = log.With(slog.String("api", "http"))
mws := multipleMiddlewares(
middlewarePanicRecovery(log),
middlewareLogger(log),
)
h.mux.Handle("/api/v1/stats/", mws(listStatsHandler(sr)))
h.mux.Handle("/api/v1/stats/{id}", mws(postStatsHandler(sr)))
}
func (s *handlersBuilder) MountProfileHandlers() {
s.mux.HandleFunc("/debug/pprof", pprof.Index)
s.mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
s.mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
func (s *handlersBuilder) Build() http.Handler {
return s.mux
}
// ListenAndServe runs server to accept incoming connections. This function blocks on
// handling connections.
func listStatsHandler(sr store.Stats) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
stats, err := sr.List(r.Context())
if err != nil {
http.Error(w, fmt.Errorf("listing stats: %w", err).Error(), http.StatusInternalServerError)
return
}
w.Header().Set("content-type", "application/json")
err = json.NewEncoder(w).Encode(stats)
if err != nil {
http.Error(w, fmt.Errorf("encoding payload: %w", err).Error(), http.StatusInternalServerError)
}
})
}
func postStatsHandler(sr store.Stats) http.HandlerFunc {
type request struct {
IncomingTraffic int `json:"incoming_traffic"`
OutgoingTraffic int `json:"outgoing_traffic"`
IncomingRPS int `json:"incoming_rps"`
ReadRPS int `json:"read_rps"`
WriteRPS int `json:"write_rps"`
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
id := r.PathValue("id")
if id == "" {
http.Error(w, "no id provided", http.StatusBadRequest)
return
}
var reqbody request
err := json.NewDecoder(r.Body).Decode(&reqbody)
if err != nil {
http.Error(w, fmt.Errorf("decoding body: %w", err).Error(), http.StatusBadRequest)
return
}
ctx := r.Context()
err = sr.Upsert(ctx, entities.DeviceStatistics{
ID: entities.DeviceID(id),
IncomingTrafficBytes: reqbody.IncomingTraffic,
OutgoingTrafficBytes: reqbody.OutgoingTraffic,
IncomingRPS: reqbody.IncomingRPS,
ReadRPS: reqbody.ReadRPS,
WriteRPS: reqbody.WriteRPS,
})
if err != nil {
http.Error(w, fmt.Errorf("upserting stat metric: %w", err).Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
})
}
type middlewareFunc func(http.Handler) http.Handler
func middlewarePanicRecovery(log *slog.Logger) middlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
rec := recover()
if rec == nil {
return
}
log.ErrorContext(
r.Context(), "panic acquired during request",
slog.Any("panic", rec),
)
}()
next.ServeHTTP(w, r)
})
}
}
func middlewareLogger(log *slog.Logger) middlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
requestID := r.Header.Get("x-request-id")
if requestID == "" {
requestID = randomID()
}
w.Header().Set("x-request-id", requestID)
method := r.Method
path := r.URL.Path
query := r.URL.Query().Encode()
log.InfoContext(
r.Context(), "request processing",
slog.String("request_id", requestID),
slog.String("method", method),
slog.String("path", path),
slog.String("query", query),
)
next.ServeHTTP(w, r)
elapsed := time.Since(start)
log.InfoContext(
r.Context(), "request finished",
slog.Duration("elapsed", elapsed.Truncate(time.Millisecond)),
)
})
}
}
func multipleMiddlewares(mws ...middlewareFunc) middlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, mw := range mws {
next = mw(next)
}
next.ServeHTTP(w, r)
})
}
}
func randomID() string {
return ""
}

View File

@ -0,0 +1,110 @@
package http_test
import (
"bytes"
"encoding/json"
"log/slog"
stdhttp "net/http"
"net/http/httptest"
"os"
"testing"
"git.loyso.art/frx/devsim/internal/api/http"
"git.loyso.art/frx/devsim/internal/entities"
"git.loyso.art/frx/devsim/internal/store/mock"
"github.com/stretchr/testify/require"
)
func prepareEssentials(t testing.TB) (*mock.MockedStore, *slog.Logger) {
t.Helper()
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{}))
return mock.NewMock(), log
}
func TestList(t *testing.T) {
require := require.New(t)
store, log := prepareEssentials(t)
expectedStatistics := []entities.DeviceStatistics{
{
ID: entities.DeviceID("test-1"),
OutgoingTrafficBytes: 10,
},
{
ID: entities.DeviceID("test-2"),
IncomingTrafficBytes: 20,
},
}
store.RegisterOnList(func() ([]entities.DeviceStatistics, error) {
return expectedStatistics, nil
})
hb := http.NewHandlersBuilder()
hb.MountStatsHandlers(store, log)
handler := hb.Build()
server := httptest.NewServer(handler)
defer server.Close()
httpClient := server.Client()
req, err := stdhttp.NewRequest(stdhttp.MethodGet, server.URL+"/api/v1/stats", nil)
require.NoError(err)
resp, err := httpClient.Do(req)
require.NoError(err)
stats := make([]entities.DeviceStatistics, 0, 2)
err = json.NewDecoder(resp.Body).Decode(&stats)
require.NoError(err)
require.ElementsMatch(stats, expectedStatistics)
}
func TestUpsert(t *testing.T) {
require := require.New(t)
store, log := prepareEssentials(t)
expectedStatistics := entities.DeviceStatistics{
ID: entities.DeviceID("test-1"),
IncomingTrafficBytes: 5,
OutgoingTrafficBytes: 10,
IncomingRPS: 8,
WriteRPS: 6,
ReadRPS: 3,
}
incomingStats := new(entities.DeviceStatistics)
store.RegisterOnUpsert(func(ds entities.DeviceStatistics) error {
*incomingStats = ds
return nil
})
hb := http.NewHandlersBuilder()
hb.MountStatsHandlers(store, log)
handler := hb.Build()
server := httptest.NewServer(handler)
defer server.Close()
httpClient := server.Client()
requestBody, _ := json.Marshal(map[string]any{
"incoming_traffic": 5,
"outgoing_traffic": 10,
"incoming_rps": 8,
"write_rps": 6,
"read_rps": 3,
})
req, err := stdhttp.NewRequest(stdhttp.MethodPost, server.URL+"/api/v1/stats/"+string(expectedStatistics.ID), bytes.NewReader(requestBody))
require.NoError(err)
resp, err := httpClient.Do(req)
require.NoError(err)
require.Equal(resp.StatusCode, stdhttp.StatusOK)
require.Equal(*incomingStats, expectedStatistics)
}