Removed httprouter dependency from HTTP server.
This commit is contained in:
parent
a9e230ad61
commit
0677147b63
@ -7,50 +7,17 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type HttpServer struct {
|
type HttpServer struct {
|
||||||
server *http.Server
|
s http.Server
|
||||||
router *httprouter.Router
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHttpServer() *HttpServer {
|
func NewHttpServer(r http.Handler) *HttpServer {
|
||||||
r := httprouter.New()
|
return &HttpServer{s: http.Server{
|
||||||
return &HttpServer{
|
|
||||||
server: &http.Server{
|
|
||||||
ReadTimeout: 3 * time.Second,
|
ReadTimeout: 3 * time.Second,
|
||||||
WriteTimeout: 3 * time.Second,
|
WriteTimeout: 3 * time.Second,
|
||||||
Handler: r,
|
Handler: r}}
|
||||||
},
|
|
||||||
router: r,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *HttpServer) GET(path string, handler http.HandlerFunc) {
|
|
||||||
s.router.Handler(http.MethodGet, path, handler)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *HttpServer) POST(path string, handler http.HandlerFunc) {
|
|
||||||
s.router.Handler(http.MethodPost, path, handler)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *HttpServer) DELETE(path string, handler http.HandlerFunc) {
|
|
||||||
s.router.Handler(http.MethodDelete, 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 (s *HttpServer) Start(network, address string) error {
|
func (s *HttpServer) Start(network, address string) error {
|
||||||
@ -64,7 +31,7 @@ func (s *HttpServer) Start(network, address string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
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)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -76,7 +43,7 @@ func (s *HttpServer) Stop() error {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if err := s.server.Shutdown(ctx); err != nil {
|
if err := s.s.Shutdown(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user