-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (106 loc) · 2.49 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/cgi"
"os"
"time"
"github.com/pkg/errors"
)
var f *os.File
// A GuestbookLine object contains a single guestbook entry
type GuestbookLine struct {
Name string `json:"name"`
Email string `json:"email"`
Message string `json:"message"`
Timestamp time.Time `json:"timestamp"`
}
func ParseGuestbook(f *os.File) ([]GuestbookLine, error) {
scanner := bufio.NewScanner(f)
f.Seek(0, 0)
guestbooks := []GuestbookLine{}
for scanner.Scan() {
t := []byte(scanner.Text())
gbk := GuestbookLine{}
err := json.Unmarshal(t, &gbk)
if err != nil {
return guestbooks, err
}
guestbooks = append(guestbooks, gbk)
}
return guestbooks, nil
}
// WriteGuestbook takes in a string line that contains the json output of a
// guestbook entry and saves it to disk
func WriteGuestbook(line *GuestbookLine, file *os.File) error {
jsonBytes, err := json.Marshal(line)
if err != nil {
return errors.Wrapf(err, "error marshalling")
}
_, err = file.Write(jsonBytes)
if err != nil {
return errors.Wrapf(err, "error writing file")
}
_, err = file.WriteString("\n")
if err != nil {
return errors.Wrapf(err, "error writing string")
}
return nil
}
func doPost(w http.ResponseWriter, r *http.Request, f *os.File) {
r.ParseForm()
post := r.PostForm
name := post.Get("name")
email := post.Get("email")
message := post.Get("message")
date := time.Now()
// save the content into a file
line := &GuestbookLine{
Name: name,
Email: email,
Message: message,
Timestamp: date,
}
err := WriteGuestbook(line, f)
if err != nil {
log.Fatal(err)
}
for k := range post {
fmt.Fprintln(w, "PostForm", k+":", post.Get(k))
}
}
func doGet(w http.ResponseWriter, r *http.Request, f *os.File) {
guestbooks, err := ParseGuestbook(f)
if err != nil {
log.Fatal(err)
}
for _, v := range guestbooks {
fmt.Fprintf(w, "<div><p>Name: %s<br />Time: %s<br />Message: %s</p></div>", v.Name, v.Timestamp, v.Message)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
f, err := os.OpenFile("guestbook.dat", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
header := w.Header()
header.Set("Content-Type", "text/html; charset=utf-8")
switch r.Method {
case "GET":
doGet(w, r, f)
case "POST":
doPost(w, r, f)
default:
fmt.Fprintln(w, "no")
}
}
func main() {
err := cgi.Serve(http.HandlerFunc(handler))
if err != nil {
fmt.Println(err)
}
}