Files
leanote/app/controllers/init.go

160 lines
4.7 KiB
Go
Raw Normal View History

2014-05-07 13:06:24 +08:00
package controllers
import (
"github.com/leanote/leanote/app/info"
2014-11-09 16:24:19 +08:00
"github.com/leanote/leanote/app/lea/blog"
2015-11-13 17:58:41 +08:00
"github.com/leanote/leanote/app/service"
// . "github.com/leanote/leanote/app/lea"
2014-05-07 13:06:24 +08:00
"github.com/revel/revel"
"strings"
)
var userService *service.UserService
var noteService *service.NoteService
var trashService *service.TrashService
var notebookService *service.NotebookService
var noteContentHistoryService *service.NoteContentHistoryService
var authService *service.AuthService
var shareService *service.ShareService
var blogService *service.BlogService
var tagService *service.TagService
var pwdService *service.PwdService
var tokenService *service.TokenService
var suggestionService *service.SuggestionService
var albumService *service.AlbumService
var noteImageService *service.NoteImageService
var fileService *service.FileService
2014-09-21 22:05:04 +08:00
var attachService *service.AttachService
var configService *service.ConfigService
2014-10-22 16:20:45 +08:00
var emailService *service.EmailService
var sessionService *service.SessionService
2014-11-09 16:24:19 +08:00
var themeService *service.ThemeService
2014-05-07 13:06:24 +08:00
var pageSize = 1000
var defaultSortField = "UpdatedTime"
// 拦截器
// 不需要拦截的url
// Index 除了Note之外都不需要
var commonUrl = map[string]map[string]bool{"Index": map[string]bool{"Index": true,
"Login": true,
"DoLogin": true,
"Logout": true,
"Register": true,
"DoRegister": true,
"FindPasswword": true,
"DoFindPassword": true,
"FindPassword2": true,
"FindPasswordUpdate": true,
"Suggestion": true,
},
2015-10-17 17:15:33 +08:00
"Note": map[string]bool{"ToPdf": true},
2014-05-07 13:06:24 +08:00
"Blog": map[string]bool{"Index": true,
"View": true,
"AboutMe": true,
"Cate": true,
"ListCateLatest": true,
"Search": true,
2014-10-22 16:20:45 +08:00
"GetLikeAndComments": true,
"IncReadNum": true,
"ListComments": true,
"Single": true,
"Archive": true,
"Tags": true,
},
2014-05-07 13:06:24 +08:00
// 用户的激活与修改邮箱都不需要登录, 通过链接地址
"User": map[string]bool{"UpdateEmail": true,
"ActiveEmail": true,
},
"Oauth": map[string]bool{"GithubCallback": true},
"File": map[string]bool{"OutputImage": true, "OutputFile": true},
2015-11-13 17:58:41 +08:00
"Attach": map[string]bool{"Download": true /*, "DownloadAll": true*/},
2014-05-07 13:06:24 +08:00
}
2014-05-07 13:06:24 +08:00
func needValidate(controller, method string) bool {
// 在里面
if v, ok := commonUrl[controller]; ok {
2014-09-19 17:18:53 +08:00
// 在commonUrl里
2014-05-07 13:06:24 +08:00
if _, ok2 := v[method]; ok2 {
return false
}
return true
} else {
// controller不在这里的, 肯定要验证
return true
2014-05-07 13:06:24 +08:00
}
}
func AuthInterceptor(c *revel.Controller) revel.Result {
// 全部变成首字大写
var controller = strings.Title(c.Name)
var method = strings.Title(c.MethodName)
2014-05-07 13:06:24 +08:00
// 是否需要验证?
if !needValidate(controller, method) {
return nil
}
2014-05-07 13:06:24 +08:00
// 验证是否已登录
if userId, ok := c.Session["UserId"]; ok && userId != "" {
return nil // 已登录
}
2014-05-07 13:06:24 +08:00
// 没有登录, 判断是否是ajax操作
if c.Request.Header.Get("X-Requested-With") == "XMLHttpRequest" {
re := info.NewRe()
re.Msg = "NOTLOGIN"
return c.RenderJSON(re)
2014-05-07 13:06:24 +08:00
}
2014-05-07 13:06:24 +08:00
return c.Redirect("/login")
}
2014-09-22 19:15:28 +08:00
// 最外层init.go调用
// 获取service, 单例
func InitService() {
notebookService = service.NotebookS
noteService = service.NoteS
noteContentHistoryService = service.NoteContentHistoryS
trashService = service.TrashS
shareService = service.ShareS
userService = service.UserS
tagService = service.TagS
blogService = service.BlogS
tokenService = service.TokenS
noteImageService = service.NoteImageS
fileService = service.FileS
albumService = service.AlbumS
2014-09-22 19:15:28 +08:00
attachService = service.AttachS
pwdService = service.PwdS
suggestionService = service.SuggestionS
authService = service.AuthS
configService = service.ConfigS
2014-10-22 16:20:45 +08:00
emailService = service.EmailS
sessionService = service.SessionS
2014-11-09 16:24:19 +08:00
themeService = service.ThemeS
2014-09-22 19:15:28 +08:00
}
// 初始化博客模板
// 博客模板不由revel的
func initBlogTemplate() {
}
2014-05-07 13:06:24 +08:00
func init() {
// interceptor
// revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &Index{}) // Index.Note自己校验
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &Notebook{})
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &Note{})
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &Share{})
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &User{})
2015-10-14 18:47:01 +08:00
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &Album{})
2014-05-07 13:06:24 +08:00
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &File{})
2014-09-22 19:15:28 +08:00
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &Attach{})
2015-10-14 18:47:01 +08:00
// revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &Blog{})
2014-05-07 13:06:24 +08:00
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &NoteContentHistory{})
2014-05-07 13:06:24 +08:00
revel.OnAppStart(func() {
2014-11-09 16:24:19 +08:00
// 博客初始化模板
blog.Init()
2014-05-07 13:06:24 +08:00
})
}