Basic server app with WS connection.
Some checks failed
Linting and tests / Linting (push) Failing after 37s
Some checks failed
Linting and tests / Linting (push) Failing after 37s
This commit is contained in:
91
server/internal/services/core/httpserver/httpserver.go
Normal file
91
server/internal/services/core/httpserver/httpserver.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"bunker/server/internal/application"
|
||||
"bunker/server/internal/services/core"
|
||||
)
|
||||
|
||||
var (
|
||||
_ = core.HTTPServer(&httpServer{})
|
||||
|
||||
errHTTPServer = errors.New("HTTP server core service")
|
||||
)
|
||||
|
||||
type httpServer struct {
|
||||
app *application.Application
|
||||
logger *slog.Logger
|
||||
db core.Database
|
||||
httpSrv *http.Server
|
||||
}
|
||||
|
||||
// Initialize initializes service.
|
||||
func Initialize(app *application.Application) error {
|
||||
httpSrv := &httpServer{
|
||||
app: app,
|
||||
}
|
||||
|
||||
if err := app.RegisterService(httpSrv); err != nil {
|
||||
return fmt.Errorf("%w: %w", errHTTPServer, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *httpServer) Configure() error {
|
||||
h.logger.Debug("Configuring service...")
|
||||
|
||||
if err := h.configureHTTPServer(); err != nil {
|
||||
return fmt.Errorf("configure: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *httpServer) ConnectDependencies() error {
|
||||
databaseRaw := h.app.Service(core.ServiceNameDatabase)
|
||||
if databaseRaw == nil {
|
||||
return fmt.Errorf("connect dependencies: get database service: %w", application.ErrServiceNotFound)
|
||||
}
|
||||
|
||||
database, valid := databaseRaw.(core.Database)
|
||||
if !valid {
|
||||
return fmt.Errorf("connect dependencies: type assert database service: %w", core.ErrDatabaseIsInvalid)
|
||||
}
|
||||
|
||||
h.db = database
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *httpServer) Initialize() error {
|
||||
h.logger = h.app.NewLogger("service", core.ServiceNameHTTPServer)
|
||||
|
||||
h.logger.Info("Initializing...")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *httpServer) Name() string {
|
||||
return core.ServiceNameHTTPServer
|
||||
}
|
||||
|
||||
func (h *httpServer) LaunchStartupTasks() error {
|
||||
h.logger.Debug("Launching startup tasks...")
|
||||
|
||||
go h.startHTTPServer()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *httpServer) Shutdown() error {
|
||||
if err := h.stopHTTPServer(); err != nil {
|
||||
return fmt.Errorf("%w: Shutdown: %w", errHTTPServer, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
85
server/internal/services/core/httpserver/server.go
Normal file
85
server/internal/services/core/httpserver/server.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
)
|
||||
|
||||
const httpServerAddrEnvVar = "BUNKERD_HTTP_ADDRESS"
|
||||
|
||||
var (
|
||||
errHTTPServerAddrInvalid = errors.New("BUNKERD_HTTP_ADDRESS environment variable contains invalid address to " +
|
||||
"listen, should be 'host:port'")
|
||||
errHTTPServerAddrNotFound = errors.New("BUNKERD_HTTP_ADDRESS environment variable empty")
|
||||
)
|
||||
|
||||
func (h *httpServer) configureHTTPServer() error {
|
||||
httpSrvAddr, found := os.LookupEnv(httpServerAddrEnvVar)
|
||||
if !found {
|
||||
return fmt.Errorf("configure HTTP server: get address from environment variable: %w", errHTTPServerAddrNotFound)
|
||||
}
|
||||
|
||||
host, port, err := net.SplitHostPort(httpSrvAddr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("configure HTTP server: validate HTTP server address: %w", err)
|
||||
}
|
||||
|
||||
if httpSrvAddr != host+":"+port {
|
||||
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.httpSrv = &http.Server{
|
||||
Addr: httpSrvAddr,
|
||||
Handler: mux,
|
||||
ReadHeaderTimeout: time.Second * 3,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *httpServer) handleWebsocketRequest(w http.ResponseWriter, r *http.Request) {
|
||||
wsConn, err := websocket.Accept(w, r, &websocket.AcceptOptions{
|
||||
OnPingReceived: func(_ context.Context, _ []byte) bool {
|
||||
return true
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
h.logger.Error("Failed to accept WS connection!", "error", err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := wsConn.CloseNow(); err != nil {
|
||||
h.logger.Warn("Failed to close WS connection in defer!", "error", err.Error())
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (h *httpServer) startHTTPServer() {
|
||||
h.logger.Info("Starting listening for HTTP requests.", "address", h.httpSrv.Addr)
|
||||
|
||||
if err := h.httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
h.logger.Warn("Error when listening to ", "error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (h *httpServer) stopHTTPServer() error {
|
||||
h.logger.Info("Stopping HTTP server...")
|
||||
|
||||
if err := h.httpSrv.Shutdown(h.app.ContextWithTimeout(time.Second * 3)); err != nil {
|
||||
return fmt.Errorf("stopping HTTP server: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user