The Great Sources Linting.

This commit is contained in:
Stanislav Nikitin 2021-06-14 23:48:34 +05:00
parent 3265c5a4b2
commit 6ea6e2e144
Signed by: pztrn
GPG Key ID: 1E944A0F0568B550
43 changed files with 95 additions and 155 deletions

View File

@ -12,6 +12,9 @@ linters:
- funlen
# Magic numbers everywhere and we can't get rid of them.
- 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:
lll:
line-length: 420
@ -19,3 +22,10 @@ linters-settings:
min-complexity: 50
gocyclo:
min-complexity: 40
issues:
exclude-rules:
# There will be some ToDos.
- linters:
- godox
text: "TODO"

View File

@ -25,12 +25,10 @@
package main
import (
// stdlib
"os"
"os/signal"
"syscall"
// local
"go.dev.pztrn.name/fastpastebin/domains/dbnotavailable"
"go.dev.pztrn.name/fastpastebin/domains/indexpage"
"go.dev.pztrn.name/fastpastebin/domains/pastes"

View File

@ -25,17 +25,13 @@
package dbnotavailable
import (
// stdlib
"net/http"
// local
"go.dev.pztrn.name/fastpastebin/internal/templater"
// other
"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 {
htmlData := templater.GetTemplate(ec, "database_not_available.html", nil)

View File

@ -25,13 +25,10 @@
package dbnotavailable
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
)
var (
c *context.Context
)
var c *context.Context
// New initializes pastes package and adds necessary HTTP and API
// endpoints.

View File

@ -25,13 +25,10 @@
package indexpage
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
)
var (
c *context.Context
)
var c *context.Context
// New initializes pastes package and adds necessary HTTP and API
// endpoints.

View File

