Skip to content

Commit

Permalink
Feat: Allow to setup Graphjin with a pre-existing Postgres databse #382
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dosco committed Sep 19, 2022
1 parent 38146e8 commit 73bfbdf
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
58 changes: 55 additions & 3 deletions internal/cmd/cmd_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"embed"
"net/url"
"os"
"path"
"strings"
Expand All @@ -13,13 +14,19 @@ import (
"golang.org/x/text/language"
)

var (
dbURL string
)

func newCmd() *cobra.Command {
c := &cobra.Command{
Use: "new <app-name>",
Short: "Create a new application",
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
}

Expand All @@ -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
Expand Down Expand Up @@ -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}
}

Expand Down
9 changes: 4 additions & 5 deletions internal/cmd/cmd_serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
14 changes: 9 additions & 5 deletions internal/cmd/tmpl/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/tmpl/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version: "3.4"
services:
{{- if .CreateDB }}
# Postgres DB
db:
image: postgres:12
Expand All @@ -9,6 +9,7 @@ services:
ports:
- "5432:5432"

{{- end }}
api:
image: dosco/graphjin:latest
command: serve
Expand All @@ -18,5 +19,7 @@ services:
- ./config:/home/nonroot/config
ports:
- "8080:8080"
{{- if .CreateDB }}
depends_on:
- db
{{- end }}
14 changes: 9 additions & 5 deletions internal/cmd/tmpl/prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 73bfbdf

Please sign in to comment.