Initial commit.
This commit is contained in:
20
internal/httpserver/handler.go
Normal file
20
internal/httpserver/handler.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go.dev.pztrn.name/metricator/internal/common"
|
||||
)
|
||||
|
||||
// HTTP requests handler.
|
||||
type handler struct {
|
||||
handler common.HTTPHandlerFunc
|
||||
}
|
||||
|
||||
// Registers request's handler.
|
||||
func (h *handler) register(hndl common.HTTPHandlerFunc) {
|
||||
h.handler = hndl
|
||||
}
|
||||
|
||||
// ServeHTTP handles every HTTP request.
|
||||
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {}
|
79
internal/httpserver/httpserver.go
Normal file
79
internal/httpserver/httpserver.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.dev.pztrn.name/metricator/internal/configuration"
|
||||
)
|
||||
|
||||
// HTTPServer is a controlling structure for HTTP server.
|
||||
type HTTPServer struct {
|
||||
config *configuration.Config
|
||||
ctx context.Context
|
||||
doneChan chan struct{}
|
||||
handler *handler
|
||||
server *http.Server
|
||||
}
|
||||
|
||||
func NewHTTPServer(ctx context.Context, cfg *configuration.Config) (*HTTPServer, chan struct{}) {
|
||||
h := &HTTPServer{
|
||||
config: cfg,
|
||||
ctx: ctx,
|
||||
doneChan: make(chan struct{}),
|
||||
}
|
||||
h.initialize()
|
||||
|
||||
return h, h.doneChan
|
||||
}
|
||||
|
||||
// Returns request's context based on main context of application.
|
||||
// Basically it returns main context and does nothing more.
|
||||
func (h *HTTPServer) getRequestContext(_ net.Listener) context.Context {
|
||||
return h.ctx
|
||||
}
|
||||
|
||||
// Initializes handler and HTTP server structure.
|
||||
func (h *HTTPServer) initialize() {
|
||||
h.handler = &handler{}
|
||||
h.server = &http.Server{
|
||||
// ToDo: make it all configurable.
|
||||
Addr: ":34421",
|
||||
BaseContext: h.getRequestContext,
|
||||
Handler: h.handler,
|
||||
ReadTimeout: time.Second * 10,
|
||||
WriteTimeout: time.Second * 10,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
}
|
||||
}
|
||||
|
||||
// Start starts HTTP server in another goroutine and one more goroutine which
|
||||
// is listening to main context's Cancel() call to stop HTTP server.
|
||||
func (h *HTTPServer) Start() {
|
||||
go func() {
|
||||
err := h.server.ListenAndServe()
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "Server closed") {
|
||||
log.Println("HTTP server failed to listen:", err.Error())
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
<-h.ctx.Done()
|
||||
log.Println("Shutting down HTTP server")
|
||||
|
||||
err := h.server.Shutdown(h.ctx)
|
||||
if err != nil && !strings.Contains(err.Error(), "context canceled") {
|
||||
log.Println("Failed to stop HTTP server:", err.Error())
|
||||
}
|
||||
|
||||
log.Println("HTTP server stopped")
|
||||
|
||||
h.doneChan <- struct{}{}
|
||||
}()
|
||||
}
|
Reference in New Issue
Block a user