-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (126 loc) · 2.84 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
136
137
138
139
140
141
142
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"sync"
)
type record struct {
Key uint64 `json:"key"`
Value []byte `json:"value"`
}
type memLog struct {
mu sync.Mutex
records []record
}
type listing struct {
Records []record `json:"records"`
}
type appendRequest struct {
Record record `json:"record"`
}
type appendResponse struct {
Key uint64 `json:"key"`
}
type getRequest struct {
Key uint64 `json:"key"`
}
type getResponse struct {
Record record `json:"record"`
}
var errKeyNotFound = fmt.Errorf("key not found")
func newMemLog() *memLog {
return &memLog{}
}
func (l *memLog) append(r record) (uint64, error) {
l.mu.Lock()
defer l.mu.Unlock()
r.Key = uint64(len(l.records)) // = index of r
l.records = append(l.records, r)
return r.Key, nil
}
func (l *memLog) get(key uint64) (record, error) {
l.mu.Lock()
defer l.mu.Unlock()
if key >= uint64(len(l.records)) {
return record{}, errKeyNotFound
}
return l.records[key], nil
}
func (l *memLog) handleAppend(w http.ResponseWriter, r *http.Request) {
var req appendRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
key, err := l.append(req.Record)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
res := appendResponse{Key: key}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(res)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (l *memLog) handleGet(w http.ResponseWriter, r *http.Request) {
var req getRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err == io.EOF {
if len(l.records) > 0 {
res := listing{Records: l.records}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(res)
}
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
record, err := l.get(req.Key)
if err == errKeyNotFound {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
res := getResponse{Record: record}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(res)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (l *memLog) indexHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
l.handleGet(w, r)
case http.MethodPost:
l.handleAppend(w, r)
default:
w.Header().Set("Allow", "GET, POST")
http.Error(w, "Method Not Allowed", 405)
}
}
func main() {
port, ok := os.LookupEnv("HTTP_PLATFORM_PORT")
if !ok {
port = "8080"
}
l := newMemLog()
mux := http.NewServeMux()
mux.HandleFunc("/", l.indexHandler)
err := http.ListenAndServe(":"+port, mux)
log.Fatal(err)
}