summaryrefslogtreecommitdiff
path: root/server/main.go
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-09-16 15:56:31 -0400
committerBen Burwell <ben@benburwell.com>2019-09-16 16:29:16 -0400
commit679a62a9407c09be0cfb4e22455dca5ae694ce01 (patch)
treea99e8d61ada1677aee6f63a78545463308c5badd /server/main.go
parent528352cddbe0290653c56a27a4134637ad0624e5 (diff)
Flesh out
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))
+ }
+}