-
Notifications
You must be signed in to change notification settings - Fork 1
/
january.go
386 lines (332 loc) · 8.9 KB
/
january.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package january
import (
"database/sql"
"fmt"
"github.com/CloudyKit/jet/v6"
"github.com/akshanshgusain/january/cache"
"github.com/akshanshgusain/january/mailer"
"github.com/alexedwards/scs/v2"
"github.com/dgraph-io/badger/v3"
"github.com/go-chi/chi/v5"
"github.com/gomodule/redigo/redis"
"github.com/joho/godotenv"
"github.com/robfig/cron/v3"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
const version = "1.0.0"
var myRedisCache *cache.RedisCache
var myBadgerCache *cache.BadgerCache
var redisPool *redis.Pool
var badgerConn *badger.DB
type January struct {
AppName string
Debug bool
Version string
ErrorLog *log.Logger
InfoLog *log.Logger
RootPath string
Routes *chi.Mux
TemplateEngine *TemplateEngine
Session *scs.SessionManager
DB Database
JetViews *jet.Set
config configuration
EncryptionKey string
Cache cache.Cache
Scheduler *cron.Cron
Mail mailer.Mail
Server Server
}
type configuration struct {
port string
templateEngine string
cookie cookieConfig
sessionType string
database databaseConfig
redis redisConfig
}
type Server struct {
ServerName string
Port string
Secure bool
URL string
}
func (j *January) New(rootPath string) error {
pathConfig := initPaths{
rootPath: rootPath,
folderNames: []string{"handlers", "migrations", "views", "mail", "data", "public", "tmp", "logs", "middleware"},
}
if err := j.Init(pathConfig); err != nil {
return err
}
// TODO: add a bette way to do sanity check
if err := j.checkDotEnv(rootPath); err != nil {
return err
}
// reading .env with extern lib: joho/godotenv
if err := godotenv.Load(rootPath + "/.env"); err != nil {
return err
}
j.Debug, _ = strconv.ParseBool(os.Getenv("DEBUG"))
j.Version = version
j.RootPath = rootPath
// configuration
j.config = configuration{
port: os.Getenv("PORT"),
templateEngine: os.Getenv("TEMPLATE_ENGINE"),
cookie: cookieConfig{
name: os.Getenv("COOKIE_NAME"),
lifetime: os.Getenv("COOKIE_LIFETIME"),
persist: os.Getenv("COOKIE_PERSIST"),
secure: os.Getenv("COOKIE_SECURE"),
domain: os.Getenv("COOKIE_DOMAIN"),
},
sessionType: os.Getenv("SESSION_TYPE"),
database: databaseConfig{
database: os.Getenv("DATABASE_TYPE"),
dsn: j.BuildDSN(),
},
redis: redisConfig{
host: os.Getenv("REDIS_HOST"),
password: os.Getenv("REDIS_PASSWORD"),
prefix: os.Getenv("REDIS_PREFIX"),
},
}
// create loggers
infoLog, errorLog := j.startLoggers()
j.ErrorLog = errorLog
j.InfoLog = infoLog
// create mailer
j.Mail = j.createMailer()
// set scheduler
scheduler := cron.New()
j.Scheduler = scheduler
// configure caches ----------------------------
// check if redis is available
if os.Getenv("CACHE") == "redis" || os.Getenv("SESSION_TYPE") == "redis" {
myRedisCache = j.createClientRedisCache()
j.Cache = myRedisCache
redisPool = myRedisCache.Conn // we need to close conn when the Server is closed
}
// check if badger is available
if os.Getenv("CACHE") == "badger" {
myBadgerCache = j.createClientBadgerCache()
j.Cache = myBadgerCache
badgerConn = myBadgerCache.Conn // we need to close conn when the Server is closed
// garbage collection/clean up
_, err := j.Scheduler.AddFunc("@daily", func() {
_ = myBadgerCache.Conn.RunValueLogGC(0.7)
})
if err != nil {
return err
}
}
// configure caches ------------ ends ---------
// connect to database
if os.Getenv("DATABASE_TYPE") != "" {
db, err := j.OpenDBConnection(os.Getenv("DATABASE_TYPE"), j.BuildDSN())
if err != nil {
errorLog.Println(err)
os.Exit(1)
}
j.DB = Database{
DataType: os.Getenv("DATABASE_TYPE"),
Pool: db,
}
}
// fill in server details
secure := true
if strings.ToLower(os.Getenv("SECURE")) == "false" {
secure = false
}
j.Server = Server{
ServerName: os.Getenv("SERVER_NAME"),
Port: os.Getenv("PORT"),
Secure: secure,
URL: os.Getenv("APP_URL"),
}
// create session
s := Session{
CookieLifetime: j.config.cookie.lifetime,
CookiePersist: j.config.cookie.persist,
CookieName: j.config.cookie.name,
SessionType: j.config.sessionType,
CookieDomain: j.config.cookie.domain,
}
// switch session storage
switch j.config.sessionType {
case "redis":
s.RedisPool = myRedisCache.Conn
case "mysql", "postgres", "mariadb", "postgresql":
s.DBPool = j.DB.Pool
}
j.Session = s.InitSession()
j.EncryptionKey = os.Getenv("KEY")
// add routes
j.Routes = j.routes().(*chi.Mux)
// jet views
if j.Debug {
j.JetViews = jet.NewSet(
jet.NewOSFileSystemLoader(fmt.Sprintf("%s/views", rootPath)),
jet.InDevelopmentMode(),
)
} else {
j.JetViews = jet.NewSet(
jet.NewOSFileSystemLoader(fmt.Sprintf("%s/views", rootPath)),
)
}
// add Template Engine
j.createTemplateEngine()
// mailer-background task: go routine to listen to mails in the background
go j.Mail.ListenForMail()
return nil
}
func (j *January) Init(p initPaths) error {
// root path of web app
root := p.rootPath
for _, path := range p.folderNames {
// create the folder if not present
err := j.CreateDirIfNotExist(root + "/" + path)
if err != nil {
return err
}
}
return nil
}
func (j *January) checkDotEnv(p string) error {
if err := j.CreateFileIfNotExist(fmt.Sprintf("%s/.env", p)); err != nil {
return err
}
return nil
}
func (j *January) startLoggers() (*log.Logger, *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 (j *January) createTemplateEngine() {
j.TemplateEngine = &TemplateEngine{
TemplateEngine: j.config.templateEngine,
RootPath: j.RootPath,
Port: j.config.port,
JetViews: j.JetViews,
Session: j.Session,
}
}
func (j *January) createMailer() mailer.Mail {
port, _ := strconv.Atoi(os.Getenv("SMTP_PORT"))
m := mailer.Mail{
Domain: os.Getenv("MAIL_DOMAIN"),
Templates: j.RootPath + "/mail",
Host: os.Getenv("SMTP_HOST"),
Port: port,
Username: os.Getenv("SMTP_USERNAME"),
Password: os.Getenv("SMTP_PASSWORD"),
Encryption: os.Getenv("SMTP_ENCRYPTION"),
FromName: os.Getenv("FROM_NAME"),
FromAddress: os.Getenv("FROM_ADDRESS"),
// TODO: figure out a way to dynamically change the channel size
Jobs: make(chan mailer.Message, 20), // 20 Jobs at a time
Results: make(chan mailer.Result, 20),
API: os.Getenv("MAILER_API"),
APIKey: os.Getenv("MAILER_KEY"),
APIUrl: os.Getenv("MAILER_URL"),
}
return m
}
func (j *January) RunServer() {
s := &http.Server{
Addr: fmt.Sprintf(":%s", os.Getenv("PORT")),
ErrorLog: j.ErrorLog,
Handler: j.Routes,
IdleTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
}
// TODO: close db connection
defer func(Pool *sql.DB) {
err := Pool.Close()
if err != nil {
j.ErrorLog.Fatal(err)
}
}(j.DB.Pool)
defer func(redisPool *redis.Pool) {
err := redisPool.Close()
if err != nil {
}
}(redisPool)
defer func(badgerConn *badger.DB) {
err := badgerConn.Close()
if err != nil {
}
}(badgerConn)
j.InfoLog.Printf("Starting January server at http://127.0.0.1:%s/", os.Getenv("PORT"))
j.InfoLog.Printf("Quit the server with control+c")
if err := s.ListenAndServe(); err != nil {
j.ErrorLog.Fatal(err)
}
}
func (j *January) 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"))
if os.Getenv("DATABASE_PASS") != "" {
dsn = fmt.Sprintf("%s password=%s",
dsn,
os.Getenv("DATABASE_PASS"))
}
default:
}
return dsn
}
func (j *January) createRedisPool() *redis.Pool {
return &redis.Pool{
MaxIdle: 50,
MaxActive: 10000,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp",
j.config.redis.host,
redis.DialPassword(j.config.redis.password),
)
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
if err != nil {
return err
}
return nil
},
}
}
func (j *January) createClientRedisCache() *cache.RedisCache {
cacheClient := cache.RedisCache{
Conn: j.createRedisPool(),
Prefix: j.config.redis.prefix,
}
return &cacheClient
}
func (j *January) createBadgerConn() *badger.DB {
db, err := badger.Open(badger.DefaultOptions(j.RootPath + "/tmp/cache/badger"))
if err != nil {
return nil
}
return db
}
func (j *January) createClientBadgerCache() *cache.BadgerCache {
cacheClient := cache.BadgerCache{
Conn: j.createBadgerConn(),
}
return &cacheClient
}