Use SQL database instead of bolt #71

Merged
mrngm merged 6 commits from sql into master 2021-05-05 21:34:29 +02:00
9 changed files with 49 additions and 50 deletions
Showing only changes of commit f36fa30eff - Show all commits

View File

@ -5,7 +5,7 @@ import (
"log"
"gitea.hashru.nl/dsprenkels/rushlink"
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
db "gitea.hashru.nl/dsprenkels/rushlink/internal/boltdb"
)
var (

View File

@ -11,7 +11,7 @@ import (
"strings"
"time"
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
"gitea.hashru.nl/dsprenkels/rushlink/internal/boltdb"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/pkg/errors"
@ -96,18 +96,18 @@ func (rl *rushlink) viewPasteHandlerMeta(w http.ResponseWriter, r *http.Request)
func (rl *rushlink) viewPasteHandlerFlags(w http.ResponseWriter, r *http.Request, flags viewPaste) {
vars := mux.Vars(r)
key := vars["key"]
var p *db.Paste
var fu *db.FileUpload
var p *boltdb.Paste
var fu *boltdb.FileUpload
err := rl.db.Bolt.View(func(tx *bolt.Tx) error {
var err error
p, err = db.GetPaste(tx, key)
p, err = boltdb.GetPaste(tx, key)
if err != nil {
return err
}
if p != nil && p.Type == db.PasteTypeFileUpload {
if p != nil && p.Type == boltdb.PasteTypeFileUpload {
var id uuid.UUID
copy(id[:], p.Content)
fu, err = db.GetFileUpload(tx, id)
fu, err = boltdb.GetFileUpload(tx, id)
if err != nil {
return err
}
@ -115,7 +115,7 @@ func (rl *rushlink) viewPasteHandlerFlags(w http.ResponseWriter, r *http.Request
return nil
})
if err != nil {
status := db.ErrHTTPStatusCode(err)
status := boltdb.ErrHTTPStatusCode(err)
if status == http.StatusInternalServerError {
panic(err)
}
@ -126,22 +126,22 @@ func (rl *rushlink) viewPasteHandlerFlags(w http.ResponseWriter, r *http.Request
rl.viewPasteHandlerInner(w, r, flags, p, fu)
}
func (rl *rushlink) viewPasteHandlerInner(w http.ResponseWriter, r *http.Request, flags viewPaste, p *db.Paste, fu *db.FileUpload) {
func (rl *rushlink) viewPasteHandlerInner(w http.ResponseWriter, r *http.Request, flags viewPaste, p *boltdb.Paste, fu *boltdb.FileUpload) {
if flags&viewShowMeta != 0 {
rl.viewPasteHandlerInnerMeta(w, r, p, fu)
return
}
switch p.State {
case db.PasteStatePresent:
case boltdb.PasteStatePresent:
switch p.Type {
case db.PasteTypeFileUpload:
case boltdb.PasteTypeFileUpload:
if fu == nil {
panic(fmt.Sprintf("file for id %v does not exist in database\n", string(p.Content)))
}
rl.viewFileUploadHandler(w, r, fu)
return
case db.PasteTypeRedirect:
case boltdb.PasteTypeRedirect:
if flags&viewNoRedirect != 0 {
w.Write([]byte(p.RedirectURL().String()))
return
@ -152,7 +152,7 @@ func (rl *rushlink) viewPasteHandlerInner(w http.ResponseWriter, r *http.Request
panic("paste type unsupported")
}
case db.PasteStateDeleted:
case boltdb.PasteStateDeleted:
rl.renderError(w, r, http.StatusGone, "paste has been deleted\n")
return
default:
@ -160,7 +160,7 @@ func (rl *rushlink) viewPasteHandlerInner(w http.ResponseWriter, r *http.Request
}
}
func (rl *rushlink) viewFileUploadHandler(w http.ResponseWriter, r *http.Request, fu *db.FileUpload) {
func (rl *rushlink) viewFileUploadHandler(w http.ResponseWriter, r *http.Request, fu *boltdb.FileUpload) {
filePath := fu.Path(rl.fs)
file, err := os.Open(filePath)
if err != nil {
@ -191,7 +191,7 @@ func (rl *rushlink) viewFileUploadHandler(w http.ResponseWriter, r *http.Request
http.ServeContent(w, r, fu.FileName, modtime, file)
}
func (rl *rushlink) viewPasteHandlerInnerMeta(w http.ResponseWriter, r *http.Request, p *db.Paste, fu *db.FileUpload) {
func (rl *rushlink) viewPasteHandlerInnerMeta(w http.ResponseWriter, r *http.Request, p *boltdb.Paste, fu *boltdb.FileUpload) {
var cd canDelete
deleteToken := getDeleteTokenFromRequest(r)
if deleteToken != "" {
@ -213,7 +213,7 @@ func (rl *rushlink) viewPasteHandlerInnerMeta(w http.ResponseWriter, r *http.Req
"CanDeleteBool": cd.Bool(),
}
var status int
if p.State == db.PasteStateDeleted {
if p.State == boltdb.PasteStateDeleted {
status = http.StatusGone
} else {
status = http.StatusOK
@ -222,7 +222,7 @@ func (rl *rushlink) viewPasteHandlerInnerMeta(w http.ResponseWriter, r *http.Req
return
}
func (rl *rushlink) viewActionSuccess(w http.ResponseWriter, r *http.Request, p *db.Paste, fu *db.FileUpload) {
func (rl *rushlink) viewActionSuccess(w http.ResponseWriter, r *http.Request, p *boltdb.Paste, fu *boltdb.FileUpload) {
var fileExt string
if fu != nil {
fileExt = fu.Ext()
@ -280,11 +280,11 @@ func (rl *rushlink) newPasteHandler(w http.ResponseWriter, r *http.Request) {
}
func (rl *rushlink) newFileUploadPasteHandler(w http.ResponseWriter, r *http.Request, file multipart.File, header multipart.FileHeader) {
var fu *db.FileUpload
var paste *db.Paste
var fu *boltdb.FileUpload
var paste *boltdb.Paste
if err := rl.db.Bolt.Update(func(tx *bolt.Tx) error {
var err error
fu, err = db.NewFileUpload(rl.fs, file, header.Filename)
fu, err = boltdb.NewFileUpload(rl.fs, file, header.Filename)
if err != nil {
panic(errors.Wrap(err, "creating fileUpload"))
}
@ -316,7 +316,7 @@ func (rl *rushlink) newRedirectPasteHandler(w http.ResponseWriter, r *http.Reque
return
}
var paste *db.Paste
var paste *boltdb.Paste
if err := rl.db.Bolt.Update(func(tx *bolt.Tx) error {
var err error
paste, err = shortenURL(tx, userURL)
@ -339,15 +339,15 @@ func (rl *rushlink) deletePasteHandler(w http.ResponseWriter, r *http.Request) {
}
var errorCode int
var paste *db.Paste
var paste *boltdb.Paste
if err := rl.db.Bolt.Update(func(tx *bolt.Tx) error {
var err error
paste, err = db.GetPaste(tx, key)
paste, err = boltdb.GetPaste(tx, key)
if err != nil {
errorCode = http.StatusNotFound
return err
}
if paste.State == db.PasteStateDeleted {
if paste.State == boltdb.PasteStateDeleted {
errorCode = http.StatusGone
return errors.New("already deleted")
}
@ -372,39 +372,39 @@ func (rl *rushlink) deletePasteHandler(w http.ResponseWriter, r *http.Request) {
//
// Returns the new paste key if the fileUpload was successfully added to the
// database
func shortenFileUploadID(tx *bolt.Tx, id uuid.UUID) (*db.Paste, error) {
return shorten(tx, db.PasteTypeFileUpload, id[:])
func shortenFileUploadID(tx *bolt.Tx, id uuid.UUID) (*boltdb.Paste, error) {
return shorten(tx, boltdb.PasteTypeFileUpload, id[:])
}
// Add a new URL to the database
//
// Returns the new paste key if the url was successfully shortened
func shortenURL(tx *bolt.Tx, userURL *url.URL) (*db.Paste, error) {
return shorten(tx, db.PasteTypeRedirect, []byte(userURL.String()))
func shortenURL(tx *bolt.Tx, userURL *url.URL) (*boltdb.Paste, error) {
return shorten(tx, boltdb.PasteTypeRedirect, []byte(userURL.String()))
}
// Add a paste (of any kind) to the database with arbitrary content.
func shorten(tx *bolt.Tx, ty db.PasteType, content []byte) (*db.Paste, error) {
func shorten(tx *bolt.Tx, ty boltdb.PasteType, content []byte) (*boltdb.Paste, error) {
// Generate the paste key
var keyEntropy int
if ty == db.PasteTypeFileUpload || ty == db.PasteTypePaste {
if ty == boltdb.PasteTypeFileUpload || ty == boltdb.PasteTypePaste {
keyEntropy = highOnlineEntropy
}
pasteKey, err := db.GeneratePasteKey(tx, keyEntropy)
pasteKey, err := boltdb.GeneratePasteKey(tx, keyEntropy)
if err != nil {
return nil, errors.Wrap(err, "generating paste key")
}
// Also generate a deleteToken
deleteToken, err := db.GenerateDeleteToken()
deleteToken, err := boltdb.GenerateDeleteToken()
if err != nil {
return nil, errors.Wrap(err, "generating delete token")
}
// Store the new key
p := db.Paste{
p := boltdb.Paste{
Type: ty,
State: db.PasteStatePresent,
State: boltdb.PasteStatePresent,
Content: content,
Key: pasteKey,
DeleteToken: deleteToken,

View File

@ -13,7 +13,7 @@ import (
"strings"
"testing"
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
"gitea.hashru.nl/dsprenkels/rushlink/internal/boltdb"
"github.com/gorilla/mux"
"go.etcd.io/bbolt"
)
@ -31,12 +31,12 @@ func createTemporaryRouter(t *testing.T) (*mux.Router, *rushlink) {
os.RemoveAll(tempDir)
})
fileStore, err := db.OpenFileStore(filepath.Join(tempDir, "filestore"))
fileStore, err := boltdb.OpenFileStore(filepath.Join(tempDir, "filestore"))
if err != nil {
t.Fatalf("opening temporary filestore: %s\n", err)
}
databasePath := filepath.Join(tempDir, "rushlink.db")
database, err := db.OpenDB(databasePath, fileStore)
database, err := boltdb.OpenDB(databasePath, fileStore)
if err != nil {
t.Fatalf("opening temporary database: %s\n", err)
}
@ -136,7 +136,7 @@ func TestIssue53(t *testing.T) {
// Check that any attempt to do directory traversal has failed.
rl.db.Bolt.View(func(tx *bbolt.Tx) error {
fus, err := db.AllFileUploads(tx)
fus, err := boltdb.AllFileUploads(tx)
if err != nil {
t.Fatal(err)
}

View File

@ -1,4 +1,4 @@
package db
package boltdb
import (
"bytes"

View File

@ -1,4 +1,4 @@
package db
package boltdb
import (
"bytes"

View File

@ -1,4 +1,4 @@
package db
package boltdb
import (
"crypto/rand"

View File

@ -1,4 +1,4 @@
package db
package boltdb
import "testing"

View File

@ -5,8 +5,7 @@ import (
"net/http"
"time"
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
"gitea.hashru.nl/dsprenkels/rushlink/internal/boltdb"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
@ -23,7 +22,7 @@ var metricRequestsTotalCounter = prometheus.NewCounterVec(prometheus.CounterOpts
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
}, []string{"code", "method"})
func metricURLsTotal(database *db.Database) float64 {
func metricURLsTotal(database *boltdb.Database) float64 {
var metric float64
if err := database.Bolt.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte("pastes"))
@ -40,7 +39,7 @@ func metricURLsTotal(database *db.Database) float64 {
}
// StartMetricsServer starts sering Prometheus metrics exports on addr
func StartMetricsServer(addr string, database *db.Database, fs *db.FileStore) {
func StartMetricsServer(addr string, database *boltdb.Database, fs *boltdb.FileStore) {
prometheus.MustRegister(metricRequestsTotalCounter)
prometheus.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{

View File

@ -9,7 +9,7 @@ import (
"strconv"
"time"
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
"gitea.hashru.nl/dsprenkels/rushlink/internal/boltdb"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
@ -19,8 +19,8 @@ const urlKeyExpr = "{key:[A-Za-z0-9-_]{4,}}"
const urlKeyWithExtExpr = urlKeyExpr + "{ext:\\.[A-Za-z0-9-_]+}"
type rushlink struct {
db *db.Database
fs *db.FileStore
db *boltdb.Database
fs *boltdb.FileStore
rootURL *url.URL
}
@ -111,7 +111,7 @@ func InitMainRouter(r *mux.Router, rl *rushlink) {
}
// StartMainServer starts the main http server listening on addr.
func StartMainServer(addr string, db *db.Database, fs *db.FileStore, rawRootURL string) {
func StartMainServer(addr string, db *boltdb.Database, fs *boltdb.FileStore, rawRootURL string) {
var rootURL *url.URL
if rawRootURL != "" {
var err error