Skip to content

Commit

Permalink
chore: polish code (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH authored Apr 11, 2024
1 parent aeb479e commit e8eb754
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 32 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ func main() {
// schedule async v2job to get v2 appstore list
go func() {
// run once at startup
if err := service.MyService.V2AppStore().UpdateCatalog(); err != nil {
if err := service.MyService.AppStoreManagement().UpdateCatalog(); err != nil {
logger.Error("error when updating AppStore catalog at startup", zap.Error(err))
}
}()

if _, err := crontab.AddFunc("@every 10m", func() {
if err := service.MyService.V2AppStore().UpdateCatalog(); err != nil {
if err := service.MyService.AppStoreManagement().UpdateCatalog(); err != nil {
logger.Error("error when updating AppStore catalog", zap.Error(err))
}
}); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions route/v2/appstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (a *AppManagement) UnregisterAppStore(ctx echo.Context, id codegen.AppStore
}

func (a *AppManagement) ComposeAppStoreInfoList(ctx echo.Context, params codegen.ComposeAppStoreInfoListParams) error {
catalog, err := service.MyService.V2AppStore().Catalog()
catalog, err := service.MyService.AppStoreManagement().Catalog()
if err != nil {
message := err.Error()
logger.Error("failed to get catalog", zap.Error(err))
Expand All @@ -105,7 +105,7 @@ func (a *AppManagement) ComposeAppStoreInfoList(ctx echo.Context, params codegen

if params.Recommend != nil && *params.Recommend {
// recommend
recommendedList, err := service.MyService.V2AppStore().Recommend()
recommendedList, err := service.MyService.AppStoreManagement().Recommend()
if err != nil {
message := err.Error()
logger.Error("failed to get recommend list", zap.Error(err))
Expand Down Expand Up @@ -161,7 +161,7 @@ func (a *AppManagement) ComposeAppStoreInfoList(ctx echo.Context, params codegen
}

func (a *AppManagement) ComposeAppStoreInfo(ctx echo.Context, id codegen.StoreAppIDString) error {
composeApp, err := service.MyService.V2AppStore().ComposeApp(id)
composeApp, err := service.MyService.AppStoreManagement().ComposeApp(id)
if err != nil {
message := err.Error()
return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{Message: &message})
Expand All @@ -186,7 +186,7 @@ func (a *AppManagement) ComposeAppStoreInfo(ctx echo.Context, id codegen.StoreAp
}

func (a *AppManagement) ComposeAppMainStableTag(ctx echo.Context, id codegen.StoreAppIDString) error {
composeApp, err := service.MyService.V2AppStore().ComposeApp(id)
composeApp, err := service.MyService.AppStoreManagement().ComposeApp(id)
if err != nil {
message := err.Error()
return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{Message: &message})
Expand Down Expand Up @@ -215,7 +215,7 @@ func (a *AppManagement) ComposeAppMainStableTag(ctx echo.Context, id codegen.Sto
}

func (a *AppManagement) ComposeAppServiceStableTag(ctx echo.Context, id codegen.StoreAppIDString, serviceName string) error {
composeApp, err := service.MyService.V2AppStore().ComposeApp(id)
composeApp, err := service.MyService.AppStoreManagement().ComposeApp(id)
if err != nil {
message := err.Error()
return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{Message: &message})
Expand Down Expand Up @@ -244,7 +244,7 @@ func (a *AppManagement) ComposeAppServiceStableTag(ctx echo.Context, id codegen.
}

func (a *AppManagement) ComposeApp(ctx echo.Context, id codegen.StoreAppIDString) error {
composeApp, err := service.MyService.V2AppStore().ComposeApp(id)
composeApp, err := service.MyService.AppStoreManagement().ComposeApp(id)
if err != nil {
message := err.Error()
return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{Message: &message})
Expand Down Expand Up @@ -387,7 +387,7 @@ func (a *AppManagement) UpgradableAppList(ctx echo.Context) error {
title = []byte("unknown")
}

storeComposeApp, err := service.MyService.V2AppStore().ComposeApp(id)
storeComposeApp, err := service.MyService.AppStoreManagement().ComposeApp(id)
if err != nil || storeComposeApp == nil {
logger.Error("failed to get compose app", zap.Error(err), zap.String("appStoreID", id))
continue
Expand Down
2 changes: 1 addition & 1 deletion route/v2/compose_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (a *AppManagement) IsNewComposeUncontrolled(newComposeApp *service.ComposeA
return false, nil
} else {
// compare store info
StoreApp, err := service.MyService.V2AppStore().ComposeApp(newComposeApp.Name)
StoreApp, err := service.MyService.AppStoreManagement().ComposeApp(newComposeApp.Name)
if err != nil {
return false, err
}
Expand Down
10 changes: 5 additions & 5 deletions service/compose_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (a *ComposeApp) Update(ctx context.Context) error {
return ErrStoreInfoNotFound
}

storeComposeApp, err := MyService.V2AppStore().ComposeApp(*storeInfo.StoreAppID)
storeComposeApp, err := MyService.AppStoreManagement().ComposeApp(*storeInfo.StoreAppID)
if err != nil {
return err
}
Expand Down Expand Up @@ -339,7 +339,7 @@ func (a *ComposeApp) Pull(ctx context.Context) error {
return nil
}

func injectEnvVariableToComposeApp(a *ComposeApp) {
func (a *ComposeApp) injectEnvVariableToComposeApp() {
for _, service := range a.Services {
for k, v := range config.Global {
// if there is same name var declared in environment in compose yaml
Expand All @@ -352,7 +352,7 @@ func injectEnvVariableToComposeApp(a *ComposeApp) {
}

func (a *ComposeApp) Up(ctx context.Context, service api.Service) error {
injectEnvVariableToComposeApp(a)
a.injectEnvVariableToComposeApp()

if err := service.Up(ctx, (*codegen.ComposeApp)(a), api.UpOptions{
Start: api.StartOptions{
Expand Down Expand Up @@ -465,7 +465,7 @@ func (a *ComposeApp) PullAndApply(ctx context.Context, newComposeYAML []byte) er
}

func (a *ComposeApp) Create(ctx context.Context, options api.CreateOptions, service api.Service) error {
injectEnvVariableToComposeApp(a)
a.injectEnvVariableToComposeApp()
return service.Create(ctx, (*codegen.ComposeApp)(a), api.CreateOptions{})
}

Expand Down Expand Up @@ -922,7 +922,7 @@ func NewComposeAppFromYAML(yaml []byte, skipInterpolation, skipValidation bool)

// the WEBUI_PORT interpolate will tiger twice. In `pulished` and `port-map`.
// So we need to promise multiple WEBUI_PORT interpolate is a same value.
port, err := port.GetAvailablePort("tcp")
port, _ := port.GetAvailablePort("tcp")

project, err := loader.Load(
types.ConfigDetails{
Expand Down
19 changes: 2 additions & 17 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ var (
type Services interface {
AppStoreManagement() *AppStoreManagement

V2AppStore() AppStore

// Git() *GitService
Compose() *ComposeService
Docker() DockerService
Expand All @@ -48,25 +46,20 @@ func NewService(RuntimePath string) Services {
panic(err)
}

v2appStore := AppStore(NewAppStoreManagement())

return &store{
gateway: gatewayManagement,
notify: external.NewNotifyService(RuntimePath),

appStoreManagement: NewAppStoreManagement(),

v2appStore: v2appStore,
compose: NewComposeService(),
docker: NewDockerService(),
compose: NewComposeService(),
docker: NewDockerService(),
}
}

type store struct {
appStoreManagement *AppStoreManagement

v2appStore AppStore

// git *GitService
compose *ComposeService
docker DockerService
Expand All @@ -86,14 +79,6 @@ func (c *store) AppStoreManagement() *AppStoreManagement {
return c.appStoreManagement
}

func (c *store) V2AppStore() AppStore {
return c.v2appStore
}

// func (c *store) Git() *GitService {
// return c.git
// }

func (c *store) Compose() *ComposeService {
return c.compose
}
Expand Down

0 comments on commit e8eb754

Please sign in to comment.