/
server.go
154 lines (124 loc) · 3.91 KB
/
server.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package server
import (
"github.com/gladiusio/gladius-application-server/internal/controller"
asrouting "github.com/gladiusio/gladius-application-server/internal/routing"
"github.com/gladiusio/gladius-common/pkg/utils"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"os"
"strings"
)
var Database *gorm.DB
var Router *mux.Router
func initializeConfiguration() (string, error) {
// TODO: Use the gladius-common definition when it's in
base, err := utils.GetGladiusBase()
if err != nil {
return "Error retrieving base directory", err
}
// Add config file name and searching
viper.SetConfigName("gladius-application-server")
viper.AddConfigPath(base)
// Setup env variable handling
viper.SetEnvPrefix("APPLICATIONSERVER")
r := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(r)
viper.AutomaticEnv()
// Load config
err = viper.ReadInConfig()
var message = "Using provided config file and overriding default"
if err != nil {
return "Error reading config file, it may not exist or is corrupted. Using defaults.", err
}
// Build our config options
buildOptions(base)
return message, err
}
func buildOptions(base string) {
// Log options
ConfigOption("Log.Level", "info")
ConfigOption("Log.Pretty", true)
// Blockchain options
ConfigOption("Blockchain.Provider", "https://mainnet.infura.io/v3/1d3545f907ff4598893997c522e46676")
ConfigOption("Blockchain.MarketAddress", "0x27a9390283236f836a0b3c8dfdbed2ed854322fc")
// API options
ConfigOption("API.Port", 3333)
ConfigOption("API.DebugRequests", true)
ConfigOption("API.RemoteConnectionsAllowed", true)
// Database
ConfigOption("Database.Type", "sqlite3")
ConfigOption("Database.Connection", "local.db")
ConfigOption("Database.InitializePoolInfo", true)
// Service
ConfigOption("Service.Name", "GladiusApplicationServer")
ConfigOption("Service.DisplayName", "Application Server")
ConfigOption("Service.Description", "Gladius Application Server")
ConfigOption("Service.Debug", true)
// Applications
ConfigOption("Apllications.AutoAccept", false)
ConfigOption("Apllications.PoolOwner", "")
// Misc.
ConfigOption("GladiusBase", base) // Convenient option to have, not needed though
}
func ConfigOption(key string, defaultValue interface{}) string {
viper.SetDefault(key, defaultValue)
return key
}
func initializeService() {
// Router Setup
as := asrouting.New(viper.GetString("API.Port"), Database)
as.Start()
select {}
}
func initializeDatabase(dbType, dbConnection string) {
db, err := gorm.Open(dbType, dbConnection)
if err != nil {
log.Fatal()
}
Database, err = controller.Initialize(db)
if err != nil {
log.Fatal()
}
if viper.GetBool("Database.InitializePoolInfo") {
controller.InitializePoolManager(db)
}
}
func initializeLogging() {
// Setup logging level
switch loglevel := viper.GetString("Log.Level"); strings.ToLower(loglevel) {
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "warning":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "fatal":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
case "panic":
zerolog.SetGlobalLevel(zerolog.PanicLevel)
case "disabled":
zerolog.SetGlobalLevel(zerolog.Disabled)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
if !viper.IsSet("Log.Pretty") || (viper.IsSet("Log.Pretty") && viper.GetBool("Log.Pretty")) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
}
func InitializeApplicationServer() {
// Grab default configuration
message, err := initializeConfiguration()
initializeLogging()
if err != nil {
log.Warn().Err(err).Msg(message)
}
initializeDatabase(viper.GetString("Database.Type"), viper.GetString("Database.Connection"))
initializeService()
}