Skip to content

Commit 1db4bc0

Browse files
committed
add functionality to manage custom db
Signed-off-by: Alwin Doss <alwindoss84@gmail.com>
1 parent 9395d67 commit 1db4bc0

File tree

4 files changed

+49
-15
lines changed

4 files changed

+49
-15
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ Follow the instructions to install the app and enjoy exploring the boltdb.
1717
#### To run the installed version of **Astra** assuming your $GOBIN is added to the $PATH environment variable
1818

1919
Run `astra` from anywhere in the terminal
20+
21+
#### If you want to view and manage your boltdb using **Astra** then using the flags as shown below
22+
23+
Run `astra -loc=/tmp/astra -name=dummy.db`
24+
25+
Run `astra -h` to see the options

cmd/astra/main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
package main
22

33
import (
4+
"flag"
5+
46
"github.com/alwindoss/astra"
57
"github.com/alwindoss/astra/internal/server"
68
)
79

10+
var (
11+
dbName string
12+
dbPath string
13+
)
14+
15+
func init() {
16+
flag.StringVar(&dbName, "name", "astra.db", "-name=dbname.db (default is astra.db)")
17+
flag.StringVar(&dbPath, "loc", "", "-loc=/opt/astra/db/ (default is home directory)")
18+
}
19+
820
func main() {
9-
cfg := astra.DefaultConfig()
21+
flag.Parse()
22+
cfg := astra.DefaultConfig(dbPath, dbName)
1023

1124
server.Run(cfg)
1225
}

config.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,42 @@ import (
1111

1212
type Config struct {
1313
Location string
14+
DBName string
1415
Port string
1516
InProduction bool
1617
TemplateCache map[string]*template.Template
1718
}
1819

19-
func DefaultConfig() *Config {
20+
func DefaultConfig(dbPath, dbName string) *Config {
2021
port := os.Getenv("PORT")
2122
if port == "" {
2223
port = "8080"
2324
}
24-
homeDir, err := homedir.Dir()
25-
if err != nil {
26-
log.Printf("unable to find the home directory: %v", err)
27-
os.Exit(1)
28-
}
29-
cfgLoc := filepath.Join(homeDir, ".astra")
30-
if err = os.MkdirAll(cfgLoc, os.ModeDir); err != nil {
31-
log.Printf("unable to create the config folder at the location %s: %v", cfgLoc, err)
32-
os.Exit(1)
33-
}
3425
cfg := &Config{
35-
Location: cfgLoc,
36-
Port: port,
26+
DBName: dbName,
27+
Port: port,
28+
}
29+
if dbPath == "" {
30+
homeDir, err := homedir.Dir()
31+
if err != nil {
32+
log.Printf("unable to find the home directory: %v", err)
33+
os.Exit(1)
34+
}
35+
cfgLoc := filepath.Join(homeDir, ".astra")
36+
if err = os.MkdirAll(cfgLoc, 0750); err != nil {
37+
log.Printf("unable to create the config folder at the location %s: %v", cfgLoc, err)
38+
os.Exit(1)
39+
}
40+
cfg.Location = cfgLoc
41+
42+
} else {
43+
if err := os.MkdirAll(dbPath, 0750); err != nil {
44+
log.Printf("unable to create the config folder at the location %s: %v", dbPath, err)
45+
os.Exit(1)
46+
}
47+
cfg.Location = dbPath
3748
}
49+
log.Printf("Created and set the config location %s", cfg.Location)
3850
cfg.InProduction = false
3951

4052
return cfg

internal/server/server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55
"net/http"
66
"os"
7+
"path/filepath"
78
"time"
89

910
"github.com/alexedwards/scs/v2"
@@ -30,11 +31,13 @@ func Run(cfg *astra.Config) {
3031
session.Cookie.SameSite = http.SameSiteLaxMode
3132
session.Cookie.Secure = cfg.InProduction
3233

33-
db, err := bbolt.Open(cfg.Location+"astra.db", 0600, nil)
34+
dbLoc := filepath.Join(cfg.Location, cfg.DBName)
35+
db, err := bbolt.Open(dbLoc, 0600, nil)
3436
if err != nil {
3537
log.Printf("unable to open the astra db: %v", err)
3638
os.Exit(1)
3739
}
40+
log.Printf("Opened DB at location %s", dbLoc)
3841
defer db.Close()
3942

4043
repo := dbase.NewBoltDBRepository(db)

0 commit comments

Comments
 (0)