diff --git a/cmd/migrate.go b/cmd/migrate.go new file mode 100644 index 0000000..fa75342 --- /dev/null +++ b/cmd/migrate.go @@ -0,0 +1,34 @@ +package cmd + +import ( + "code.icod.de/auth/accountserver/service" + "github.com/spf13/cobra" +) + +// migrateCmd represents the migrate command +var migrateCmd = &cobra.Command{ + Use: "migrate", + Short: "Create Schema", + RunE: func(cmd *cobra.Command, args []string) error { + s := service.NewService() + s.Connect() + if e := s.CreateSchema(); e != nil { + return e + } + return s.Close() + }, +} + +func init() { + rootCmd.AddCommand(migrateCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // migrateCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // migrateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/serve.go b/cmd/serve.go index 8490fee..c78a563 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -19,6 +19,8 @@ func init() { // Here you will define your flags and configuration settings. + serveCmd.Flags().BoolVar(&configuration.Debug, "debug", false, "enable debug mode") + serveCmd.Flags().BoolVar(&configuration.RunHTTP, "http", true, "Run http server") serveCmd.Flags().BoolVar(&configuration.RunGRPC, "grpc", true, "Run grpc server") diff --git a/configuration/app.go b/configuration/app.go index e2c4da2..c2515d8 100644 --- a/configuration/app.go +++ b/configuration/app.go @@ -1,6 +1,7 @@ package configuration var ( + Debug = false RunHTTP = true RunGRPC = true ) diff --git a/configuration/database.go b/configuration/database.go new file mode 100644 index 0000000..2e88a52 --- /dev/null +++ b/configuration/database.go @@ -0,0 +1,14 @@ +package configuration + +import "fmt" + +var ( + DATABASE_HOST = "localhost:5432" + DATABASE_NAME = "postgres" + DATABASE_USER = "postgres" + DATABASE_PASS = "postgres" +) + +func GetDatabaseConnectionString() string { + return fmt.Sprintf("postgresql://%s:%s@%s/%s?sslmode=disable", DATABASE_USER, DATABASE_PASS, DATABASE_HOST, DATABASE_NAME) +} diff --git a/service/account.go b/service/account.go index 6d43c33..6bf63b2 100644 --- a/service/account.go +++ b/service/account.go @@ -1 +1,8 @@ package service + +type CreateAccountInput struct{} + +// CreateAccount creates an account, returns nil if successful or error if not +func (s *Service) CreateAccount(in *CreateAccountInput) error { + panic("implement me") +} diff --git a/service/service.go b/service/service.go index 6f3df5f..51bfc12 100644 --- a/service/service.go +++ b/service/service.go @@ -1,9 +1,44 @@ package service +import ( + "code.icod.de/auth/accountserver/configuration" + "code.icod.de/auth/accountserver/ent" + "context" + "database/sql" + "entgo.io/ent/dialect" + entsql "entgo.io/ent/dialect/sql" + "log" +) + type Service struct { + client *ent.Client } func NewService() *Service { s := new(Service) return s } + +func (s *Service) Connect() { + s.client = open(configuration.GetDatabaseConnectionString()) +} + +func (s *Service) Close() error { + return s.client.Close() +} + +func (s *Service) CreateSchema() error { + return s.client.Schema.Create(context.Background()) +} + +// open new connection +func open(databaseUrl string) *ent.Client { + db, err := sql.Open("pgx", databaseUrl) + if err != nil { + log.Fatal(err) + } + + // Create an ent.Driver from `db`. + drv := entsql.OpenDB(dialect.Postgres, db) + return ent.NewClient(ent.Driver(drv)) +}