/
server.go
177 lines (136 loc) · 3.77 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package cmd
import (
"fmt"
"net/http"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/acme/autocert"
"github.com/sterrasi/stepwise/logging"
"github.com/sterrasi/stepwise/users"
"github.com/sterrasi/stepwise/util"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
var (
applicationName = "stepwise"
)
type AutoTLSConfig struct {
CertCacheDir string `mapstructure:"cert-cache-dir"`
HostWhiteList []string `mapstructure:"host-white-list"`
}
type TLSConfig struct {
Cert string `mapstructure:"cert"`
Key string `mapstructure:"key"`
}
type ServerConfig struct {
Address string `mapstructure:"address"`
AutoTLS *AutoTLSConfig `mapstructure:"auto-tls"`
TLS *TLSConfig `mapstructure:"tls"`
}
var serverCmd = &cobra.Command{
Use: "server",
Short: "start the Stepwise web application",
Long: "Starts an instance of the stepwise web application.",
Run: func(cmd *cobra.Command, args []string) {
// logging
if err := initLogging(); err != nil {
panic(err.Error())
}
defer logging.DeinitializeLogging()
// database
db, err := initDatabase()
if err != nil {
panic(err.Error())
}
defer db.Close()
// server
e := echo.New()
// Middleware
e.Pre(middleware.RemoveTrailingSlash())
e.Pre(middleware.HTTPSRedirect())
e.Use(middleware.Gzip())
e.Use(middleware.Secure())
e.Use(logging.LoggerMiddleware())
e.Use(middleware.Recover())
// Register Users API
usersConfig := &users.Config{}
if err := viper.UnmarshalKey("users", usersConfig); err != nil {
panic(err.Error())
}
users.Register(e.Group("/users"), db, usersConfig)
e.GET("/", hello)
// Start server
e.Logger.Fatal(startServer(e))
},
}
func init() {
RootCmd.AddCommand(serverCmd)
}
// Handler
func hello(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}
// initializes the GORM database
func initDatabase() (*gorm.DB, error) {
databaseConfig := &util.DatabaseConfig{}
var err error
var db *gorm.DB
if err = viper.UnmarshalKey("database", databaseConfig); err != nil {
return nil, err
}
db, err = util.InitDatabase(databaseConfig)
if err != nil {
return nil, err
}
if databaseConfig.Migrate {
db.AutoMigrate(&users.User{})
}
return db, nil
}
func startServer(e *echo.Echo) error {
serverConfig := &ServerConfig{}
if err := viper.UnmarshalKey("server", serverConfig); err != nil {
return err
}
// check for autoTLS
if serverConfig.AutoTLS != nil {
logrus.Infof("Starting server using AutoTLS")
// apply a white list policy
if len(serverConfig.AutoTLS.HostWhiteList) > 0 {
e.AutoTLSManager.HostPolicy = autocert.HostWhitelist(serverConfig.AutoTLS.HostWhiteList...)
}
// Cache certificates in a local directory
e.AutoTLSManager.Cache = autocert.DirCache(serverConfig.AutoTLS.CertCacheDir)
// start server
return e.StartAutoTLS(serverConfig.Address)
} else if serverConfig.TLS != nil {
logrus.Infof("Starting server using TLS")
if serverConfig.TLS.Cert == "" {
return fmt.Errorf("Certificate not provided for TLS enabled server")
}
if serverConfig.TLS.Key == "" {
return fmt.Errorf("Key not provided for TLS enabled server")
}
return e.StartTLS(serverConfig.Address, serverConfig.TLS.Cert, serverConfig.TLS.Key)
}
return fmt.Errorf("Either the [server.auto-tls] or [server.tls] section must be filled out")
}
func initLogging() error {
logConfig := &logging.LogConfig{}
if err := viper.UnmarshalKey("logging", logConfig); err != nil {
return err
}
if err := logging.InitLogging(logConfig); err != nil {
return err
}
if logConfig.LogStartupInfo {
logStartupInfo()
}
return nil
}
func logStartupInfo() {
logrus.Info("starting Stepwise...")
}