-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
96 lines (78 loc) · 2.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"log"
"os"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/joho/godotenv"
"github.com/looskie/capybara-api/utils"
v1 "github.com/looskie/capybara-api/v1"
)
func main() {
godotenv.Load()
capyImages, _ := os.ReadDir("capys")
utils.NUMBER_OF_IMAGES = len(capyImages)
if err := utils.LoadCapyAlts("utils/alt.json"); err != nil {
log.Printf("could not load alt text, using default response: %s", err)
}
app := fiber.New(fiber.Config{
EnableTrustedProxyCheck: true,
TrustedProxies: []string{"10.50.0.0/24"},
})
app.Use(recover.New(recover.Config{
Next: nil,
EnableStackTrace: true,
}))
app.Use(logger.New(logger.Config{
Format: "${time} | ${cyan}${status} ${reset}| ${latency} | ${ip} on ${cyan}${ua} ${reset}| ${cyan}${method} ${reset}${path} \n",
}))
app.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowMethods: "GET",
}))
app.Use(limiter.New(limiter.Config{
Max: 500,
Expiration: 30 * time.Second,
LimitReached: func(c *fiber.Ctx) error {
return c.Status(429).JSON(utils.Response{
Success: false,
Message: "You are being rate limited",
})
},
KeyGenerator: func(c *fiber.Ctx) string {
return c.Get("X-Forwarded-For")
},
}))
app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(utils.Response{
Success: true,
Message: "ok you pull up",
})
})
v1Group := app.Group("/v1")
v1Group.Get("/", func(c *fiber.Ctx) error {
return c.JSON(utils.Response{
Success: true,
Message: "welcome to v1 of capybara heaven",
})
})
v1Group.Get("/capybaras", v1.GetCapybaras)
v1Group.Get("/capybara", v1.GetCapybara)
v1Group.Get("/capybara/:index", v1.GetCapybaraByIndex)
v1Group.Get("/capyday", v1.GetCapybaraOfTheDay)
v1Group.Get("/capyoftheday", v1.GetCapybaraOfTheDay) // Alias
v1Group.Get("/capyhour", v1.GetCapyHour)
v1Group.Get("/capyofthehour", v1.GetCapyHour) // Alias
// Capybara facts
v1Group.Get("/fact", v1.GetCapyFact)
v1Group.Get("/facts", v1.GetCapyFacts)
var port = os.Getenv("PORT")
if len(port) == 0 {
port = "3000"
}
log.Fatal(app.Listen(":" + port))
}