Skip to content

Commit

Permalink
Normalise paths and add cgo_enabled for sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 3, 2024
1 parent 20ee952 commit cba1634
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch actor as tester",
"name": "Launch actor as bahner",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/actor/",
"console": "integratedTerminal",
"args": [
"--nick",
"tester"
"bahner"
],
"env": {
"GOLOG_FILE": "debug.log",
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ MODULE_NAME = github.com/bahner/go-ma-actor
export VERSION = "v0.0.2"

GO ?= go
# This is required for sqlite3 cross-compilation
export CGO_ENABLED=1
BUILDFLAGS ?= -ldflags="-s -w"
TAR ?= tar cJf
XZ ?= xz -zf
PREFIX ?= /usr/local
KEYSET = $(NAME)-create-keyset
Expand Down
17 changes: 13 additions & 4 deletions cmd/actor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/config/db"
"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma-actor/mode/relay"
"github.com/bahner/go-ma-actor/p2p"
Expand All @@ -16,6 +17,11 @@ import (

func main() {

var (
err error
p *p2p.P2P
)

// Always parse the flags first
pflag.Parse()

Expand All @@ -27,14 +33,17 @@ func main() {
config.Init(mode)
fmt.Printf("Starting in %s mode.\n", mode)

// DB
fmt.Print("Initialising DB ...")
_, err = db.Init()
if err != nil {
log.Fatalf("failed to initialize db: %v", err)
}
fmt.Println("done.")
// P2P

// Configure and start P2P
fmt.Print("Initialising libp2p...")
var (
p *p2p.P2P
err error
)

if config.RelayMode() {
fmt.Print("Relay mode enabled.")
Expand Down
8 changes: 6 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
config string = ""
configHome string = xdg.ConfigHome + "/" + ma.NAME + "/"
dataHome string = xdg.DataHome + "/" + ma.NAME + "/"
defaultConfigFile string = configHome + defaultActor + ".yaml"
defaultConfigFile string = NormalisePath(configHome + defaultActor + ".yaml")
)

func init() {
Expand Down Expand Up @@ -165,7 +165,7 @@ func configFile() string {
filename = configHome + configName() + ".yaml"
}

return filepath.FromSlash(filepath.Clean(filename))
return filepath.Clean(filename)

}

Expand Down Expand Up @@ -245,3 +245,7 @@ func forceFlag() bool {

return forceFlag
}

func NormalisePath(path string) string {
return filepath.ToSlash(filepath.Clean(path))
}
6 changes: 1 addition & 5 deletions config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@ const (
)

var (
defaultDbFile = dataHome + defaultDBFilename
DefaultDbFile = NormalisePath(dataHome + defaultDBFilename)
)

func DefaultDBFile() string {
return defaultDbFile
}
29 changes: 20 additions & 9 deletions config/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package db
import (
"database/sql"
"fmt"
"path/filepath"
"sync"

"github.com/bahner/go-ma-actor/config"
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
Expand All @@ -21,7 +21,7 @@ const (
var (
once sync.Once
db *sql.DB
defaultDbFile = config.DefaultDBFile()
defaultDbFile = config.DefaultDbFile
)

func init() {
Expand All @@ -36,13 +36,18 @@ func init() {
}

// Initiates the database connection and creates the tables if they do not exist
func Get() (*sql.DB, error) {
func Init() (*sql.DB, error) {

var onceErr error

once.Do(func() {
var err error
db, err = sql.Open("sqlite3", dbfile())
f, err := dbfile()
if err != nil {
onceErr = fmt.Errorf("error expanding db file path: %s", err)
return
}
db, err = sql.Open("sqlite3", f)
if err != nil {
onceErr = fmt.Errorf("error opening database: %s", err)
return
Expand All @@ -64,6 +69,8 @@ func Get() (*sql.DB, error) {
db.SetMaxOpenConns(dbMaxConnections)
db.Exec("PRAGMA busy_timeout = " + fmt.Sprintf("%d", timeout()))

log.Infof("Connected to database: %s", f)

})

if onceErr != nil {
Expand All @@ -72,18 +79,22 @@ func Get() (*sql.DB, error) {

return db, nil
}
func Get() (*sql.DB, error) {

return Init()
}

// Returns expanded path to the db-file file
// If the expansion fails it returns an empty string
func dbfile() string {
func dbfile() (string, error) {

path := viper.GetString("db.file")
path, err := homedir.Expand(path)
p := viper.GetString("db.file")
p, err := homedir.Expand(p)
if err != nil {
return ""
return "", err
}

return filepath.FromSlash(filepath.Clean(path))
return config.NormalisePath(p), nil

}

Expand Down
2 changes: 1 addition & 1 deletion config/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func generateConfigFile(identity string, node string) {
"nick": nick,
},
"db": map[string]interface{}{
"file": defaultDbFile,
"file": DefaultDbFile,
},
"log": map[string]interface{}{
"level": defaultLogLevel,
Expand Down
4 changes: 2 additions & 2 deletions config/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
logFilePerm os.FileMode = 0640
)

var defaultLogfile string = dataHome + defaultActor + ".log"
var defaultLogfile string = NormalisePath(dataHome + defaultActor + ".log")

func init() {

Expand Down Expand Up @@ -78,5 +78,5 @@ func getLogFile() (string, error) {
if err != nil {
return "", err
}
return filepath.FromSlash(filepath.Clean(lf)), nil
return NormalisePath(lf), nil
}

0 comments on commit cba1634

Please sign in to comment.