-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
executable file
·241 lines (227 loc) · 5.07 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"fmt"
"template"
"http"
"compress/gzip"
"mime"
"path"
"os"
"strconv"
"sort"
)
const errHtml = `
<html>
<head>
<style>
body{
font-family: Arial;
}
div{
padding: 15px 30px;
border: 1px solid black;
width: 640px;
margin-top: 40px;
margin-left: auto;
margin-right: auto;
background: white;
color: #369;
}
</style>
</head><body>
<div>
<h1>%v - %s</h1>
<p>Kengal 0.9.1</p>
</div>
</body>
</html>`
type ServerError struct {
Code int
Msg string
}
func (se *ServerError) Write(w http.ResponseWriter) {
w.WriteHeader(se.Code)
errOut := fmt.Sprintf(errHtml, se.Code, se.Msg)
w.Write([]byte(errOut))
w.Flush()
}
func Dispatch(w http.ResponseWriter) os.Error {
t := View.Themes.Current()
if t==nil{
se := &ServerError{404, "Not Found"}
se.Write(w)
return os.ENOENT
}
w.SetHeader("Content-Type", "text/html; charset=utf-8")
w.SetHeader("Content-Encoding", "gzip")
templ, err := template.Parse(t.Index, nil)
if err != nil {
return err
}
gz, err := gzip.NewWriter(w)
if err != nil {
return err
}
err = templ.Execute(gz, View)
if err != nil {
return err
}
gz.Close()
return nil
}
func ParseParameters(url, host string) os.Error {
View.Host = host
View.Imprint = false
View.Index = 0
View.Rubric = 0
View.Article = 0
dir, file := path.Split(url)
if file == "" {
// is Index Startpage
View.Index = 1
return nil
}
i, err := strconv.Atoi(file)
if err == nil {
//is Index non Startpage
View.Index = i
if ((i - 1) * PaginatorMax) > len(View.Articles.Index()) {
View.Index = 0
return nil
}
return nil
}
if file == "impressum" {
// is Impressum
View.Imprint = true
return nil
}
nextdir := path.Clean(dir)
dir, file = path.Split(nextdir)
i, err = strconv.Atoi(file)
if err != nil {
return err
}
if dir == "/kategorie/" {
//is Rubricpage
View.Rubric = i
if View.Rubrics.Index().Current() == nil {
View.Rubric = 0
}
return nil
}
if dir == "/artikel/" {
//is Articlepage
View.Article = i
if View.Articles.Index().Current() == nil {
View.Article = 0
}
return nil
}
return os.ENOTDIR
}
func Controller(w http.ResponseWriter, r *http.Request) {
ParseParameters(r.URL.Path, r.Host)
if ! sort.IsSorted(View.Articles){
sort.Sort(View.Articles)
}
w.SetHeader("Content-Type", "text/html; charset=utf-8")
if View.Blogs.Current() == nil {
se := &ServerError{403, "Forbidden"}
se.Write(w)
return
}
if View.Index != 0 {
w.SetHeader("Content-Encoding", "gzip")
View.HeadMeta = fmt.Sprintf(`<meta name="description" content="%s" />`, View.Blogs.Current().Description)
View.HeadMeta += fmt.Sprintf(`<meta name="keywords" content="%s" />`, View.Blogs.Current().Keywords)
Dispatch(w)
return
}
if View.Article != 0 {
w.SetHeader("Content-Encoding", "gzip")
View.HeadMeta = fmt.Sprintf(`<meta name="description" content="%s" />`, View.Articles.Current().Description)
View.HeadMeta += fmt.Sprintf(`<meta name="keywords" content="%s" />`, View.Articles.Current().Keywords)
Dispatch(w)
return
}
if View.Rubric != 0 {
w.SetHeader("Content-Encoding", "gzip")
View.HeadMeta = fmt.Sprintf(`<meta name="description" content="%s" />`, View.Rubrics.Current().Description)
View.HeadMeta += fmt.Sprintf(`<meta name="keywords" content="%s" />`, View.Rubrics.Current().Keywords)
Dispatch(w)
return
}
if View.Imprint {
w.SetHeader("Content-Encoding", "gzip")
View.HeadMeta = fmt.Sprintf(`<meta name="description" content="%s" />`, "Impressum")
View.HeadMeta += fmt.Sprintf(`<meta name="keywords" content="%s" />`, "Impressum")
Dispatch(w)
return
}
se := &ServerError{404, "Not Found"}
se.Write(w)
return
}
func Images(w http.ResponseWriter, r *http.Request) {
imagePath := path.Base(r.URL.Path)
mimeType := mime.TypeByExtension(path.Ext(imagePath))
w.SetHeader("Content-Type", mimeType)
w.SetHeader("Cache-Control", "max-age=31104000, public")
current := View.Themes.Current()
if current == nil{
se := &ServerError{404, "Not Found"}
se.Write(w)
return
}
for _, v := range View.Resources {
if v.Template == current.ID {
if v.Name == imagePath {
w.Write(v.Data)
w.Flush()
return
}
}
}
se := &ServerError{404, "Not Found"}
se.Write(w)
return
}
func GlobalController(w http.ResponseWriter, r *http.Request) {
imagePath := path.Base(r.URL.Path)
mimeType := mime.TypeByExtension(path.Ext(imagePath))
w.SetHeader("Content-Type", mimeType)
data := make([]byte, 0)
for _, v := range View.Globals {
if v.Name == imagePath {
data = v.Data
}
}
kind, _ := path.Split(mimeType)
if kind == "image/" {
w.Write(data)
return
}
w.SetHeader("Content-Encoding", "gzip")
gz, _ := gzip.NewWriter(w)
gz.Write(data)
gz.Close()
}
func Css(w http.ResponseWriter, r *http.Request) {
t := View.Themes.Current()
if t == nil{
se := &ServerError{404, "Not Found"}
se.Write(w)
return
}
gz, err := gzip.NewWriter(w)
if err != nil{
se := &ServerError{500, "Internal Error"}
se.Write(w)
return
}
w.SetHeader("Content-Encoding", "gzip")
w.SetHeader("Content-Type", "text/css")
gz.Write([]byte(t.Style))
gz.Close()
}