From 216c6d85b2cb1b8df0156cbafda9a4a29518f9d0 Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Sat, 15 May 2021 14:32:26 +0200 Subject: [PATCH] Added support for sqlite as database backend --- app.go | 21 +++++++++++++++++---- cmd/headscale/cli/utils.go | 2 ++ db.go | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app.go b/app.go index d7fd3f2e..917522ff 100644 --- a/app.go +++ b/app.go @@ -22,6 +22,8 @@ type Config struct { PrivateKeyPath string DerpMap *tailcfg.DERPMap + DBtype string + DBpath string DBhost string DBport int DBname string @@ -60,11 +62,22 @@ func NewHeadscale(cfg Config) (*Headscale, error) { return nil, err } pubKey := privKey.Public() + + var dbString string + switch cfg.DBtype { + case "postgres": + dbString = fmt.Sprintf("host=%s port=%d dbname=%s user=%s password=%s sslmode=disable", cfg.DBhost, + cfg.DBport, cfg.DBname, cfg.DBuser, cfg.DBpass) + case "sqlite3": + dbString = cfg.DBpath + default: + return nil, errors.New("Unsupported DB") + } + h := Headscale{ - cfg: cfg, - dbType: "postgres", - dbString: fmt.Sprintf("host=%s port=%d dbname=%s user=%s password=%s sslmode=disable", cfg.DBhost, - cfg.DBport, cfg.DBname, cfg.DBuser, cfg.DBpass), + cfg: cfg, + dbType: cfg.DBtype, + dbString: dbString, privateKey: privKey, publicKey: &pubKey, } diff --git a/cmd/headscale/cli/utils.go b/cmd/headscale/cli/utils.go index a9897fb4..823089d2 100644 --- a/cmd/headscale/cli/utils.go +++ b/cmd/headscale/cli/utils.go @@ -43,6 +43,8 @@ func getHeadscaleApp() (*headscale.Headscale, error) { PrivateKeyPath: absPath(viper.GetString("private_key_path")), DerpMap: derpMap, + DBtype: viper.GetString("db_type"), + DBpath: viper.GetString("db_path"), DBhost: viper.GetString("db_host"), DBport: viper.GetInt("db_port"), DBname: viper.GetString("db_name"), diff --git a/db.go b/db.go index 0723c529..5e08d9bd 100644 --- a/db.go +++ b/db.go @@ -5,6 +5,7 @@ import ( "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" // sql driver + _ "github.com/jinzhu/gorm/dialects/sqlite" // sql driver ) const dbVersion = "1"