summaryrefslogtreecommitdiff
path: root/server/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'server/main.go')
-rw-r--r--server/main.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/server/main.go b/server/main.go
new file mode 100644
index 0000000..23f2959
--- /dev/null
+++ b/server/main.go
@@ -0,0 +1,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))
+ }
+}