summaryrefslogtreecommitdiff
path: root/server/main.go
blob: 9ebcb22d808a04768098b02dc410b64d95420f15 (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
package main

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

	"bnbl.io/caesar"
)

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

func handleCaesar() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		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))
	}
}