/
server.go
163 lines (150 loc) · 5.56 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
// Copyright 2017 NDP Systèmes. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/gleke/hexya/src/actions"
"github.com/gleke/hexya/src/controllers"
"github.com/gleke/hexya/src/i18n"
"github.com/gleke/hexya/src/menus"
"github.com/gleke/hexya/src/models"
"github.com/gleke/hexya/src/server"
"github.com/gleke/hexya/src/templates"
"github.com/gleke/hexya/src/tools/logging"
"github.com/gleke/hexya/src/views"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var serverCmd = &cobra.Command{
Use: "server [projectDir]",
Short: "Start the Hexya server",
Long: `Start the Hexya server of the project in 'projectDir'.
If projectDir is omitted, defaults to the current directory.`,
Run: func(cmd *cobra.Command, args []string) {
projectDir := "."
if len(args) > 0 {
projectDir = args[0]
}
runProject(projectDir, "server", args)
},
}
// runProject builds and run the executable of the given project with the given command and arguments.
func runProject(projectDir, cmd string, args []string) {
fmt.Println("Please wait, Hexya is starting ...")
viper.Set("ProjectDir", projectDir)
absProjectDir, err := filepath.Abs(projectDir)
if err != nil {
panic(err)
}
cmdName := filepath.Base(absProjectDir)
if err = runCommand("go", "build", "-o", cmdName, absProjectDir); err != nil {
os.Exit(1)
}
runCommand(filepath.Join(absProjectDir, cmdName), append([]string{cmd}, args...)...)
}
// StartServer starts the Hexya server. It is meant to be called from
// a project start file which imports all the project's module.
func StartServer() {
setupLogger()
defer log.Sync()
setupDebug()
resourceDir, err := filepath.Abs(viper.GetString("ResourceDir"))
if err != nil {
log.Panic("Unable to find Resource directory", "error", err)
}
server.ResourceDir = resourceDir
server.PreInit()
connectToDB()
i18n.BootStrap()
models.BootStrap()
models.RunWorkerLoop()
server.LoadTranslations(resourceDir, i18n.Langs)
server.LoadInternalResources(resourceDir)
views.BootStrap()
templates.BootStrap()
actions.BootStrap()
controllers.BootStrap()
menus.BootStrap()
server.PostInit()
srv := server.GetServer()
address := fmt.Sprintf("%s:%s", viper.GetString("Server.Interface"), viper.GetString("Server.Port"))
cert := viper.GetString("Server.Certificate")
key := viper.GetString("Server.PrivateKey")
domain := viper.GetString("Server.Domain")
switch {
case cert != "":
srv.RunTLS(address, cert, key)
case domain != "":
srv.RunAutoTLS(domain)
default:
srv.Run(address)
}
}
// setupLogger initializes the logger
func setupLogger() {
logging.Initialize()
log = logging.GetLogger("init")
}
// setupDebug updates the server for debugging if Debug is enabled
func setupDebug() {
if !viper.GetBool("Debug") {
return
}
gin.SetMode(gin.DebugMode)
pprof.Register(server.GetServer().Engine)
}
// connectToDB creates the connection to the database
func connectToDB() {
models.DBConnect(viper.GetString("DB.Driver"), models.ConnectionParams{
Host: viper.GetString("DB.Host"),
Port: viper.GetString("DB.Port"),
User: viper.GetString("DB.User"),
Password: viper.GetString("DB.Password"),
DBName: viper.GetString("DB.Name"),
SSLMode: viper.GetString("DB.SSLMode"),
SSLCert: viper.GetString("DB.SSLCert"),
SSLKey: viper.GetString("DB.SSLKey"),
SSLCA: viper.GetString("DB.SSLCA"),
})
}
// SetServerFlags adds the server flags to the given command.
func SetServerFlags(c *cobra.Command) {
c.PersistentFlags().StringP("interface", "i", "", "Interface on which the server should listen. Empty string is all interfaces")
viper.BindPFlag("Server.Interface", c.PersistentFlags().Lookup("interface"))
c.PersistentFlags().StringP("port", "p", "8080", "Port on which the server should listen.")
viper.BindPFlag("Server.Port", c.PersistentFlags().Lookup("port"))
c.PersistentFlags().StringSliceP("languages", "l", []string{}, "Comma separated list of language codes to load (ex: fr,de,es).")
viper.BindPFlag("Server.Languages", c.PersistentFlags().Lookup("languages"))
c.PersistentFlags().StringP("domain", "d", "", "Domain name of the server. When set, interface and port are set to 0.0.0.0:443 and it will automatically get an HTTPS certificate from Letsencrypt")
viper.BindPFlag("Server.Domain", c.PersistentFlags().Lookup("domain"))
c.PersistentFlags().StringP("certificate", "C", "", "Certificate file for HTTPS. If neither certificate nor domain is set, the server will run on plain HTTP. When certificate is set, private-key must also be set.")
viper.BindPFlag("Server.Certificate", c.PersistentFlags().Lookup("certificate"))
c.PersistentFlags().StringP("private-key", "K", "", "Private key file for HTTPS.")
viper.BindPFlag("Server.PrivateKey", c.PersistentFlags().Lookup("private-key"))
}
func runCommand(c string, args ...string) error {
cmdToRun := exec.Command(c, args...)
cmdToRun.Stdout = os.Stdout
cmdToRun.Stderr = os.Stderr
return cmdToRun.Run()
}
func init() {
SetServerFlags(serverCmd)
HexyaCmd.AddCommand(serverCmd)
}