Skip to content

Commit

Permalink
add functionality to manage custom db
Browse files Browse the repository at this point in the history
Signed-off-by: Alwin Doss <[email protected]>
  • Loading branch information
alwindoss committed Mar 5, 2022
1 parent 9395d67 commit 1db4bc0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ Follow the instructions to install the app and enjoy exploring the boltdb.
#### To run the installed version of **Astra** assuming your $GOBIN is added to the $PATH environment variable

Run `astra` from anywhere in the terminal

#### If you want to view and manage your boltdb using **Astra** then using the flags as shown below

Run `astra -loc=/tmp/astra -name=dummy.db`

Run `astra -h` to see the options
15 changes: 14 additions & 1 deletion cmd/astra/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package main

import (
"flag"

"github.com/alwindoss/astra"
"github.com/alwindoss/astra/internal/server"
)

var (
dbName string
dbPath string
)

func init() {
flag.StringVar(&dbName, "name", "astra.db", "-name=dbname.db (default is astra.db)")
flag.StringVar(&dbPath, "loc", "", "-loc=/opt/astra/db/ (default is home directory)")
}

func main() {
cfg := astra.DefaultConfig()
flag.Parse()
cfg := astra.DefaultConfig(dbPath, dbName)

server.Run(cfg)
}
38 changes: 25 additions & 13 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,42 @@ import (

type Config struct {
Location string
DBName string
Port string
InProduction bool
TemplateCache map[string]*template.Template
}

func DefaultConfig() *Config {
func DefaultConfig(dbPath, dbName string) *Config {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
homeDir, err := homedir.Dir()
if err != nil {
log.Printf("unable to find the home directory: %v", err)
os.Exit(1)
}
cfgLoc := filepath.Join(homeDir, ".astra")
if err = os.MkdirAll(cfgLoc, os.ModeDir); err != nil {
log.Printf("unable to create the config folder at the location %s: %v", cfgLoc, err)
os.Exit(1)
}
cfg := &Config{
Location: cfgLoc,
Port: port,
DBName: dbName,
Port: port,
}
if dbPath == "" {
homeDir, err := homedir.Dir()
if err != nil {
log.Printf("unable to find the home directory: %v", err)
os.Exit(1)
}
cfgLoc := filepath.Join(homeDir, ".astra")
if err = os.MkdirAll(cfgLoc, 0750); err != nil {
log.Printf("unable to create the config folder at the location %s: %v", cfgLoc, err)
os.Exit(1)
}
cfg.Location = cfgLoc

} else {
if err := os.MkdirAll(dbPath, 0750); err != nil {
log.Printf("unable to create the config folder at the location %s: %v", dbPath, err)
os.Exit(1)
}
cfg.Location = dbPath
}
log.Printf("Created and set the config location %s", cfg.Location)
cfg.InProduction = false

return cfg
Expand Down
5 changes: 4 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"time"

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

db, err := bbolt.Open(cfg.Location+"astra.db", 0600, nil)
dbLoc := filepath.Join(cfg.Location, cfg.DBName)
db, err := bbolt.Open(dbLoc, 0600, nil)
if err != nil {
log.Printf("unable to open the astra db: %v", err)
os.Exit(1)
}
log.Printf("Opened DB at location %s", dbLoc)
defer db.Close()

repo := dbase.NewBoltDBRepository(db)
Expand Down

0 comments on commit 1db4bc0

Please sign in to comment.