go: allow choosing db type, setting connstr
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
f1bb52c58f
commit
0a34bca1c1
@ -16,6 +16,8 @@ type Settings struct {
|
||||
assetsPath string
|
||||
templatesPath string
|
||||
version string
|
||||
dbConnstring string
|
||||
dbType string
|
||||
dbIsSetUp bool
|
||||
}
|
||||
|
||||
@ -125,6 +127,16 @@ func (s *Settings) DbIsSetUp() bool {
|
||||
return s.dbIsSetUp
|
||||
}
|
||||
|
||||
// DbConnstring returns the dbConnString.
|
||||
func (s *Settings) DbConnstring() string {
|
||||
return s.dbConnstring
|
||||
}
|
||||
|
||||
// DbType returns the dbType.
|
||||
func (s *Settings) DbType() string {
|
||||
return s.dbType
|
||||
}
|
||||
|
||||
// SetHost sets the host.
|
||||
func (s *Settings) SetHost(host string) {
|
||||
s.host = host
|
||||
@ -175,6 +187,16 @@ func (s *Settings) SetVersion(version string) {
|
||||
s.version = version
|
||||
}
|
||||
|
||||
// SetDbConnstring sets the dbConnString.
|
||||
func (s *Settings) SetDbConnstring(connstring string) {
|
||||
s.dbConnstring = connstring
|
||||
}
|
||||
|
||||
// SetDbType sets the dbType.
|
||||
func (s *Settings) SetDbType(dbType string) {
|
||||
s.dbType = dbType
|
||||
}
|
||||
|
||||
// SetDbIsSetUp sets the dbIsSetUp.
|
||||
func (s *Settings) SetDbIsSetUp(is bool) {
|
||||
s.dbIsSetUp = is
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
{
|
||||
env.CGO_ENABLED = 0;
|
||||
env.PCMT_DBTYPE = "postgres";
|
||||
env.PCMT_CONNSTRING = "host=127.0.0.1 sslmode=disable port=5432 user=postgres dbname=postgres password=postgres";
|
||||
|
||||
packages = with pkgs; [
|
||||
git
|
||||
|
1
go.mod
1
go.mod
@ -8,6 +8,7 @@ require (
|
||||
github.com/gorilla/sessions v1.2.1
|
||||
github.com/labstack/echo-contrib v0.14.1
|
||||
github.com/labstack/echo/v4 v4.10.2
|
||||
github.com/lib/pq v1.10.7
|
||||
github.com/microcosm-cc/bluemonday v1.0.23
|
||||
github.com/philandstuff/dhall-golang/v6 v6.0.2
|
||||
github.com/xiaoqidun/entps v0.0.0-20230328150929-94b1b92d8c03
|
||||
|
2
go.sum
2
go.sum
@ -62,6 +62,8 @@ github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8
|
||||
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
|
||||
github.com/leanovate/gopter v0.2.5-0.20190402064358-634a59d12406 h1:+OUpk+IVvmKU0jivOVFGtOzA6U5AWFs8HE4DRzWLOUE=
|
||||
github.com/leanovate/gopter v0.2.5-0.20190402064358-634a59d12406/go.mod h1:gNcbPWNEWRe4lm+bycKqxUYoH5uoVje5SkOJ3uoLer8=
|
||||
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
|
||||
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
|
30
run.go
30
run.go
@ -11,6 +11,8 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
// pure go postgres driver.
|
||||
_ "github.com/lib/pq"
|
||||
// ent pure go sqlite3 driver instead of "github.com/mattn/go-sqlite3".
|
||||
_ "github.com/xiaoqidun/entps"
|
||||
|
||||
@ -89,13 +91,27 @@ func run() error {
|
||||
return err
|
||||
}
|
||||
|
||||
// for "github.com/xiaoqidun/entps".
|
||||
connstr := "file:ent?mode=memory&cache=shared&_fk=1"
|
||||
setting := settings.New()
|
||||
|
||||
setting.Consolidate(
|
||||
conf, host, port, devel, version,
|
||||
)
|
||||
|
||||
// expected connstring form for "github.com/xiaoqidun/entps":
|
||||
// "file:ent?mode=memory&cache=shared&_fk=1"
|
||||
// and for the postgres driver "github.com/lib/pq":
|
||||
// "host=127.0.0.1 sslmode=disable port=5432 user=postgres dbname=postgres password=postgres".
|
||||
|
||||
connstr := os.Getenv("PCMT_CONNSTRING")
|
||||
dbtype := os.Getenv("PCMT_DBTYPE")
|
||||
|
||||
setting.SetDbConnstring(connstr)
|
||||
// type can be one of "postgres" or "sqlite3".
|
||||
setting.SetDbType(dbtype)
|
||||
|
||||
log.Infof("connecting to db at '%s'", connstr)
|
||||
|
||||
// TODO: conditionally set db type.
|
||||
db, err := ent.Open("sqlite3", connstr)
|
||||
db, err := ent.Open(setting.DbType(), setting.DbConnstring())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open a connection to sqlite: %v", err)
|
||||
}
|
||||
@ -116,14 +132,8 @@ func run() error {
|
||||
return fmt.Errorf("failed creating schema resources: %v", err)
|
||||
}
|
||||
|
||||
setting := settings.New()
|
||||
|
||||
setting.SetDbIsSetUp(true)
|
||||
|
||||
setting.Consolidate(
|
||||
conf, host, port, devel, version,
|
||||
)
|
||||
|
||||
a := &app.App{}
|
||||
|
||||
err = a.Init(setting, log, db)
|
||||
|
Loading…
Reference in New Issue
Block a user