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 {
|
|
|
|
return func(ec echo.Context) error {
|
|
|
|
c.Logger.Info().
|
|
|
|
Str("IP", ec.RealIP()).
|
|
|
|
Str("Host", ec.Request().Host).
|
|
|
|
Str("Method", ec.Request().Method).
|
|
|
|
Str("Path", ec.Request().URL.Path).
|
|
|
|
Str("UA", ec.Request().UserAgent()).
|
|
|
|
Msg("HTTP request")
|
|
|
|
|
2019-10-13 16:26:52 +05:00
|
|
|
return next(ec)
|
2019-03-07 08:43:28 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|