-
Notifications
You must be signed in to change notification settings - Fork 0
/
napoleon.go
317 lines (264 loc) · 6.94 KB
/
napoleon.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package napoleon
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/CloudyKit/jet/v6"
"github.com/alexedwards/scs/v2"
"github.com/dgraph-io/badger/v3"
"github.com/go-chi/chi/v5"
"github.com/gomodule/redigo/redis"
"github.com/hilsonxhero/napoleon/cache"
"github.com/hilsonxhero/napoleon/render"
"github.com/hilsonxhero/napoleon/session"
"github.com/joho/godotenv"
"github.com/robfig/cron/v3"
)
const version = "1.0.0"
var myRedisCache *cache.RedisCache
var myBadgerCache *cache.BadgerCache
var redisPool *redis.Pool
var badgerConn *badger.DB
type Napoleon struct {
AppName string
Debug bool
Version string
ErrorLog *log.Logger
InfoLog *log.Logger
RootPath string
Routes *chi.Mux
Render *render.Render
Session scs.SessionManager
DB Database
JetViews jet.Set
config config
EncryptionKey string
Cache cache.Cache
Scheduler *cron.Cron
}
type config struct {
port string
renderer string
cookie cookieConfig
sessionType string
database DatabaseConfig
redis RedisConfig
}
func (n *Napoleon) New(rootPath string) error {
pathConfig := initPaths{
rootPath: rootPath,
folderNames: []string{"handlers", "migrations", "views", "data", "public", "tmp", "logs", "middlewares"},
}
err := n.Init(pathConfig)
if err != nil {
return err
}
err = n.checkEnvFile(rootPath)
if err != nil {
return err
}
err = godotenv.Load(rootPath + "/.env")
if err != nil {
return err
}
infoLog, errorLog := n.startLogegrs()
// connect to database
if os.Getenv("DATABASE_TYPE") != "" {
db, err := n.OpenDB(os.Getenv("DATABASE_TYPE"), n.BuildDSN())
if err != nil {
errorLog.Println(err)
os.Exit(1)
}
n.DB = Database{
DataType: os.Getenv("DATABASE_TYPE"),
Pool: db,
}
}
scheduler := cron.New()
n.Scheduler = scheduler
if os.Getenv("CACHE") == "redis" || os.Getenv("SESSION_TYPE") == "redis" {
myRedisCache = n.createClientRedisCache()
n.Cache = myRedisCache
}
if os.Getenv("CACHE") == "badger" {
myBadgerCache = n.createClientBadgerCache()
n.Cache = myBadgerCache
_, err = n.Scheduler.AddFunc("@daily", func() {
_ = myBadgerCache.Conn.RunValueLogGC(0.7)
})
if err != nil {
return err
}
}
n.ErrorLog = errorLog
n.InfoLog = infoLog
n.Debug, _ = strconv.ParseBool(os.Getenv("DEBUG"))
n.Version = version
n.RootPath = rootPath
n.Routes = n.routes().(*chi.Mux)
n.config = config{
port: os.Getenv("PORT"),
renderer: os.Getenv("RENDERER"),
cookie: cookieConfig{
name: os.Getenv("COOKIE_NAME"),
lifetime: os.Getenv("COOKIE_LIFETIME"),
parsist: os.Getenv("COOKIE_PARSIST"),
srcure: os.Getenv("SESSION_SECURE"),
domain: os.Getenv("SESSION_DOMAIN"),
},
sessionType: os.Getenv("SESSION_TYPE"),
database: DatabaseConfig{
database: os.Getenv("DATABASE_TYPE"),
dsn: n.BuildDSN(),
},
redis: RedisConfig{
host: os.Getenv("REDIS_HOST"),
password: os.Getenv("REDIS_PASSWORD"),
prefix: os.Getenv("REDIS_PREFIX"),
},
}
sess := session.Session{
CookieLifetime: n.config.cookie.lifetime,
CookiePersist: n.config.cookie.parsist,
CookieName: n.config.cookie.name,
SessionType: n.config.sessionType,
CookieDomain: n.config.cookie.domain,
DBPool: n.DB.Pool,
}
switch n.config.sessionType {
case "redis":
sess.RedisPool = myRedisCache.Conn
case "mysql", "postgres", "mariadb", "postgresql":
sess.DBPool = n.DB.Pool
}
n.Session = *sess.InitSession()
n.EncryptionKey = os.Getenv("KEY")
if n.Debug {
var views = jet.NewSet(
jet.NewOSFileSystemLoader(fmt.Sprintf("%s/views", rootPath)),
jet.InDevelopmentMode(),
)
n.JetViews = *views
} else {
var views = jet.NewSet(
jet.NewOSFileSystemLoader(fmt.Sprintf("%s/views", rootPath)),
// jet.InDevelopmentMode(),
)
n.JetViews = *views
}
n.createRenderer()
return nil
}
func (n *Napoleon) Init(p initPaths) error {
root := p.rootPath
for _, path := range p.folderNames {
err := n.CreateDirIfNotExist(root + "/" + path)
if err != nil {
return err
}
}
return nil
}
func (n *Napoleon) ListenAndServe() {
srv := &http.Server{
Addr: fmt.Sprintf(":%s", os.Getenv("PORT")),
ErrorLog: n.ErrorLog,
Handler: n.Routes,
IdleTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 600 * time.Second,
}
if n.DB.Pool != nil {
defer n.DB.Pool.Close()
}
if redisPool != nil {
defer redisPool.Close()
}
if badgerConn != nil {
defer badgerConn.Close()
}
n.InfoLog.Printf("Listening on port %s", os.Getenv("PORT"))
err := srv.ListenAndServe()
n.ErrorLog.Fatal(err)
}
func (n *Napoleon) checkEnvFile(path string) error {
err := n.CreateFileIfNotExist(fmt.Sprintf("%s/.env", path))
if err != nil {
return err
}
return nil
}
func (n *Napoleon) startLogegrs() (*log.Logger, *log.Logger) {
var infoLog *log.Logger
var errorLog *log.Logger
infoLog = log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
errorLog = log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
return infoLog, errorLog
}
func (n *Napoleon) createRenderer() {
myRenderer := render.Render{
Renderer: n.config.renderer,
RootPath: n.RootPath,
Port: n.config.port,
JetViews: n.JetViews,
Session: n.Session,
}
n.Render = &myRenderer
}
func (n *Napoleon) createClientRedisCache() *cache.RedisCache {
cacheClient := cache.RedisCache{
Conn: n.createRedisPool(),
Prefix: n.config.redis.prefix,
}
return &cacheClient
}
func (n *Napoleon) createClientBadgerCache() *cache.BadgerCache {
cacheClient := cache.BadgerCache{
Conn: n.createBadgerConn(),
}
return &cacheClient
}
func (n *Napoleon) createRedisPool() *redis.Pool {
return &redis.Pool{
MaxIdle: 50,
MaxActive: 10000,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", n.config.redis.host, redis.DialPassword(n.config.redis.password))
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}
func (n *Napoleon) createBadgerConn() *badger.DB {
db, err := badger.Open(badger.DefaultOptions(n.RootPath + "/tmp/badger"))
if err != nil {
return nil
}
return db
}
// BuildDSN builds the datasource name for our database, and returns it as a string
func (n *Napoleon) BuildDSN() string {
var dsn string
switch os.Getenv("DATABASE_TYPE") {
case "postgres", "postgresql":
dsn = fmt.Sprintf("host=%s port=%s user=%s dbname=%s sslmode=%s timezone=UTC connect_timeout=5",
os.Getenv("DATABASE_HOST"),
os.Getenv("DATABASE_PORT"),
os.Getenv("DATABASE_USER"),
os.Getenv("DATABASE_NAME"),
os.Getenv("DATABASE_SSL_MODE"))
// we check to see if a database passsword has been supplied, since including "password=" with nothing
// after it sometimes causes postgres to fail to allow a connection.
if os.Getenv("DATABASE_PASS") != "" {
dsn = fmt.Sprintf("%s password=%s", dsn, os.Getenv("DATABASE_PASS"))
}
default:
}
return dsn
}