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

@ -207,6 +207,4 @@ func (c Attach) DownloadAll(noteId string) revel.Result {
// file, _ := os.Open(dir + "/" + filename) // file, _ := os.Open(dir + "/" + filename)
// fw.Seek(0, 0) // fw.Seek(0, 0)
return c.RenderBinary(fw, filename, revel.Attachment, time.Now()) // revel.Attachment return c.RenderBinary(fw, filename, revel.Attachment, time.Now()) // revel.Attachment
} }

View File

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

View File

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

View File

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