blob: 8d3c523aaf87a00f1c2637b222467a7bf6f95f48 (
plain) (
blame)
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 http
import (
"github.com/valyala/fasthttp"
"toast.cafe/x/brpaste/v2/storage"
)
// Get generates a handler for the /:key[/:lang] endpoint
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 {
ctx.SuccessString("text/html", CodeTemplate(lang, res)) // render template
}
default:
ctx.NotFound()
}
}
}
|