forked from Looskie/capybara-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (115 loc) · 3.26 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"crypto/tls"
"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"
"github.com/redis/go-redis/v9"
)
func ping(r *redis.Client) {
for range time.Tick(time.Second * 10) {
log.Println("Pinging")
if err := r.Ping(context.Background()).Err(); err != nil {
log.Fatal(err.Error())
break
}
}
}
func main() {
godotenv.Load()
containerId, err := os.Hostname()
if err != nil {
containerId = "UNKNOWN"
}
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)
}
var tlsObj *tls.Config = nil
if os.Getenv("REDIS_SECURE_SKIP") == "true" {
tlsObj = &tls.Config{
InsecureSkipVerify: true,
}
}
rdb := redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS"),
Username: os.Getenv("REDIS_USERNAME"),
Password: os.Getenv("REDIS_PASSWORD"),
DB: 0,
TLSConfig: tlsObj,
})
go ping(rdb)
println("Attempting connection at ", rdb.Options().Addr, rdb.Options().Username, rdb.Options().Password, rdb.Options().DB)
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.GetReqHeaders()["X-Forwarded-For"]
},
}))
app.Use(func(c *fiber.Ctx) error {
error := rdb.Incr(context.Background(), "visits").Err()
if error != nil {
println(error.Error())
}
return c.Next()
})
app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(utils.Response{
Success: true,
Message: "ok you pull up (Powered by hop.io) " + containerId,
})
})
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 (Powered by hop.io) " + containerId,
})
})
v1Group.Get("/capybaras", v1.GetCapybaras)
v1Group.Get("/capybara", v1.GetCapybara)
v1Group.Get("/capybara/:index", v1.GetCapybaraByIndex)
v1Group.Get("/capyoftheday", v1.GetCapybaraOfTheDay)
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))
}