Skip to content

Commit

Permalink
optimize docker
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Nov 12, 2024
1 parent a9a70ee commit e276914
Show file tree
Hide file tree
Showing 15 changed files with 411 additions and 229 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
.idea
.DS_Store
.vscode
goravel*
goravel_*
/vendor
6 changes: 4 additions & 2 deletions contracts/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ type DatabaseDriver interface {
Build() error
// Config get database configuration.
Config() DatabaseConfig
// Database returns a new instance with a new database, the Build method needs to be called first.
Database(name string) (DatabaseDriver, error)
// Driver gets the database driver name.
Driver() database.Driver
// Fresh the database.
Fresh() error
// Image gets the database image.
Image(image Image)
// Driver gets the database driver name.
Driver() database.Driver
// Stop the database.
Stop() error
}
Expand Down
3 changes: 2 additions & 1 deletion foundation/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package foundation
import (
"context"
"flag"
"github.com/goravel/framework/support/env"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -305,7 +306,7 @@ func setEnv() {
}

func setRootPath() {
support.RootPath = getCurrentAbsolutePath()
support.RootPath = env.CurrentAbsolutePath()
}

func getEnvPath() string {
Expand Down
51 changes: 0 additions & 51 deletions foundation/path.go

This file was deleted.

91 changes: 72 additions & 19 deletions support/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,48 @@ package docker

import (
"bytes"
"io"
"os"
"path/filepath"

"github.com/goravel/framework/contracts/testing"
"github.com/goravel/framework/foundation/json"
"github.com/goravel/framework/support/color"
"github.com/goravel/framework/support/file"
"io"
"os"
"path/filepath"
)

type Container struct {
file string
type ContainerManager struct {
file string
lockFile string
}

func NewContainer() *Container {
return &Container{
file: filepath.Join(os.TempDir(), "goravel_docker.txt"),
func NewContainerManager() *ContainerManager {
return &ContainerManager{
file: filepath.Join(os.TempDir(), "goravel_docker.txt"),
lockFile: filepath.Join(os.TempDir(), "goravel_docker.lock"),
}
}

func (r *Container) Add(containerType ContainerType, config testing.DatabaseConfig) {
containerTypeToContainers := r.All()
containerTypeToContainers[containerType] = append(containerTypeToContainers[containerType], config)
func (r *ContainerManager) Add(containerType ContainerType, databaseDriver testing.DatabaseDriver) {
containerTypeToDatabaseDrivers := r.All()
containerTypeToDatabaseDrivers[containerType] = append(containerTypeToDatabaseDrivers[containerType], databaseDriver)

containerTypeToDatabaseConfigs := make(map[ContainerType][]testing.DatabaseConfig)
for k, v := range containerTypeToDatabaseDrivers {
containerTypeToDatabaseConfigs[k] = make([]testing.DatabaseConfig, len(v))
for i, driver := range v {
containerTypeToDatabaseConfigs[k][i] = driver.Config()
}
}

color.Red().Println("add", r.file, databaseDriver.Config(), containerTypeToDatabaseConfigs)

f, err := os.OpenFile(r.file, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
panic(err)
}
defer f.Close()

content, err := json.NewJson().Marshal(containerTypeToContainers)
content, err := json.NewJson().Marshal(containerTypeToDatabaseConfigs)
if err != nil {
panic(err)
}
Expand All @@ -42,10 +54,10 @@ func (r *Container) Add(containerType ContainerType, config testing.DatabaseConf
}
}

func (r *Container) All() map[ContainerType][]testing.DatabaseConfig {
var containerTypeToContainers map[ContainerType][]testing.DatabaseConfig
func (r *ContainerManager) All() map[ContainerType][]testing.DatabaseDriver {
containerTypeToDatabaseDrivers := make(map[ContainerType][]testing.DatabaseDriver)
if !file.Exists(r.file) {
return make(map[ContainerType][]testing.DatabaseConfig)
return containerTypeToDatabaseDrivers
}

f, err := os.OpenFile(r.file, os.O_RDONLY, 0666)
Expand All @@ -59,13 +71,54 @@ func (r *Container) All() map[ContainerType][]testing.DatabaseConfig {
panic(err)
}

if err := json.NewJson().Unmarshal(bytes.TrimSpace(content), &containerTypeToContainers); err != nil {
var containerTypeToDatabaseConfigs map[ContainerType][]testing.DatabaseConfig
if err := json.NewJson().Unmarshal(bytes.TrimSpace(content), &containerTypeToDatabaseConfigs); err != nil {
panic(err)
}

if len(containerTypeToDatabaseConfigs) == 0 {
return containerTypeToDatabaseDrivers
}

containerTypeToDatabaseConfigs1 := make(map[ContainerType][]testing.DatabaseConfig)
for containerType, databaseConfigs := range containerTypeToDatabaseConfigs {
for _, databaseConfig := range databaseConfigs {
// If the port is not occupied, provide the container is released.
if databaseConfig.Port != 0 && !isPortUsing(databaseConfig.Port) {
continue
}
containerTypeToDatabaseConfigs1[containerType] = append(containerTypeToDatabaseConfigs1[containerType], databaseConfig)
databaseDriver := NewDatabaseDriverByExist(containerType, databaseConfig.ContainerID, databaseConfig.Database, databaseConfig.Username, databaseConfig.Password, databaseConfig.Port)
containerTypeToDatabaseDrivers[containerType] = append(containerTypeToDatabaseDrivers[containerType], databaseDriver)
}
}

color.Red().Println("all", r.file, containerTypeToDatabaseConfigs1)

return containerTypeToDatabaseDrivers
}

func (r *ContainerManager) Lock() {
for {
if !file.Exists(r.lockFile) {
break
}
}
if err := file.Create(r.lockFile, ""); err != nil {
panic(err)
}
}

return containerTypeToContainers
func (r *ContainerManager) Unlock() {
if err := file.Remove(r.lockFile); err != nil {
panic(err)
}
}

func (r *Container) Remove() error {
func (r *ContainerManager) Remove() error {
if err := file.Remove(r.lockFile); err != nil {
return err
}

return file.Remove(r.file)
}
68 changes: 39 additions & 29 deletions support/docker/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,50 @@ import (
"testing"

"github.com/stretchr/testify/assert"

contractstesting "github.com/goravel/framework/contracts/testing"
)

func TestContainer(t *testing.T) {
container := NewContainer()
assert.NoError(t, container.Remove())

config1 := contractstesting.DatabaseConfig{
Host: "localhost",
Port: 5432,
Database: "goravel",
Username: "goravel",
Password: "Framework!123",
ContainerID: "123456",
}
config2 := contractstesting.DatabaseConfig{
Host: "localhost1",
Port: 5432,
Database: "goravel",
Username: "goravel",
Password: "Framework!123",
ContainerID: "123456",
}

container.Add(ContainerTypePostgres, config1)
container.Add(ContainerTypePostgres, config2)
container.Add(ContainerTypeSqlite, config1)
testPortUsing = true
container := NewContainerManager()

postgresDriver1 := NewPostgresImpl("goravel", "goravel", "Framework!123")
postgresDriver1.port = 5432
postgresDriver1.containerID = "123456"

postgresDriver2 := NewPostgresImpl("goravel", "goravel", "Framework!123")
postgresDriver2.port = 5433
postgresDriver2.containerID = "1234565"

sqliteDriver := NewSqliteImpl("goravel")

mysqlDriver := NewMysqlImpl("goravel", "goravel", "Framework!123")
mysqlDriver.port = 5432
mysqlDriver.containerID = "123456"

sqlserverDriver := NewSqlserverImpl("goravel", "goravel", "Framework!123")
sqlserverDriver.port = 5432
sqlserverDriver.containerID = "123456"

container.Add(ContainerTypePostgres, postgresDriver1)
container.Add(ContainerTypePostgres, postgresDriver2)
container.Add(ContainerTypeSqlite, sqliteDriver)
container.Add(ContainerTypeMysql, mysqlDriver)
container.Add(ContainerTypeSqlserver, sqlserverDriver)

containers := container.All()
assert.Len(t, containers, 2)
assert.Len(t, containers, 4)
assert.Len(t, containers[ContainerTypePostgres], 2)
assert.Len(t, containers[ContainerTypeSqlite], 1)
assert.Equal(t, containers[ContainerTypePostgres][0], config1)
assert.Equal(t, containers[ContainerTypePostgres][1], config2)
assert.Equal(t, containers[ContainerTypeSqlite][0], config1)
assert.Len(t, containers[ContainerTypeMysql], 1)
assert.Len(t, containers[ContainerTypeSqlserver], 1)
assert.Equal(t, postgresDriver1, containers[ContainerTypePostgres][0])
assert.Equal(t, postgresDriver2, containers[ContainerTypePostgres][1])
assert.Equal(t, sqliteDriver, containers[ContainerTypeSqlite][0])
assert.Equal(t, mysqlDriver, containers[ContainerTypeMysql][0])
assert.Equal(t, sqlserverDriver, containers[ContainerTypeSqlserver][0])

defer func() {
testPortUsing = false
assert.NoError(t, container.Remove())
}()
}
Loading

0 comments on commit e276914

Please sign in to comment.