@ -25,30 +25,27 @@
package indexpage
import (
// stdlib
"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/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.
func indexGet(ec echo.Context) error {
// We should check if database connection available.
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")
}
// Generate list of available languages to highlight.
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 {
availableLexersSelectOpts += "<option value='" + availableLexers[i] + "'>" + availableLexers[i] + "</option>"
}

View File

@ -25,20 +25,14 @@
package pastes
import (
// stdlib
"regexp"
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
)
var (
regexInts = regexp.MustCompile("[0-9]+")
)
var regexInts = regexp.MustCompile("[0-9]+")
var (
c *context.Context
)
var c *context.Context
// New initializes pastes package and adds necessary HTTP and API
// endpoints.

View File

@ -1,23 +1,20 @@
package pastes
import (
// stdlib
"bytes"
"net/http"
"strconv"
"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/formatters"
htmlfmt "github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"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 (
@ -38,12 +35,14 @@ func pasteGetData(pasteID int, timestamp int64, cookieValue string) (*structs.Pa
paste, err1 := c.Database.GetPaste(pasteID)
if err1 != nil {
c.Logger.Error().Err(err1).Int("paste ID", pasteID).Msg("Failed to get paste")
return nil, pasteNotFound
}
// Check if paste is expired.
if paste.IsExpired() {
c.Logger.Error().Int("paste ID", pasteID).Msg("Paste is expired")
return nil, pasteExpired
}
@ -52,6 +51,7 @@ func pasteGetData(pasteID int, timestamp int64, cookieValue string) (*structs.Pa
pasteTs := paste.CreatedAt.Unix()
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")
return nil, pasteTimestampInvalid
}
}
@ -109,18 +109,20 @@ func pasteGETWebInterface(ec echo.Context) error {
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.
if error == pasteExpired || error == pasteNotFound || error == pasteTimestampInvalid {
if err == pasteExpired || err == pasteNotFound || err == pasteTimestampInvalid {
errtpl := templater.GetErrorTemplate(ec, "Paste #"+pasteIDRaw+" not found")
return ec.HTML(http.StatusNotFound, errtpl)
}
// If passed cookie value was invalid - go to paste authorization
// page.
if error == pasteCookieInvalid {
if err == pasteCookieInvalid {
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")
}
@ -212,6 +214,7 @@ func pastePasswordedVerifyGet(ec echo.Context) error {
if cookieValue == cookie.Value {
c.Logger.Info().Msg("Valid cookie, redirecting to paste page...")
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.
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")
}
@ -284,7 +287,7 @@ func pastePasswordedVerifyPost(ec echo.Context) error {
func pasteRawGETWebInterface(ec echo.Context) error {
// We should check if database connection available.
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")
}
@ -298,11 +301,13 @@ func pasteRawGETWebInterface(ec echo.Context) error {
paste, err1 := c.Database.GetPaste(pasteID)
if err1 != nil {
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.")
}
if paste.IsExpired() {
c.Logger.Error().Int("paste ID", pasteID).Msg("Paste is expired")
return ec.HTML(http.StatusBadRequest, "Paste #"+pasteIDRaw+" does not exist.")
}

View File

@ -1,30 +1,29 @@
package pastes
import (
// stdlib
"net/http"
"regexp"
"strconv"
"strings"
"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/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
// "/pastes/CREATED_PASTE_ID". This handler will do all the job for
// requests comes from browsers via web interface.
func pastePOSTWebInterface(ec echo.Context) error {
// We should check if database connection available.
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")
}
@ -48,7 +47,7 @@ func pastePOSTWebInterface(ec echo.Context) error {
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")
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
keepForUnit := 0
if params["paste-keep-for"][0] != "forever" {
if params["paste-keep-for"][0] != KeepPastesForever {
keepForUnitRegex := regexp.MustCompile("[Mmhd]")
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)
if err != nil {
if params["paste-keep-for"][0] == "forever" {
if params["paste-keep-for"][0] == KeepPastesForever {
c.Logger.Debug().Msg("Keeping paste forever!")
keepFor = 0

View File

@ -25,17 +25,14 @@
package pastes
import (
// stdlib
"net/http"
"strconv"
"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/templater"
// other
"github.com/labstack/echo"
)
// GET for "/pastes/", a list of publicly available pastes.
@ -43,13 +40,13 @@ import (
func pastesGET(ec echo.Context) error {
// We should check if database connection available.
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")
}
pageFromParamRaw := ec.Param("page")
var page = 1
page := 1
if pageFromParamRaw != "" {
pageRaw := regexInts.FindAllString(pageFromParamRaw, 1)[0]
@ -62,7 +59,7 @@ func pastesGET(ec echo.Context) error {
pastes, err3 := c.Database.GetPagedPastes(page)
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.
if err3 != nil {

View File

@ -25,13 +25,10 @@
package captcha
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
// other
"github.com/dchest/captcha"
"github.com/labstack/echo"
"github.com/rs/zerolog"
"go.dev.pztrn.name/fastpastebin/internal/context"
)
var (

View File

@ -28,6 +28,6 @@ package config
type HTTP struct {
Address string `yaml:"address"`
Port string `yaml:"port"`
AllowInsecure bool `yaml:"allow_insecure"`
MaxBodySizeMegabytes string `yaml:"max_body_size_megabytes"`
AllowInsecure bool `yaml:"allow_insecure"`
}

View File

@ -26,7 +26,7 @@ package config
// Logging describes logger configuration.
type Logging struct {
LogToFile bool `yaml:"log_to_file"`
FileName string `yaml:"filename"`
LogLevel string `yaml:"loglevel"`
LogToFile bool `yaml:"log_to_file"`
}

View File

@ -25,18 +25,14 @@
package context
import (
// stdlib
"io/ioutil"
"os"
"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/rs/zerolog"
"go.dev.pztrn.name/fastpastebin/internal/config"
databaseinterface "go.dev.pztrn.name/fastpastebin/internal/database/interface"
"go.dev.pztrn.name/flagger"
"gopkg.in/yaml.v2"
)

View File

@ -1,12 +1,9 @@
package context
import (
// local
"go.dev.pztrn.name/fastpastebin/assets/static"
// other
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"go.dev.pztrn.name/fastpastebin/assets/static"
)
func (c *Context) initializeHTTPServer() {

View File

@ -1,14 +1,12 @@
package context
import (
// stdlib
"fmt"
"os"
"runtime"
"strings"
"time"
// other
"github.com/rs/zerolog"
)

View File

@ -25,19 +25,15 @@
package database
import (
// stdlib
"database/sql"
"time"
// local
_ "github.com/go-sql-driver/mysql"
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/flatfiles"
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/postgresql"
"go.dev.pztrn.name/fastpastebin/internal/structs"
// other
_ "github.com/go-sql-driver/mysql"
)
// Database represents control structure for database connection.
@ -111,7 +107,7 @@ func (db *Database) Initialize() {
if c.Config.Database.Type == "mysql" {
mysql.New(c)
} else if c.Config.Database.Type == "flatfiles" {
} else if c.Config.Database.Type == flatfiles.FlatFileDialect {
flatfiles.New(c)
} else if c.Config.Database.Type == "postgresql" {
postgresql.New(c)

View File

@ -25,11 +25,12 @@
package flatfiles
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
)
const FlatFileDialect = "flatfiles"
var (
c *context.Context
f *FlatFiles

View File

@ -25,7 +25,6 @@
package flatfiles
import (
// stdlib
"database/sql"
"encoding/json"
"io/ioutil"
@ -36,14 +35,13 @@ import (
"strings"
"sync"
// local
"go.dev.pztrn.name/fastpastebin/internal/structs"
)
type FlatFiles struct {
writeMutex sync.Mutex
path string
pastesIndex []Index
writeMutex sync.Mutex
}
// 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)
if err != nil {
c.Logger.Debug().Err(err).Msg("Failed to read paste from storage")
return nil, err
}
@ -100,6 +99,7 @@ func (ff *FlatFiles) GetPaste(pasteID int) (*structs.Paste, error) {
err1 := json.Unmarshal(pasteInBytes, paste)
if err1 != nil {
c.Logger.Error().Err(err1).Msgf("Failed to parse paste")
return nil, err1
}
@ -132,11 +132,13 @@ func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) {
if idx < startPagination {
c.Logger.Debug().Int("paste index", idx).Msg("Paste isn't in pagination query: too low index")
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)) {
c.Logger.Debug().Int("paste index", idx).Msg("Paste isn't in pagination query: too high index")
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"))
if err != nil {
c.Logger.Error().Err(err).Msg("Failed to read paste data")
continue
}
err1 := json.Unmarshal(pasteRawData, pasteData)
if err1 != nil {
c.Logger.Error().Err(err1).Msg("Failed to parse paste data")
continue
}
@ -253,12 +257,14 @@ func (ff *FlatFiles) SavePaste(p *structs.Paste) (int64, error) {
data, err := json.Marshal(p)
if err != nil {
ff.writeMutex.Unlock()
return 0, err
}
err = ioutil.WriteFile(filepath.Join(ff.path, "pastes", strconv.Itoa(pasteID)+".json"), data, 0644)
if err != nil {
ff.writeMutex.Unlock()
return 0, err
}
@ -278,12 +284,14 @@ func (ff *FlatFiles) Shutdown() {
indexData, err := json.Marshal(ff.pastesIndex)
if err != nil {
c.Logger.Error().Err(err).Msg("Failed to encode index data into JSON")
return
}
err1 := ioutil.WriteFile(filepath.Join(ff.path, "pastes", "index.json"), indexData, 0644)
if err1 != nil {
c.Logger.Error().Err(err1).Msg("Failed to write index data to file. Pretty sure that you've lost your pastes.")
return
}
}

View File

@ -25,10 +25,8 @@
package flatfiles
import (
// stdlib
"database/sql"
// local
"go.dev.pztrn.name/fastpastebin/internal/structs"
)

View File

@ -25,10 +25,8 @@
package dialectinterface
import (
// stdlib
"database/sql"
// local
"go.dev.pztrn.name/fastpastebin/internal/structs"
)

View File

@ -25,7 +25,6 @@
package mysql
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
)

View File

@ -25,10 +25,8 @@
package mysql
import (
// stdlib
"database/sql"
// local
"go.dev.pztrn.name/fastpastebin/internal/structs"
)

View File

@ -25,7 +25,6 @@
package migrations
import (
// stdlib
"database/sql"
)

View File

@ -25,7 +25,6 @@
package migrations
import (
// stdlib
"database/sql"
)

View File

@ -25,7 +25,6 @@
package migrations
import (
// stdlib
"database/sql"
)

View File

@ -25,7 +25,6 @@
package migrations
import (
// stdlib
"database/sql"
)

View File

@ -25,17 +25,11 @@
package migrations
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
// other
//"gitlab.com/jmoiron/sqlx"
"github.com/pressly/goose"
"go.dev.pztrn.name/fastpastebin/internal/context"
)
var (
c *context.Context
)
var c *context.Context
// New initializes migrations.
func New(cc *context.Context) {

View File

@ -25,17 +25,13 @@
package mysql
import (
// stdlib
"database/sql"
"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/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.
@ -169,6 +165,7 @@ func (db *Database) Initialize() {
dbConn, err := sqlx.Connect("mysql", dbConnString)
if err != nil {
c.Logger.Error().Err(err).Msg("Failed to connect to database")
return
}

View File

@ -25,7 +25,6 @@
package postgresql
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
)

View File

@ -25,10 +25,8 @@
package postgresql
import (
// stdlib
"database/sql"
// local
"go.dev.pztrn.name/fastpastebin/internal/structs"
)

View File

@ -25,7 +25,6 @@
package migrations
import (
// stdlib
"database/sql"
)

View File

@ -25,7 +25,6 @@
package migrations
import (
// stdlib
"database/sql"
)

View File

@ -25,7 +25,6 @@
package migrations
import (
// stdlib
"database/sql"
)

View File

@ -25,7 +25,6 @@
package migrations
import (
// stdlib
"database/sql"
)

View File

@ -25,17 +25,11 @@
package migrations
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
// other
//"gitlab.com/jmoiron/sqlx"
"github.com/pressly/goose"
"go.dev.pztrn.name/fastpastebin/internal/context"
)
var (
c *context.Context
)
var c *context.Context
// New initializes migrations.
func New(cc *context.Context) {

View File

@ -25,19 +25,14 @@
package postgresql
import (
// stdlib
"database/sql"
"fmt"
"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/structs"
// other
"github.com/jmoiron/sqlx"
// postgresql adapter
_ "github.com/lib/pq"
)
// Database is a PostgreSQL connection controlling structure.
@ -182,6 +177,7 @@ func (db *Database) Initialize() {
dbConn, err := sqlx.Connect("postgres", dbConnString)
if err != nil {
c.Logger.Error().Err(err).Msg("Failed to connect to database")
return
}

View File

@ -25,7 +25,6 @@
package database
import (
// local
"go.dev.pztrn.name/fastpastebin/internal/context"
databaseinterface "go.dev.pztrn.name/fastpastebin/internal/database/interface"
)

View File

@ -25,11 +25,8 @@
package database
import (
// stdlib
"database/sql"
// local
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
"go.dev.pztrn.name/fastpastebin/internal/structs"
)

View File

@ -25,10 +25,8 @@
package databaseinterface
import (
// stdlib
"database/sql"
// local
dialectinterface "go.dev.pztrn.name/fastpastebin/internal/database/dialects/interface"
"go.dev.pztrn.name/fastpastebin/internal/structs"
)

View File

@ -1,11 +1,9 @@
package pagination
import (
// stdlib
"strconv"
"strings"
// local
"go.dev.pztrn.name/fastpastebin/assets/static"
)
@ -48,9 +46,11 @@ func CreateHTML(currentPage int, pages int, linksBase string) string {
)
for i <= pages {
// ToDo: fix it!
// nolint:nestif
if pages > 5 {
if currentPage-3 < i && currentPage+3 > i || i == pages {
var paginationItemRaw = string(paginationLinkRaw)
paginationItemRaw := string(paginationLinkRaw)
if i == currentPage {
paginationItemRaw = string(paginationLinkCurrentRaw)
}
@ -68,7 +68,7 @@ func CreateHTML(currentPage int, pages int, linksBase string) string {
}
}
} else {
var paginationItemRaw = string(paginationLinkRaw)
paginationItemRaw := string(paginationLinkRaw)
if i == currentPage {
paginationItemRaw = string(paginationLinkCurrentRaw)
}

View File

@ -25,13 +25,11 @@
package structs
import (
// stdlib
"crypto/sha256"
"fmt"
"math/rand"
"time"
// other
"golang.org/x/crypto/scrypt"
)
@ -95,6 +93,7 @@ func (p *Paste) CreatePassword(password string) error {
// GenerateCryptedCookieValue generates crypted cookie value for paste.
func (p *Paste) GenerateCryptedCookieValue() string {
cookieValueCrypted, _ := scrypt.Key([]byte(p.Password), []byte(p.PasswordSalt), 131072, 8, 1, 64)
return fmt.Sprintf("%x", sha256.Sum256(cookieValueCrypted))
}

View File

@ -25,17 +25,13 @@
package templater
import (
// stdlib
"net/http"
"strings"
// local
"go.dev.pztrn.name/fastpastebin/assets/static"
"go.dev.pztrn.name/fastpastebin/internal/context"
// other
"github.com/labstack/echo"
"github.com/rs/zerolog"
"go.dev.pztrn.name/fastpastebin/assets/static"
"go.dev.pztrn.name/fastpastebin/internal/context"
)
var (
@ -59,6 +55,7 @@ func GetRawTemplate(ec echo.Context, templateName string, data map[string]string
tplRaw, err := static.ReadFile(templateName)
if err != nil {
_ = ec.String(http.StatusBadRequest, templateName+" not found.")
return ""
}
@ -79,6 +76,7 @@ func GetTemplate(ec echo.Context, name string, data map[string]string) string {
mainhtml, err := static.ReadFile("main.html")
if err != nil {
_ = ec.String(http.StatusBadRequest, "main.html not found.")
return ""
}
@ -86,6 +84,7 @@ func GetTemplate(ec echo.Context, name string, data map[string]string) string {
navhtml, err1 := static.ReadFile("navigation.html")
if err1 != nil {
_ = ec.String(http.StatusBadRequest, "navigation.html not found.")
return ""
}
@ -93,6 +92,7 @@ func GetTemplate(ec echo.Context, name string, data map[string]string) string {
footerhtml, err2 := static.ReadFile("footer.html")
if err2 != nil {
_ = ec.String(http.StatusBadRequest, "footer.html not found.")
return ""
}
@ -106,6 +106,7 @@ func GetTemplate(ec echo.Context, name string, data map[string]string) string {
reqhtml, err3 := static.ReadFile(name)
if err3 != nil {
_ = ec.String(http.StatusBadRequest, name+" not found.")
return ""
}