yaxc/internal/server/server.go

50 lines
1,018 B
Go
Raw Normal View History

2021-03-24 23:11:23 +08:00
package server
2021-03-25 21:36:25 +08:00
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/limiter"
"strings"
"time"
)
2021-03-24 23:11:23 +08:00
func (s *yAxCServer) Start() {
log.Info("Starting YAxC server on", s.BindAddress)
2021-03-25 21:36:25 +08:00
cfg := &fiber.Config{}
if s.ProxyHeader != "" {
if s.ProxyHeader == "$proxy" {
s.ProxyHeader = "X-Forwarded-For"
}
cfg.ProxyHeader = s.ProxyHeader
}
s.App = fiber.New(*cfg)
// limiter middleware
s.App.Use(limiter.New(limiter.Config{
Next: func(c *fiber.Ctx) bool {
if c.IP() == "127.0.0.1" {
return true
}
if strings.HasPrefix(c.Path(), "/hash/") {
return true
}
return false
},
Max: 30,
Expiration: 60 * time.Second,
}))
2021-03-24 23:11:23 +08:00
// register routes
s.App.Get("/", func(ctx *fiber.Ctx) error {
return ctx.SendString(body)
})
s.App.Get("/:anywhere", s.handleGetAnywhere)
s.App.Post("/:anywhere", s.handlePostAnywhere)
2021-03-26 18:18:40 +08:00
s.App.Get("/hash/:anywhere", s.handleGetHashAnywhere)
2021-03-24 23:11:23 +08:00
if err := s.App.Listen(s.BindAddress); err != nil {
log.Critical(err)
}
}