-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
53 lines (43 loc) · 1.1 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
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/redis/go-redis/v9"
"github.com/rollbar/rollbar-go"
)
var ctx context.Context
var rdb *redis.Client
func main() {
err := godotenv.Load()
if err != nil {
log.Fatalln("error loading .env file")
}
if os.Getenv("ROLLBAR_TOKEN") != "" {
rollbar.SetToken(os.Getenv("ROLLBAR_TOKEN"))
rollbar.SetEnvironment(os.Getenv("GIN_MODE"))
rollbar.SetCodeVersion(getCurrentGitHeadHash())
defer rollbar.Close()
}
ctx = context.Background()
rdb = redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS_ADDRESS"),
})
defer rdb.Close()
_, err = rdb.Ping(ctx).Result()
if err != nil {
rollbar.Critical(fmt.Sprintf("redis connection was refused; addr = %s", rdb.Options().Addr))
panic("redis connection failed")
}
gin.SetMode(os.Getenv("GIN_MODE"))
router := gin.Default()
router.Use(cors.Default())
router.GET("/", getREADME)
router.GET("/json", getAllData)
router.GET("/:key", getDataByKey)
router.Run(fmt.Sprintf(":%s", os.Getenv("SERVER_PORT")))
}