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.") }