-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
139 lines (118 loc) · 2.88 KB
/
server.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
package main
import (
"os"
"fmt"
"flag"
"bytes"
"net/http"
"io/ioutil"
"crypto/sha1"
"crypto/rand"
"text/template"
"github.com/go-martini/martini"
"github.com/garyburd/redigo/redis"
)
var (
redisAddress = flag.String("redis-address", ":6379", "Address to the Redis server")
maxConnections = flag.Int("max-connections", 10, "Max connections to Redis")
database = flag.Int("database", 1, "Redis database index")
secret = flag.String("secret", "If you can dream it, you can do it", "For edit URLs")
bind = flag.String("bind", "127.0.0.1:3342", "Listen on this address")
)
func obscurity(id string) string {
hash := sha1.New()
hash.Write([]byte(*secret))
hash.Write([]byte(id))
sum := hash.Sum(nil)
return fmt.Sprintf("%s/%s", id, fmt.Sprintf("%x", sum)[:6])
}
func security(id string, sums string) bool {
hash := sha1.New()
hash.Write([]byte(*secret))
hash.Write([]byte(id))
sum := hash.Sum(nil)
return fmt.Sprintf("%x", sum)[:6] == sums
}
func genID() string {
b := make([]byte, 3)
rand.Read(b)
return fmt.Sprintf("%x", b)
}
func Save(pool *redis.Pool, req *http.Request, params martini.Params) (int, string) {
c := pool.Get()
defer c.Close()
id := genID()
param_id, ok := params["id"]
if ok {
if security(param_id, params["sum"]) {
id = param_id
} else {
return 401, "Not your bill"
}
} else {
for i := 0; i < 10; i++ {
exists, err := c.Do("EXISTS", id)
if err != nil { panic(err) }
if exists == int64(0) {
break
}
id = genID()
}
}
body, err := ioutil.ReadAll(req.Body)
if err != nil { panic(err) }
if len(body) > 10000 {
return 422, "Body too large"
}
_, err = c.Do("SET", id, body)
if err != nil { panic(err) }
return 200, obscurity(id)
}
func Load(pool *redis.Pool, t *template.Template, params martini.Params) (int, string) {
c := pool.Get()
defer c.Close()
var doc bytes.Buffer
key, ok := params["id"]
if ok {
state, err := redis.String(c.Do("GET", key))
if err != nil { panic(err) }
t.Execute(&doc, state)
} else {
t.Execute(&doc, "")
}
return 200, doc.String()
}
func main() {
flag.Parse()
m := martini.New()
r := martini.NewRouter()
m.Use(martini.Logger())
m.Use(martini.Recovery())
m.MapTo(r, (*martini.Routes)(nil))
m.Action(r.Handle)
var pool = redis.NewPool(func() (redis.Conn, error) {
c, err := redis.Dial("tcp", *redisAddress)
if err != nil { panic(err) }
_, err = c.Do("SELECT", *database)
if err != nil { panic(err) }
return c, err
}, *maxConnections)
defer pool.Close()
m.Map(pool)
if os.Getenv("DEV") == "1" {
m.Use(martini.Static("resources/public"))
}
f, _ := ioutil.ReadFile("resources/public/index.html")
t, _ := template.New("index").Parse(string(f))
m.Map(t)
r.Post("/save", Save)
r.Post("/:id/:sum", Save)
r.Get("/", Load)
r.Get("/:id", Load)
r.Get("/:id/:sum", Load)
if os.Getenv("DEV") == "1" {
m.Run()
} else {
m.RunOnAddr(*bind)
}
}