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