-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpkv.go
65 lines (56 loc) · 1.29 KB
/
httpkv.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
package main
import (
"log"
"fmt"
"io/ioutil"
"net/http"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "data.db")
checkErr(err)
defer db.Close()
_, err = db.Exec("create table if not exists data (key BLOB PRIMARY KEY, value BLOB)")
checkErr(err)
handler := &KVHandler{db: db}
http.Handle("/", handler)
err = http.ListenAndServe(":8765", nil)
log.Fatal(err)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
type KVHandler struct {
db *sql.DB
}
func (handler KVHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
handler.Get(w, r)
case http.MethodPut:
handler.Put(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func (handler KVHandler) Get(w http.ResponseWriter, r *http.Request) {
rows, err := handler.db.Query("select value from data where key = ?", r.URL.Path)
checkErr(err)
defer rows.Close()
if !rows.Next() {
w.WriteHeader(http.StatusNotFound)
return
}
var value string
rows.Scan(&value)
fmt.Fprintf(w, value)
}
func (handler KVHandler) Put(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
checkErr(err)
_, err = handler.db.Exec("insert or replace into data values (?,?)", r.URL.Path, body)
checkErr(err)
}