forked from gocms-io/gocms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
216 lines (178 loc) · 4.74 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/cqlcorp/gocms/context"
"github.com/cqlcorp/gocms/init/controller"
"github.com/cqlcorp/gocms/init/database"
"github.com/cqlcorp/gocms/init/repository"
"github.com/cqlcorp/gocms/init/service"
"github.com/cqlcorp/gocms/utility/log"
"github.com/cqlcorp/gocms/utility/security"
"golang.org/x/sync/errgroup"
)
var (
egocms *Engine
igocms *InternalEngine
g errgroup.Group
)
type Engine struct {
Gin *gin.Engine
ControllersGroup *controller.ControllersGroup
ServicesGroup *service.ServicesGroup
RepositoriesGroup *repository.RepositoriesGroup
Database *database.Database
}
type InternalEngine struct {
Gin *gin.Engine
InternalControllersGroup *controller.InternalControllersGroup
ServicesGroup *service.ServicesGroup
RepositoriesGroup *repository.RepositoriesGroup
Database *database.Database
}
type gocmsFlags struct {
}
type gocmsRuntimeSettings struct {
help bool
port string
msPort string
noExtneralServices bool
runInternalServices bool
}
// todo write an optimizer for requirejs
//go:generate apidoc -c ./ -i ./models -i ./controllers/ -o ./content/docs/ -f ".*\\.go$" -f ".*\\.js$"
func Default() (e *Engine, ie *InternalEngine) {
// setup database
db := database.DefaultSQL()
// migrate cms db
db.SQL.MigrateSql()
// check for rsa keys
security.CheckOrGenRSAKeysAndSecrets(db.SQL.Dbx)
// setup log level
switch context.Config.EnvVars.LogLevel {
case log.LOG_LEVEL_CRITICAL:
fallthrough
case log.LOG_LEVEL_ERROR:
gin.SetMode(gin.ReleaseMode)
case log.LOG_LEVEL_WARNING:
gin.SetMode(gin.TestMode)
case log.LOG_LEVEL_DEBUG:
gin.SetMode(gin.DebugMode)
}
r := gin.Default()
ir := gin.Default()
// setup repositories
rg := repository.DefaultRepositoriesGroup(db.SQL.Dbx)
// setup services
sg := service.DefaultServicesGroup(rg, db)
// setup controllers
cg := controller.DefaultControllerGroup(r, sg)
icg := controller.DefaultInternalControllerGroup(ir, sg)
// create engine
e = &Engine{
Gin: r,
ControllersGroup: cg,
ServicesGroup: sg,
RepositoriesGroup: rg,
Database: db,
}
// create engine
ie = &InternalEngine{
Gin: ir,
InternalControllersGroup: icg,
ServicesGroup: sg,
RepositoriesGroup: rg,
Database: db,
}
fmt.Println("\n-- CQL/GOCMS IS RUNNING --")
return e, ie
}
func (engine *Engine) Listen(uri string) error {
err := http.ListenAndServe(uri, engine.Gin)
if err == nil {
log.Infof("Listening on: %v\n", uri)
}
return err
}
func (engine *InternalEngine) Listen(uri string) error {
err := http.ListenAndServe(uri, engine.Gin)
if err == nil {
log.Infof("(Internal API) Listening on: %v\n", uri)
}
return err
}
func main() {
// startup defaults
egocms, igocms = Default()
// get ports
rs := getRuntimeSettings()
// skip external if needed
if !rs.noExtneralServices {
g.Go(func() error {
return egocms.Listen(":" + rs.port)
})
}
// run internal if needed
if rs.runInternalServices {
g.Go(func() error {
return igocms.Listen(":" + rs.msPort)
})
}
if err := g.Wait(); err != nil {
log.Criticalf("Error launching services: %v\n", err.Error())
}
}
func getRuntimeSettings() *gocmsRuntimeSettings {
// define flags
portFlag := flag.String("port", "", "port to run on. Overrides all.")
msPortFlag := flag.String("msPort", "", "msPort to run on. Overrides all.")
noExternalServiceFlag := flag.Bool("noExternal", false, "noExternal when this flag is set gocms will not run external services.")
runInternalServiceFlag := flag.Bool("runInternal", false, "runInternal when this flag is set gocms will run internal services.")
flag.Parse()
noExternalService := *noExternalServiceFlag
runInternalService := *runInternalServiceFlag
///////// PORT ///////////
// get server port in order of importance
// 3. db
port := context.Config.DbVars.Port
// 2. env
portEnv := os.Getenv("PORT")
if portEnv != "" {
port = portEnv
}
// 1. flag
if *portFlag != "" {
port = *portFlag
}
// 0. if still unset
if port == "" {
port = "8080"
}
///////// MS PORT ///////////
// get server port in order of importance
// 3. db
msPort := context.Config.DbVars.MsPort
// 2. env
msPortEnv := os.Getenv("msPort")
if msPortEnv != "" {
msPort = msPortEnv
}
// 1. flag
// check for msPort flag and override all
if *msPortFlag != "" {
msPort = *msPortFlag
}
// 0. if still unset
if msPort == "" {
msPort = "8081"
}
return &gocmsRuntimeSettings{
port: port,
msPort: msPort,
noExtneralServices: noExternalService,
runInternalServices: runInternalService,
}
}