connect, createschema, close
This commit is contained in:
parent
6943eb83f4
commit
fd8fdc899c
34
cmd/migrate.go
Normal file
34
cmd/migrate.go
Normal file
@ -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")
|
||||
}
|
@ -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")
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package configuration
|
||||
|
||||
var (
|
||||
Debug = false
|
||||
RunHTTP = true
|
||||
RunGRPC = true
|
||||
)
|
||||
|
14
configuration/database.go
Normal file
14
configuration/database.go
Normal file
@ -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)
|
||||
}
|
@ -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")
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user