-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoravel.go
379 lines (321 loc) · 9.17 KB
/
goravel.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
package goravel
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"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/joho/godotenv"
"github.com/robfig/cron/v3"
"github.com/saalikmubeen/goravel/cache"
"github.com/saalikmubeen/goravel/mailer"
"github.com/saalikmubeen/goravel/render"
"github.com/saalikmubeen/goravel/session"
)
const (
// Version of Goravel
Version = "1.6.0"
// http://patorjk.com/software/taag/#p=display&f=Ogre&t=Goravel
Banner = `
___ _
/ _ \___ _ __ __ ___ _____| |
/ /_\/ _ \| '__/ _ \ \ / / _ \ |
/ /_\\ (_) | | | (_| |\ V / __/ |
\____/\___/|_| \__,_| \_/ \___|_| %s
PHP's Laravel like web framework supercharged with Go
____________________________________O/_______
O\
`
)
var myRedisCache *cache.RedisCache
var redisPool *redis.Pool
var myBadgerCache *cache.BadgerCache
var badgerConn *badger.DB
type Goravel struct {
AppName string
GoAppURL string
Debug bool // true for development mode
Version string
Server Server
ErrorLog *log.Logger
InfoLog *log.Logger
RootPath string // rootPath is the path that we are in when we start the goravel app
Render *render.Render
Routes *chi.Mux
JetViews *jet.Set
Session *scs.SessionManager
DB Database
Cache cache.Cache
EncryptionKey string
Mail mailer.Mail
Scheduler *cron.Cron
// not exported, used internally
// contains mostly loaded environment variables.
config config
}
type config struct {
port string // port that the server will listen on
renderer string // name of the rendering engine that we want to use ("go" or" jet")
cookie cookieConfig
sessionType string
database databaseConfig
redis redisConfig
}
// CreateFolderStructure creates necessary folders for our Goravel application
func (g *Goravel) CreateFolderStructure(p InitPaths) error {
rootPath := p.RootPath // string that holds the full pathname to the root level of my web app
for _, folderName := range p.FolderNames {
// create a folder in the rootPath if it doesn't exist
err := g.CreateDirIfNotExists(fmt.Sprintf("%s/%s", rootPath, folderName))
if err != nil {
return err
}
}
return nil
}
func (g *Goravel) checkDotEnv(path string) error {
err := g.CreateFileIfNotExists(fmt.Sprintf("%s/.env", path))
if err != nil {
return err
}
return nil
}
func (g *Goravel) CreateLoggers() (*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 (g *Goravel) createRenderer() {
myRenderer := render.Render{
Renderer: g.config.renderer,
RootPath: g.RootPath,
Port: g.config.port,
JetViews: g.JetViews,
Session: g.Session,
}
g.Render = &myRenderer
}
func (g *Goravel) BuildDSN() string {
var dsn string = ""
dbType := os.Getenv("DATABASE_TYPE")
switch dbType {
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_SSLMODE"))
if os.Getenv("DATABASE_PASSWORD") != "" {
dsn = fmt.Sprintf("%s password=%s", dsn, os.Getenv("DATABASE_PASSWORD"))
}
case "mysql":
default:
}
return dsn
}
func (g *Goravel) createRedisPool() *redis.Pool {
return &redis.Pool{
MaxIdle: 50,
MaxActive: 10000,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp",
g.config.redis.host,
redis.DialPassword(g.config.redis.password))
},
TestOnBorrow: func(conn redis.Conn, t time.Time) error {
_, err := conn.Do("PING")
return err
},
}
}
func (g *Goravel) createRedisCache() *cache.RedisCache {
cacheClient := cache.RedisCache{
Conn: g.createRedisPool(),
Prefix: g.config.redis.prefix,
}
return &cacheClient
}
func (g *Goravel) createBadgerConn() *badger.DB {
db, err := badger.Open(badger.DefaultOptions(g.RootPath + "/tmp/badger"))
if err != nil {
return nil
}
return db
}
func (g *Goravel) createBadgerCache() *cache.BadgerCache {
cacheClient := cache.BadgerCache{
Conn: g.createBadgerConn(),
}
return &cacheClient
}
func (g *Goravel) createMailer() mailer.Mail {
port, _ := strconv.Atoi(os.Getenv("SMTP_PORT"))
m := mailer.Mail{
Domain: os.Getenv("MAIL_DOMAIN"),
Templates: g.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"),
Jobs: make(chan mailer.Message, 20),
Results: make(chan mailer.Result, 20),
API: os.Getenv("MAILER_API"),
APIKey: os.Getenv("MAILER_KEY"),
APIUrl: os.Getenv("MAILER_URL"),
}
return m
}
func (g *Goravel) New(rootPath string) error {
paths := InitPaths{
RootPath: rootPath,
FolderNames: []string{"handlers", "migrations", "views", "mail", "models", "public", "tmp", "logs", "middleware"},
}
// ** create the necessary folders
err := g.CreateFolderStructure(paths)
if err != nil {
return err
}
// ** Check if the .env file exists, if not create it
err = g.checkDotEnv(rootPath)
if err != nil {
return err
}
// Load the .env file into the environment
// read .env
err = godotenv.Load(rootPath + "/.env")
if err != nil {
return err
}
// ** create the loggers
infoLog, errorLog := g.CreateLoggers()
g.InfoLog = infoLog
g.ErrorLog = errorLog
g.AppName = os.Getenv("APP_NAME")
g.GoAppURL = os.Getenv("GO_APP_URL")
g.Debug, _ = strconv.ParseBool(os.Getenv("DEBUG"))
g.Version = Version
g.RootPath = rootPath
g.EncryptionKey = os.Getenv("KEY")
g.config = config{
port: os.Getenv("PORT"),
renderer: os.Getenv("RENDERER"),
cookie: cookieConfig{
name: os.Getenv("COOKIE_NAME"),
lifetime: os.Getenv("COOKIE_LIFETIME"),
persist: os.Getenv("COOKIE_PERSISTS"),
secure: os.Getenv("COOKIE_SECURE"),
domain: os.Getenv("COOKIE_DOMAIN"),
},
database: databaseConfig{
dsn: g.BuildDSN(),
databaseType: os.Getenv("DATABASE_TYPE"),
},
sessionType: os.Getenv("SESSION_TYPE"),
redis: redisConfig{
host: os.Getenv("REDIS_HOST"),
password: os.Getenv("REDIS_PASSWORD"),
prefix: os.Getenv("REDIS_PREFIX"),
},
}
secure := false
if strings.ToLower(os.Getenv("SECURE")) == "true" {
secure = true
} else {
secure = false
}
g.Server = Server{
ServerName: os.Getenv("SERVER_NAME"),
Port: os.Getenv("PORT"),
Secure: secure,
URL: os.Getenv("APP_URL"),
}
// ** create the mailer
g.Mail = g.createMailer()
// ** connect to the database
dbType := os.Getenv("DATABASE_TYPE")
if dbType != "" {
dsn := g.BuildDSN()
db, err := g.ConnectToDatabase(dbType, dsn)
if err != nil {
errorLog.Panicln(err)
os.Exit(1)
}
g.DB = Database{
DatabaseType: dbType,
Pool: db,
}
}
// ** create the scheduler
scheduler := cron.New()
g.Scheduler = scheduler
// ** Initilize the cache
if os.Getenv("CACHE") == "redis" || os.Getenv("SESSION_TYPE") == "redis" {
myRedisCache = g.createRedisCache()
redisPool = myRedisCache.Conn
g.Cache = myRedisCache
}
if os.Getenv("CACHE") == "badger" {
myBadgerCache = g.createBadgerCache()
g.Cache = myBadgerCache
badgerConn = myBadgerCache.Conn
// Run the garbage collector on the badger database
// every 24 hours
_, err = g.Scheduler.AddFunc("@daily", func() {
_ = myBadgerCache.Conn.RunValueLogGC(0.7)
})
if err != nil {
return err
}
}
// ** Create and initialize the session
session := session.Session{
CookieLifetime: g.config.cookie.lifetime,
CookiePersist: g.config.cookie.persist,
CookieName: g.config.cookie.name,
CookieDomain: g.config.cookie.domain,
CookieSecure: g.config.cookie.secure,
SessionType: g.config.sessionType,
}
// set the session store
switch g.config.sessionType {
case "redis":
session.RedisPool = myRedisCache.Conn
case "mysql", "postgres", "postgresql", "mariadb":
session.DBPool = g.DB.Pool
}
g.Session = session.InitSession()
//** create the routes
// Routes have to be created after the session has been initialized
// because the session is used in the routes
g.Routes = g.initRoutes().(*chi.Mux)
// ** Initialize and create the Jet views
if g.Debug {
var views = jet.NewSet(
jet.NewOSFileSystemLoader(fmt.Sprintf("%s/views", rootPath)),
jet.InDevelopmentMode(),
)
g.JetViews = views
} else {
var views = jet.NewSet(
jet.NewOSFileSystemLoader(fmt.Sprintf("%s/views", rootPath)),
)
g.JetViews = views
}
// ** create the renderer that renders our templates
// The renderer has to be created after the Jet views and session is initialized
// because the renderer uses the Jet views and session
g.createRenderer()
// ** Start the mail listener
go g.Mail.ListenForMail()
return nil
}