summaryrefslogtreecommitdiff
path: root/server/main.go
blob: 23f29591ac5effdfbcf46bd6c7995942c5075e1a (plain)
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
package main

import (
	"io/ioutil"
	"net/http"

	"bnbl.io/caesar"
)

func main() {
	server := handleCaesar(TokenAuthenticator())
	http.ListenAndServe(":8088", server)
}

func handleCaesar(auth Authenticator) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if !auth(r) {
			w.WriteHeader(http.StatusUnauthorized)
			return
		}
		if r.Method != http.MethodPost {
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		body, err := ioutil.ReadAll(r.Body)
		defer r.Body.Close()
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		encoded := caesar.Encode(string(body))
		w.Write([]byte(encoded))
	}
}