forked from electricdusk/rushlink
Merge branch 'structure_rework' of dsprenkels/rushlink into master
This commit is contained in:
commit
e26e37c97d
@ -4,22 +4,20 @@ import (
|
||||
"flag"
|
||||
"log"
|
||||
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/db"
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/handlers"
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/metrics"
|
||||
"gitea.hashru.nl/dsprenkels/rushlink"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var databasePath, fileStorePath string
|
||||
var databasePath string
|
||||
|
||||
flag.StringVar(&databasePath, "database", "", "Location of the database file")
|
||||
flag.Parse()
|
||||
|
||||
if err := db.Open(databasePath); err != nil {
|
||||
if err := rushlink.Open(databasePath); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
defer db.Close()
|
||||
defer rushlink.Close()
|
||||
|
||||
go metrics.StartMetricsServer()
|
||||
handlers.StartMainServer()
|
||||
go rushlink.StartMetricsServer()
|
||||
rushlink.StartMainServer()
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package db
|
||||
package rushlink
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -7,8 +7,6 @@ import (
|
||||
|
||||
"github.com/pkg/errors"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/gobmarsh"
|
||||
)
|
||||
|
||||
var DB *bolt.DB
|
||||
@ -97,7 +95,7 @@ func dbVersion(tx *bolt.Tx) (int, error) {
|
||||
|
||||
// Version was already stored
|
||||
var dbVersion int
|
||||
if err := gobmarsh.Unmarshal(dbVersionBytes, &dbVersion); err != nil {
|
||||
if err := Unmarshal(dbVersionBytes, &dbVersion); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if dbVersion == 0 {
|
||||
@ -116,7 +114,7 @@ func setDBVersion(tx *bolt.Tx, version int) error {
|
||||
return err
|
||||
}
|
||||
|
||||
versionBytes, err := gobmarsh.Marshal(version)
|
||||
versionBytes, err := Marshal(version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package rushlink
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
@ -12,8 +12,6 @@ import (
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/pkg/errors"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/db"
|
||||
)
|
||||
|
||||
type viewPaste uint
|
||||
@ -79,7 +77,7 @@ func viewPasteHandlerInner(w http.ResponseWriter, r *http.Request, flags viewPas
|
||||
vars := mux.Vars(r)
|
||||
key := vars["key"]
|
||||
var p *paste
|
||||
if err := db.DB.View(func(tx *bolt.Tx) error {
|
||||
if err := DB.View(func(tx *bolt.Tx) error {
|
||||
var err error
|
||||
p, err = getPaste(tx, key)
|
||||
return err
|
||||
@ -176,7 +174,7 @@ func newRedirectPasteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var paste *paste
|
||||
if err := db.DB.Update(func(tx *bolt.Tx) error {
|
||||
if err := DB.Update(func(tx *bolt.Tx) error {
|
||||
// Generate a new delete token for this paste
|
||||
var err error
|
||||
paste, err = shortenURL(tx, userURL)
|
||||
@ -200,7 +198,7 @@ func deletePasteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var errorCode int
|
||||
if err := db.DB.Update(func(tx *bolt.Tx) error {
|
||||
if err := DB.Update(func(tx *bolt.Tx) error {
|
||||
p, err := getPaste(tx, key)
|
||||
if err != nil {
|
||||
errorCode = http.StatusNotFound
|
@ -1,4 +1,4 @@
|
||||
package gobmarsh
|
||||
package rushlink
|
||||
|
||||
// Easier marshalling to and from gob encoding
|
||||
|
@ -1,4 +1,4 @@
|
||||
package metrics
|
||||
package rushlink
|
||||
|
||||
import (
|
||||
"log"
|
||||
@ -11,8 +11,6 @@ import (
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/db"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -28,7 +26,7 @@ func StartMetricsServer() {
|
||||
Help: "The current amount of pastes in the database.",
|
||||
}, func() float64 {
|
||||
var metric float64
|
||||
if err := db.DB.View(func(tx *bolt.Tx) error {
|
||||
if err := DB.View(func(tx *bolt.Tx) error {
|
||||
bucket := tx.Bucket([]byte("pastes"))
|
||||
if bucket == nil {
|
||||
return errors.New("bucket 'pastes' could not be found")
|
@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package rushlink
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
@ -6,8 +6,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/db"
|
||||
"gitea.hashru.nl/dsprenkels/rushlink/gobmarsh"
|
||||
"github.com/pkg/errors"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
)
|
||||
@ -38,26 +36,26 @@ const (
|
||||
|
||||
// Retrieve a paste from the database
|
||||
func getPaste(tx *bolt.Tx, key string) (*paste, error) {
|
||||
pastesBucket := tx.Bucket([]byte(db.BUCKET_PASTES))
|
||||
pastesBucket := tx.Bucket([]byte(BUCKET_PASTES))
|
||||
if pastesBucket == nil {
|
||||
return nil, errors.Errorf("bucket %v does not exist", db.BUCKET_PASTES)
|
||||
return nil, errors.Errorf("bucket %v does not exist", BUCKET_PASTES)
|
||||
}
|
||||
storedBytes := pastesBucket.Get([]byte(key))
|
||||
if storedBytes == nil {
|
||||
return nil, nil
|
||||
}
|
||||
p := &paste{}
|
||||
err := gobmarsh.Unmarshal(storedBytes, p)
|
||||
err := Unmarshal(storedBytes, p)
|
||||
return p, err
|
||||
}
|
||||
|
||||
func (p *paste) save(tx *bolt.Tx) error {
|
||||
bucket := tx.Bucket([]byte(db.BUCKET_PASTES))
|
||||
bucket := tx.Bucket([]byte(BUCKET_PASTES))
|
||||
if bucket == nil {
|
||||
return errors.Errorf("bucket %v does not exist", db.BUCKET_PASTES)
|
||||
return errors.Errorf("bucket %v does not exist", BUCKET_PASTES)
|
||||
}
|
||||
|
||||
buf, err := gobmarsh.Marshal(p)
|
||||
buf, err := Marshal(p)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "encoding for database failed")
|
||||
}
|
||||
@ -79,9 +77,9 @@ func (p paste) delete(tx *bolt.Tx) error {
|
||||
// Generate a key until it is not in the database, this occurs in O(log N),
|
||||
// where N is the amount of keys stored in the url-shorten database.
|
||||
func generatePasteKey(tx *bolt.Tx) (string, error) {
|
||||
pastesBucket := tx.Bucket([]byte(db.BUCKET_PASTES))
|
||||
pastesBucket := tx.Bucket([]byte(BUCKET_PASTES))
|
||||
if pastesBucket == nil {
|
||||
return "", errors.Errorf("bucket %v does not exist", db.BUCKET_PASTES)
|
||||
return "", errors.Errorf("bucket %v does not exist", BUCKET_PASTES)
|
||||
}
|
||||
|
||||
epoch := 0
|
@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package rushlink
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,6 +1,6 @@
|
||||
package handlers
|
||||
package rushlink
|
||||
|
||||
//go:generate go-bindata -pkg $GOPACKAGE -prefix ../assets ../assets/...
|
||||
//go:generate go-bindata -pkg $GOPACKAGE -prefix ./assets ./assets/...
|
||||
|
||||
import (
|
||||
"bytes"
|
Loading…
Reference in New Issue
Block a user