-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
89 lines (76 loc) · 2.59 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
package main
import (
"fmt"
"time"
"github.com/fsnotify/fsnotify"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/supernova106/kubestorm/routers"
"github.com/supernova106/kubestorm/utils"
_ "github.com/supernova106/kubestorm/docs" // docs is generated by Swag CLI, you have to import it.
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
func init() {
awsRegion := utils.GetEnv("AWS_DEFAULT_REGION", "us-west-2")
viper.SetDefault("awsRegion", awsRegion)
viper.SetConfigType("yaml")
viper.SetConfigName("config")
viper.AddConfigPath(".")
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s", err))
}
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
}
// @title Swagger Kubestorm API
// @version 1.0
// @description This is a API to interact with Kubernetes Objects
// @termsOfService http://swagger.io/terms/
// @contact.name Binh Nguyen
// @contact.url http://www.swagger.io/support
// @contact.email ntbinh106@gmail.com
// @license.name MIT Licensed
// @license.url https://opensource.org/licenses/MIT
// @host localhost:8080
// @BasePath /api/v1
// @schemes http https
func main() {
gin.ForceConsoleColor()
router := gin.New()
// LoggerWithFormatter middleware will write the logs to gin.DefaultWriter
// By default gin.DefaultWriter = os.Stdout
router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
// your custom format
return fmt.Sprintf("{\"ClientIP\": \"%s\",\"TimeStamp\": \"%s\",\"Method\": \"%s\",\"Path\": \"%s\",\"Protocol\": \"%s\",\"StatusCode\": \"%d\",\"Latency\": \"%s\",\"UserAgent\": \"%s\",\"Message\": \"%s\"}\n",
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage,
)
}))
router.Use(gin.Recovery())
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "HEAD", "OPTIONS", "POST"},
}))
apiv1 := router.Group("/api/v1")
{
apiv1.GET("/resources", routers.GetResources)
apiv1.POST("/auth/:cluster", routers.Auth)
apiv1.GET("/auth/:cluster", routers.Auth)
apiv1.DELETE("/auth/:cluster", routers.Auth)
apiv1.GET("/status", routers.GetStatus)
}
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
router.Run(":8080") // listen and serve on 0.0.0.0:8080
}