init service

This commit is contained in:
life
2014-09-22 19:15:28 +08:00
parent b89721a0f4
commit 416dd77717
5 changed files with 80 additions and 50 deletions

View File

@ -208,5 +208,3 @@ func (c Attach) DownloadAll(noteId string) revel.Result {
// fw.Seek(0, 0)
return c.RenderBinary(fw, filename, revel.Attachment, time.Now()) // revel.Attachment
}

View File

@ -3,18 +3,16 @@ package controllers
import (
"github.com/leanote/leanote/app/service"
"github.com/leanote/leanote/app/info"
// . "github.com/leanote/leanote/app/lea"
"github.com/revel/revel"
"strings"
)
// 该文件初始化所有service方法
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
@ -22,8 +20,8 @@ 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
var attachService *service.AttachService
@ -59,6 +57,7 @@ var commonUrl = map[string]map[string]bool{"Index": map[string]bool{"Index": tru
},
"Oauth": map[string]bool{"GithubCallback": true},
"File": map[string]bool{"OutputImage": true, "OutputFile": true},
"Attach": map[string]bool{"Download": true, "DownloadAll": true},
}
func needValidate(controller, method string) bool {
// 在里面
@ -78,13 +77,11 @@ func AuthInterceptor(c *revel.Controller) revel.Result {
var controller = strings.Title(c.Name)
var method = strings.Title(c.MethodName)
// 是否需要验证?
if !needValidate(controller, method) {
return nil
}
// 验证是否已登录
if userId, ok := c.Session["UserId"]; ok && userId != "" {
return nil // 已登录
@ -100,6 +97,27 @@ func AuthInterceptor(c *revel.Controller) revel.Result {
return c.Redirect("/login")
}
// 最外层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
attachService = service.AttachS
pwdService = service.PwdS
suggestionService = service.SuggestionS
authService = service.AuthS
}
func init() {
// interceptor
// revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &Index{}) // Index.Note自己校验
@ -108,24 +126,10 @@ func init() {
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &Share{})
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &User{})
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &File{})
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &Attach{})
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &Blog{})
revel.InterceptFunc(AuthInterceptor, revel.BEFORE, &NoteContentHistory{})
// service
userService = &service.UserService{}
noteService = &service.NoteService{}
trashService = &service.TrashService{}
notebookService = &service.NotebookService{}
noteContentHistoryService = &service.NoteContentHistoryService{}
authService = &service.AuthService{}
shareService = &service.ShareService{}
blogService = &service.BlogService{}
tagService = &service.TagService{}
pwdService = &service.PwdService{}
tokenService = &service.TokenService{}
suggestionService = &service.SuggestionService{}
revel.OnAppStart(func() {
leanoteUserId, _ = revel.Config.String("adminUsername")
siteUrl, _ = revel.Config.String("site.url")

View File

@ -3,6 +3,8 @@ package app
import (
"github.com/revel/revel"
. "github.com/leanote/leanote/app/lea"
"github.com/leanote/leanote/app/service"
"github.com/leanote/leanote/app/controllers"
_ "github.com/leanote/leanote/app/lea/binder"
"reflect"
"fmt"
@ -116,5 +118,8 @@ func init() {
// init Email
revel.OnAppStart(func() {
InitEmail()
service.InitService()
controllers.InitService()
})
}

View File

@ -8,30 +8,53 @@ import (
// 初始化, 实例service
// 为了共享service
var notebookService *NotebookService
var noteService *NoteService
var noteContentHistoryService *NoteContentHistoryService
var trashService *TrashService
var shareService *ShareService
var userService *UserService
var tagService *TagService
var blogService *BlogService
var tokenService *TokenService
var noteImageService *NoteImageService
var fileService *FileService
var attachService *AttachService
var notebookService, NotebookS *NotebookService
var noteService, NoteS *NoteService
var noteContentHistoryService, NoteContentHistoryS *NoteContentHistoryService
var trashService, TrashS *TrashService
var shareService, ShareS *ShareService
var userService, UserS *UserService
var tagService, TagS *TagService
var blogService, BlogS *BlogService
var tokenService, TokenS *TokenService
var noteImageService, NoteImageS *NoteImageService
var fileService, FileS *FileService
var albumService, AlbumS *AlbumService
var attachService, AttachS *AttachService
var PwdS *PwdService
var SuggestionS *SuggestionService
var AuthS *AuthService
func init() {
notebookService = &NotebookService{}
noteService = &NoteService{}
noteContentHistoryService = &NoteContentHistoryService{}
trashService = &TrashService{}
shareService = &ShareService{}
userService = &UserService{}
tagService = &TagService{}
blogService = &BlogService{}
tokenService = &TokenService{}
fileService = &FileService{}
attachService = &AttachService{}
noteImageService = &NoteImageService{}
// onAppStart调用
func InitService() {
NotebookS = &NotebookService{}
NoteS = &NoteService{}
NoteContentHistoryS = &NoteContentHistoryService{}
TrashS = &TrashService{}
ShareS = &ShareService{}
UserS = &UserService{}
TagS = &TagService{}
BlogS = &BlogService{}
TokenS = &TokenService{}
NoteImageS = &NoteImageService{}
FileS = &FileService{}
AlbumS = &AlbumService{}
AttachS = &AttachService{}
PwdS = &PwdService{}
SuggestionS = &SuggestionService{}
AuthS = &AuthService{}
notebookService = NotebookS
noteService = NoteS
noteContentHistoryService = NoteContentHistoryS
trashService = TrashS
shareService = ShareS
userService = UserS
tagService = TagS
blogService = BlogS
tokenService = TokenS
noteImageService = NoteImageS
fileService = FileS
albumService = AlbumS
attachService = AttachS
}

View File

@ -117,4 +117,4 @@
writeScripts();
})(this);
// $hash: 2adcfc4a6218fe6af3637ab6842f075f
// $hash: 4128985475d955f4e25ca4be38045cbb