rest-api/server/server.go
Piotr Biernat e1019a5ecc
All checks were successful
continuous-integration/drone/push Build is passing
Some refactor. Rewritted to Echo Framework
2020-09-26 23:01:44 +02:00

54 lines
1.0 KiB
Go

package server
import (
"context"
"go-rest-api/handler"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
// Server struct
type Server struct {
port string
server *http.Server
}
// NewServer func
func NewServer(port string) Server {
return Server{
server: &http.Server{
Addr: port,
},
}
}
// Serve func
func (s *Server) Serve() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"http://localhost:8000"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
}))
handler.AttachArticleHandlersToRouter(e)
e.Static("/", "./public")
e.GET("/", s.defaultHandler)
e.Logger.Fatal(e.StartServer(s.server))
}
// Shutdown func
func (s *Server) Shutdown(tc context.Context) {
s.server.Shutdown(tc)
}
func (s *Server) defaultHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusMethodNotAllowed, "Method not allowed.")
}