-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal.go
82 lines (57 loc) · 2.21 KB
/
portal.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
package main
import (
"net/http"
"path"
"strconv"
"varname/app/controllers"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
func (self *VarName) runPortal() {
m := martini.Classic()
m.Use(render.Renderer(render.Options{
Directory: path.Join(self.root, "app/views/"+self.config.Theme),
Extensions: []string{".gohtml", ".tmpl", ".html"},
}))
m.Get("/search", martiniSafeHandler("search", self.handleSearch))
m.Get("/e/:entry/new", martiniSafeHandler("new", self.handleNewEntry))
m.Get("/e/:entry/edit", martiniSafeHandler("edit", self.handleEditEntry))
m.Get("/e/:entry", martiniSafeHandler("entry", self.handleEntry))
m.Get("/(?P<page>(about|terms))", martiniSafeHandler("page", self.handlePage))
m.Get("/**", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/index.html", http.StatusTemporaryRedirect)
})
m.RunOnAddr(self.config.PortalAddr)
}
func (self *VarName) handleIndex(req *http.Request, params martini.Params, data map[string]interface{}) {
}
func (self *VarName) handleSearch(req *http.Request, params martini.Params, data map[string]interface{}) {
var err error
var offset int = 0
var count int = self.config.ResultsPerPage
qs := req.FormValue("q")
qs = controllers.Normalize(qs)
offset, _ = strconv.Atoi(req.FormValue("start"))
cc, err := req.Cookie("rpp")
if err == nil && cc != nil {
count, _ = strconv.Atoi(cc.Value)
}
data["Title"] = qs
data["PageClass"] = "search"
data["SearchString"] = qs
data["SearchList"], data["SearchNav"], err = controllers.Search(offset, count, qs)
data["SearchRelates"] = []string{"", ""} //TODO
check(err)
}
func (self *VarName) handleEntry(req *http.Request, params martini.Params, data map[string]interface{}) {
data["Entry"] = params["entry"]
}
func (self *VarName) handleNewEntry(req *http.Request, params martini.Params, data map[string]interface{}) {
}
func (self *VarName) handleEditEntry(req *http.Request, params martini.Params, data map[string]interface{}) {
data["Entry"] = params["entry"]
}
func (self *VarName) handlePage(req *http.Request, params martini.Params, data map[string]interface{}) {
data["PageHead"] = params["page"]
data["PageContent"] = "This is " + params["page"] + " page."
}