HTTP server with WebSockets.
All checks were successful
Linting and tests / Linting (push) Successful in 5s

This commit is contained in:
2025-09-20 09:59:09 +05:00
parent cc5d010204
commit f1617efb0f
6 changed files with 71 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ package core
import (
"errors"
"net/http"
)
// ServiceNameHTTPServer is a name for HTTP server service.
@@ -11,6 +12,12 @@ const ServiceNameHTTPServer = "core/http_server"
var ErrHTTPServerIsInvalid = errors.New("HTTP server service implementation is invalid")
// HTTPServer is an interface for HTTP server service.
//
//nolint:iface
type HTTPServer interface{}
type HTTPServer interface {
// RegisterHandler registers HTTP handler.
RegisterHandler(method, path string, handler http.HandlerFunc)
// RegisterMiddleware registers HTTP server middlewares.
RegisterMiddleware(middleware HTTPMiddlewareFunc)
}
// HTTPMiddlewareFunc is a function that acts as middleware for HTTP requests.
type HTTPMiddlewareFunc func(fn http.HandlerFunc) http.HandlerFunc

View File

@@ -0,0 +1,8 @@
package httpserver
import "net/http"
func (h *httpServer) defaultHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte("Unknown path."))
}

View File

@@ -21,6 +21,9 @@ type httpServer struct {
logger *slog.Logger
db core.Database
httpSrv *http.Server
httpMux *http.ServeMux
middlewares []core.HTTPMiddlewareFunc
}
// Initialize initializes service.
@@ -67,6 +70,10 @@ func (h *httpServer) Initialize() error {
h.logger.Info("Initializing...")
h.middlewares = make([]core.HTTPMiddlewareFunc, 0)
h.RegisterMiddleware(h.requestLoggingMiddleware)
return nil
}

View File

@@ -0,0 +1,24 @@
package httpserver
import (
"fmt"
"net/http"
"time"
)
func (h *httpServer) requestLoggingMiddleware(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
fn(w, r)
h.logger.Info(
"HTTP request.",
"remote_addr", r.RemoteAddr,
"user_agent", r.UserAgent(),
"host", r.Host,
"path", fmt.Sprintf("%s %s", r.Method, r.RequestURI),
"duration", time.Since(startTime),
)
}
}

View File

@@ -9,6 +9,8 @@ import (
"os"
"time"
"bunker/server/internal/services/core"
"github.com/coder/websocket"
)
@@ -35,12 +37,15 @@ func (h *httpServer) configureHTTPServer() error {
return fmt.Errorf("configure HTTP server: validate HTTP server address: %w", errHTTPServerAddrInvalid)
}
mux := new(http.ServeMux)
mux.HandleFunc("GET /api/v1/socket", h.handleWebsocketRequest)
h.httpMux = new(http.ServeMux)
// Default catch-all handler.
h.RegisterHandler("", "/", h.defaultHandler)
h.RegisterHandler(http.MethodGet, "/api/v1/socket", h.handleWebsocketRequest)
h.httpSrv = &http.Server{
Addr: httpSrvAddr,
Handler: mux,
Handler: h.httpMux,
ReadHeaderTimeout: time.Second * 3,
}
@@ -66,6 +71,20 @@ func (h *httpServer) handleWebsocketRequest(w http.ResponseWriter, r *http.Reque
}()
}
func (h *httpServer) RegisterHandler(method, path string, handler http.HandlerFunc) {
h.httpMux.HandleFunc(fmt.Sprintf("%s %s", method, path), func(w http.ResponseWriter, r *http.Request) {
for i := len(h.middlewares) - 1; i >= 0; i-- {
handler = h.middlewares[i](handler)
}
handler(w, r)
})
}
func (h *httpServer) RegisterMiddleware(middleware core.HTTPMiddlewareFunc) {
h.middlewares = append(h.middlewares, middleware)
}
func (h *httpServer) startHTTPServer() {
h.logger.Info("Starting listening for HTTP requests.", "address", h.httpSrv.Addr)

View File

@@ -11,6 +11,4 @@ const ServiceNameOptions = "core/options"
var ErrOptionsIsInvalid = errors.New("options service implementation is invalid")
// Options is an interface for options service.
//
//nolint:iface
type Options interface{}