feat: use risefront to replace overseer in order to supports Windows

This commit is contained in:
Hintay 2024-10-31 02:07:08 +09:00 committed by Jacky
parent cf531e3f09
commit bfba7ba2c2
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
2 changed files with 36 additions and 21 deletions

View file

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -16,7 +17,6 @@ import (
"github.com/0xJacky/Nginx-UI/internal/helper" "github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/version" "github.com/0xJacky/Nginx-UI/internal/version"
"github.com/0xJacky/Nginx-UI/settings" "github.com/0xJacky/Nginx-UI/settings"
"github.com/jpillora/overseer"
"github.com/minio/selfupdate" "github.com/minio/selfupdate"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/uozi-tech/cosy/logger" "github.com/uozi-tech/cosy/logger"
@ -269,7 +269,6 @@ func (u *Upgrader) PerformCoreUpgrade(tarPath string) (err error) {
} }
// gracefully restart // gracefully restart
overseer.Restart() cmd := exec.Command(os.Args[0])
return cmd.Start()
return
} }

48
main.go
View file

@ -5,17 +5,25 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"time"
"context"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"github.com/0xJacky/Nginx-UI/internal/cert" "github.com/0xJacky/Nginx-UI/internal/cert"
"github.com/0xJacky/Nginx-UI/internal/cmd" "github.com/0xJacky/Nginx-UI/internal/cmd"
"code.pfad.fr/risefront"
"github.com/0xJacky/Nginx-UI/internal/kernel" "github.com/0xJacky/Nginx-UI/internal/kernel"
"github.com/0xJacky/Nginx-UI/internal/migrate" "github.com/0xJacky/Nginx-UI/internal/migrate"
"github.com/0xJacky/Nginx-UI/model" "github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/router" "github.com/0xJacky/Nginx-UI/router"
"github.com/0xJacky/Nginx-UI/settings" "github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jpillora/overseer" "github.com/pkg/errors"
"github.com/uozi-tech/cosy" "github.com/uozi-tech/cosy"
cKernel "github.com/uozi-tech/cosy/kernel" cKernel "github.com/uozi-tech/cosy/kernel"
"github.com/uozi-tech/cosy/logger" "github.com/uozi-tech/cosy/logger"
@ -23,9 +31,9 @@ import (
cSettings "github.com/uozi-tech/cosy/settings" cSettings "github.com/uozi-tech/cosy/settings"
) )
//go:generate go generate ./cmd/... func Program(confPath string) func(l []net.Listener) error {
func Program(confPath string) func(state overseer.State) { return func(l []net.Listener) error {
return func(state overseer.State) { listener := l[0]
defer logger.Sync() defer logger.Sync()
defer logger.Info("Server exited") defer logger.Info("Server exited")
@ -47,20 +55,22 @@ func Program(confPath string) func(state overseer.State) {
logger.Init(cSettings.ServerSettings.RunMode) logger.Init(cSettings.ServerSettings.RunMode)
defer logger.Sync() defer logger.Sync()
if state.Listener == nil {
return
}
// Gin router initialization // Gin router initialization
cRouter.Init() cRouter.Init()
// Kernel boot // Kernel boot
cKernel.Boot() cKernel.Boot()
addr := fmt.Sprintf("%s:%d", cSettings.ServerSettings.Host, cSettings.ServerSettings.Port)
srv := &http.Server{ srv := &http.Server{
Addr: addr,
Handler: cRouter.GetEngine(), Handler: cRouter.GetEngine(),
} }
// defer Shutdown to wait for ongoing requests to be served before returning
defer func(srv *http.Server, ctx context.Context) {
err := srv.Shutdown(ctx)
if err != nil {
logger.Fatal(err)
}
}(srv, context.Background())
var err error var err error
if cSettings.ServerSettings.EnableHTTPS { if cSettings.ServerSettings.EnableHTTPS {
// Load TLS certificate // Load TLS certificate
@ -80,11 +90,11 @@ func Program(confPath string) func(state overseer.State) {
srv.TLSConfig = tlsConfig srv.TLSConfig = tlsConfig
logger.Info("Starting HTTPS server") logger.Info("Starting HTTPS server")
tlsListener := tls.NewListener(state.Listener, tlsConfig) tlsListener := tls.NewListener(listener, tlsConfig)
err = srv.Serve(tlsListener) err = srv.Serve(tlsListener)
} else { } else {
logger.Info("Starting HTTP server") logger.Info("Starting HTTP server")
err = srv.Serve(state.Listener) err = srv.Serve(listener)
} }
if err != nil && !errors.Is(err, http.ErrServerClosed) { if err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Fatalf("listen: %s\n", err) logger.Fatalf("listen: %s\n", err)
@ -97,10 +107,16 @@ func main() {
confPath := appCmd.String("config") confPath := appCmd.String("config")
settings.Init(confPath) settings.Init(confPath)
overseer.Run(overseer.Config{
Program: Program(confPath), ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
Address: fmt.Sprintf("%s:%d", cSettings.ServerSettings.Host, cSettings.ServerSettings.Port), defer cancel()
TerminateTimeout: 5 * time.Second,
err := risefront.New(ctx, risefront.Config{
Run: Program(confPath),
Debug: cSettings.ServerSettings.RunMode == gin.DebugMode, Debug: cSettings.ServerSettings.RunMode == gin.DebugMode,
Addresses: []string{fmt.Sprintf("%s:%d", cSettings.ServerSettings.Host, cSettings.ServerSettings.Port)},
}) })
if !errors.Is(err, context.DeadlineExceeded) {
logger.Fatal(err)
}
} }