-
Notifications
You must be signed in to change notification settings - Fork 0
/
tenant-api.go
109 lines (86 loc) · 2.62 KB
/
tenant-api.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
/*
Copyright 2022 Jan Lauber
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"os"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/gofiber/template/html"
"github.com/natron-io/tenant-api/routes"
"github.com/natron-io/tenant-api/util"
"github.com/slack-go/slack"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/flowcontrol"
)
func init() {
util.InitLoggers()
util.Status = "Running"
// creates the in-cluster config with ratelimiter to qps: 20 and burst: 50
config, err := rest.InClusterConfig()
if err != nil {
util.ErrorLogger.Println("Error creating in-cluster config")
os.Exit(1)
}
config.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(20, 50)
// creates the clientset
util.Clientset, err = kubernetes.NewForConfig(config)
if err != nil {
util.ErrorLogger.Printf("Error creating clientset: %v", err)
os.Exit(1)
}
// load util config envs
if err := util.LoadEnv(); err != nil {
util.ErrorLogger.Println("Error loading env variables")
os.Exit(1)
}
}
func main() {
engine := html.New("./views", ".html")
if util.SLACK_TOKEN != "" {
util.SlackClient = slack.New(util.SLACK_TOKEN)
}
app := fiber.New(fiber.Config{
Views: engine,
})
app.Use(cors.New(cors.Config{
AllowMethods: "GET",
AllowCredentials: true,
AllowOrigins: util.CORS,
}))
app.Use(limiter.New(limiter.Config{
Max: util.MAX_REQUESTS,
Expiration: 30 * time.Second,
LimitReached: func(c *fiber.Ctx) error {
return c.Status(429).SendString("Too many requests")
},
}))
app.Static("/styles", "./static/styles")
app.Static("/images", "./static/images")
app.Get("/", func(c *fiber.Ctx) error {
// set header to html
c.Set("Content-Type", "text/html") //TODO render css
return c.Render("index", fiber.Map{
"title": "Tenant API",
"status": util.GetStatus(),
})
})
routes.Setup(app, util.Clientset)
util.InfoLogger.Println("Tenant API is running on port 8000")
if err := app.Listen(":8000"); err != nil {
util.ErrorLogger.Println("Error starting server:", err)
os.Exit(1)
}
}