-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
123 lines (90 loc) · 3.2 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
package main
import (
"database/sql"
"github.com/riser-platform/riser-server/pkg/environment"
"github.com/riser-platform/riser-server/pkg/namespace"
"github.com/riser-platform/riser-server/pkg/util"
"github.com/riser-platform/riser-server/api"
"github.com/riser-platform/riser-server/pkg/login"
"github.com/riser-platform/riser-server/pkg/core"
"github.com/riser-platform/riser-server/pkg/postgres"
"os"
apiv1 "github.com/riser-platform/riser-server/api/v1"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/labstack/echo/v4"
echolog "github.com/onrik/logrus/echo"
"github.com/sirupsen/logrus"
)
// All env vars are prefixed with RISER_
const envPrefix = "RISER"
// DotEnv file typically used For local development
const dotEnvFile = ".env"
var logger = logrus.StandardLogger()
func main() {
logger.SetFormatter(&logrus.JSONFormatter{})
err := loadDotEnv()
exitIfError(err, "Error loading .env file")
var rc core.RuntimeConfig
err = envconfig.Process(envPrefix, &rc)
exitIfError(err, "Error loading environment variables")
if rc.DeveloperMode {
logger.SetFormatter(&logrus.TextFormatter{})
logger.Info("Developer mode active")
}
logger.Infof("Server version %s", util.VersionString)
logger.Info("Initializing postgres")
postgresConn, err := postgres.AddAuthToConnString(rc.PostgresUrl, rc.PostgresUsername, rc.PostgresPassword)
exitIfError(err, "Error creating postgres connection url")
postgresDb, err := postgres.NewDB(postgresConn)
exitIfError(err, "Error initializing postgres")
if rc.PostgresMigrateOnStartup {
logger.Info("Applying Postgres migrations")
err = postgres.Migrate(postgresConn, logger)
exitIfError(err, "Error performing Postgres migrations")
}
repoSettings := environment.RepoSettings{
URL: rc.GitUrl,
BaseGitDir: rc.GitDir,
}
repoCache := environment.NewBranchPerEnvRepoCache(repoSettings)
bootstrapApiKey(postgresDb, &rc)
bootstrapDefaultNamespace(postgresDb)
e := echo.New()
e.HideBanner = true
e.Logger = echolog.NewLogger(logger, "")
e.Use(echolog.Middleware(echolog.DefaultConfig))
e.HTTPErrorHandler = api.ErrorHandler
e.Binder = &api.DataBinder{}
apiv1.RegisterRoutes(e, repoCache, postgresDb)
err = e.Start(rc.BindAddress)
exitIfError(err, "Error starting server")
}
func bootstrapDefaultNamespace(db *sql.DB) {
namespaceService := namespace.NewService(postgres.NewNamespaceRepository(db), postgres.NewEnvironmentRepository(db))
err := namespaceService.EnsureDefaultNamespace()
exitIfError(err, "Error ensuring default namespace")
}
func bootstrapApiKey(db *sql.DB, rc *core.RuntimeConfig) {
loginService := login.NewService(postgres.NewUserRepository(db), postgres.NewApiKeyRepository(db))
err := loginService.BootstrapRootUser(rc.BootstrapApikey)
if err != nil {
if err == login.ErrRootUserExists {
logger.Info("Ignoring environment variable RISER_BOOTSTRAP_APIKEY: root user already exists.")
} else {
exitIfError(err, "Unable to bootstrap API KEY")
}
}
}
func loadDotEnv() error {
_, err := os.Stat(dotEnvFile)
if !os.IsNotExist(err) {
return godotenv.Load(dotEnvFile)
}
return nil
}
func exitIfError(err error, message string) {
if err != nil {
logger.Fatalf("%s: %s", message, err)
}
}