summaryrefslogtreecommitdiffhomepage
path: root/http/get.go
blob: 459d18f639f92183d7d3d4e9f30008a3c7d968c5 (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
35
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()
		}
	}
}