-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (44 loc) · 972 Bytes
/
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
package main
import (
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/softpuff/esecret/mutate"
)
func rootHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello %q", html.EscapeString(r.URL.Path))
}
func handleMutate(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
sendError(err, w)
return
}
mutated, err := mutate.Mutate(body)
if err != nil {
sendError(err, w)
return
}
w.WriteHeader(http.StatusOK)
w.Write(mutated)
}
func sendError(err error, w http.ResponseWriter) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error %v", err)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", rootHandler)
mux.HandleFunc("/mutate", handleMutate)
s := &http.Server{
Addr: ":8443",
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
log.Fatal(s.ListenAndServe())
}