|
|
|
|
|
|
|
|
|
|
|
package cmd |
|
|
|
|
|
import ( |
|
|
"context" |
|
|
"errors" |
|
|
"os/signal" |
|
|
"syscall" |
|
|
"time" |
|
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/api" |
|
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/config" |
|
|
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy" |
|
|
log "github.com/sirupsen/logrus" |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func StartService(cfg *config.Config, configPath string, localPassword string) { |
|
|
builder := cliproxy.NewBuilder(). |
|
|
WithConfig(cfg). |
|
|
WithConfigPath(configPath). |
|
|
WithLocalManagementPassword(localPassword) |
|
|
|
|
|
ctxSignal, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) |
|
|
defer cancel() |
|
|
|
|
|
runCtx := ctxSignal |
|
|
if localPassword != "" { |
|
|
var keepAliveCancel context.CancelFunc |
|
|
runCtx, keepAliveCancel = context.WithCancel(ctxSignal) |
|
|
builder = builder.WithServerOptions(api.WithKeepAliveEndpoint(10*time.Second, func() { |
|
|
log.Warn("keep-alive endpoint idle for 10s, shutting down") |
|
|
keepAliveCancel() |
|
|
})) |
|
|
} |
|
|
|
|
|
service, err := builder.Build() |
|
|
if err != nil { |
|
|
log.Errorf("failed to build proxy service: %v", err) |
|
|
return |
|
|
} |
|
|
|
|
|
err = service.Run(runCtx) |
|
|
if err != nil && !errors.Is(err, context.Canceled) { |
|
|
log.Errorf("proxy service exited with error: %v", err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func WaitForCloudDeploy() { |
|
|
|
|
|
log.Info("Cloud deploy mode: No config found; standing by for configuration. API server is not started. Press Ctrl+C to exit.") |
|
|
|
|
|
ctxSignal, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) |
|
|
defer cancel() |
|
|
|
|
|
|
|
|
<-ctxSignal.Done() |
|
|
log.Info("Cloud deploy mode: Shutdown signal received; exiting") |
|
|
} |
|
|
|