-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.go
More file actions
89 lines (72 loc) · 1.5 KB
/
app.go
File metadata and controls
89 lines (72 loc) · 1.5 KB
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
package main
import (
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"log"
"gopkg.in/go-playground/validator.v9"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/gin-contrib/cors"
"github.com/RangelReale/osin"
)
type App struct {
engine *gin.Engine
db *gorm.DB
responseHandler ResponseHandler
requestHandler RequestHandler
validator *validator.Validate
oauth2Server *osin.Server
}
func InitApp() *App {
db, err := gorm.Open("sqlite3", "./notes.db")
db.LogMode(true)
db.SingularTable(true)
if err != nil {
log.Fatal("Could not connect database")
}
// Migrate the schema
db.AutoMigrate(
&Note{},
&Tag{},
&User{},
&OAuth2Client{},
&OAuth2RefreshToken{},
&OAuth2AccessToken{},
)
validator := NewValidator()
responseHandler := NewResponseHandler()
r := gin.Default()
r.Use(cors.Default())
r.NoRoute(responseHandler.NoRoute)
oauth2 := NewOAuth2Server(db)
app := &App{
r,
db,
responseHandler,
NewRequestHandler(),
validator,
oauth2,
}
InitHandlers(app)
return app
}
func (app *App) Run() {
app.Engine().Run()
}
func (app *App) Engine() *gin.Engine {
return app.engine
}
func (app *App) Db() *gorm.DB {
return app.db
}
func (app *App) ResponseHandler() ResponseHandler {
return app.responseHandler
}
func (app *App) Validator() *validator.Validate {
return app.validator
}
func (app *App) OAuth2Server() *osin.Server {
return app.oauth2Server
}
func (app *App) RequestHandler() RequestHandler {
return app.requestHandler
}