-
Notifications
You must be signed in to change notification settings - Fork 0
/
coderegistry.go
207 lines (188 loc) · 5.65 KB
/
coderegistry.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
package main
import (
"encoding/gob"
"fmt"
"net"
"net/http"
"os"
"strconv"
"syscall"
"time"
)
var messageCodes = make(map[int]*MessageCode)
type MessageCode struct {
Code int
Deleted bool
Title string
Description string
Created time.Time
CreatedIp net.IP
Modified time.Time
ModifiedIp net.IP
}
var gobFilename = "codes.gob"
//
func main() {
gob.Register(messageCodes)
file, err := os.Open(gobFilename)
if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOENT {
fmt.Println("No existing file. We'll just start from scratch.")
} else if err != nil {
fmt.Println(err)
os.Exit(1)
} else {
defer file.Close()
decoder := gob.NewDecoder(file)
err = decoder.Decode(&messageCodes)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
http.HandleFunc("/", indexHandler)
http.HandleFunc("/add", addHandler)
http.HandleFunc("/modify", modifyHandler)
fmt.Println(http.ListenAndServeTLS(":13579", "ssl.crt", "ssl.key", nil))
os.Exit(1)
}
//
func outputHeader(w http.ResponseWriter, title string) {
fmt.Fprintf(w, "<html>\n<head>\n<title>%s • Bumble Message Code Registry</title>\n", title)
fmt.Fprintf(w, "<style>\n.submit {width:80px}\n.deleted * {color:#CCC}\n.deleted .undelete {color:#000}\n.id {}\n.title {font-weight:bold}\n.title input {font-weight:bold;width:300px}\n.description {}\n.description input {width:300px}\n</style>\n")
fmt.Fprintf(w, "</head>\n<body><h1>%s</h1>\n", title)
}
func outputFooter(w http.ResponseWriter) {
fmt.Fprintln(w, "</body>\n</html>")
}
//
var modlock = make(chan bool, 1)
func addCode(title string, description string, ip net.IP) *MessageCode {
modlock <- true
code := new(MessageCode)
code.Code = len(messageCodes)
code.Deleted = false
code.Title = title
code.Description = description
code.Created = time.Now().UTC()
code.Modified = code.Created
code.CreatedIp = ip
code.ModifiedIp = code.CreatedIp
messageCodes[code.Code] = code
file, err := os.Create("codes.gob")
if err != nil {
<-modlock
return nil
}
defer file.Close()
encoder := gob.NewEncoder(file)
err = encoder.Encode(messageCodes)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
<-modlock
return code
}
func modifyCode(id int, title string, description string, ip net.IP) *MessageCode {
modlock <- true
code := messageCodes[id]
code.Title = title
code.Description = description
code.Modified = time.Now().UTC()
code.ModifiedIp = ip
messageCodes[code.Code] = code
file, err := os.Create("codes.gob")
if err != nil {
<-modlock
return nil
}
defer file.Close()
encoder := gob.NewEncoder(file)
err = encoder.Encode(messageCodes)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
<-modlock
return code
}
func deleteCode(id int, deleted bool, ip net.IP) *MessageCode {
modlock <- true
code := messageCodes[id]
code.Deleted = deleted
messageCodes[code.Code] = code
file, err := os.Create("codes.gob")
if err != nil {
<-modlock
return nil
}
defer file.Close()
encoder := gob.NewEncoder(file)
err = encoder.Encode(messageCodes)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
<-modlock
return code
}
//
func indexHandler(w http.ResponseWriter, r *http.Request) {
outputHeader(w, "Message Code List")
defer outputFooter(w)
fmt.Fprintln(w, "<table>")
fmt.Fprintln(w, "<tr><th>ID</th><th>Title</th><th>Description</th><th>Actions</th></tr>")
fmt.Fprintf(w, "<form method='post' action='add'><tr class='add'><td class='id'>NEW</td><td class='title'><input id='startfocus' name='title' /></td><td class='description'><input name='description' /></td><td><input class='submit' type='submit' name='save' value='Save' /></td></tr></form>\n<script>document.getElementById('startfocus').focus()</script>\n")
for i := 0; i < len(messageCodes); i++ {
code := messageCodes[i]
class := ""
deleteName := "delete"
deleteValue := "Delete"
if code.Deleted {
class = "deleted"
deleteName = "undelete"
deleteValue = "Undelete"
}
fmt.Fprintf(w, "<form method='post' action='modify'><input type='hidden' name='id' value='%d' /><tr class='%s' title='Modified :%s via %s\nCreated :%s via %s'><td class='id'>%d</td><td class='title'><input name='title' value='%s' /></td><td class='description'><input name='description' value='%s' /></td><td><input class='submit' type='submit' name='save' value='Save' /> <input class='submit %s' type='submit' name='%s' value='%s' /></td></tr></form>\n", code.Code, class, code.Modified, code.ModifiedIp, code.Created, code.CreatedIp, code.Code, code.Title, code.Description, deleteName, deleteName, deleteValue)
}
fmt.Fprintln(w, "</table>")
}
func addHandler(w http.ResponseWriter, r *http.Request) {
title := r.FormValue("title")
if title == "" {
fmt.Fprintln(w, "Requires TITLE field.")
return
}
description := r.FormValue("description")
host, _, _ := net.SplitHostPort(r.RemoteAddr)
addCode(title, description, net.ParseIP(host))
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func modifyHandler(w http.ResponseWriter, r *http.Request) {
host, _, _ := net.SplitHostPort(r.RemoteAddr)
idStr := r.FormValue("id")
if idStr == "" {
fmt.Fprintln(w, "Requires ID field.")
return
}
id, err := strconv.ParseInt(idStr, 10, 0)
if err != nil {
fmt.Fprintln(w, "Requires ID field that is an integer.")
return
}
deleteStr := r.FormValue("delete")
delete := deleteStr != ""
undeleteStr := r.FormValue("undelete")
undelete := undeleteStr != ""
if delete || undelete {
deleteCode(int(id), delete, net.ParseIP(host))
}
title := r.FormValue("title")
if title == "" {
fmt.Fprintln(w, "Requires TITLE field.")
return
}
description := r.FormValue("description")
modifyCode(int(id), title, description, net.ParseIP(host))
http.Redirect(w, r, "/", http.StatusSeeOther)
}