The Great Sources Linting.
This commit is contained in:
parent
3265c5a4b2
commit
6ea6e2e144
@ -12,6 +12,9 @@ linters:
|
|||||||
- funlen
|
- funlen
|
||||||
# Magic numbers everywhere and we can't get rid of them.
|
# Magic numbers everywhere and we can't get rid of them.
|
||||||
- gomnd
|
- gomnd
|
||||||
|
# This linter MIGHT BE good, but who decided that I want keepFor in
|
||||||
|
# JSON instead of keep_for for KeepFor field?
|
||||||
|
- tagliatelle
|
||||||
linters-settings:
|
linters-settings:
|
||||||
lll:
|
lll:
|
||||||
line-length: 420
|
line-length: 420
|
||||||
@ -19,3 +22,10 @@ linters-settings:
|
|||||||
min-complexity: 50
|
min-complexity: 50
|
||||||
gocyclo:
|
gocyclo:
|
||||||
min-complexity: 40
|
min-complexity: 40
|
||||||
|
|
||||||
|
issues:
|
||||||
|
exclude-rules:
|
||||||
|
# There will be some ToDos.
|
||||||
|
- linters:
|
||||||
|
- godox
|
||||||
|
text: "TODO"
|
||||||
|
@ -25,12 +25,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/domains/dbnotavailable"
|
"go.dev.pztrn.name/fastpastebin/domains/dbnotavailable"
|
||||||
"go.dev.pztrn.name/fastpastebin/domains/indexpage"
|
"go.dev.pztrn.name/fastpastebin/domains/indexpage"
|
||||||
"go.dev.pztrn.name/fastpastebin/domains/pastes"
|
"go.dev.pztrn.name/fastpastebin/domains/pastes"
|
||||||
|
@ -25,17 +25,13 @@
|
|||||||
package dbnotavailable
|
package dbnotavailable
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/templater"
|
|
||||||
|
|
||||||
// other
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/templater"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Database not available error page
|
// Database not available error page.
|
||||||
func dbNotAvailableGet(ec echo.Context) error {
|
func dbNotAvailableGet(ec echo.Context) error {
|
||||||
htmlData := templater.GetTemplate(ec, "database_not_available.html", nil)
|
htmlData := templater.GetTemplate(ec, "database_not_available.html", nil)
|
||||||
|
|
||||||
|
@ -25,13 +25,10 @@
|
|||||||
package dbnotavailable
|
package dbnotavailable
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/context"
|
"go.dev.pztrn.name/fastpastebin/internal/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var c *context.Context
|
||||||
c *context.Context
|
|
||||||
)
|
|
||||||
|
|
||||||
// New initializes pastes package and adds necessary HTTP and API
|
// New initializes pastes package and adds necessary HTTP and API
|
||||||
// endpoints.
|
// endpoints.
|
||||||
|
@ -25,13 +25,10 @@
|
|||||||
package indexpage
|
package indexpage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/context"
|
"go.dev.pztrn.name/fastpastebin/internal/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var c *context.Context
|
||||||
c *context.Context
|
|
||||||
)
|
|
||||||
|
|
||||||
// New initializes pastes package and adds necessary HTTP and API
|
// New initializes pastes package and adds necessary HTTP and API
|
||||||
// endpoints.
|
// endpoints.
|
||||||
|
@ -25,30 +25,27 @@
|
|||||||
package indexpage
|
package indexpage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/captcha"
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/templater"
|
|
||||||
|
|
||||||
// other
|
|
||||||
"github.com/alecthomas/chroma/lexers"
|
"github.com/alecthomas/chroma/lexers"
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/captcha"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/flatfiles"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/templater"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Index of this site.
|
// Index of this site.
|
||||||
func indexGet(ec echo.Context) error {
|
func indexGet(ec echo.Context) error {
|
||||||
// We should check if database connection available.
|
// We should check if database connection available.
|
||||||
dbConn := c.Database.GetDatabaseConnection()
|
dbConn := c.Database.GetDatabaseConnection()
|
||||||
if c.Config.Database.Type != "flatfiles" && dbConn == nil {
|
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
|
||||||
return ec.Redirect(http.StatusFound, "/database_not_available")
|
return ec.Redirect(http.StatusFound, "/database_not_available")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate list of available languages to highlight.
|
// Generate list of available languages to highlight.
|
||||||
availableLexers := lexers.Names(false)
|
availableLexers := lexers.Names(false)
|
||||||
|
|
||||||
var availableLexersSelectOpts = "<option value='text'>Text</option><option value='autodetect'>Auto detect</option><option disabled>-----</option>"
|
availableLexersSelectOpts := "<option value='text'>Text</option><option value='autodetect'>Auto detect</option><option disabled>-----</option>"
|
||||||
for i := range availableLexers {
|
for i := range availableLexers {
|
||||||
availableLexersSelectOpts += "<option value='" + availableLexers[i] + "'>" + availableLexers[i] + "</option>"
|
availableLexersSelectOpts += "<option value='" + availableLexers[i] + "'>" + availableLexers[i] + "</option>"
|
||||||
}
|
}
|
||||||
|
@ -25,20 +25,14 @@
|
|||||||
package pastes
|
package pastes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/context"
|
"go.dev.pztrn.name/fastpastebin/internal/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var regexInts = regexp.MustCompile("[0-9]+")
|
||||||
regexInts = regexp.MustCompile("[0-9]+")
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var c *context.Context
|
||||||
c *context.Context
|
|
||||||
)
|
|
||||||
|
|
||||||
// New initializes pastes package and adds necessary HTTP and API
|
// New initializes pastes package and adds necessary HTTP and API
|
||||||
// endpoints.
|
// endpoints.
|
||||||
|
@ -1,23 +1,20 @@
|
|||||||
package pastes
|
package pastes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/templater"
|
|
||||||
|
|
||||||
// other
|
|
||||||
"github.com/alecthomas/chroma"
|
"github.com/alecthomas/chroma"
|
||||||
"github.com/alecthomas/chroma/formatters"
|
"github.com/alecthomas/chroma/formatters"
|
||||||
htmlfmt "github.com/alecthomas/chroma/formatters/html"
|
htmlfmt "github.com/alecthomas/chroma/formatters/html"
|
||||||
"github.com/alecthomas/chroma/lexers"
|
"github.com/alecthomas/chroma/lexers"
|
||||||
"github.com/alecthomas/chroma/styles"
|
"github.com/alecthomas/chroma/styles"
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/flatfiles"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/templater"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -38,12 +35,14 @@ func pasteGetData(pasteID int, timestamp int64, cookieValue string) (*structs.Pa
|
|||||||
paste, err1 := c.Database.GetPaste(pasteID)
|
paste, err1 := c.Database.GetPaste(pasteID)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
c.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste")
|
c.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste")
|
||||||
|
|
||||||
return nil, pasteNotFound
|
return nil, pasteNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if paste is expired.
|
// Check if paste is expired.
|
||||||
if paste.IsExpired() {
|
if paste.IsExpired() {
|
||||||
c.Logger.Error().Int("paste ID", pasteID).Msg("Paste is expired")
|
c.Logger.Error().Int("paste ID", pasteID).Msg("Paste is expired")
|
||||||
|
|
||||||
return nil, pasteExpired
|
return nil, pasteExpired
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,6 +51,7 @@ func pasteGetData(pasteID int, timestamp int64, cookieValue string) (*structs.Pa
|
|||||||
pasteTs := paste.CreatedAt.Unix()
|
pasteTs := paste.CreatedAt.Unix()
|
||||||
if timestamp != pasteTs {
|
if timestamp != pasteTs {
|
||||||
c.Logger.Error().Int("paste ID", pasteID).Int64("paste timestamp", pasteTs).Int64("provided timestamp", timestamp).Msg("Incorrect timestamp provided for private paste")
|
c.Logger.Error().Int("paste ID", pasteID).Int64("paste timestamp", pasteTs).Int64("provided timestamp", timestamp).Msg("Incorrect timestamp provided for private paste")
|
||||||
|
|
||||||
return nil, pasteTimestampInvalid
|
return nil, pasteTimestampInvalid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,18 +109,20 @@ func pasteGETWebInterface(ec echo.Context) error {
|
|||||||
cookieValue = cookie.Value
|
cookieValue = cookie.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
paste, error := pasteGetData(pasteID, timestamp, cookieValue)
|
paste, err := pasteGetData(pasteID, timestamp, cookieValue)
|
||||||
|
|
||||||
// For these cases we should return 404 Not Found page.
|
// For these cases we should return 404 Not Found page.
|
||||||
if error == pasteExpired || error == pasteNotFound || error == pasteTimestampInvalid {
|
if err == pasteExpired || err == pasteNotFound || err == pasteTimestampInvalid {
|
||||||
errtpl := templater.GetErrorTemplate(ec, "Paste #"+pasteIDRaw+" not found")
|
errtpl := templater.GetErrorTemplate(ec, "Paste #"+pasteIDRaw+" not found")
|
||||||
|
|
||||||
return ec.HTML(http.StatusNotFound, errtpl)
|
return ec.HTML(http.StatusNotFound, errtpl)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If passed cookie value was invalid - go to paste authorization
|
// If passed cookie value was invalid - go to paste authorization
|
||||||
// page.
|
// page.
|
||||||
if error == pasteCookieInvalid {
|
if err == pasteCookieInvalid {
|
||||||
c.Logger.Info().Int("paste ID", pasteID).Msg("Invalid cookie, redirecting to auth page")
|
c.Logger.Info().Int("paste ID", pasteID).Msg("Invalid cookie, redirecting to auth page")
|
||||||
|
|
||||||
return ec.Redirect(http.StatusMovedPermanently, "/paste/"+pasteIDStr+"/"+ec.Param("timestamp")+"/verify")
|
return ec.Redirect(http.StatusMovedPermanently, "/paste/"+pasteIDStr+"/"+ec.Param("timestamp")+"/verify")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,6 +214,7 @@ func pastePasswordedVerifyGet(ec echo.Context) error {
|
|||||||
|
|
||||||
if cookieValue == cookie.Value {
|
if cookieValue == cookie.Value {
|
||||||
c.Logger.Info().Msg("Valid cookie, redirecting to paste page...")
|
c.Logger.Info().Msg("Valid cookie, redirecting to paste page...")
|
||||||
|
|
||||||
return ec.Redirect(http.StatusMovedPermanently, "/paste/"+pasteIDRaw+"/"+ec.Param("timestamp"))
|
return ec.Redirect(http.StatusMovedPermanently, "/paste/"+pasteIDRaw+"/"+ec.Param("timestamp"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,7 +236,7 @@ func pastePasswordedVerifyPost(ec echo.Context) error {
|
|||||||
// We should check if database connection available.
|
// We should check if database connection available.
|
||||||
dbConn := c.Database.GetDatabaseConnection()
|
dbConn := c.Database.GetDatabaseConnection()
|
||||||
|
|
||||||
if c.Config.Database.Type != "flatfiles" && dbConn == nil {
|
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
|
||||||
return ec.Redirect(http.StatusFound, "/database_not_available")
|
return ec.Redirect(http.StatusFound, "/database_not_available")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,7 +287,7 @@ func pastePasswordedVerifyPost(ec echo.Context) error {
|
|||||||
func pasteRawGETWebInterface(ec echo.Context) error {
|
func pasteRawGETWebInterface(ec echo.Context) error {
|
||||||
// We should check if database connection available.
|
// We should check if database connection available.
|
||||||
dbConn := c.Database.GetDatabaseConnection()
|
dbConn := c.Database.GetDatabaseConnection()
|
||||||
if c.Config.Database.Type != "flatfiles" && dbConn == nil {
|
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
|
||||||
return ec.Redirect(http.StatusFound, "/database_not_available/raw")
|
return ec.Redirect(http.StatusFound, "/database_not_available/raw")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,11 +301,13 @@ func pasteRawGETWebInterface(ec echo.Context) error {
|
|||||||
paste, err1 := c.Database.GetPaste(pasteID)
|
paste, err1 := c.Database.GetPaste(pasteID)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
c.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste from database")
|
c.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste from database")
|
||||||
|
|
||||||
return ec.HTML(http.StatusBadRequest, "Paste #"+pasteIDRaw+" does not exist.")
|
return ec.HTML(http.StatusBadRequest, "Paste #"+pasteIDRaw+" does not exist.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if paste.IsExpired() {
|
if paste.IsExpired() {
|
||||||
c.Logger.Error().Int("paste ID", pasteID).Msg("Paste is expired")
|
c.Logger.Error().Int("paste ID", pasteID).Msg("Paste is expired")
|
||||||
|
|
||||||
return ec.HTML(http.StatusBadRequest, "Paste #"+pasteIDRaw+" does not exist.")
|
return ec.HTML(http.StatusBadRequest, "Paste #"+pasteIDRaw+" does not exist.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,30 +1,29 @@
|
|||||||
package pastes
|
package pastes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/captcha"
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/templater"
|
|
||||||
|
|
||||||
// other
|
|
||||||
"github.com/alecthomas/chroma/lexers"
|
"github.com/alecthomas/chroma/lexers"
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/captcha"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/flatfiles"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/templater"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const KeepPastesForever = "forever"
|
||||||
|
|
||||||
// POST for "/paste/" which will create new paste and redirect to
|
// POST for "/paste/" which will create new paste and redirect to
|
||||||
// "/pastes/CREATED_PASTE_ID". This handler will do all the job for
|
// "/pastes/CREATED_PASTE_ID". This handler will do all the job for
|
||||||
// requests comes from browsers via web interface.
|
// requests comes from browsers via web interface.
|
||||||
func pastePOSTWebInterface(ec echo.Context) error {
|
func pastePOSTWebInterface(ec echo.Context) error {
|
||||||
// We should check if database connection available.
|
// We should check if database connection available.
|
||||||
dbConn := c.Database.GetDatabaseConnection()
|
dbConn := c.Database.GetDatabaseConnection()
|
||||||
if c.Config.Database.Type != "flatfiles" && dbConn == nil {
|
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
|
||||||
return ec.Redirect(http.StatusFound, "/database_not_available")
|
return ec.Redirect(http.StatusFound, "/database_not_available")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +47,7 @@ func pastePOSTWebInterface(ec echo.Context) error {
|
|||||||
return ec.HTML(http.StatusBadRequest, errtpl)
|
return ec.HTML(http.StatusBadRequest, errtpl)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.ContainsAny(params["paste-keep-for"][0], "Mmhd") && params["paste-keep-for"][0] != "forever" {
|
if !strings.ContainsAny(params["paste-keep-for"][0], "Mmhd") && params["paste-keep-for"][0] != KeepPastesForever {
|
||||||
c.Logger.Debug().Str("field value", params["paste-keep-for"][0]).Msg("'Keep paste for' field have invalid value")
|
c.Logger.Debug().Str("field value", params["paste-keep-for"][0]).Msg("'Keep paste for' field have invalid value")
|
||||||
|
|
||||||
errtpl := templater.GetErrorTemplate(ec, "Invalid 'Paste should be available for' parameter passed. Please do not try to hack us ;).")
|
errtpl := templater.GetErrorTemplate(ec, "Invalid 'Paste should be available for' parameter passed. Please do not try to hack us ;).")
|
||||||
@ -80,7 +79,7 @@ func pastePOSTWebInterface(ec echo.Context) error {
|
|||||||
keepFor := 0
|
keepFor := 0
|
||||||
keepForUnit := 0
|
keepForUnit := 0
|
||||||
|
|
||||||
if params["paste-keep-for"][0] != "forever" {
|
if params["paste-keep-for"][0] != KeepPastesForever {
|
||||||
keepForUnitRegex := regexp.MustCompile("[Mmhd]")
|
keepForUnitRegex := regexp.MustCompile("[Mmhd]")
|
||||||
|
|
||||||
keepForRaw := regexInts.FindAllString(params["paste-keep-for"][0], 1)[0]
|
keepForRaw := regexInts.FindAllString(params["paste-keep-for"][0], 1)[0]
|
||||||
@ -89,7 +88,7 @@ func pastePOSTWebInterface(ec echo.Context) error {
|
|||||||
|
|
||||||
keepFor, err = strconv.Atoi(keepForRaw)
|
keepFor, err = strconv.Atoi(keepForRaw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if params["paste-keep-for"][0] == "forever" {
|
if params["paste-keep-for"][0] == KeepPastesForever {
|
||||||
c.Logger.Debug().Msg("Keeping paste forever!")
|
c.Logger.Debug().Msg("Keeping paste forever!")
|
||||||
|
|
||||||
keepFor = 0
|
keepFor = 0
|
||||||
|
@ -25,17 +25,14 @@
|
|||||||
package pastes
|
package pastes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
// local
|
"github.com/labstack/echo"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/flatfiles"
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/pagination"
|
"go.dev.pztrn.name/fastpastebin/internal/pagination"
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/templater"
|
"go.dev.pztrn.name/fastpastebin/internal/templater"
|
||||||
|
|
||||||
// other
|
|
||||||
"github.com/labstack/echo"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GET for "/pastes/", a list of publicly available pastes.
|
// GET for "/pastes/", a list of publicly available pastes.
|
||||||
@ -43,13 +40,13 @@ import (
|
|||||||
func pastesGET(ec echo.Context) error {
|
func pastesGET(ec echo.Context) error {
|
||||||
// We should check if database connection available.
|
// We should check if database connection available.
|
||||||
dbConn := c.Database.GetDatabaseConnection()
|
dbConn := c.Database.GetDatabaseConnection()
|
||||||
if c.Config.Database.Type != "flatfiles" && dbConn == nil {
|
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
|
||||||
return ec.Redirect(http.StatusFound, "/database_not_available")
|
return ec.Redirect(http.StatusFound, "/database_not_available")
|
||||||
}
|
}
|
||||||
|
|
||||||
pageFromParamRaw := ec.Param("page")
|
pageFromParamRaw := ec.Param("page")
|
||||||
|
|
||||||
var page = 1
|
page := 1
|
||||||
|
|
||||||
if pageFromParamRaw != "" {
|
if pageFromParamRaw != "" {
|
||||||
pageRaw := regexInts.FindAllString(pageFromParamRaw, 1)[0]
|
pageRaw := regexInts.FindAllString(pageFromParamRaw, 1)[0]
|
||||||
@ -62,7 +59,7 @@ func pastesGET(ec echo.Context) error {
|
|||||||
pastes, err3 := c.Database.GetPagedPastes(page)
|
pastes, err3 := c.Database.GetPagedPastes(page)
|
||||||
c.Logger.Debug().Int("count", len(pastes)).Msg("Got pastes")
|
c.Logger.Debug().Int("count", len(pastes)).Msg("Got pastes")
|
||||||
|
|
||||||
var pastesString = "No pastes to show."
|
pastesString := "No pastes to show."
|
||||||
|
|
||||||
// Show "No pastes to show" on any error for now.
|
// Show "No pastes to show" on any error for now.
|
||||||
if err3 != nil {
|
if err3 != nil {
|
||||||
|
@ -25,13 +25,10 @@
|
|||||||
package captcha
|
package captcha
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/context"
|
|
||||||
|
|
||||||
// other
|
|
||||||
"github.com/dchest/captcha"
|
"github.com/dchest/captcha"
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -28,6 +28,6 @@ package config
|
|||||||
type HTTP struct {
|
type HTTP struct {
|
||||||
Address string `yaml:"address"`
|
Address string `yaml:"address"`
|
||||||
Port string `yaml:"port"`
|
Port string `yaml:"port"`
|
||||||
AllowInsecure bool `yaml:"allow_insecure"`
|
|
||||||
MaxBodySizeMegabytes string `yaml:"max_body_size_megabytes"`
|
MaxBodySizeMegabytes string `yaml:"max_body_size_megabytes"`
|
||||||
|
AllowInsecure bool `yaml:"allow_insecure"`
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ package config
|
|||||||
|
|
||||||
// Logging describes logger configuration.
|
// Logging describes logger configuration.
|
||||||
type Logging struct {
|
type Logging struct {
|
||||||
LogToFile bool `yaml:"log_to_file"`
|
|
||||||
FileName string `yaml:"filename"`
|
FileName string `yaml:"filename"`
|
||||||
LogLevel string `yaml:"loglevel"`
|
LogLevel string `yaml:"loglevel"`
|
||||||
|
LogToFile bool `yaml:"log_to_file"`
|
||||||
}
|
}
|
||||||
|
@ -25,18 +25,14 @@
|
|||||||
package context
|
package context
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/config"
|
|
||||||
databaseinterface "go.dev.pztrn.name/fastpastebin/internal/database/interface"
|
|
||||||
|
|
||||||
// other
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/config"
|
||||||
|
databaseinterface "go.dev.pztrn.name/fastpastebin/internal/database/interface"
|
||||||
"go.dev.pztrn.name/flagger"
|
"go.dev.pztrn.name/flagger"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
package context
|
package context
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/assets/static"
|
|
||||||
|
|
||||||
// other
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"github.com/labstack/echo/middleware"
|
"github.com/labstack/echo/middleware"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/assets/static"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Context) initializeHTTPServer() {
|
func (c *Context) initializeHTTPServer() {
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
package context
|
package context
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
// other
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,19 +25,15 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
// local
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/flatfiles"
|
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/flatfiles"
|
||||||
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
|
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/mysql"
|
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/mysql"
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/postgresql"
|
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/postgresql"
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
||||||
|
|
||||||
// other
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Database represents control structure for database connection.
|
// Database represents control structure for database connection.
|
||||||
@ -111,7 +107,7 @@ func (db *Database) Initialize() {
|
|||||||
|
|
||||||
if c.Config.Database.Type == "mysql" {
|
if c.Config.Database.Type == "mysql" {
|
||||||
mysql.New(c)
|
mysql.New(c)
|
||||||
} else if c.Config.Database.Type == "flatfiles" {
|
} else if c.Config.Database.Type == flatfiles.FlatFileDialect {
|
||||||
flatfiles.New(c)
|
flatfiles.New(c)
|
||||||
} else if c.Config.Database.Type == "postgresql" {
|
} else if c.Config.Database.Type == "postgresql" {
|
||||||
postgresql.New(c)
|
postgresql.New(c)
|
||||||
|
@ -25,11 +25,12 @@
|
|||||||
package flatfiles
|
package flatfiles
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/context"
|
"go.dev.pztrn.name/fastpastebin/internal/context"
|
||||||
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
|
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const FlatFileDialect = "flatfiles"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
c *context.Context
|
c *context.Context
|
||||||
f *FlatFiles
|
f *FlatFiles
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
package flatfiles
|
package flatfiles
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -36,14 +35,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FlatFiles struct {
|
type FlatFiles struct {
|
||||||
|
writeMutex sync.Mutex
|
||||||
path string
|
path string
|
||||||
pastesIndex []Index
|
pastesIndex []Index
|
||||||
writeMutex sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeletePaste deletes paste from disk and index.
|
// DeletePaste deletes paste from disk and index.
|
||||||
@ -89,6 +87,7 @@ func (ff *FlatFiles) GetPaste(pasteID int) (*structs.Paste, error) {
|
|||||||
pasteInBytes, err := ioutil.ReadFile(pastePath)
|
pasteInBytes, err := ioutil.ReadFile(pastePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Logger.Debug().Err(err).Msg("Failed to read paste from storage")
|
c.Logger.Debug().Err(err).Msg("Failed to read paste from storage")
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,6 +99,7 @@ func (ff *FlatFiles) GetPaste(pasteID int) (*structs.Paste, error) {
|
|||||||
err1 := json.Unmarshal(pasteInBytes, paste)
|
err1 := json.Unmarshal(pasteInBytes, paste)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
c.Logger.Error().Err(err1).Msgf("Failed to parse paste")
|
c.Logger.Error().Err(err1).Msgf("Failed to parse paste")
|
||||||
|
|
||||||
return nil, err1
|
return nil, err1
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,11 +132,13 @@ func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) {
|
|||||||
|
|
||||||
if idx < startPagination {
|
if idx < startPagination {
|
||||||
c.Logger.Debug().Int("paste index", idx).Msg("Paste isn't in pagination query: too low index")
|
c.Logger.Debug().Int("paste index", idx).Msg("Paste isn't in pagination query: too low index")
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (idx-1 >= startPagination && page > 1 && idx > startPagination+((page-1)*c.Config.Pastes.Pagination)) || (idx-1 >= startPagination && page == 1 && idx > startPagination+(page*c.Config.Pastes.Pagination)) {
|
if (idx-1 >= startPagination && page > 1 && idx > startPagination+((page-1)*c.Config.Pastes.Pagination)) || (idx-1 >= startPagination && page == 1 && idx > startPagination+(page*c.Config.Pastes.Pagination)) {
|
||||||
c.Logger.Debug().Int("paste index", idx).Msg("Paste isn't in pagination query: too high index")
|
c.Logger.Debug().Int("paste index", idx).Msg("Paste isn't in pagination query: too high index")
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,12 +150,14 @@ func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) {
|
|||||||
pasteRawData, err := ioutil.ReadFile(filepath.Join(ff.path, "pastes", strconv.Itoa(paste.ID)+".json"))
|
pasteRawData, err := ioutil.ReadFile(filepath.Join(ff.path, "pastes", strconv.Itoa(paste.ID)+".json"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Logger.Error().Err(err).Msg("Failed to read paste data")
|
c.Logger.Error().Err(err).Msg("Failed to read paste data")
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err1 := json.Unmarshal(pasteRawData, pasteData)
|
err1 := json.Unmarshal(pasteRawData, pasteData)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
c.Logger.Error().Err(err1).Msg("Failed to parse paste data")
|
c.Logger.Error().Err(err1).Msg("Failed to parse paste data")
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -253,12 +257,14 @@ func (ff *FlatFiles) SavePaste(p *structs.Paste) (int64, error) {
|
|||||||
data, err := json.Marshal(p)
|
data, err := json.Marshal(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ff.writeMutex.Unlock()
|
ff.writeMutex.Unlock()
|
||||||
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ioutil.WriteFile(filepath.Join(ff.path, "pastes", strconv.Itoa(pasteID)+".json"), data, 0644)
|
err = ioutil.WriteFile(filepath.Join(ff.path, "pastes", strconv.Itoa(pasteID)+".json"), data, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ff.writeMutex.Unlock()
|
ff.writeMutex.Unlock()
|
||||||
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,12 +284,14 @@ func (ff *FlatFiles) Shutdown() {
|
|||||||
indexData, err := json.Marshal(ff.pastesIndex)
|
indexData, err := json.Marshal(ff.pastesIndex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Logger.Error().Err(err).Msg("Failed to encode index data into JSON")
|
c.Logger.Error().Err(err).Msg("Failed to encode index data into JSON")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err1 := ioutil.WriteFile(filepath.Join(ff.path, "pastes", "index.json"), indexData, 0644)
|
err1 := ioutil.WriteFile(filepath.Join(ff.path, "pastes", "index.json"), indexData, 0644)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
c.Logger.Error().Err(err1).Msg("Failed to write index data to file. Pretty sure that you've lost your pastes.")
|
c.Logger.Error().Err(err1).Msg("Failed to write index data to file. Pretty sure that you've lost your pastes.")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,10 +25,8 @@
|
|||||||
package flatfiles
|
package flatfiles
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,10 +25,8 @@
|
|||||||
package dialectinterface
|
package dialectinterface
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
package mysql
|
package mysql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/context"
|
"go.dev.pztrn.name/fastpastebin/internal/context"
|
||||||
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
|
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
|
||||||
)
|
)
|
||||||
|
@ -25,10 +25,8 @@
|
|||||||
package mysql
|
package mysql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,17 +25,11 @@
|
|||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/context"
|
|
||||||
|
|
||||||
// other
|
|
||||||
//"gitlab.com/jmoiron/sqlx"
|
|
||||||
"github.com/pressly/goose"
|
"github.com/pressly/goose"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var c *context.Context
|
||||||
c *context.Context
|
|
||||||
)
|
|
||||||
|
|
||||||
// New initializes migrations.
|
// New initializes migrations.
|
||||||
func New(cc *context.Context) {
|
func New(cc *context.Context) {
|
||||||
|
@ -25,17 +25,13 @@
|
|||||||
package mysql
|
package mysql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/mysql/migrations"
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
|
||||||
|
|
||||||
// other
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/mysql/migrations"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Database is a MySQL/MariaDB connection controlling structure.
|
// Database is a MySQL/MariaDB connection controlling structure.
|
||||||
@ -169,6 +165,7 @@ func (db *Database) Initialize() {
|
|||||||
dbConn, err := sqlx.Connect("mysql", dbConnString)
|
dbConn, err := sqlx.Connect("mysql", dbConnString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Logger.Error().Err(err).Msg("Failed to connect to database")
|
c.Logger.Error().Err(err).Msg("Failed to connect to database")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
package postgresql
|
package postgresql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/context"
|
"go.dev.pztrn.name/fastpastebin/internal/context"
|
||||||
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
|
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
|
||||||
)
|
)
|
||||||
|
@ -25,10 +25,8 @@
|
|||||||
package postgresql
|
package postgresql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,17 +25,11 @@
|
|||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/context"
|
|
||||||
|
|
||||||
// other
|
|
||||||
//"gitlab.com/jmoiron/sqlx"
|
|
||||||
"github.com/pressly/goose"
|
"github.com/pressly/goose"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var c *context.Context
|
||||||
c *context.Context
|
|
||||||
)
|
|
||||||
|
|
||||||
// New initializes migrations.
|
// New initializes migrations.
|
||||||
func New(cc *context.Context) {
|
func New(cc *context.Context) {
|
||||||
|
@ -25,19 +25,14 @@
|
|||||||
package postgresql
|
package postgresql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
// local
|
"github.com/jmoiron/sqlx"
|
||||||
|
_ "github.com/lib/pq"
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/postgresql/migrations"
|
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/postgresql/migrations"
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
||||||
|
|
||||||
// other
|
|
||||||
"github.com/jmoiron/sqlx"
|
|
||||||
// postgresql adapter
|
|
||||||
_ "github.com/lib/pq"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Database is a PostgreSQL connection controlling structure.
|
// Database is a PostgreSQL connection controlling structure.
|
||||||
@ -182,6 +177,7 @@ func (db *Database) Initialize() {
|
|||||||
dbConn, err := sqlx.Connect("postgres", dbConnString)
|
dbConn, err := sqlx.Connect("postgres", dbConnString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Logger.Error().Err(err).Msg("Failed to connect to database")
|
c.Logger.Error().Err(err).Msg("Failed to connect to database")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/context"
|
"go.dev.pztrn.name/fastpastebin/internal/context"
|
||||||
databaseinterface "go.dev.pztrn.name/fastpastebin/internal/database/interface"
|
databaseinterface "go.dev.pztrn.name/fastpastebin/internal/database/interface"
|
||||||
)
|
)
|
||||||
|
@ -25,11 +25,8 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
// local
|
|
||||||
|
|
||||||
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
|
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
||||||
)
|
)
|
||||||
|
@ -25,10 +25,8 @@
|
|||||||
package databaseinterface
|
package databaseinterface
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
// local
|
|
||||||
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
|
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
"go.dev.pztrn.name/fastpastebin/internal/structs"
|
||||||
)
|
)
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package pagination
|
package pagination
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/assets/static"
|
"go.dev.pztrn.name/fastpastebin/assets/static"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -48,9 +46,11 @@ func CreateHTML(currentPage int, pages int, linksBase string) string {
|
|||||||
)
|
)
|
||||||
|
|
||||||
for i <= pages {
|
for i <= pages {
|
||||||
|
// ToDo: fix it!
|
||||||
|
// nolint:nestif
|
||||||
if pages > 5 {
|
if pages > 5 {
|
||||||
if currentPage-3 < i && currentPage+3 > i || i == pages {
|
if currentPage-3 < i && currentPage+3 > i || i == pages {
|
||||||
var paginationItemRaw = string(paginationLinkRaw)
|
paginationItemRaw := string(paginationLinkRaw)
|
||||||
if i == currentPage {
|
if i == currentPage {
|
||||||
paginationItemRaw = string(paginationLinkCurrentRaw)
|
paginationItemRaw = string(paginationLinkCurrentRaw)
|
||||||
}
|
}
|
||||||
@ -68,7 +68,7 @@ func CreateHTML(currentPage int, pages int, linksBase string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var paginationItemRaw = string(paginationLinkRaw)
|
paginationItemRaw := string(paginationLinkRaw)
|
||||||
if i == currentPage {
|
if i == currentPage {
|
||||||
paginationItemRaw = string(paginationLinkCurrentRaw)
|
paginationItemRaw = string(paginationLinkCurrentRaw)
|
||||||
}
|
}
|
||||||
|
@ -25,13 +25,11 @@
|
|||||||
package structs
|
package structs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
// other
|
|
||||||
"golang.org/x/crypto/scrypt"
|
"golang.org/x/crypto/scrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -95,6 +93,7 @@ func (p *Paste) CreatePassword(password string) error {
|
|||||||
// GenerateCryptedCookieValue generates crypted cookie value for paste.
|
// GenerateCryptedCookieValue generates crypted cookie value for paste.
|
||||||
func (p *Paste) GenerateCryptedCookieValue() string {
|
func (p *Paste) GenerateCryptedCookieValue() string {
|
||||||
cookieValueCrypted, _ := scrypt.Key([]byte(p.Password), []byte(p.PasswordSalt), 131072, 8, 1, 64)
|
cookieValueCrypted, _ := scrypt.Key([]byte(p.Password), []byte(p.PasswordSalt), 131072, 8, 1, 64)
|
||||||
|
|
||||||
return fmt.Sprintf("%x", sha256.Sum256(cookieValueCrypted))
|
return fmt.Sprintf("%x", sha256.Sum256(cookieValueCrypted))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,17 +25,13 @@
|
|||||||
package templater
|
package templater
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// stdlib
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
// local
|
|
||||||
"go.dev.pztrn.name/fastpastebin/assets/static"
|
|
||||||
"go.dev.pztrn.name/fastpastebin/internal/context"
|
|
||||||
|
|
||||||
// other
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/assets/static"
|
||||||
|
"go.dev.pztrn.name/fastpastebin/internal/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -59,6 +55,7 @@ func GetRawTemplate(ec echo.Context, templateName string, data map[string]string
|
|||||||
tplRaw, err := static.ReadFile(templateName)
|
tplRaw, err := static.ReadFile(templateName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = ec.String(http.StatusBadRequest, templateName+" not found.")
|
_ = ec.String(http.StatusBadRequest, templateName+" not found.")
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,6 +76,7 @@ func GetTemplate(ec echo.Context, name string, data map[string]string) string {
|
|||||||
mainhtml, err := static.ReadFile("main.html")
|
mainhtml, err := static.ReadFile("main.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = ec.String(http.StatusBadRequest, "main.html not found.")
|
_ = ec.String(http.StatusBadRequest, "main.html not found.")
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,6 +84,7 @@ func GetTemplate(ec echo.Context, name string, data map[string]string) string {
|
|||||||
navhtml, err1 := static.ReadFile("navigation.html")
|
navhtml, err1 := static.ReadFile("navigation.html")
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
_ = ec.String(http.StatusBadRequest, "navigation.html not found.")
|
_ = ec.String(http.StatusBadRequest, "navigation.html not found.")
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,6 +92,7 @@ func GetTemplate(ec echo.Context, name string, data map[string]string) string {
|
|||||||
footerhtml, err2 := static.ReadFile("footer.html")
|
footerhtml, err2 := static.ReadFile("footer.html")
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
_ = ec.String(http.StatusBadRequest, "footer.html not found.")
|
_ = ec.String(http.StatusBadRequest, "footer.html not found.")
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,6 +106,7 @@ func GetTemplate(ec echo.Context, name string, data map[string]string) string {
|
|||||||
reqhtml, err3 := static.ReadFile(name)
|
reqhtml, err3 := static.ReadFile(name)
|
||||||
if err3 != nil {
|
if err3 != nil {
|
||||||
_ = ec.String(http.StatusBadRequest, name+" not found.")
|
_ = ec.String(http.StatusBadRequest, name+" not found.")
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user