-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add database connection and migrations
- Loading branch information
Showing
12 changed files
with
287 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
version: '3' | ||
|
||
env: | ||
GOOSE_DRIVER: mysql | ||
GOOSE_DBSTRING: "root:bwizard@/bwizard" | ||
|
||
tasks: | ||
setup: | ||
cmds: | ||
- go install github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@latest | ||
- go install github.com/pressly/goose/v3/cmd/goose@latest | ||
migrations-create: | ||
cmd: goose -dir ./migrations create {{.CLI_ARGS}} sql | ||
migrations-migrate: | ||
cmd: goose -dir ./migrations up | ||
gen-http: | ||
cmds: | ||
- oapi-codegen -package wizard -generate types,server ./api/openapi/wizard/wizard.yml > ./api/openapi/wizard/wizard.gen.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,30 @@ | ||
package main | ||
|
||
import "bwizard/internal/pkg/wizardapi" | ||
import ( | ||
"bwizard/internal/pkg/wizard/application" | ||
"bwizard/internal/pkg/wizard/infrastructure/mysql" | ||
"bwizard/internal/pkg/wizardapi" | ||
"github.com/rs/zerolog" | ||
"os" | ||
"time" | ||
) | ||
|
||
func main() { | ||
wizardapi.StartAPIServer() | ||
logger := zerolog.New( | ||
zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}, | ||
).Level(zerolog.TraceLevel). | ||
With(). | ||
Timestamp(). | ||
Caller(). | ||
Logger() | ||
|
||
db, err := mysql.Connect(&logger) | ||
if err != nil { | ||
logger.Error().Msg(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
app := application.NewApplication(db) | ||
|
||
wizardapi.StartAPIServer(app) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package application | ||
|
||
import ( | ||
deviceSvcImpl "bwizard/internal/pkg/wizard/application/svc/device" | ||
"bwizard/internal/pkg/wizard/domain/entity/device" | ||
deviceRepo "bwizard/internal/pkg/wizard/domain/repo/device" | ||
deviceSvc "bwizard/internal/pkg/wizard/domain/svc/device" | ||
deviceValue "bwizard/internal/pkg/wizard/domain/valueobject/device" | ||
deviceRepoImpl "bwizard/internal/pkg/wizard/infrastructure/mysql/repo/device" | ||
"database/sql" | ||
) | ||
|
||
type Application interface { | ||
// SetupDevice setups a new device | ||
SetupDevice(ips []deviceValue.IPAddress, kind deviceValue.Kind, name *string) (*device.Device, error) | ||
} | ||
|
||
type Impl struct { | ||
deviceRepo deviceRepo.Repository | ||
deviceInspectionSvc deviceSvc.InspectionService | ||
} | ||
|
||
var _ Application = &Impl{} | ||
|
||
// NewApplication returns a new application | ||
func NewApplication(db *sql.DB) *Impl { | ||
deviceRepository := deviceRepoImpl.NewRepository(db) | ||
deviceInspectionSvc := deviceSvcImpl.NewInspectionSvc() | ||
|
||
return &Impl{ | ||
deviceRepo: deviceRepository, | ||
deviceInspectionSvc: deviceInspectionSvc, | ||
} | ||
} | ||
|
||
// SetupDevice setups a new device | ||
func (i *Impl) SetupDevice(ips []deviceValue.IPAddress, kind deviceValue.Kind, name *string) (*device.Device, error) { | ||
inspection, err := i.deviceInspectionSvc.Inspect(ips) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if inspection == nil { | ||
// TODO: Throw some kind of error if inspection failed | ||
} | ||
|
||
if name == nil { | ||
name = &inspection.Hostname | ||
} | ||
|
||
deviceEntity := device.NewBuilder(). | ||
Name(*name). | ||
Kind(kind). | ||
IPs(ips). | ||
Build() | ||
|
||
return deviceEntity, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package device | ||
|
||
import ( | ||
"bwizard/internal/pkg/wizard/domain/valueobject/device" | ||
"github.com/google/uuid" | ||
"time" | ||
) | ||
|
||
type Builder struct { | ||
*Device | ||
} | ||
|
||
func NewBuilder() *Builder { | ||
return &Builder{} | ||
} | ||
|
||
func (b *Builder) ID(v uuid.UUID) *Builder { | ||
b.Device.ID = v | ||
|
||
return b | ||
} | ||
|
||
func (b *Builder) Name(v string) *Builder { | ||
b.Device.Name = v | ||
|
||
return b | ||
} | ||
|
||
func (b *Builder) Kind(v device.Kind) *Builder { | ||
b.Device.Kind = v | ||
|
||
return b | ||
} | ||
|
||
func (b *Builder) Protection(v device.ProtectionStatus) *Builder { | ||
b.Device.Protection = v | ||
|
||
return b | ||
} | ||
|
||
func (b *Builder) LastBackup(v *time.Time) *Builder { | ||
b.Device.LastBackup = v | ||
|
||
return b | ||
} | ||
|
||
func (b *Builder) IPs(v []device.IPAddress) *Builder { | ||
b.Device.IPs = v | ||
|
||
return b | ||
} | ||
|
||
func (b *Builder) Agent(v string) *Builder { | ||
b.Device.Agent = v | ||
|
||
return b | ||
} | ||
|
||
func (b *Builder) CreatedAt(v time.Time) *Builder { | ||
b.Device.CreatedAt = v | ||
|
||
return b | ||
} | ||
|
||
func (b *Builder) UpdatedAt(v time.Time) *Builder { | ||
b.Device.UpdatedAt = v | ||
|
||
return b | ||
} | ||
|
||
func (b *Builder) Build() *Device { | ||
CreatedAt := b.Device.CreatedAt | ||
UpdatedAt := b.Device.UpdatedAt | ||
|
||
if CreatedAt.IsZero() { | ||
b.Device.CreatedAt = time.Now() | ||
} | ||
|
||
if UpdatedAt.IsZero() { | ||
b.Device.UpdatedAt = time.Now() | ||
} | ||
|
||
return &Device{ | ||
ID: b.Device.ID, | ||
Name: b.Device.Name, | ||
Kind: b.Device.Kind, | ||
Protection: b.Device.Protection, | ||
LastBackup: b.Device.LastBackup, | ||
IPs: b.Device.IPs, | ||
Agent: b.Device.Agent, | ||
CreatedAt: b.Device.CreatedAt, | ||
UpdatedAt: b.Device.UpdatedAt, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package mysql | ||
|
||
import ( | ||
"database/sql" | ||
"github.com/go-sql-driver/mysql" | ||
"github.com/rs/zerolog" | ||
"os" | ||
) | ||
|
||
func Connect(logger *zerolog.Logger) (*sql.DB, error) { | ||
logger.Info().Msg("try to connect to database...") | ||
|
||
cfg := mysql.Config{ | ||
User: os.Getenv("DB_USER"), | ||
Passwd: os.Getenv("DB_PASS"), | ||
Net: "tcp", | ||
Addr: os.Getenv("DB_HOST"), | ||
DBName: os.Getenv("DB_DATABASE"), | ||
AllowNativePasswords: true, | ||
} | ||
|
||
db, err := sql.Open("mysql", cfg.FormatDSN()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pingErr := db.Ping() | ||
if pingErr != nil { | ||
return nil, pingErr | ||
} | ||
|
||
logger.Info().Msg("successfully connected to database") | ||
|
||
return db, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.