forked from easykoo/go-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
164 lines (145 loc) · 5.63 KB
/
server.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
package main
import (
"github.com/lc3091/binding"
"github.com/lc3091/sessions"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"github.com/russross/blackfriday"
. "github.com/lc3091/go-blog/auth"
. "github.com/lc3091/go-blog/common"
"github.com/lc3091/go-blog/handler"
"github.com/lc3091/go-blog/middleware"
"github.com/lc3091/go-blog/model"
"encoding/gob"
"html/template"
"os"
"time"
)
func init() {
SetConfig()
SetLog()
gob.Register(model.User{})
gob.Register(model.Settings{})
Log.Debug("server initializing...")
}
func newMartini() *martini.ClassicMartini {
r := martini.NewRouter()
m := martini.New()
m.Use(middleware.GetLogger())
m.Map(model.SetEngine())
m.Use(martini.Recovery())
m.Use(martini.Static("public"))
m.MapTo(r, (*martini.Routes)(nil))
m.Action(r.Handle)
m.Use(sessions.Sessions("my_session", middleware.NewDbStore(7*24*60*60)))
m.Use(render.Renderer(render.Options{
Directory: "templates",
Extensions: []string{".tmpl", ".html"},
Charset: "UTF-8",
Funcs: []template.FuncMap{
{
"formatTime": func(args ...interface{}) string {
return args[0].(time.Time).Format("Jan _2 15:04")
},
"cnFormatTime": func(args ...interface{}) string {
return args[0].(time.Time).Format("2006-01-02 15:04")
},
"mdToHtml": func(args ...interface{}) template.HTML {
return template.HTML(string(blackfriday.Run([]byte(args[0].(string)))))
},
"unescaped": func(args ...interface{}) template.HTML {
return template.HTML(args[0].(string))
},
"equal": func(args ...interface{}) bool {
return args[0] == args[1]
},
"tsl": func(lang string, format string) string {
return Translate(lang, format)
},
"tslf": func(lang string, format string, args ...interface{}) string {
return Translatef(lang, format, args...)
},
"privilege": func(user interface{}, module int) bool {
if user == nil {
return false
}
return CheckPermission(user, module)
},
"plus": func(args ...int) int {
var result int
for _, val := range args {
result += val
}
return result
},
},
},
}))
m.Use(middleware.InitContext())
m.Use(middleware.RecordVisit())
return &martini.ClassicMartini{m, r}
}
func main() {
m := newMartini()
m.Get("/", handler.Blog)
m.Get("/index", handler.Blog)
m.Get("/about", handler.About)
m.Any("/contact", binding.Form(model.Feedback{}), handler.ContactHandler)
m.Get("/language/change/:lang", handler.LangHandler)
m.Group("/user", func(r martini.Router) {
r.Any("/all", AuthRequest(Module_Account), handler.AllUserHandler)
r.Any("/logout", handler.LogoutHandler)
r.Any("/login", binding.Form(model.UserLoginForm{}), handler.LoginHandler)
r.Any("/register", binding.Form(model.UserRegisterForm{}), handler.RegisterHandler)
r.Any("/delete", AuthRequest(Module_Account), handler.DeleteUsers)
r.Any("/delete/:id", AuthRequest(Module_Account), handler.DeleteUser)
r.Any("/role", AuthRequest(Module_Account), handler.SetRole)
r.Any("/ban/:id", AuthRequest(Module_Account), handler.BanUser)
r.Any("/lift/:id", AuthRequest(Module_Account), handler.LiftUser)
})
m.Group("/profile", func(r martini.Router) {
r.Any("/profile", AuthRequest(SignInRequired), binding.Form(model.User{}), handler.ProfileHandler)
r.Any("/preferences", AuthRequest(SignInRequired), handler.PreferencesHandler)
r.Any("/password", AuthRequest(SignInRequired), binding.Form(model.Password{}), handler.PasswordHandler)
r.Any("/checkEmail", AuthRequest(SignInRequired), binding.Form(model.User{}), handler.CheckEmail)
})
m.Group("/admin", func(r martini.Router) {
r.Get("/dashboard", AuthRequest(SignInRequired), handler.DashboardHandler)
r.Any("/settings", AuthRequest(Module_Admin), binding.Form(model.Settings{}), handler.SettingsHandler)
r.Post("/about", AuthRequest(Module_Admin), handler.AboutHandler)
})
m.Group("/feedback", func(r martini.Router) {
r.Any("/all", AuthRequest(Module_Feedback), handler.AllFeedback)
r.Any("/info", AuthRequest(Module_Feedback), handler.FeedbackInfo)
r.Any("/delete", AuthRequest(Module_Feedback), handler.DeleteFeedbackArray)
r.Any("/delete/:id", AuthRequest(Module_Feedback), handler.DeleteFeedback)
r.Any("/view/:id", AuthRequest(Module_Feedback), handler.ViewFeedback)
})
m.Group("/link", func(r martini.Router) {
r.Any("/all", AuthRequest(Module_Link), handler.AllLink)
r.Any("/insert", AuthRequest(Module_Link), binding.Form(model.Link{}), handler.InsertLink)
r.Any("/delete", AuthRequest(Module_Link), handler.DeleteLinkArray)
r.Any("/delete/:id", AuthRequest(Module_Link), handler.DeleteLink)
r.Any("/edit/:id", AuthRequest(Module_Link), handler.EditLink)
})
m.Group("/blog", func(r martini.Router) {
r.Any("", handler.Blog)
r.Any("/tag/:tag", handler.BlogWithTag)
r.Any("/view/:id", handler.ViewBlog)
r.Any("/all", AuthRequest(Module_Blog), handler.AllBlog)
r.Any("/publish", AuthRequest(Module_Blog), binding.Form(model.Blog{}), handler.PublishBlog)
r.Any("/save", AuthRequest(Module_Blog), binding.Form(model.Blog{}), handler.SaveBlog)
r.Any("/edit/:id", AuthRequest(Module_Blog), handler.EditBlog)
r.Any("/delete", AuthRequest(Module_Blog), handler.DeleteBlogArray)
r.Any("/delete/:id", AuthRequest(Module_Blog), handler.DeleteBlog)
r.Any("/forbid/:id", AuthRequest(Module_Blog), handler.ForbidBlog)
r.Any("/permit/:id", AuthRequest(Module_Blog), handler.PermitBlog)
})
m.Group("/blog/comment", func(r martini.Router) {
r.Any("", handler.Comment)
r.Any("/delete/:blogId/:seq", AuthRequest(Module_Blog), handler.DeleteComment)
})
Log.Info("server is started...")
os.Setenv("PORT", Cfg.MustValue("", "http_port", "3000"))
m.Run()
}