Skip to content

Commit

Permalink
refactor: 应用支持依赖注入
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Dec 15, 2024
1 parent 1fdc86c commit a64660d
Show file tree
Hide file tree
Showing 57 changed files with 663 additions and 861 deletions.
6 changes: 3 additions & 3 deletions cmd/cli/wire_gen.go

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

3 changes: 2 additions & 1 deletion cmd/web/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/TheTNB/panel/internal/app"
"github.com/TheTNB/panel/internal/apps"
"github.com/TheTNB/panel/internal/bootstrap"
"github.com/TheTNB/panel/internal/data"
"github.com/TheTNB/panel/internal/http/middleware"
Expand All @@ -16,5 +17,5 @@ import (

// initWeb init application.
func initWeb() (*app.Web, error) {
panic(wire.Build(bootstrap.ProviderSet, middleware.ProviderSet, route.ProviderSet, service.ProviderSet, data.ProviderSet, job.ProviderSet, app.NewWeb))
panic(wire.Build(bootstrap.ProviderSet, middleware.ProviderSet, route.ProviderSet, service.ProviderSet, data.ProviderSet, apps.ProviderSet, job.ProviderSet, app.NewWeb))
}
46 changes: 42 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.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.23
require (
github.com/bddjr/hlfhr v1.3.5
github.com/beevik/ntp v1.4.3
github.com/cloudflare/tableflip v1.2.3
github.com/creack/pty v1.1.24
github.com/expr-lang/expr v1.16.9
github.com/glebarez/sqlite v1.11.0
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/bddjr/hlfhr v1.3.5 h1:nBbye1k7XQ+4KJIGKQADk6lIuDUV+fcXG+TUUsFihPk=
github.com/bddjr/hlfhr v1.3.5/go.mod h1:oyIv4Q9JpCgZFdtH3KyTNWp7YYRWl4zl8k4ozrMAB4g=
github.com/beevik/ntp v1.4.3 h1:PlbTvE5NNy4QHmA4Mg57n7mcFTmr1W1j3gcK7L1lqho=
github.com/beevik/ntp v1.4.3/go.mod h1:Unr8Zg+2dRn7d8bHFuehIMSvvUYssHMxW3Q5Nx4RW5Q=
github.com/cloudflare/tableflip v1.2.3 h1:8I+B99QnnEWPHOY3fWipwVKxS70LGgUsslG7CSfmHMw=
github.com/cloudflare/tableflip v1.2.3/go.mod h1:P4gRehmV6Z2bY5ao5ml9Pd8u6kuEnlB37pUFMmv7j2E=
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -190,7 +188,6 @@ golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
8 changes: 0 additions & 8 deletions internal/app/global.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package app

import (
"gorm.io/gorm"
)

var (
Orm *gorm.DB
)

// 面板状态常量
const (
StatusNormal = iota
Expand Down
84 changes: 11 additions & 73 deletions internal/app/web.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package app

import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"path/filepath"

"github.com/bddjr/hlfhr"
"github.com/cloudflare/tableflip"
"github.com/go-chi/chi/v5"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/gookit/validate"
Expand Down Expand Up @@ -51,73 +44,18 @@ func (r *Web) Run() error {
fmt.Println("[CRON] cron scheduler started")

// run http server
if runtime.GOOS != "windows" {
return r.runServer()
}

return r.runServerFallback()
}

// runServer graceful run server
func (r *Web) runServer() error {
upg, err := tableflip.New(tableflip.Options{})
if err != nil {
return err
}
defer upg.Stop()

// By prefixing PID to log, easy to interrupt from another process.
log.SetPrefix(fmt.Sprintf("[PID %d]", os.Getpid()))

// Listen for the process signal to trigger the tableflip upgrade.
go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP)
for range sig {
if err = upg.Upgrade(); err != nil {
log.Println("[Graceful] upgrade failed:", err)
}
if r.conf.Bool("http.tls") {
cert := filepath.Join(Root, "panel/storage/cert.pem")
key := filepath.Join(Root, "panel/storage/cert.key")
fmt.Println("[HTTP] listening and serving on port", r.conf.MustInt("http.port"), "with tls")
if err := r.server.ListenAndServeTLS(cert, key); !errors.Is(err, http.ErrServerClosed) {
return err
}
}()

ln, err := upg.Listen("tcp", r.conf.MustString("http.address"))
if err != nil {
return err
}
defer ln.Close()

fmt.Println("[HTTP] listening and serving on", r.conf.MustString("http.address"))
go func() {
if err = r.server.Serve(ln); !errors.Is(err, http.ErrServerClosed) {
log.Println("[HTTP] server error:", err)
} else {
fmt.Println("[HTTP] listening and serving on port", r.conf.MustInt("http.port"))
if err := r.server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
return err
}
}()

// tableflip ready
if err = upg.Ready(); err != nil {
return err
}

fmt.Println("[Graceful] ready for upgrade")
<-upg.Exit()

// Make sure to set a deadline on exiting the process
// after upg.Exit() is closed. No new upgrades can be
// performed if the parent doesn't exit.
time.AfterFunc(60*time.Second, func() {
log.Println("[Graceful] shutdown timeout, force exit")
os.Exit(1)
})

// Wait for connections to drain.
return r.server.Shutdown(context.Background())
}

// runServerFallback fallback for windows
func (r *Web) runServerFallback() error {
fmt.Println("[HTTP] listening and serving on", r.conf.MustString("http.address"))
if err := r.server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
return err
}

return nil
Expand Down
Loading

0 comments on commit a64660d

Please sign in to comment.