1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00

Rework project structure

This commit is contained in:
Lukas Schulte Pelkum
2021-04-15 20:15:42 +02:00
parent de2add44ec
commit 1792ef1c38
20 changed files with 238 additions and 328 deletions

View File

@@ -2,9 +2,12 @@ package v1
import (
"encoding/json"
"time"
"github.com/lus/pasty/internal/pastes"
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/shared"
"github.com/lus/pasty/internal/storage"
"github.com/lus/pasty/internal/utils"
"github.com/valyala/fasthttp"
)
@@ -15,10 +18,8 @@ func HastebinSupportHandler(ctx *fasthttp.RequestCtx) {
switch string(ctx.Request.Header.ContentType()) {
case "text/plain":
content = string(ctx.PostBody())
break
case "multipart/form-data":
content = string(ctx.FormValue("data"))
break
default:
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.SetBodyString("invalid content type")
@@ -34,11 +35,12 @@ func HastebinSupportHandler(ctx *fasthttp.RequestCtx) {
}
// Create the paste object
paste, err := pastes.Create(id, content)
if err != nil {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetBodyString(err.Error())
return
paste := &shared.Paste{
ID: id,
Content: content,
DeletionToken: utils.RandomString(config.Current.DeletionTokenLength),
Created: time.Now().Unix(),
AutoDelete: config.Current.AutoDelete.Enabled,
}
// Hash the deletion token

View File

@@ -2,10 +2,13 @@ package v1
import (
"encoding/json"
"time"
"github.com/fasthttp/router"
"github.com/lus/pasty/internal/pastes"
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/shared"
"github.com/lus/pasty/internal/storage"
"github.com/lus/pasty/internal/utils"
limitFasthttp "github.com/ulule/limiter/v3/drivers/middleware/fasthttp"
"github.com/valyala/fasthttp"
)
@@ -73,11 +76,12 @@ func v1PostPaste(ctx *fasthttp.RequestCtx) {
}
// Create the paste object
paste, err := pastes.Create(id, values["content"])
if err != nil {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetBodyString(err.Error())
return
paste := &shared.Paste{
ID: id,
Content: values["content"],
DeletionToken: utils.RandomString(config.Current.DeletionTokenLength),
Created: time.Now().Unix(),
AutoDelete: config.Current.AutoDelete.Enabled,
}
// Hash the deletion token

View File

@@ -6,5 +6,4 @@ type nilLogger struct {
// Printf prints nothing
func (logger *nilLogger) Printf(string, ...interface{}) {
return
}

View File

@@ -6,7 +6,7 @@ import (
"strings"
routing "github.com/fasthttp/router"
"github.com/lus/pasty/internal/env"
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/static"
v1 "github.com/lus/pasty/internal/web/controllers/v1"
"github.com/ulule/limiter/v3"
@@ -38,7 +38,7 @@ func Serve() error {
})
// Set up the rate limiter
rate, err := limiter.NewRateFromFormatted(env.Get("RATE_LIMIT", "30-M"))
rate, err := limiter.NewRateFromFormatted(config.Current.RateLimit)
if err != nil {
return err
}
@@ -61,12 +61,11 @@ func Serve() error {
}
// Route the hastebin documents route if hastebin support is enabled
if env.Bool("HASTEBIN_SUPPORT", false) {
if config.Current.HastebinSupport {
router.POST("/documents", rateLimiterMiddleware.Handle(v1.HastebinSupportHandler))
}
// Serve the web resources
address := env.Get("WEB_ADDRESS", ":8080")
return (&fasthttp.Server{
Handler: func(ctx *fasthttp.RequestCtx) {
// Add the CORS headers
@@ -77,7 +76,7 @@ func Serve() error {
router.Handler(ctx)
},
Logger: new(nilLogger),
}).ListenAndServe(address)
}).ListenAndServe(config.Current.WebAddress)
}
// frontendHandler handles the frontend routing