This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (90 loc) · 3.33 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
// Copyright (c) Spectro Cloud
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"strings"
"github.com/rs/zerolog/log"
_ "go.uber.org/automaxprocs"
"spectrocloud.com/spectromate/endpoints"
"spectrocloud.com/spectromate/internal"
)
var (
globalRedisPort int64
globalRedisURL string
globalRedisClient internal.Cache
globalRedisPassword string
globalRedisUser string
globalRedisTLS string
globalTraceLevel string
globalHost string
globalPort string
globalHostURL string = globalHost + ":" + globalPort
globalSigningSecret string
globalMendableAPIKey string
Version string
)
func init() {
globalTraceLevel = strings.ToUpper(internal.Getenv("TRACE", "INFO"))
internal.InitLogger(globalTraceLevel)
globalSigningSecret = internal.Getenv("SLACK_SIGNING_SECRET", "")
globalMendableAPIKey = internal.Getenv("MENDABLE_API_KEY", "")
globalRedisTLS = strings.ToLower(internal.Getenv("REDIS_TLS", "false"))
redisTLS := globalRedisTLS
port := internal.Getenv("PORT", "3000")
host := internal.Getenv("HOST", "0.0.0.0")
globalHost = host
globalPort = port
globalHostURL = host + ":" + port
globalRedisPort = internal.StringToInt64(internal.Getenv("REDIS_PORT", "6379"))
globalRedisURL = internal.Getenv("REDIS_URL", "localhost")
globalRedisPassword = internal.Getenv("REDIS_PASSWORD", "")
globalRedisUser = internal.Getenv("REDIS_USER", "")
reditConnectionString := fmt.Sprintf("%s:%d", globalRedisURL, globalRedisPort)
if globalSigningSecret == "" {
log.Fatal().Msg("The required environment variable SLACK_SIGNING_SECRET is not set. Exiting...")
}
if globalMendableAPIKey == "" {
log.Fatal().Msg("The required environment variable MENDABLE_API_KEY is not set. Exiting...")
}
var tlsConfig *tls.Config
if redisTLS == "true" {
tlsConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
}
}
rdb := internal.NewCache(reditConnectionString,
globalRedisUser,
globalRedisPassword,
tlsConfig)
log.Debug().Msg("Checking database connection...")
err := rdb.Ping()
if err != nil {
log.Debug().Msg("Redis is not available")
log.Fatal().Err(err).Msg("Error connecting to redis")
}
globalRedisClient = rdb
}
func main() {
ctx := context.Background()
rdb := globalRedisClient
healthRoute := endpoints.NewHealthHandlerContext(ctx, Version)
slackRoute := endpoints.NewSlackHandlerContext(ctx, globalSigningSecret, globalMendableAPIKey, rdb, Version)
slackActionsRoute := endpoints.NewActionsHandlerContext(ctx, globalSigningSecret, globalMendableAPIKey, Version)
http.HandleFunc(internal.ApiPrefixV1+"health", healthRoute.HealthHTTPHandler)
http.HandleFunc(internal.ApiPrefixV1+"slack", slackRoute.SlackHTTPHandler)
http.HandleFunc(internal.ApiPrefixV1+"slack/actions", slackActionsRoute.ActionsHTTPHandler)
log.Info().Msgf("Server is configured for port %s and listing on %s", globalPort, globalHostURL)
log.Info().Msgf("API Server version: %s", Version)
log.Info().Msgf("Redis is configured for %s:%d", globalRedisURL, globalRedisPort)
log.Info().Msgf("Trace level set to: %s", globalTraceLevel)
log.Info().Msg("Starting server...")
http.DefaultClient = internal.DefaultHTTPClient()
err := http.ListenAndServe(globalHostURL, nil)
if err != nil {
log.Fatal().Err(err).Msg("There's an error with the server")
}
}