Implement raw paste endpoint

This commit is contained in:
Lukas Schulte Pelkum 2021-06-21 20:09:40 +02:00
parent 185f1511a6
commit 4c392b4b52
No known key found for this signature in database
GPG Key ID: 408DA7CA81DB885C
1 changed files with 28 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
routing "github.com/fasthttp/router"
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/static"
"github.com/lus/pasty/internal/storage"
v1 "github.com/lus/pasty/internal/web/controllers/v1"
"github.com/ulule/limiter/v3"
limitFasthttp "github.com/ulule/limiter/v3/drivers/middleware/fasthttp"
@ -28,6 +29,7 @@ func Serve() error {
// Route the frontend requests
frontend := frontendHandler()
raw := rawHandler()
router.GET("/{path:*}", func(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
if !strings.HasPrefix(path, "/api") && (strings.Count(path, "/") == 1 || strings.HasPrefix(path, "/assets")) {
@ -36,6 +38,9 @@ func Serve() error {
}
frontend(ctx)
return
} else if strings.HasSuffix(strings.TrimSuffix(path, "/"), "/raw") {
raw(ctx)
return
}
router.NotFound(ctx)
})
@ -101,3 +106,26 @@ func frontendHandler() fasthttp.RequestHandler {
}
return fs.NewRequestHandler()
}
func rawHandler() fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
pathSanitized := strings.TrimPrefix(strings.TrimSuffix(path, "/"), "/")
pasteID := strings.TrimSuffix(pathSanitized, "/raw")
paste, err := storage.Current.Get(pasteID)
if err != nil {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetBodyString(err.Error())
return
}
if paste == nil {
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.SetBodyString("paste not found")
return
}
ctx.SetBodyString(paste.Content)
}
}