/
share.go
93 lines (77 loc) · 1.94 KB
/
share.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
// Copyright 2011-2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"crypto/sha1"
"flag"
"fmt"
"io"
"log"
"net/http"
"github.com/boltdb/bolt"
)
const (
maxSnippetSize = 64 * 1024
)
var (
flagDatabaseFile = flag.String("db", "playground.db", "Database file.")
flagAllowShare = flag.Bool("allow-share", false, "Allow storing and sharing snippets.")
)
var (
db *bolt.DB
bucketSnippets = []byte("snippets")
bucketCache = []byte("cache")
bucketConfig = []byte("config")
salt []byte
)
type Snippet struct {
Body []byte
}
func (s *Snippet) Id() string {
h := sha1.New()
h.Write(salt)
h.Write(s.Body)
sum := h.Sum(nil)
return fmt.Sprintf("%x", sum)
}
func createBucket(name []byte) error {
return db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(name)
return err
})
}
func shareHandler(w http.ResponseWriter, r *http.Request) {
if !*flagAllowShare || r.Method != "POST" {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
var body bytes.Buffer
_, err := io.Copy(&body, io.LimitReader(r.Body, maxSnippetSize+1))
r.Body.Close()
if err != nil {
log.Printf("Error reading body: %q", err)
http.Error(w, "Server Error", http.StatusInternalServerError)
return
}
if body.Len() > maxSnippetSize {
http.Error(w, "Snippet is too large", http.StatusRequestEntityTooLarge)
return
}
snip := &Snippet{Body: body.Bytes()}
id := snip.Id()
key := []byte(id)
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketSnippets)
return b.Put(key, snip.Body)
})
if err != nil {
log.Printf("Error putting snippet: %q", err)
http.Error(w, "Server Error", http.StatusInternalServerError)
return
}
w.Header().Set("Access-Control-Allow-Methods", "POST")
w.Header().Set("Access-Control-Allow-Origin", *flagAllowOriginHeader)
fmt.Fprint(w, id)
}