Database dialects, proper database shutdown, pagination in configuration.

Added possibility to use different database (storage) backends. Currently
supported: flatfiles and mysql. Fixes #2.

Added database shutdown call. This call will properly shutdown database
connections (in case of RDBMS) or flush pastes index cache on disk (in
case of flatfiles).

De-hardcoded pagination count. Fixes #12.
This commit is contained in:
2018-05-27 12:25:01 +05:00
parent 296e2771d6
commit 0cca0f453f
22 changed files with 854 additions and 175 deletions

View File

@@ -36,6 +36,7 @@ import (
// local
"github.com/pztrn/fastpastebin/captcha"
"github.com/pztrn/fastpastebin/pagination"
"github.com/pztrn/fastpastebin/pastes/model"
"github.com/pztrn/fastpastebin/templater"
// other
@@ -61,7 +62,7 @@ func pasteGET(ec echo.Context) error {
c.Logger.Debug().Msgf("Requesting paste #%+v", pasteID)
// Get paste.
paste, err1 := GetByID(pasteID)
paste, err1 := c.Database.GetPaste(pasteID)
if err1 != nil {
c.Logger.Error().Msgf("Failed to get paste #%d: %s", pasteID, err1.Error())
errtpl := templater.GetErrorTemplate(ec, "Paste #"+strconv.Itoa(pasteID)+" not found")
@@ -172,7 +173,7 @@ func pastePasswordedVerifyGet(ec echo.Context) error {
pasteID, _ := strconv.Atoi(regexInts.FindAllString(pasteIDRaw, 1)[0])
// Get paste.
paste, err1 := GetByID(pasteID)
paste, err1 := c.Database.GetPaste(pasteID)
if err1 != nil {
c.Logger.Error().Msgf("Failed to get paste #%d: %s", pasteID, err1.Error())
errtpl := templater.GetErrorTemplate(ec, "Paste #"+pasteIDRaw+" not found")
@@ -216,7 +217,7 @@ func pastePasswordedVerifyPost(ec echo.Context) error {
c.Logger.Debug().Msgf("Requesting paste #%+v", pasteID)
// Get paste.
paste, err1 := GetByID(pasteID)
paste, err1 := c.Database.GetPaste(pasteID)
if err1 != nil {
c.Logger.Error().Msgf("Failed to get paste #%d: %s", pasteID, err1.Error())
errtpl := templater.GetErrorTemplate(ec, "Paste #"+strconv.Itoa(pasteID)+" not found")
@@ -276,7 +277,7 @@ func pastePOST(ec echo.Context) error {
return ec.HTML(http.StatusBadRequest, errtpl)
}
paste := &Paste{
paste := &pastesmodel.Paste{
Title: params["paste-title"][0],
Data: params["paste-contents"][0],
Language: params["paste-language"][0],
@@ -301,7 +302,7 @@ func pastePOST(ec echo.Context) error {
paste.KeepFor = keepFor
keepForUnitRaw := keepForUnitRegex.FindAllString(params["paste-keep-for"][0], 1)[0]
keepForUnit := PASTE_KEEPS_CORELLATION[keepForUnitRaw]
keepForUnit := pastesmodel.PASTE_KEEPS_CORELLATION[keepForUnitRaw]
paste.KeepForUnitType = keepForUnit
// Try to autodetect if it was selected.
@@ -326,7 +327,7 @@ func pastePOST(ec echo.Context) error {
paste.CreatePassword(pastePassword[0])
}
id, err2 := Save(paste)
id, err2 := c.Database.SavePaste(paste)
if err2 != nil {
c.Logger.Debug().Msgf("Failed to save paste: %s", err2.Error())
errtpl := templater.GetErrorTemplate(ec, "Failed to save paste. Please, try again later.")
@@ -353,7 +354,7 @@ func pasteRawGET(ec echo.Context) error {
c.Logger.Debug().Msgf("Requesting paste #%+v", pasteID)
// Get paste.
paste, err1 := GetByID(pasteID)
paste, err1 := c.Database.GetPaste(pasteID)
if err1 != nil {
c.Logger.Error().Msgf("Failed to get paste #%d from database: %s", pasteID, err1.Error())
return ec.HTML(http.StatusBadRequest, "Paste #"+pasteIDRaw+" does not exist.")
@@ -401,7 +402,7 @@ func pastesGET(ec echo.Context) error {
c.Logger.Debug().Msgf("Requested page #%d", page)
// Get pastes IDs.
pastes, err3 := GetPagedPastes(page)
pastes, err3 := c.Database.GetPagedPastes(page)
c.Logger.Debug().Msgf("Got %d pastes", len(pastes))
var pastesString = "No pastes to show."
@@ -438,7 +439,7 @@ func pastesGET(ec echo.Context) error {
}
// Pagination.
pages := GetPastesPages()
pages := c.Database.GetPastesPages()
c.Logger.Debug().Msgf("Total pages: %d, current: %d", pages, page)
paginationHTML := pagination.CreateHTML(page, pages, "/pastes/")

View File

@@ -22,7 +22,7 @@
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package pastes
package pastesmodel
import (
// stdlib
@@ -55,16 +55,16 @@ var (
// Paste represents paste itself.
type Paste struct {
ID int `db:"id"`
Title string `db:"title"`
Data string `db:"data"`
CreatedAt *time.Time `db:"created_at"`
KeepFor int `db:"keep_for"`
KeepForUnitType int `db:"keep_for_unit_type"`
Language string `db:"language"`
Private bool `db:"private"`
Password string `db:"password"`
PasswordSalt string `db:"password_salt"`
ID int `db:"id" json:"id"`
Title string `db:"title" json:"title"`
Data string `db:"data" json:"data"`
CreatedAt *time.Time `db:"created_at" json:"created_at"`
KeepFor int `db:"keep_for" json:"keep_for"`
KeepForUnitType int `db:"keep_for_unit_type" json:"keep_for_unit_type"`
Language string `db:"language" json:"language"`
Private bool `db:"private" json:"private"`
Password string `db:"password" json:"password"`
PasswordSalt string `db:"password_salt" json:"password_salt"`
}
// CreatePassword creates password for current paste.

View File

@@ -1,115 +0,0 @@
// Fast Paste Bin - uberfast and easy-to-use pastebin.
//
// Copyright (c) 2018, Stanislav N. aka pztrn and Fast Paste Bin
// developers.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package pastes
const (
// Pagination. Hardcoded for 10 for now.
PAGINATION = 10
)
// GetByID returns a single paste by ID.
func GetByID(id int) (*Paste, error) {
p := &Paste{}
dbConn := c.Database.GetDatabaseConnection()
err := dbConn.Get(p, dbConn.Rebind("SELECT * FROM `pastes` WHERE id=?"), id)
if err != nil {
return nil, err
}
// Lets go with checking.
return p, nil
}
// GetPagedPastes returns a paged slice of pastes.
func GetPagedPastes(page int) ([]Paste, error) {
var pastesRaw []Paste
var pastes []Paste
dbConn := c.Database.GetDatabaseConnection()
// Pagination - 10 pastes on page.
var startPagination = 0
if page > 1 {
startPagination = (page - 1) * PAGINATION
}
err := dbConn.Select(&pastesRaw, dbConn.Rebind("SELECT * FROM `pastes` WHERE private != true ORDER BY id DESC LIMIT ? OFFSET ?"), PAGINATION, startPagination)
if err != nil {
return nil, err
}
for i := range pastesRaw {
if !pastesRaw[i].IsExpired() {
pastes = append(pastes, pastesRaw[i])
}
}
return pastes, nil
}
// GetPastesPages returns an integer that represents quantity of pages
// that can be requested (or drawn in paginator).
func GetPastesPages() int {
var pastesRaw []Paste
var pastes []Paste
dbConn := c.Database.GetDatabaseConnection()
err := dbConn.Get(&pastesRaw, "SELECT * FROM `pastes` WHERE private != true")
if err != nil {
return 1
}
// Check if pastes isn't expired.
for i := range pastesRaw {
if !pastesRaw[i].IsExpired() {
pastes = append(pastes, pastesRaw[i])
}
}
// Calculate pages.
pages := len(pastes) / PAGINATION
// Check if we have any remainder. Add 1 to pages count if so.
if len(pastes)%PAGINATION > 0 {
pages++
}
return pages
}
// Save saves paste to database and returns it's ID.
func Save(p *Paste) (int64, error) {
dbConn := c.Database.GetDatabaseConnection()
result, err := dbConn.NamedExec("INSERT INTO `pastes` (title, data, created_at, keep_for, keep_for_unit_type, language, private, password, password_salt) VALUES (:title, :data, :created_at, :keep_for, :keep_for_unit_type, :language, :private, :password, :password_salt)", p)
if err != nil {
return 0, err
}
ID, err1 := result.LastInsertId()
if err1 != nil {
return 0, err
}
return ID, nil
}