-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.go
87 lines (75 loc) · 1.79 KB
/
board.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
package main
import (
"bytes"
"fmt"
"math/rand"
"net/http"
"text/template"
)
type ThreadInfo struct {
thread []PostInfo
}
type PostInfo struct {
Name string
Email string
Message string
IP string
BlessedByAbu bool
}
var templatePost = template.Must(template.New("post").Funcs(
template.FuncMap{"blessed": blessed, "linkWidget": inflateLinkWidgets},
).Parse(
`
<hr>
<div style=border:1px><a href=mailto:{{.Email}}>{{.Name}}</a><br>{{.Message|linkWidget}}{{.BlessedByAbu|blessed}}</div>
`))
var templateThread = template.Must(template.New("thread").Parse(
`
<ul>
{{range .Thread}}
<li><a href=/board/{{.ID}}>{{.ID}}</a>
{{end}}
</ul>
`))
func blessed(arg bool) string {
if arg {
return "<br><br>Пост благославлен Абу"
}
return ""
}
func (p PostInfo) String() string {
buf := &bytes.Buffer{}
templatePost.Execute(buf, p)
return buf.String()
}
func (t ThreadInfo) String() string {
buf := &bytes.Buffer{}
templateThread.Execute(buf, t)
return buf.String()
}
var boardhash = []PostInfo{}
func boardHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
msg := r.PostFormValue("message")
msg = linkReplace(msg)
tmp := PostInfo{
Name: "Anonymous",
Email: "",
Message: msg,
IP: r.RemoteAddr,
BlessedByAbu: rand.Int()%10 > 8,
}
boardhash = append(boardhash, tmp)
w.Header().Add("Location", "/board/")
w.WriteHeader(http.StatusSeeOther)
return
}
w.Header().Add("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "%v", boardhash)
fmt.Fprintf(w, `
<form action=/board/ method=post>
<textarea cols=50 rows=5 name=message></textarea><br>
<input type=submit>
</form>
`)
}