From 1b3ba9443ad62b12b11ea7da14ff9eee2226de6c Mon Sep 17 00:00:00 2001 From: Gerdriaan Mulder Date: Sat, 9 Nov 2019 15:50:12 +0100 Subject: [PATCH] Move several packages to root package --- cmd/rushlink/main.go | 14 ++++++-------- db/db.go => db.go | 8 +++----- handlers/handlers.go => handlers.go | 10 ++++------ gobmarsh/marshal.go => marshal.go | 2 +- metrics/metrics.go => metrics.go | 6 ++---- handlers/paste.go => paste.go | 20 +++++++++----------- handlers/router.go => router.go | 2 +- handlers/views.go => views.go | 4 ++-- 8 files changed, 28 insertions(+), 38 deletions(-) rename db/db.go => db.go (93%) rename handlers/handlers.go => handlers.go (96%) rename gobmarsh/marshal.go => marshal.go (95%) rename metrics/metrics.go => metrics.go (91%) rename handlers/paste.go => paste.go (84%) rename handlers/router.go => router.go (98%) rename handlers/views.go => views.go (98%) diff --git a/cmd/rushlink/main.go b/cmd/rushlink/main.go index 2af0166..5b9803d 100644 --- a/cmd/rushlink/main.go +++ b/cmd/rushlink/main.go @@ -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() } diff --git a/db/db.go b/db.go similarity index 93% rename from db/db.go rename to db.go index 2cf4ca1..b749011 100644 --- a/db/db.go +++ b/db.go @@ -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 } diff --git a/handlers/handlers.go b/handlers.go similarity index 96% rename from handlers/handlers.go rename to handlers.go index 3c17e86..dfb9a17 100644 --- a/handlers/handlers.go +++ b/handlers.go @@ -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 diff --git a/gobmarsh/marshal.go b/marshal.go similarity index 95% rename from gobmarsh/marshal.go rename to marshal.go index 329fa4e..838e222 100644 --- a/gobmarsh/marshal.go +++ b/marshal.go @@ -1,4 +1,4 @@ -package gobmarsh +package rushlink // Easier marshalling to and from gob encoding diff --git a/metrics/metrics.go b/metrics.go similarity index 91% rename from metrics/metrics.go rename to metrics.go index 2b9ab8b..47b62f4 100644 --- a/metrics/metrics.go +++ b/metrics.go @@ -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") diff --git a/handlers/paste.go b/paste.go similarity index 84% rename from handlers/paste.go rename to paste.go index e54ba59..e885b76 100644 --- a/handlers/paste.go +++ b/paste.go @@ -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 diff --git a/handlers/router.go b/router.go similarity index 98% rename from handlers/router.go rename to router.go index 1749c26..be723af 100644 --- a/handlers/router.go +++ b/router.go @@ -1,4 +1,4 @@ -package handlers +package rushlink import ( "fmt" diff --git a/handlers/views.go b/views.go similarity index 98% rename from handlers/views.go rename to views.go index 2328f6d..ad35ae2 100644 --- a/handlers/views.go +++ b/views.go @@ -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"