-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi.go
99 lines (79 loc) · 1.98 KB
/
api.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
package main
import (
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/go-redis/redis"
gh "github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/urfave/negroni"
)
const salt = "salmon rosado"
var redisClient *redis.Client
func main() {
redisClient = redis.NewClient(&redis.Options{
Addr: "redis:6379",
Password: "",
DB: 0,
})
r := mux.NewRouter()
corsRouter := mux.NewRouter()
corsHandler := gh.CORS(gh.AllowCredentials(), gh.AllowedHeaders([]string{"x-requested-with", "content-type"}), gh.AllowedOrigins([]string{"*"}))
r.HandleFunc("/ping", ping).Methods("GET")
corsRouter.HandleFunc("/shares", share).Methods("POST")
corsRouter.HandleFunc("/shares/{id}", shares).Methods("GET")
n := negroni.Classic()
r.PathPrefix("/").Handler(negroni.New(negroni.Wrap(corsHandler(corsRouter))))
n.UseHandler(r)
httpServer := http.Server{
Addr: "0.0.0.0:8080",
Handler: n,
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
}
err := httpServer.ListenAndServe()
log.Fatal(err)
}
func ping(rw http.ResponseWriter, req *http.Request) {
}
func share(rw http.ResponseWriter, req *http.Request) {
b, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
i := id(b)
err = redisClient.Set(i, b, 0).Err()
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Fprintln(rw, i)
}
func shares(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
i := vars["id"]
b, err := redisClient.Get(i).Result()
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Fprint(rw, b)
}
func id(body []byte) string {
h := sha1.New()
io.WriteString(h, salt)
h.Write(body)
sum := h.Sum(nil)
b := make([]byte, base64.URLEncoding.EncodedLen(len(sum)))
base64.URLEncoding.Encode(b, sum)
return string(b)[:10]
}