From 4b1bddd92318e9a1d102b3e06c7925ba32422510 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 13 Oct 2024 20:01:27 +0200 Subject: Simplify Server type It is not required to create a struct definition with only one member. The new solution is more elegant. --- server.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/server.go b/server.go index 0c26188..685e2a5 100644 --- a/server.go +++ b/server.go @@ -13,14 +13,12 @@ import ( "github.com/gorilla/mux" ) -type Server struct { - backend *http.Server -} +type Server http.Server //go:embed view/static/simple.css/simple.css view/static/ceres.js var static embed.FS -func NewServer(addr string, db *model.DB) Server { +func NewServer(addr string, db *model.DB) *Server { var r *mux.Router = mux.NewRouter() r.PathPrefix("/static/"). @@ -49,15 +47,15 @@ func NewServer(addr string, db *model.DB) Server { log.Printf("Configured server to listen on http://%s\n", srv.Addr) - return Server{backend: &srv} + return (*Server)(&srv) } -func (s Server) Start() { - s.backend.ListenAndServe() +func (s *Server) Start() { + (*http.Server)(s).ListenAndServe() } -func (s Server) Stop() { - err := s.backend.Shutdown(context.Background()) +func (s *Server) Stop() { + err := (*http.Server)(s).Shutdown(context.Background()) if err != nil { log.Printf("Failed to shutdown HTTP server: %v\n", err) } else { -- cgit v1.2.3-70-g09d2