diff options
| author | 2019-11-23 20:11:10 -0500 | |
|---|---|---|
| committer | 2019-11-23 20:11:10 -0500 | |
| commit | 79707242bd59cf4deb7f999a7a953275b323bb3b (patch) | |
| tree | 491c76768efd849aa7ebc0cb7601ff8561a1e60b /http | |
| parent | Remove D stuff from gitignore (diff) | |
Initial golang rewrite
Diffstat (limited to 'http')
| -rw-r--r-- | http/get.go | 36 | ||||
| -rw-r--r-- | http/index.go | 12 | ||||
| -rw-r--r-- | http/put.go | 50 | ||||
| -rw-r--r-- | http/router.go | 23 | ||||
| -rw-r--r-- | http/type.go | 5 | ||||
| -rw-r--r-- | http/ua_detector.go | 23 |
6 files changed, 149 insertions, 0 deletions
diff --git a/http/get.go b/http/get.go new file mode 100644 index 0000000..459d18f --- /dev/null +++ b/http/get.go @@ -0,0 +1,36 @@ +package http + +import ( + "github.com/valyala/fasthttp" + "toast.cafe/x/brpaste/v2/storage" + "toast.cafe/x/brpaste/v2/template" +) + +func Get(store storage.CHR) handler { + return func(ctx *fasthttp.RequestCtx) { + ukey := ctx.UserValue("key") + ulang := ctx.UserValue("lang") + + var key, lang string + key = ukey.(string) // there's no recovering otherwise + if ulang != nil { + lang = ulang.(string) + } + + res, err := store.Read(key) + switch err { + case storage.Unhealthy: + ctx.Error("Backend did not respond", fasthttp.StatusInternalServerError) + case nil: // all good + if lang == "raw" { + ctx.SuccessString("text/plain", res) + } else { + //b := new(bytes.Buffer) + //template.WriteCode(b, lang, res) + ctx.SuccessString("text/html", template.Code(lang, res)) // render template + } + default: + ctx.NotFound() + } + } +} diff --git a/http/index.go b/http/index.go new file mode 100644 index 0000000..4bb9567 --- /dev/null +++ b/http/index.go @@ -0,0 +1,12 @@ +package http + +import ( + "github.com/valyala/fasthttp" + "toast.cafe/x/brpaste/v2/template" +) + +func Index(ctx *fasthttp.RequestCtx) { + //b := new(bytes.Buffer) + //template.Index(b) + ctx.SuccessString("text/html", template.Index()) // render template +} diff --git a/http/put.go b/http/put.go new file mode 100644 index 0000000..bc49e74 --- /dev/null +++ b/http/put.go @@ -0,0 +1,50 @@ +package http + +import ( + "encoding/base64" + "fmt" + + "github.com/twmb/murmur3" + "github.com/valyala/fasthttp" + "toast.cafe/x/brpaste/v2/storage" +) + +func Put(store storage.CHR, put bool) handler { + return func(ctx *fasthttp.RequestCtx) { + data := ctx.FormValue("data") + if len(data) == 0 { // works with nil + ctx.Error("Missing data field", fasthttp.StatusBadRequest) + return + } + + ukey := ctx.UserValue("key") + var key string + if ukey != nil { + key = ukey.(string) + } else { + hasher := murmur3.New32() + hasher.Write(data) + keybuf := hasher.Sum(nil) + key = base64.RawURLEncoding.EncodeToString(keybuf) + } + val := string(data) + + err := store.Create(key, val, put) + + switch err { + case storage.Collision: + ctx.Error("Collision detected when undesired", fasthttp.StatusConflict) + case storage.Unhealthy: + ctx.Error("Backend did not respond", fasthttp.StatusInternalServerError) + case nil: // everything succeeded + if isBrowser(string(ctx.UserAgent())) { + ctx.Redirect(fmt.Sprintf("/%s", key), fasthttp.StatusSeeOther) + } else { + ctx.SetStatusCode(fasthttp.StatusCreated) + ctx.SetBodyString(key) + } + default: + ctx.Error(err.Error(), fasthttp.StatusInternalServerError) + } + } +} diff --git a/http/router.go b/http/router.go new file mode 100644 index 0000000..f1a0b47 --- /dev/null +++ b/http/router.go @@ -0,0 +1,23 @@ +package http + +import ( + "github.com/fasthttp/router" + "github.com/valyala/fasthttp" + "toast.cafe/x/brpaste/v2/storage" +) + +// GenHandler generates the brpaste handler +func GenHandler(store storage.CHR) func(ctx *fasthttp.RequestCtx) { + get := Get(store) + post := Put(store, false) + put := Put(store, true) + + r := router.New() + r.GET("/", Index) + r.GET("/:key", get) + r.GET("/:key/:lang", get) + r.POST("/", post) + r.PUT("/:key", put) + + return r.Handler +} diff --git a/http/type.go b/http/type.go new file mode 100644 index 0000000..22aab0d --- /dev/null +++ b/http/type.go @@ -0,0 +1,5 @@ +package http + +import "github.com/valyala/fasthttp" + +type handler = fasthttp.RequestHandler diff --git a/http/ua_detector.go b/http/ua_detector.go new file mode 100644 index 0000000..aa25025 --- /dev/null +++ b/http/ua_detector.go @@ -0,0 +1,23 @@ +package http + +import "strings" + +var ( + browsers = []string{ + "Firefox/", + "Chrome/", + "Safari/", + "OPR/", + "Edge/", + "Trident/", + } +) + +func isBrowser(ua string) bool { + for _, el := range browsers { + if strings.Contains(ua, el) { + return true + } + } + return false +} |
