From 73bfbdf58ddbada78c647002afac9fc90c9acc27 Mon Sep 17 00:00:00 2001 From: dosco <832235+dosco@users.noreply.github.com> Date: Sun, 18 Sep 2022 23:51:19 -0700 Subject: [PATCH] Feat: Allow to setup Graphjin with a pre-existing Postgres databse #382 New argument `-db-url` on the `new` sub-command allows you to point a new app to a local database. Eg. graphjin new testapp --db-url=postgres://user:pass@localhost/testapp-development --- internal/cmd/cmd_new.go | 58 ++++++++++++++++++++++++++-- internal/cmd/cmd_serv.go | 9 ++--- internal/cmd/tmpl/dev.yml | 14 ++++--- internal/cmd/tmpl/docker-compose.yml | 5 ++- internal/cmd/tmpl/prod.yml | 14 ++++--- 5 files changed, 81 insertions(+), 19 deletions(-) diff --git a/internal/cmd/cmd_new.go b/internal/cmd/cmd_new.go index b70ee7de..ebc0459d 100644 --- a/internal/cmd/cmd_new.go +++ b/internal/cmd/cmd_new.go @@ -3,6 +3,7 @@ package cmd import ( "bytes" "embed" + "net/url" "os" "path" "strings" @@ -13,6 +14,10 @@ import ( "golang.org/x/text/language" ) +var ( + dbURL string +) + func newCmd() *cobra.Command { c := &cobra.Command{ Use: "new ", @@ -20,6 +25,8 @@ func newCmd() *cobra.Command { Long: "Generate all the required files to start on a new GraphJin app", Run: cmdNew, } + + c.PersistentFlags().StringVar(&dbURL, "db-url", "", "URL of the database") return c } @@ -29,10 +36,55 @@ func cmdNew(cmd *cobra.Command, args []string) { os.Exit(1) } + dbType := "postgres" + dbHost := "db" + dbPort := "5432" + dbName := "" + dbUser := "postgres" + dbPass := "postgres" + + if dbURL != "" { + u, err := url.Parse(dbURL) + if err != nil { + log.Fatal(err) + } + dbType = u.Scheme + dbHost = u.Host + + if v := u.Port(); v != "" { + dbPort = v + } else if dbType == "mysql" { + dbPort = "3306" + } + + if v := u.User.Username(); v != "" { + dbUser = v + } else if dbType == "mysql" { + dbPort = "root" + } + + if v, ok := u.User.Password(); ok { + dbPass = v + } else if dbType == "mysql" { + dbPort = "" + } + + if v := u.Path; len(v) > 1 { + dbName = v[1:] + } + } + en := cases.Title(language.English) - tmpl := newTempl(map[string]string{ + tmpl := newTempl(map[string]interface{}{ "AppName": en.String(strings.Join(args, " ")), "AppNameSlug": strings.ToLower(strings.Join(args, "_")), + "CreateDB": (dbURL == ""), + "DBType": dbType, + "DBHost": dbHost, + "DBPort": dbPort, + "DBUser": dbUser, + "DBPass": dbPass, + "DBName": dbName, }) // Create app folder and add relevant files @@ -139,10 +191,10 @@ func cmdNew(cmd *cobra.Command, args []string) { var tmpl embed.FS type Templ struct { - values map[string]string + values map[string]interface{} } -func newTempl(values map[string]string) *Templ { +func newTempl(values map[string]interface{}) *Templ { return &Templ{values} } diff --git a/internal/cmd/cmd_serv.go b/internal/cmd/cmd_serv.go index c72f2769..812f057b 100644 --- a/internal/cmd/cmd_serv.go +++ b/internal/cmd/cmd_serv.go @@ -14,13 +14,13 @@ func servCmd() *cobra.Command { Use: "serve", Aliases: []string{"serv"}, Short: "Run the GraphJin service", - RunE: cmdServ, + Run: cmdServ, } c.Flags().BoolVar(&deployActive, "deploy-active", false, "Deploy active config") return c } -func cmdServ(*cobra.Command, []string) error { +func cmdServ(*cobra.Command, []string) { setup(cpath) var opt []serv.Option @@ -30,11 +30,10 @@ func cmdServ(*cobra.Command, []string) error { gj, err := serv.NewGraphJinService(conf, opt...) if err != nil { - return err + log.Fatalf("%s", err) } if err := gj.Start(); err != nil { - return err + log.Fatalf("%s", err) } - return nil } diff --git a/internal/cmd/tmpl/dev.yml b/internal/cmd/tmpl/dev.yml index 492984b9..14fcdab6 100644 --- a/internal/cmd/tmpl/dev.yml +++ b/internal/cmd/tmpl/dev.yml @@ -193,12 +193,16 @@ auths: # GJ_DATABASE_PASSWORD database: - type: postgres - host: db - port: 5432 + type: {{ .DBType }} + host: {{ .DBHost }} + port: {{ .DBPort }} + {{- if eq .DBName "" }} dbname: {{ .AppNameSlug -}}_development - user: postgres - password: postgres + {{- else }} + dbname: {{ .DBName }} + {{- end }} + user: {{ .DBUser }} + password: {{ .DBPass }} #schema: "public" # Size of database connection pool diff --git a/internal/cmd/tmpl/docker-compose.yml b/internal/cmd/tmpl/docker-compose.yml index 87ebaed0..d760a1c2 100644 --- a/internal/cmd/tmpl/docker-compose.yml +++ b/internal/cmd/tmpl/docker-compose.yml @@ -1,5 +1,5 @@ -version: "3.4" services: + {{- if .CreateDB }} # Postgres DB db: image: postgres:12 @@ -9,6 +9,7 @@ services: ports: - "5432:5432" + {{- end }} api: image: dosco/graphjin:latest command: serve @@ -18,5 +19,7 @@ services: - ./config:/home/nonroot/config ports: - "8080:8080" + {{- if .CreateDB }} depends_on: - db + {{- end }} diff --git a/internal/cmd/tmpl/prod.yml b/internal/cmd/tmpl/prod.yml index 525c5adb..17a25686 100644 --- a/internal/cmd/tmpl/prod.yml +++ b/internal/cmd/tmpl/prod.yml @@ -104,12 +104,16 @@ hot_deploy: true # sample: 0.6 database: - type: postgres - host: db - port: 5432 + type: {{ .DBType }} + host: {{ .DBHost }} + port: {{ .DBPort }} + {{- if eq .DBName "" }} dbname: {{ .AppNameSlug -}}_production - user: postgres - password: postgres + {{- else }} + dbname: {{ .DBName }} + {{- end }} + user: {{ .DBUser }} + password: {{ .DBPass }} #schema: "public" # Size of database connection pool