Skip to content

Commit

Permalink
refactor: 重构应用加载器
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jan 2, 2025
1 parent 4beb040 commit 4f5961d
Show file tree
Hide file tree
Showing 18 changed files with 346 additions and 150 deletions.
3 changes: 2 additions & 1 deletion cmd/cli/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/google/wire"

"github.com/tnb-labs/panel/internal/app"
"github.com/tnb-labs/panel/internal/apps"
"github.com/tnb-labs/panel/internal/bootstrap"
"github.com/tnb-labs/panel/internal/data"
"github.com/tnb-labs/panel/internal/route"
Expand All @@ -14,5 +15,5 @@ import (

// initCli init command line.
func initCli() (*app.Cli, error) {
panic(wire.Build(bootstrap.ProviderSet, route.ProviderSet, service.ProviderSet, data.ProviderSet, app.NewCli))
panic(wire.Build(bootstrap.ProviderSet, route.ProviderSet, service.ProviderSet, data.ProviderSet, apps.ProviderSet, app.NewCli))
}
49 changes: 48 additions & 1 deletion cmd/cli/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions cmd/web/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion internal/app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (

"github.com/go-gormigrate/gormigrate/v2"
"github.com/urfave/cli/v3"

"github.com/tnb-labs/panel/pkg/apploader"
)

type Cli struct {
cmd *cli.Command
migrator *gormigrate.Gormigrate
}

func NewCli(cmd *cli.Command, migrator *gormigrate.Gormigrate) *Cli {
func NewCli(cmd *cli.Command, migrator *gormigrate.Gormigrate, _ *apploader.Loader) *Cli {
IsCli = true
return &Cli{
cmd: cmd,
Expand Down
10 changes: 5 additions & 5 deletions internal/app/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const (

// 面板全局变量
var (
Key string
Root string
Locale string
IsCli bool
Status = StatusNormal
Key string // 密钥
Root string // 根目录
Locale string // 语言
IsCli bool // 是否命令行
Status = StatusNormal // 面板状态
)

// 自动注入
Expand Down
139 changes: 12 additions & 127 deletions internal/apps/apps.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package apps

import (
"reflect"
"slices"
"strings"

"github.com/go-chi/chi/v5"
"github.com/google/wire"

"github.com/tnb-labs/panel/internal/apps/benchmark"
Expand All @@ -16,7 +11,12 @@ import (
"github.com/tnb-labs/panel/internal/apps/memcached"
"github.com/tnb-labs/panel/internal/apps/mysql"
"github.com/tnb-labs/panel/internal/apps/nginx"
"github.com/tnb-labs/panel/internal/apps/php"
"github.com/tnb-labs/panel/internal/apps/php74"
"github.com/tnb-labs/panel/internal/apps/php80"
"github.com/tnb-labs/panel/internal/apps/php81"
"github.com/tnb-labs/panel/internal/apps/php82"
"github.com/tnb-labs/panel/internal/apps/php83"
"github.com/tnb-labs/panel/internal/apps/php84"
"github.com/tnb-labs/panel/internal/apps/phpmyadmin"
"github.com/tnb-labs/panel/internal/apps/podman"
"github.com/tnb-labs/panel/internal/apps/postgresql"
Expand All @@ -29,7 +29,6 @@ import (
)

var ProviderSet = wire.NewSet(
NewLoader,
benchmark.NewApp,
docker.NewApp,
fail2ban.NewApp,
Expand All @@ -38,7 +37,12 @@ var ProviderSet = wire.NewSet(
memcached.NewApp,
mysql.NewApp,
nginx.NewApp,
php.NewApp,
php74.NewApp,
php80.NewApp,
php81.NewApp,
php82.NewApp,
php83.NewApp,
php84.NewApp,
phpmyadmin.NewApp,
podman.NewApp,
postgresql.NewApp,
Expand All @@ -49,122 +53,3 @@ var ProviderSet = wire.NewSet(
supervisor.NewApp,
toolbox.NewApp,
)

var slugs []string

type Loader struct {
benchmark *benchmark.App
docker *docker.App
fail2ban *fail2ban.App
frp *frp.App
gitea *gitea.App
memcached *memcached.App
mysql *mysql.App
nginx *nginx.App
php *php.App
phpmyadmin *phpmyadmin.App
podman *podman.App
postgresql *postgresql.App
pureftpd *pureftpd.App
redis *redis.App
rsync *rsync.App
s3fs *s3fs.App
supervisor *supervisor.App
toolbox *toolbox.App
}

func NewLoader(
benchmark *benchmark.App,
docker *docker.App,
fail2ban *fail2ban.App,
frp *frp.App,
gitea *gitea.App,
memcached *memcached.App,
mysql *mysql.App,
nginx *nginx.App,
php *php.App,
phpmyadmin *phpmyadmin.App,
podman *podman.App,
postgresql *postgresql.App,
pureftpd *pureftpd.App,
redis *redis.App,
rsync *rsync.App,
s3fs *s3fs.App,
supervisor *supervisor.App,
toolbox *toolbox.App,
) *Loader {
loader := &Loader{
benchmark: benchmark,
docker: docker,
fail2ban: fail2ban,
frp: frp,
gitea: gitea,
memcached: memcached,
mysql: mysql,
nginx: nginx,
php: php,
phpmyadmin: phpmyadmin,
podman: podman,
postgresql: postgresql,
pureftpd: pureftpd,
redis: redis,
rsync: rsync,
s3fs: s3fs,
supervisor: supervisor,
toolbox: toolbox,
}

loader.initSlugs()
return loader
}

func (r *Loader) Register(mux chi.Router) {
mux.Route("/benchmark", r.benchmark.Route)
mux.Route("/docker", r.docker.Route)
mux.Route("/fail2ban", r.fail2ban.Route)
mux.Route("/frp", r.frp.Route)
mux.Route("/gitea", r.gitea.Route)
mux.Route("/memcached", r.memcached.Route)
mux.Route("/mysql", r.mysql.Route)
mux.Route("/nginx", r.nginx.Route)
mux.Route("/php74", r.php.Route(74))
mux.Route("/php80", r.php.Route(80))
mux.Route("/php81", r.php.Route(81))
mux.Route("/php82", r.php.Route(82))
mux.Route("/php83", r.php.Route(83))
mux.Route("/php84", r.php.Route(84))
mux.Route("/phpmyadmin", r.phpmyadmin.Route)
mux.Route("/podman", r.podman.Route)
mux.Route("/postgresql", r.postgresql.Route)
mux.Route("/pureftpd", r.pureftpd.Route)
mux.Route("/redis", r.redis.Route)
mux.Route("/rsync", r.rsync.Route)
mux.Route("/s3fs", r.s3fs.Route)
mux.Route("/supervisor", r.supervisor.Route)
mux.Route("/toolbox", r.toolbox.Route)
}

func (r *Loader) initSlugs() []string {
if len(slugs) == 0 {
v := reflect.Indirect(reflect.ValueOf(r))
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
slug := strings.ToLower(v.Type().Field(i).Name)
if !field.IsNil() {
slugs = append(slugs, slug)
}
}

// 处理php
slugs = slices.DeleteFunc(slugs, func(slug string) bool {
return slug == "php"
})
slugs = append(slugs, "php74", "php80", "php81", "php82", "php83", "php84")
}

return slugs
}

func Slugs() []string {
return slugs
}
22 changes: 22 additions & 0 deletions internal/apps/php74/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package php74

import (
"github.com/go-chi/chi/v5"

"github.com/tnb-labs/panel/internal/apps/php"
"github.com/tnb-labs/panel/internal/biz"
)

type App struct {
php *php.App
}

func NewApp(task biz.TaskRepo) *App {
return &App{
php: php.NewApp(task),
}
}

func (s *App) Route(r chi.Router) {
s.php.Route(74)(r)
}
22 changes: 22 additions & 0 deletions internal/apps/php80/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package php80

import (
"github.com/go-chi/chi/v5"

"github.com/tnb-labs/panel/internal/apps/php"
"github.com/tnb-labs/panel/internal/biz"
)

type App struct {
php *php.App
}

func NewApp(task biz.TaskRepo) *App {
return &App{
php: php.NewApp(task),
}
}

func (s *App) Route(r chi.Router) {
s.php.Route(80)(r)
}
22 changes: 22 additions & 0 deletions internal/apps/php81/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package php81

import (
"github.com/go-chi/chi/v5"

"github.com/tnb-labs/panel/internal/apps/php"
"github.com/tnb-labs/panel/internal/biz"
)

type App struct {
php *php.App
}

func NewApp(task biz.TaskRepo) *App {
return &App{
php: php.NewApp(task),
}
}

func (s *App) Route(r chi.Router) {
s.php.Route(81)(r)
}
Loading

0 comments on commit 4f5961d

Please sign in to comment.