-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (47 loc) · 1.2 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
package main
import (
"bookshelf/filters"
"bookshelf/models"
_ "bookshelf/routers"
"bookshelf/utils"
"fmt"
"os"
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/server/web"
_ "github.com/mattn/go-sqlite3"
"github.com/joho/godotenv"
)
func init() {
err := godotenv.Load(".env")
if err != nil {
fmt.Println("Error loading .env file")
}
orm.RegisterDriver("sqlite3", orm.DRSqlite)
orm.RegisterDataBase("default", "sqlite3", "file:bookshelf.db")
}
func startDb(force bool) error {
// Database alias.
name := "default"
// Print log.
verbose := true
// Debug
orm.Debug = false
// Error.
err := orm.RunSyncdb(name, force, verbose)
if force {
utils.InitialData()
}
return err
}
func main() {
DB_INITIAL_FORCE := os.Getenv("DB_INITIAL_FORCE") == "true"
if err := startDb(DB_INITIAL_FORCE); err != nil {
panic(err)
}
web.BeeApp.InsertFilter("/logout", web.BeforeRouter, filters.AuthFilter)
web.BeeApp.InsertFilter("/book/*", web.BeforeRouter, filters.AuthFilter)
web.BeeApp.InsertFilter("/review/get", web.BeforeRouter, filters.AuthFilter)
web.BeeApp.InsertFilter("/review/new", web.BeforeRouter, filters.AuthFilter)
web.AddFuncMap("HasBook", models.HasBook)
web.Run()
}