-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
167 lines (133 loc) · 3.93 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
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
package main
import (
"errors"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"qiniupkg.com/api.v7/conf"
yaml "gopkg.in/yaml.v2"
"github.com/DoloresTeam/dolores-avatar"
"github.com/DoloresTeam/easemob-resty"
"github.com/DoloresTeam/organization"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
)
// Config ...
type Config struct {
Host string
Port int
RootDN string
RootPWD string
Subffix string
QNAccessKey string
QNSecretKey string
EMClientID string
EMSecret string
EMBaseURL string
AvatarFontFacePath string
}
var config = Config{}
// 自动生成头像服务
var avatarGenerator *avatar.Generator
// 封装调用组织上架构数据库逻辑
var org *organization.Organization
// 封装调用环信API
var em *easemob.EM
// 将短时间内多次服务器通知合并起来,发给客户端
var organizationViewChangeEvent = make(chan []string, 10)
var combiner = new(func(arg1 []string) error {
if em != nil {
return em.SendCMDMsg(arg1, `sync_organization`)
}
return errors.New(`em is nil`)
})
func main() {
configFilePath := flag.String(`path`, `./conf.yaml`, `配置文件路径`)
flag.Parse()
_, err := os.Stat(*configFilePath)
if !(err == nil || os.IsExist(err)) {
panic(`配置文件不存在`)
}
data, _ := ioutil.ReadFile(*configFilePath)
err = yaml.Unmarshal(data, &config)
if err != nil {
panic(`配置文件不正确`)
}
// 自动生成头像服务
_avatarGenerator, err := avatar.New(105, config.AvatarFontFacePath, 65)
if err != nil {
log.Fatal(err)
}
avatarGenerator = _avatarGenerator
// 七牛配置
conf.ACCESS_KEY = config.QNAccessKey
conf.SECRET_KEY = config.QNSecretKey
// 环信配置
em = easemob.New(config.EMClientID, config.EMSecret, config.EMBaseURL)
_org, err := organization.NewOrganizationWithSimpleBind(config.Subffix,
config.Host,
config.RootDN,
config.RootPWD,
config.Port,
organizationViewChangeEvent)
if err != nil {
panic(err.Error())
}
go func() {
for {
combiner.push(<-organizationViewChangeEvent)
}
}()
org = _org
r := gin.Default()
// r.Use(CORSMiddleware()) // 允许跨站请求
r.Use(static.Serve(`/`, static.LocalFile(`./webroot`, true)))
clientAuth := ClientJWTMiddleware()
r.POST(`/login`, clientAuth.LoginHandler)
auth := r.Group(`/api/v1`, clientAuth.MiddlewareFunc())
{
auth.GET(`/refresh_token`, clientAuth.RefreshHandler)
auth.GET(`/upload_token`, qiniuUploadToken)
auth.GET(`/profile`, profile)
auth.GET(`/basic_profile`, basicProfiles)
auth.POST(`/update_profile`, updateProfile)
auth.GET(`/organization`, organizationMap)
auth.GET(`/sync_organization/:version`, syncOrganization)
auth.POST(`/modify_password`, modifyPassword)
}
adminAuth := ServerJWTMiddleware()
r.POST(`/admin/login`, adminAuth.LoginHandler)
admin := r.Group(`/admin/v1`, adminAuth.MiddlewareFunc())
{
admin.GET(`type`, fetchTypes)
admin.GET(`type/:id`, typeByID)
admin.PUT(`type/:id`, editType)
admin.DELETE(`type/:id`, delType)
admin.POST(`type`, createType)
admin.GET(`permission`, fetchPermissions)
admin.GET(`u_permission`, fetchPermissionByRoleUPID)
admin.GET(`p_permission`, fetchPermissionByRolePPID)
admin.GET(`permission/:id`, permissionByID)
admin.PUT(`permission/:id`, editPermission)
admin.DELETE(`permission/:id`, delPermission)
admin.POST(`permission`, createPermission)
admin.GET(`role`, fetchRoles)
admin.POST(`role`, createRole)
admin.GET(`role/:id`, roleByID)
admin.PUT(`role/:id`, editRole)
admin.DELETE(`role/:id`, delRole)
admin.GET(`member`, fetchMembers)
admin.POST(`member`, createMember)
admin.GET(`member/:id`, memberByID)
// admin.DELETE(`member/:id`, delMember)
admin.PUT(`member/:id`, editMember)
admin.GET(`department`, fetchDepartment)
admin.POST(`department`, createDepartment)
admin.GET(`department/:id`, departmentByID)
admin.PUT(`department/:id`, editDepartment)
// admin.DELETE(`department/:id`, delDepartment)
}
http.ListenAndServe(`:3280`, r)
}