From 8b439bbd5ab9034dfca6df27850f32970e143fc8 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Fri, 26 May 2023 23:55:40 +0400 Subject: [PATCH] Removed httprouter. --- internal/http/http.go | 41 ++++++++--------------------------------- 1 file changed, 8 insertions(+), 33 deletions(-) diff --git a/internal/http/http.go b/internal/http/http.go index 5a09682..7a82e8b 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -7,42 +7,17 @@ import ( "net/http" "os" "time" - - "github.com/julienschmidt/httprouter" ) type HttpServer struct { - server *http.Server - router *httprouter.Router + s http.Server } -func NewHttpServer() *HttpServer { - r := httprouter.New() - return &HttpServer{ - server: &http.Server{ - ReadTimeout: 3 * time.Second, - WriteTimeout: 3 * time.Second, - Handler: r, - }, - router: r, - } -} - -func (s *HttpServer) GET(path string, handler http.HandlerFunc) { - s.router.Handler(http.MethodGet, path, handler) -} - -func (s *HttpServer) ServeStatic(path string, fsys http.FileSystem) { - s.router.ServeFiles(path, fsys) -} - -func (s *HttpServer) SetNotFoundHandler(handler http.HandlerFunc) { - s.router.NotFound = handler -} - -// GetURLParam wrapper around underlying router for getting URL parameters. -func GetURLParam(r *http.Request, param string) string { - return httprouter.ParamsFromContext(r.Context()).ByName(param) +func NewHttpServer(r http.Handler) *HttpServer { + return &HttpServer{s: http.Server{ + ReadTimeout: 3 * time.Second, + WriteTimeout: 3 * time.Second, + Handler: r}} } func (s *HttpServer) Start(network, address string) error { @@ -56,7 +31,7 @@ func (s *HttpServer) Start(network, address string) error { } go func() { - if err = s.server.Serve(listener); err != nil && err != http.ErrServerClosed { + if err = s.s.Serve(listener); err != nil && err != http.ErrServerClosed { log.Fatalln(err) } }() @@ -68,7 +43,7 @@ func (s *HttpServer) Stop() error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - if err := s.server.Shutdown(ctx); err != nil { + if err := s.s.Shutdown(ctx); err != nil { return err }