forked from electricdusk/rushlink
Rename current db to boltdb
This commit is contained in:
parent
104dbab335
commit
f36fa30eff
@ -5,7 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
|
|
||||||
"gitea.hashru.nl/dsprenkels/rushlink"
|
"gitea.hashru.nl/dsprenkels/rushlink"
|
||||||
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
|
db "gitea.hashru.nl/dsprenkels/rushlink/internal/boltdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
66
handlers.go
66
handlers.go
@ -11,7 +11,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
|
"gitea.hashru.nl/dsprenkels/rushlink/internal/boltdb"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/pkg/errors"
|
"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) {
|
func (rl *rushlink) viewPasteHandlerFlags(w http.ResponseWriter, r *http.Request, flags viewPaste) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
key := vars["key"]
|
key := vars["key"]
|
||||||
var p *db.Paste
|
var p *boltdb.Paste
|
||||||
var fu *db.FileUpload
|
var fu *boltdb.FileUpload
|
||||||
err := rl.db.Bolt.View(func(tx *bolt.Tx) error {
|
err := rl.db.Bolt.View(func(tx *bolt.Tx) error {
|
||||||
var err error
|
var err error
|
||||||
p, err = db.GetPaste(tx, key)
|
p, err = boltdb.GetPaste(tx, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if p != nil && p.Type == db.PasteTypeFileUpload {
|
if p != nil && p.Type == boltdb.PasteTypeFileUpload {
|
||||||
var id uuid.UUID
|
var id uuid.UUID
|
||||||
copy(id[:], p.Content)
|
copy(id[:], p.Content)
|
||||||
fu, err = db.GetFileUpload(tx, id)
|
fu, err = boltdb.GetFileUpload(tx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -115,7 +115,7 @@ func (rl *rushlink) viewPasteHandlerFlags(w http.ResponseWriter, r *http.Request
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
status := db.ErrHTTPStatusCode(err)
|
status := boltdb.ErrHTTPStatusCode(err)
|
||||||
if status == http.StatusInternalServerError {
|
if status == http.StatusInternalServerError {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -126,22 +126,22 @@ func (rl *rushlink) viewPasteHandlerFlags(w http.ResponseWriter, r *http.Request
|
|||||||
rl.viewPasteHandlerInner(w, r, flags, p, fu)
|
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 {
|
if flags&viewShowMeta != 0 {
|
||||||
rl.viewPasteHandlerInnerMeta(w, r, p, fu)
|
rl.viewPasteHandlerInnerMeta(w, r, p, fu)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch p.State {
|
switch p.State {
|
||||||
case db.PasteStatePresent:
|
case boltdb.PasteStatePresent:
|
||||||
switch p.Type {
|
switch p.Type {
|
||||||
case db.PasteTypeFileUpload:
|
case boltdb.PasteTypeFileUpload:
|
||||||
if fu == nil {
|
if fu == nil {
|
||||||
panic(fmt.Sprintf("file for id %v does not exist in database\n", string(p.Content)))
|
panic(fmt.Sprintf("file for id %v does not exist in database\n", string(p.Content)))
|
||||||
}
|
}
|
||||||
rl.viewFileUploadHandler(w, r, fu)
|
rl.viewFileUploadHandler(w, r, fu)
|
||||||
return
|
return
|
||||||
case db.PasteTypeRedirect:
|
case boltdb.PasteTypeRedirect:
|
||||||
if flags&viewNoRedirect != 0 {
|
if flags&viewNoRedirect != 0 {
|
||||||
w.Write([]byte(p.RedirectURL().String()))
|
w.Write([]byte(p.RedirectURL().String()))
|
||||||
return
|
return
|
||||||
@ -152,7 +152,7 @@ func (rl *rushlink) viewPasteHandlerInner(w http.ResponseWriter, r *http.Request
|
|||||||
panic("paste type unsupported")
|
panic("paste type unsupported")
|
||||||
}
|
}
|
||||||
|
|
||||||
case db.PasteStateDeleted:
|
case boltdb.PasteStateDeleted:
|
||||||
rl.renderError(w, r, http.StatusGone, "paste has been deleted\n")
|
rl.renderError(w, r, http.StatusGone, "paste has been deleted\n")
|
||||||
return
|
return
|
||||||
default:
|
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)
|
filePath := fu.Path(rl.fs)
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(filePath)
|
||||||
if err != nil {
|
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)
|
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
|
var cd canDelete
|
||||||
deleteToken := getDeleteTokenFromRequest(r)
|
deleteToken := getDeleteTokenFromRequest(r)
|
||||||
if deleteToken != "" {
|
if deleteToken != "" {
|
||||||
@ -213,7 +213,7 @@ func (rl *rushlink) viewPasteHandlerInnerMeta(w http.ResponseWriter, r *http.Req
|
|||||||
"CanDeleteBool": cd.Bool(),
|
"CanDeleteBool": cd.Bool(),
|
||||||
}
|
}
|
||||||
var status int
|
var status int
|
||||||
if p.State == db.PasteStateDeleted {
|
if p.State == boltdb.PasteStateDeleted {
|
||||||
status = http.StatusGone
|
status = http.StatusGone
|
||||||
} else {
|
} else {
|
||||||
status = http.StatusOK
|
status = http.StatusOK
|
||||||
@ -222,7 +222,7 @@ func (rl *rushlink) viewPasteHandlerInnerMeta(w http.ResponseWriter, r *http.Req
|
|||||||
return
|
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
|
var fileExt string
|
||||||
if fu != nil {
|
if fu != nil {
|
||||||
fileExt = fu.Ext()
|
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) {
|
func (rl *rushlink) newFileUploadPasteHandler(w http.ResponseWriter, r *http.Request, file multipart.File, header multipart.FileHeader) {
|
||||||
var fu *db.FileUpload
|
var fu *boltdb.FileUpload
|
||||||
var paste *db.Paste
|
var paste *boltdb.Paste
|
||||||
if err := rl.db.Bolt.Update(func(tx *bolt.Tx) error {
|
if err := rl.db.Bolt.Update(func(tx *bolt.Tx) error {
|
||||||
var err 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 {
|
if err != nil {
|
||||||
panic(errors.Wrap(err, "creating fileUpload"))
|
panic(errors.Wrap(err, "creating fileUpload"))
|
||||||
}
|
}
|
||||||
@ -316,7 +316,7 @@ func (rl *rushlink) newRedirectPasteHandler(w http.ResponseWriter, r *http.Reque
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var paste *db.Paste
|
var paste *boltdb.Paste
|
||||||
if err := rl.db.Bolt.Update(func(tx *bolt.Tx) error {
|
if err := rl.db.Bolt.Update(func(tx *bolt.Tx) error {
|
||||||
var err error
|
var err error
|
||||||
paste, err = shortenURL(tx, userURL)
|
paste, err = shortenURL(tx, userURL)
|
||||||
@ -339,15 +339,15 @@ func (rl *rushlink) deletePasteHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var errorCode int
|
var errorCode int
|
||||||
var paste *db.Paste
|
var paste *boltdb.Paste
|
||||||
if err := rl.db.Bolt.Update(func(tx *bolt.Tx) error {
|
if err := rl.db.Bolt.Update(func(tx *bolt.Tx) error {
|
||||||
var err error
|
var err error
|
||||||
paste, err = db.GetPaste(tx, key)
|
paste, err = boltdb.GetPaste(tx, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorCode = http.StatusNotFound
|
errorCode = http.StatusNotFound
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if paste.State == db.PasteStateDeleted {
|
if paste.State == boltdb.PasteStateDeleted {
|
||||||
errorCode = http.StatusGone
|
errorCode = http.StatusGone
|
||||||
return errors.New("already deleted")
|
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
|
// Returns the new paste key if the fileUpload was successfully added to the
|
||||||
// database
|
// database
|
||||||
func shortenFileUploadID(tx *bolt.Tx, id uuid.UUID) (*db.Paste, error) {
|
func shortenFileUploadID(tx *bolt.Tx, id uuid.UUID) (*boltdb.Paste, error) {
|
||||||
return shorten(tx, db.PasteTypeFileUpload, id[:])
|
return shorten(tx, boltdb.PasteTypeFileUpload, id[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a new URL to the database
|
// Add a new URL to the database
|
||||||
//
|
//
|
||||||
// Returns the new paste key if the url was successfully shortened
|
// Returns the new paste key if the url was successfully shortened
|
||||||
func shortenURL(tx *bolt.Tx, userURL *url.URL) (*db.Paste, error) {
|
func shortenURL(tx *bolt.Tx, userURL *url.URL) (*boltdb.Paste, error) {
|
||||||
return shorten(tx, db.PasteTypeRedirect, []byte(userURL.String()))
|
return shorten(tx, boltdb.PasteTypeRedirect, []byte(userURL.String()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a paste (of any kind) to the database with arbitrary content.
|
// 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
|
// Generate the paste key
|
||||||
var keyEntropy int
|
var keyEntropy int
|
||||||
if ty == db.PasteTypeFileUpload || ty == db.PasteTypePaste {
|
if ty == boltdb.PasteTypeFileUpload || ty == boltdb.PasteTypePaste {
|
||||||
keyEntropy = highOnlineEntropy
|
keyEntropy = highOnlineEntropy
|
||||||
}
|
}
|
||||||
pasteKey, err := db.GeneratePasteKey(tx, keyEntropy)
|
pasteKey, err := boltdb.GeneratePasteKey(tx, keyEntropy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "generating paste key")
|
return nil, errors.Wrap(err, "generating paste key")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also generate a deleteToken
|
// Also generate a deleteToken
|
||||||
deleteToken, err := db.GenerateDeleteToken()
|
deleteToken, err := boltdb.GenerateDeleteToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "generating delete token")
|
return nil, errors.Wrap(err, "generating delete token")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the new key
|
// Store the new key
|
||||||
p := db.Paste{
|
p := boltdb.Paste{
|
||||||
Type: ty,
|
Type: ty,
|
||||||
State: db.PasteStatePresent,
|
State: boltdb.PasteStatePresent,
|
||||||
Content: content,
|
Content: content,
|
||||||
Key: pasteKey,
|
Key: pasteKey,
|
||||||
DeleteToken: deleteToken,
|
DeleteToken: deleteToken,
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
|
"gitea.hashru.nl/dsprenkels/rushlink/internal/boltdb"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"go.etcd.io/bbolt"
|
"go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
@ -31,12 +31,12 @@ func createTemporaryRouter(t *testing.T) (*mux.Router, *rushlink) {
|
|||||||
os.RemoveAll(tempDir)
|
os.RemoveAll(tempDir)
|
||||||
})
|
})
|
||||||
|
|
||||||
fileStore, err := db.OpenFileStore(filepath.Join(tempDir, "filestore"))
|
fileStore, err := boltdb.OpenFileStore(filepath.Join(tempDir, "filestore"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("opening temporary filestore: %s\n", err)
|
t.Fatalf("opening temporary filestore: %s\n", err)
|
||||||
}
|
}
|
||||||
databasePath := filepath.Join(tempDir, "rushlink.db")
|
databasePath := filepath.Join(tempDir, "rushlink.db")
|
||||||
database, err := db.OpenDB(databasePath, fileStore)
|
database, err := boltdb.OpenDB(databasePath, fileStore)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("opening temporary database: %s\n", err)
|
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.
|
// Check that any attempt to do directory traversal has failed.
|
||||||
rl.db.Bolt.View(func(tx *bbolt.Tx) error {
|
rl.db.Bolt.View(func(tx *bbolt.Tx) error {
|
||||||
fus, err := db.AllFileUploads(tx)
|
fus, err := boltdb.AllFileUploads(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package db
|
package boltdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
@ -1,4 +1,4 @@
|
|||||||
package db
|
package boltdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
@ -1,4 +1,4 @@
|
|||||||
package db
|
package boltdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
@ -1,4 +1,4 @@
|
|||||||
package db
|
package boltdb
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
@ -5,8 +5,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
|
"gitea.hashru.nl/dsprenkels/rushlink/internal/boltdb"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"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.",
|
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
|
||||||
}, []string{"code", "method"})
|
}, []string{"code", "method"})
|
||||||
|
|
||||||
func metricURLsTotal(database *db.Database) float64 {
|
func metricURLsTotal(database *boltdb.Database) float64 {
|
||||||
var metric float64
|
var metric float64
|
||||||
if err := database.Bolt.View(func(tx *bolt.Tx) error {
|
if err := database.Bolt.View(func(tx *bolt.Tx) error {
|
||||||
bucket := tx.Bucket([]byte("pastes"))
|
bucket := tx.Bucket([]byte("pastes"))
|
||||||
@ -40,7 +39,7 @@ func metricURLsTotal(database *db.Database) float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StartMetricsServer starts sering Prometheus metrics exports on addr
|
// 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(metricRequestsTotalCounter)
|
||||||
|
|
||||||
prometheus.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
prometheus.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.hashru.nl/dsprenkels/rushlink/internal/db"
|
"gitea.hashru.nl/dsprenkels/rushlink/internal/boltdb"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
@ -19,8 +19,8 @@ const urlKeyExpr = "{key:[A-Za-z0-9-_]{4,}}"
|
|||||||
const urlKeyWithExtExpr = urlKeyExpr + "{ext:\\.[A-Za-z0-9-_]+}"
|
const urlKeyWithExtExpr = urlKeyExpr + "{ext:\\.[A-Za-z0-9-_]+}"
|
||||||
|
|
||||||
type rushlink struct {
|
type rushlink struct {
|
||||||
db *db.Database
|
db *boltdb.Database
|
||||||
fs *db.FileStore
|
fs *boltdb.FileStore
|
||||||
rootURL *url.URL
|
rootURL *url.URL
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,7 +111,7 @@ func InitMainRouter(r *mux.Router, rl *rushlink) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StartMainServer starts the main http server listening on addr.
|
// 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
|
var rootURL *url.URL
|
||||||
if rawRootURL != "" {
|
if rawRootURL != "" {
|
||||||
var err error
|
var err error
|
||||||
|
Loading…
Reference in New Issue
Block a user