-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (89 loc) · 2.04 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
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
package main
import (
"fmt"
"log"
"github.com/iris-contrib/middleware/cors"
"github.com/iris-contrib/middleware/logger"
"github.com/kataras/iris"
"github.com/spf13/viper"
"github.com/tidwall/buntdb"
)
func main() {
viper.SetConfigName("tyt")
viper.SetConfigType("toml")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
initConfig()
// Open the data.db file. It will be created if it doesn't exist.
db, err := buntdb.Open("data.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
loadUsers()
loadLectures(db)
getCurrentUser = makeGetCurrentUser(db)
iris.Config.VScheme = viper.GetString("vscheme")
iris.Config.VHost = viper.GetString("vhost")
initOAuth(db)
installAPI(db)
staticContent()
iris.Listen(":8080")
}
func installAPI(db *buntdb.DB) {
iris.Use(logger.New())
iris.Use(cors.Default())
Auth{db: db}.install()
iris.Get("/api/me", func(ctx *iris.Context) {
user := getCurrentUser(ctx)
if user == nil {
ctx.NotFound()
return
}
ctx.JSON(200, user)
})
// users API
// TODO validation, new user must have unique email and login
API{
db: db,
resource: "user",
collection: "users",
factory: func() IEntity { return &User{} },
onCreate: func(tx *buntdb.Tx, r IEntity) error {
return onUserInserted(tx, r.(*User))
},
}.install()
// teams API
API{
db: db,
resource: "team",
collection: "teams",
factory: func() IEntity { return &Team{} },
}.install()
// organizations API
API{
db: db,
resource: "org",
collection: "orgs",
factory: func() IEntity { return &Organization{} },
}.install()
// events API
API{
db: db,
resource: "event",
collection: "events",
factory: func() IEntity { return &Event{} },
}.install()
// spectacles API
API{
db: db,
resource: "spectacle",
collection: "spectacles",
factory: func() IEntity { return &Spectacle{} },
}.install()
initPresenceAPI(db)
// TODO api to change user password
}