2019-03-07 08:43:28 +05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2021-06-16 06:22:33 +05:00
|
|
|
"net/http"
|
|
|
|
|
2019-03-07 08:43:28 +05:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/echo/middleware"
|
2021-06-16 06:22:33 +05:00
|
|
|
"go.dev.pztrn.name/fastpastebin/assets"
|
2019-03-07 08:43:28 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Context) initializeHTTPServer() {
|
|
|
|
c.Echo = echo.New()
|
|
|
|
c.Echo.Use(c.echoReqLogger())
|
|
|
|
c.Echo.Use(middleware.Recover())
|
2020-02-29 22:49:44 +05:00
|
|
|
c.Echo.Use(middleware.BodyLimit(c.Config.HTTP.MaxBodySizeMegabytes + "M"))
|
2019-03-07 08:43:28 +05:00
|
|
|
c.Echo.DisableHTTP2 = true
|
|
|
|
c.Echo.HideBanner = true
|
|
|
|
c.Echo.HidePort = true
|
|
|
|
|
|
|
|
// Static files.
|
2021-06-16 06:22:33 +05:00
|
|
|
c.Echo.GET("/static/*", echo.WrapHandler(http.FileServer(http.FS(assets.Data))))
|
2019-03-07 08:43:28 +05:00
|
|
|
|
|
|
|
listenAddress := c.Config.HTTP.Address + ":" + c.Config.HTTP.Port
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
c.Echo.Logger.Fatal(c.Echo.Start(listenAddress))
|
|
|
|
}()
|
2020-02-29 23:30:44 +05:00
|
|
|
c.Logger.Info().Str("address", listenAddress).Msg("Started HTTP server")
|
2019-03-07 08:43:28 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper around previous function.
|
|
|
|
func (c *Context) echoReqLogger() echo.MiddlewareFunc {
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
2021-11-20 22:19:58 +05:00
|
|
|
return func(ectx echo.Context) error {
|
2019-03-07 08:43:28 +05:00
|
|
|
c.Logger.Info().
|
2021-11-20 22:19:58 +05:00
|
|
|
Str("IP", ectx.RealIP()).
|
|
|
|
Str("Host", ectx.Request().Host).
|
|
|
|
Str("Method", ectx.Request().Method).
|
|
|
|
Str("Path", ectx.Request().URL.Path).
|
|
|
|
Str("UA", ectx.Request().UserAgent()).
|
2019-03-07 08:43:28 +05:00
|
|
|
Msg("HTTP request")
|
|
|
|
|
2021-11-20 22:19:58 +05:00
|
|
|
return next(ectx)
|
2019-03-07 08:43:28 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|