Skip to content

Commit

Permalink
fix: change to binding evn
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Sep 3, 2024
1 parent f998465 commit c37a40f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
GITHUB_KEY=
GITHUB_SECRET=
SERVICE_LENS_ADDR=
SERVICE_LENS_DATABASE_URI=
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_HOST=
POSTGRES_PORT=
POSTGRES_DB=
SERVICE_LENS_DATABASE_TABLE_PREFIX=
SERVICE_LENS_FGA_API_URL=
SERVICE_LENS_FGA_STORE_ID=
Expand Down
8 changes: 7 additions & 1 deletion cmd/migrate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"fmt"

"github.com/zeiss/fiber-goth/adapters"
"github.com/zeiss/service-lens/internal/adapters/db"
"github.com/zeiss/service-lens/internal/models"
Expand All @@ -12,11 +14,15 @@ import (
"gorm.io/gorm/schema"
)

const dsnFormat = "host=%s port=%s user=%s password=%s dbname=%s sslmode=disable"

var Migrate = &cobra.Command{
Use: "migrate",
Short: "Migrate the database",
RunE: func(cmd *cobra.Command, args []string) error {
conn, err := gorm.Open(postgres.Open(config.Flags.DatabaseURI), &gorm.Config{
dsn := fmt.Sprintf(dsnFormat, config.Flags.DatabaseHost, config.Flags.DatabasePort, config.Flags.DatabaseUser, config.Flags.DatabasePassword, config.Flags.DatabaseName)

conn, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: config.Flags.DatabaseTablePrefix,
},
Expand Down
10 changes: 8 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func init() {
Root.AddCommand(Migrate)

Root.PersistentFlags().StringVar(&config.Flags.Addr, "addr", config.Flags.Addr, "addr")
Root.PersistentFlags().StringVar(&config.Flags.DatabaseURI, "db-uri", config.Flags.DatabaseURI, "Database URI")
Root.PersistentFlags().StringVar(&config.Flags.DatabaseHost, "db-host", config.Flags.DatabaseHost, "Database host")
Root.PersistentFlags().StringVar(&config.Flags.DatabasePort, "db-port", config.Flags.DatabasePort, "Database port")
Root.PersistentFlags().StringVar(&config.Flags.DatabaseUser, "db-user", config.Flags.DatabaseUser, "Database user")
Root.PersistentFlags().StringVar(&config.Flags.DatabasePassword, "db-password", config.Flags.DatabasePassword, "Database password")
Root.PersistentFlags().StringVar(&config.Flags.DatabaseName, "db-name", config.Flags.DatabaseName, "Database name")
Root.PersistentFlags().StringVar(&config.Flags.DatabaseTablePrefix, "db-table-prefix", config.Flags.DatabaseTablePrefix, "Database table prefix")
Root.PersistentFlags().StringVar(&config.Flags.FGAApiUrl, "fga-api-url", config.Flags.FGAApiUrl, "FGA API URL")
Root.PersistentFlags().StringVar(&config.Flags.FGAStoreID, "fga-store-id", config.Flags.FGAStoreID, "FGA Store ID")
Expand Down Expand Up @@ -87,9 +91,11 @@ func NewWebSrv(cfg *cfg.Config) *WebSrv {
// Start starts the server.
func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.RunFunc) func() error {
return func() error {
dsn := fmt.Sprintf(dsnFormat, config.Flags.DatabaseHost, config.Flags.DatabasePort, config.Flags.DatabaseUser, config.Flags.DatabasePassword, config.Flags.DatabaseName)

providers.RegisterProvider(github.New(s.cfg.Flags.GitHubClientID, s.cfg.Flags.GitHubClientSecret, s.cfg.Flags.GitHubCallbackURL))

conn, err := gorm.Open(postgres.Open(s.cfg.Flags.DatabaseURI), &gorm.Config{
conn, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: s.cfg.Flags.DatabaseTablePrefix,
},
Expand Down
6 changes: 5 additions & 1 deletion internal/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (
type Flags struct {
Environment string `envconfig:"SERVICE_LENS_ENV" default:"production"`
Addr string `envconfig:"SERVICE_LENS_ADDR" default:":8084"`
DatabaseURI string `envconfig:"SERVICE_LENS_DATABASE_URI" default:""`
DatabaseHost string `envconfig:"POSTGRES_HOST" default:""`
DatabasePort string `envconfig:"POSTGRES_PORT" default:""`
DatabaseUser string `envconfig:"POSTGRES_USER" default:""`
DatabasePassword string `envconfig:"POSTGRES_PASSWORD" default:""`
DatabaseName string `envconfig:"POSTGRES_DB" default:""`
DatabaseTablePrefix string `envconfig:"SERVICE_LENS_DATABASE_TABLE_PREFIX" default:"service_lens_"`
FGAApiUrl string `envconfig:"SERVICE_LENS_FGA_API_URL" default:""`
FGAStoreID string `envconfig:"SERVICE_LENS_FGA_STORE_ID" default:""`
Expand Down

0 comments on commit c37a40f

Please sign in to comment.