2018-10-14 21:38:14 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-25 18:51:51 +02:00
|
|
|
"encoding/json"
|
2018-10-14 21:38:14 +02:00
|
|
|
"fmt"
|
2019-03-16 01:59:05 +02:00
|
|
|
"net/http"
|
2018-11-12 21:53:36 +02:00
|
|
|
"path/filepath"
|
2019-06-25 18:51:51 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2018-10-14 21:38:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Settings struct {
|
|
|
|
Port uint64
|
|
|
|
Dir string
|
|
|
|
}
|
|
|
|
Server struct {
|
|
|
|
engine *echo.Echo
|
|
|
|
settings Settings
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(settings Settings) *Server {
|
|
|
|
e := echo.New()
|
|
|
|
e.Debug = false
|
|
|
|
e.HideBanner = true
|
|
|
|
|
2019-03-16 01:59:05 +02:00
|
|
|
e.Use(func(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(ctx echo.Context) error {
|
|
|
|
ctx.SetCookie(&http.Cookie{
|
|
|
|
Name: "x-ferret",
|
|
|
|
Value: "e2e",
|
|
|
|
HttpOnly: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
return handlerFunc(ctx)
|
|
|
|
}
|
|
|
|
})
|
2018-10-14 21:38:14 +02:00
|
|
|
e.Static("/", settings.Dir)
|
2018-11-12 21:53:36 +02:00
|
|
|
e.File("/", filepath.Join(settings.Dir, "index.html"))
|
2019-06-25 18:51:51 +02:00
|
|
|
api := e.Group("/api")
|
|
|
|
api.GET("/ts", func(ctx echo.Context) error {
|
|
|
|
var headers string
|
|
|
|
|
|
|
|
if len(ctx.Request().Header) > 0 {
|
|
|
|
b, err := json.Marshal(ctx.Request().Header)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
headers = string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
|
|
|
|
return ctx.HTML(http.StatusOK, fmt.Sprintf(`
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<span id="timestamp">%s</span>
|
|
|
|
<span id="headers">%s</span>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`, ts, headers))
|
|
|
|
})
|
|
|
|
api.GET("/ping", func(ctx echo.Context) error {
|
|
|
|
return ctx.JSON(http.StatusOK, echo.Map{
|
|
|
|
"header": ctx.Request().Header,
|
|
|
|
"url": ctx.Request().URL,
|
|
|
|
"data": "pong",
|
|
|
|
"ts": time.Now(),
|
|
|
|
})
|
|
|
|
})
|
2018-10-14 21:38:14 +02:00
|
|
|
|
|
|
|
return &Server{e, settings}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Start() error {
|
|
|
|
return s.engine.Start(fmt.Sprintf("0.0.0.0:%d", s.settings.Port))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop(ctx context.Context) error {
|
|
|
|
return s.engine.Shutdown(ctx)
|
|
|
|
}
|