/
pages.go
218 lines (192 loc) · 4.89 KB
/
pages.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"github.com/BurntSushi/toml"
"github.com/Sirupsen/logrus"
"html/template"
"io"
"os"
"path/filepath"
"time"
)
type Renderer interface {
Render(w io.Writer, page Page)
}
type TemplateRenderer struct {
Dir string
*template.Template
}
func (tr *TemplateRenderer) Render(w io.Writer, page Page) {
if page.Template == "" {
page.Template = "default"
}
if tr.Template == nil {
logger.WithField("nil-template", tr.Template == nil).Debug("Needs to load templates")
tr.Load() //Not loaded yet!
}
tr.ExecuteTemplate(w, page.Template+".tmpl.html", page)
}
func (tr *TemplateRenderer) Load() {
if tr.Dir == "" {
tr.Template = template.New("default.tmpl.html")
template.Must(tr.Parse(pageTemplate))
} else {
tr.Template = template.New("_")
template.Must(tr.ParseGlob(filepath.Join(tr.Dir, "*.tmpl.html")))
}
}
type Pages interface {
Get(name string) Page
}
const (
MainMenuPage = "index"
LoginPage = "login"
LoginSuccessPage = "login_success"
RegistrationPage = "register"
RegistrationSuccessPage = "register_success"
LogoutPage = "logout"
AdminPage = "admin"
Error404Page = "404"
)
type Page struct {
Template string
Title template.HTML
Head template.HTML
Content template.HTML
}
type FSPages struct {
webdir string
cache map[string]*pageCacheData
}
type pageCacheData struct {
Page
NotFound bool
Filename string
LastLoadTime time.Time
}
func NewFSPages(dir string) *FSPages {
return &FSPages{
webdir: dir,
cache: make(map[string]*pageCacheData),
}
}
func (fsp FSPages) Get(name string) Page {
if data, ok := fsp.cache[name]; ok {
//File is in cache, Check if update is necessary
finfo, err := os.Stat(data.Filename)
if err != nil || finfo.ModTime().After(data.LastLoadTime) {
if os.IsNotExist(err) {
if name == Error404Page {
return Page{ //Default to prevent infinite loops if the error page can not be loaded.
Title: "404 - Page Not Found",
Content: "<p>Could not find neither the requested page not the proper 404 page.</p>",
}
}
return fsp.Get(Error404Page)
} else {
fsp.Load(name) //Modification time after last load
return fsp.Get(name) //Recursive call. Should be loaded now, but might not been found.
}
}
return data.Page
} else {
//Not loaded before. Do it.
fsp.Load(name)
return fsp.Get(name) //Recursive call since it is now in the cache.
}
}
func (fsp FSPages) Load(name string) {
pcd := pageCacheData{
Filename: filepath.Join(fsp.webdir, "pages", name) + ".toml",
LastLoadTime: time.Now(),
}
logger.WithFields(logrus.Fields{
"name": name,
"filename": pcd.Filename,
}).Info("Loading page")
_, err := toml.DecodeFile(pcd.Filename, &pcd.Page)
if os.IsNotExist(err) {
pcd.NotFound = true
}
fsp.cache[name] = &pcd
}
type ConstPages map[string]Page
var constPages = make(ConstPages)
func (cp ConstPages) Get(name string) Page {
if page, ok := cp[name]; ok {
return page
} else {
return Page{
Title: "<code>/dev/nil</code>",
Head: "",
Content: `
<p>
You appear to have tried to access a page that does not exist.
</p>`,
}
}
}
const pageTemplate = `<html>
<head>
<title>AuthProx - {{.Title}}</title>
<link rel="stylesheet" href="/proxy/static/master.css" media="screen" charset="utf-8">
<link href='http://fonts.googleapis.com/css?family=Roboto+Condensed' rel='stylesheet' type='text/css'>
{{.Head}}
</head>
<body>
<div id="card">
<h3>AuthProx</h3>
<h1>{{.Title}}</h1>
{{.Content}}
</div>
</body>
</html>
`
func init() {
constPages[LoginPage] = Page{
Title: "Login",
Content: `
<form method="POST" action="/proxy/login">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<input type="submit" value="Login">
</form>`,
}
constPages[LoginSuccessPage] = Page{
Title: "Login Success!",
Content: `
<p>
You have successfully logged in.
<a href="/">Continue</a>
</p>`,
}
constPages[RegistrationPage] = Page{
Title: "Registrations",
Head: "<script src='https://www.google.com/recaptcha/api.js'></script>",
Content: `
<form method="POST" action="/proxy/register">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<div class="g-recaptcha" data-sitekey="6LcMDgoTAAAAALJTFmdzPieTUheKAdghSG9q1_D-"></div>
<input type="submit" value="Register">
</form>`,
}
constPages[RegistrationSuccessPage] = Page{
Title: "Registration Successfull!",
Content: `
<p>
You have successfully registered your new account.
<a href="/proxy/login">Continue to login page</a>
</p>`,
}
constPages[LogoutPage] = Page{
Title: "Logout Successfull",
Content: `
<p>
You have successfully been logged out.
</p>
<p>
Please return at a later time!
</p>
`,
}
}