-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
executable file
·63 lines (55 loc) · 1.61 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
package main
import (
"encoding/gob"
"encoding/json"
_ "pet/docs"
"pet/models"
_ "pet/routers"
"pet/utils"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/orm"
_ "github.com/astaxie/beego/session/mysql"
_ "github.com/astaxie/beego/session/redis"
_ "github.com/go-sql-driver/mysql"
)
func init() {
dbAddress, _ := beego.Config("String", "DbAddress", "")
orm.RegisterDriver("mysql", orm.DR_MySQL)
orm.RegisterDataBase("default", "mysql", dbAddress.(string))
orm.Debug = true
beego.EnableAdmin = true
beego.SetLevel(beego.LevelDebug)
beego.SetLogFuncCall(true)
gob.Register(models.Users{})
gob.Register(models.UserPosition{})
}
func main() {
if beego.RunMode == "dev" {
beego.DirectoryIndex = true
beego.SetStaticPath("/doc", "static/swagger")
}
beego.SetStaticPath("/s", "static/source")
beego.EnableDocs = true
beego.InsertFilter("/*", beego.BeforeRouter, FilterUser)
beego.Run()
}
var FilterUser = func(ctx *context.Context) {
user := ctx.Input.Session("user")
if user == nil && ctx.Request.URL.Path != "/v1/users/login" && ctx.Request.URL.Path[:4] != "/doc" && !excludeLoginPath(ctx.Request.URL.Path) {
outPut := helper.Reponse(1, nil, "请先登录")
b, _ := json.Marshal(outPut)
ctx.Output.Header("Access-Control-Allow-Origin", "*")
ctx.WriteString(string(b))
}
ctx.Output.Header("Access-Control-Allow-Origin", "*")
}
func excludeLoginPath(currentPath string) bool {
excludeLoginPath := []string{"/v1/users/register", "/v1/photos/top10", "/v1/photos/"}
for _, v := range excludeLoginPath {
if v == currentPath {
return true
}
}
return false
}