GoBlog/http.go

47 lines
940 B
Go
Raw Normal View History

2020-07-28 19:17:07 +00:00
package main
import (
2020-07-28 19:52:56 +00:00
"context"
2020-07-28 19:17:07 +00:00
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
2020-07-28 19:52:56 +00:00
"log"
2020-07-28 19:17:07 +00:00
"net/http"
2020-07-28 19:52:56 +00:00
"os"
"os/signal"
2020-07-28 19:17:07 +00:00
"strconv"
2020-07-28 19:52:56 +00:00
"time"
2020-07-28 19:17:07 +00:00
)
func startServer() {
e := echo.New()
if appConfig.server.logging {
e.Use(middleware.Logger())
}
e.Use(middleware.Recover(), middleware.Gzip())
e.GET("/", hello)
e.GET("/*", servePost)
address := ":" + strconv.Itoa(appConfig.server.port)
2020-07-28 19:52:56 +00:00
go func() {
if err := e.Start(address); err != nil {
log.Println("Shutting down the server")
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
2020-07-28 19:17:07 +00:00
}
func hello(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}