Skip to content
Snippets Groups Projects
Commit 95ef7a28e54a authored by Christophe de Vienne's avatar Christophe de Vienne
Browse files

Add a 'no-db' mode

if info.x-go-orusapi-no-db is true, no database-related code is generated
parent c2de70faf5fb
No related branches found
No related tags found
No related merge requests found
Pipeline #83764 passed
......@@ -2,9 +2,10 @@
{{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
{{- $noDB := not (not (index .Info.Extensions "x-go-orusapi-no-db") ) }}
package cmd
import (
"github.com/orus-io/go-flags"
"orus.io/orus-io/go-orusapi"
......@@ -5,7 +6,8 @@
package cmd
import (
"github.com/orus-io/go-flags"
"orus.io/orus-io/go-orusapi"
{{- if not $noDB }}
"orus.io/orus-io/go-orusapi/database"
......@@ -11,4 +13,5 @@
"orus.io/orus-io/go-orusapi/database"
{{- end }}
)
var (
......@@ -27,4 +30,5 @@
var (
Logger = orusapi.DefaultLogger(os.Stdout)
LoggingOptions = orusapi.MustLoggingOptions(orusapi.NewLoggingOptions(&Logger, os.Stdout))
{{- if not $noDB }}
DatabaseOptions = &database.Options{}
......@@ -30,4 +34,5 @@
DatabaseOptions = &database.Options{}
{{- end }}
ConfigFileOption = &ConfigFile{}
InfoOptions = &Info{}
......@@ -90,9 +95,10 @@
g.Namespace="log"
g.EnvNamespace="LOG"
{{- if not $noDB }}
g, err = parser.AddGroup("Database", "Database options", DatabaseOptions)
if err != nil {
panic(err)
}
g.Namespace="db"
g.EnvNamespace="DB"
......@@ -93,9 +99,10 @@
g, err = parser.AddGroup("Database", "Database options", DatabaseOptions)
if err != nil {
panic(err)
}
g.Namespace="db"
g.EnvNamespace="DB"
{{- end }}
g, err = parser.AddGroup("Sentry", "Sentry options", SentryOptions)
if err != nil {
......
......@@ -2,6 +2,7 @@
{{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
{{- $noDB := not (not (index .Info.Extensions "x-go-orusapi-no-db") ) }}
package cmd
......@@ -5,6 +6,10 @@
package cmd
{{- if $noDB }}
// "info.x-go-orusapi-no-db" is 'true', no 'migrate' command
{{- else }}
import (
"github.com/golang-migrate/migrate/v4"
"github.com/rs/zerolog"
......@@ -70,3 +75,4 @@
Logger.Fatal().Msg(err.Error())
}
}
{{- end }}
......@@ -2,6 +2,7 @@
{{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
{{- $noDB := not (not (index .Info.Extensions "x-go-orusapi-no-db") ) }}
package cmd
......@@ -12,7 +13,9 @@
flags "github.com/orus-io/go-flags"
"github.com/prometheus/client_golang/prometheus"
orusapi "orus.io/orus-io/go-orusapi"
"orus.io/orus-io/go-orusapi/database"
{{- if not $noDB }}
"orus.io/orus-io/go-orusapi/database"
{{- end }}
"{{ joinFilePath .TargetImportPath "restapi" "operations" }}"
)
......@@ -37,4 +40,5 @@
type ServeCmd struct {
Server *orusapi.Server
{{- if not $noDB }}
AutoMigrate bool `long:"auto-migrate" description:"automatically apply database migrations if needed"`
......@@ -40,7 +44,8 @@
AutoMigrate bool `long:"auto-migrate" description:"automatically apply database migrations if needed"`
{{- end }}
API *operations.{{ pascalize .Name }}API `no-flag:"t"`
}
// Execute setup a server and runs it
func (cmd *ServeCmd) Execute([]string) error {
......@@ -41,9 +46,10 @@
API *operations.{{ pascalize .Name }}API `no-flag:"t"`
}
// Execute setup a server and runs it
func (cmd *ServeCmd) Execute([]string) error {
{{- if not $noDB }}
if cmd.AutoMigrate {
if err := database.AutoMigrate(DatabaseOptions.DSN, migration.Source, Logger); err != nil {
return err
......@@ -67,6 +73,7 @@
if err := db.Ping(); err != nil {
return err
}
{{- end}}
if cmd.Server.Prometheus {
var infoGauge = prometheus.NewGauge(prometheus.GaugeOpts{
......@@ -83,8 +90,9 @@
}
defer prometheus.Unregister(infoGauge)
{{- if not $noDB }}
collector := sqlstats.NewStatsCollector("db", db)
if err := prometheus.Register(collector); err != nil {
return err
}
defer prometheus.Unregister(collector)
......@@ -86,9 +94,10 @@
collector := sqlstats.NewStatsCollector("db", db)
if err := prometheus.Register(collector); err != nil {
return err
}
defer prometheus.Unregister(collector)
{{- end }}
}
config := restapi.Config{
Log: Logger,
......@@ -91,5 +100,6 @@
}
config := restapi.Config{
Log: Logger,
{{- if not $noDB }}
DB: db,
......@@ -95,4 +105,5 @@
DB: db,
{{- end }}
}
if err := setupServeConfig(&config); err != nil {
return err
......
......@@ -2,6 +2,7 @@
{{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
{{- $noDB := not (not (index .Info.Extensions "x-go-orusapi-no-db") ) }}
package {{ .APIPackage }}
......@@ -28,4 +29,6 @@
// Config holds the things required to configure the API
type Config struct {
Log zerolog.Logger
{{- if not $noDB }}
DB *sqlx.DB
......@@ -31,4 +34,5 @@
DB *sqlx.DB
{{- end }}
BaseURL string
Shutdown []Callback
......
......@@ -2,6 +2,7 @@
{{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
{{- $noDB := not (not (index .Info.Extensions "x-go-orusapi-no-db") ) }}
package migration
......@@ -5,6 +6,12 @@
package migration
{{- if $noDB }}
// "info.x-go-orusapi-no-db" is 'true', no actual 'migration' module
// The directory and this file remains because there is no way to skip a file
// generation based on a condition in the specs (that I know of)
{{- else }}
import (
"github.com/golang-migrate/migrate/v4/source"
bindata "github.com/golang-migrate/migrate/v4/source/go_bindata"
......@@ -23,3 +30,4 @@
}
var Source = initSource()
{{- end }}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment