mirror of
https://github.com/darmiel/yaxc.git
synced 2024-11-15 20:37:42 +08:00
49 lines
1,018 B
Go
49 lines
1,018 B
Go
package server
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/limiter"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func (s *yAxCServer) Start() {
|
|
log.Info("Starting YAxC server on", s.BindAddress)
|
|
|
|
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,
|
|
}))
|
|
|
|
// register routes
|
|
s.App.Get("/", func(ctx *fiber.Ctx) error {
|
|
return ctx.SendString(body)
|
|
})
|
|
|
|
s.App.Get("/hash/:anywhere", s.handleGetHashAnywhere)
|
|
s.App.Get("/:anywhere", s.handleGetAnywhere)
|
|
s.App.Post("/:anywhere", s.handlePostAnywhere)
|
|
|
|
if err := s.App.Listen(s.BindAddress); err != nil {
|
|
log.Critical(err)
|
|
}
|
|
}
|