mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-05 16:00:21 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8e4bd42e8c | ||
|
|
ef3199f0ca | ||
|
|
e440530acc | ||
|
|
e2ae9fe50b |
16
.github/workflows/release.yml
vendored
16
.github/workflows/release.yml
vendored
@@ -85,11 +85,25 @@ jobs:
|
|||||||
go-version: '1.24'
|
go-version: '1.24'
|
||||||
cache-dependency-path: backend/go.sum
|
cache-dependency-path: backend/go.sum
|
||||||
|
|
||||||
|
- name: Fetch tags with annotations
|
||||||
|
run: |
|
||||||
|
# 确保获取完整的 annotated tag 信息
|
||||||
|
git fetch --tags --force
|
||||||
|
|
||||||
- name: Get tag message
|
- name: Get tag message
|
||||||
id: tag_message
|
id: tag_message
|
||||||
run: |
|
run: |
|
||||||
|
TAG_NAME=${GITHUB_REF#refs/tags/}
|
||||||
|
echo "Processing tag: $TAG_NAME"
|
||||||
|
|
||||||
# 获取完整的 tag message(跳过第一行标题)
|
# 获取完整的 tag message(跳过第一行标题)
|
||||||
TAG_MESSAGE=$(git tag -l --format='%(contents:body)' ${GITHUB_REF#refs/tags/})
|
TAG_MESSAGE=$(git tag -l --format='%(contents:body)' "$TAG_NAME")
|
||||||
|
|
||||||
|
# 调试输出
|
||||||
|
echo "Tag message length: ${#TAG_MESSAGE}"
|
||||||
|
echo "Tag message preview:"
|
||||||
|
echo "$TAG_MESSAGE" | head -10
|
||||||
|
|
||||||
# 使用 EOF 分隔符处理多行内容
|
# 使用 EOF 分隔符处理多行内容
|
||||||
echo "message<<EOF" >> $GITHUB_OUTPUT
|
echo "message<<EOF" >> $GITHUB_OUTPUT
|
||||||
echo "$TAG_MESSAGE" >> $GITHUB_OUTPUT
|
echo "$TAG_MESSAGE" >> $GITHUB_OUTPUT
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ sudo journalctl -u sub2api -f
|
|||||||
sudo systemctl restart sub2api
|
sudo systemctl restart sub2api
|
||||||
|
|
||||||
# Uninstall
|
# Uninstall
|
||||||
curl -sSL https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/deploy/install.sh | sudo bash -s uninstall
|
curl -sSL https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/deploy/install.sh | sudo bash -s -- uninstall -y
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ sudo journalctl -u sub2api -f
|
|||||||
sudo systemctl restart sub2api
|
sudo systemctl restart sub2api
|
||||||
|
|
||||||
# 卸载
|
# 卸载
|
||||||
curl -sSL https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/deploy/install.sh | sudo bash -s uninstall
|
curl -sSL https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/deploy/install.sh | sudo bash -s -- uninstall -y
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -235,6 +235,18 @@ func registerRoutes(r *gin.Engine, h *handler.Handlers, s *service.Services, rep
|
|||||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Setup status endpoint (always returns needs_setup: false in normal mode)
|
||||||
|
// This is used by the frontend to detect when the service has restarted after setup
|
||||||
|
r.GET("/setup/status", func(c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"code": 0,
|
||||||
|
"data": gin.H{
|
||||||
|
"needs_setup": false,
|
||||||
|
"step": "completed",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
// API v1
|
// API v1
|
||||||
v1 := r.Group("/api/v1")
|
v1 := r.Group("/api/v1")
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ package admin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"sub2api/internal/pkg/response"
|
"sub2api/internal/pkg/response"
|
||||||
|
"sub2api/internal/pkg/sysutil"
|
||||||
"sub2api/internal/service"
|
"sub2api/internal/service"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -72,10 +74,14 @@ func (h *SystemHandler) Rollback(c *gin.Context) {
|
|||||||
// RestartService restarts the systemd service
|
// RestartService restarts the systemd service
|
||||||
// POST /api/v1/admin/system/restart
|
// POST /api/v1/admin/system/restart
|
||||||
func (h *SystemHandler) RestartService(c *gin.Context) {
|
func (h *SystemHandler) RestartService(c *gin.Context) {
|
||||||
if err := h.updateSvc.RestartService(); err != nil {
|
// Schedule service restart in background after sending response
|
||||||
response.Error(c, http.StatusInternalServerError, err.Error())
|
// This ensures the client receives the success response before the service restarts
|
||||||
return
|
go func() {
|
||||||
}
|
// Wait a moment to ensure the response is sent
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
sysutil.RestartServiceAsync()
|
||||||
|
}()
|
||||||
|
|
||||||
response.Success(c, gin.H{
|
response.Success(c, gin.H{
|
||||||
"message": "Service restart initiated",
|
"message": "Service restart initiated",
|
||||||
})
|
})
|
||||||
|
|||||||
51
backend/internal/pkg/sysutil/restart.go
Normal file
51
backend/internal/pkg/sysutil/restart.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package sysutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
const serviceName = "sub2api"
|
||||||
|
|
||||||
|
// RestartService triggers a service restart via systemd.
|
||||||
|
//
|
||||||
|
// IMPORTANT: This function initiates the restart and returns immediately.
|
||||||
|
// The actual restart happens asynchronously - the current process will be killed
|
||||||
|
// by systemd and a new process will be started.
|
||||||
|
//
|
||||||
|
// We use Start() instead of Run() because:
|
||||||
|
// - systemctl restart will kill the current process first
|
||||||
|
// - Run() waits for completion, but the process dies before completion
|
||||||
|
// - Start() spawns the command independently, allowing systemd to handle the full cycle
|
||||||
|
//
|
||||||
|
// Prerequisites:
|
||||||
|
// - Linux OS with systemd
|
||||||
|
// - NOPASSWD sudo access configured (install.sh creates /etc/sudoers.d/sub2api)
|
||||||
|
func RestartService() error {
|
||||||
|
if runtime.GOOS != "linux" {
|
||||||
|
return fmt.Errorf("systemd restart only available on Linux")
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Initiating service restart...")
|
||||||
|
|
||||||
|
// The sub2api user has NOPASSWD sudo access for systemctl commands
|
||||||
|
// (configured by install.sh in /etc/sudoers.d/sub2api).
|
||||||
|
cmd := exec.Command("sudo", "systemctl", "restart", serviceName)
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
return fmt.Errorf("failed to initiate service restart: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Service restart initiated successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RestartServiceAsync is a fire-and-forget version of RestartService.
|
||||||
|
// It logs errors instead of returning them, suitable for goroutine usage.
|
||||||
|
func RestartServiceAsync() {
|
||||||
|
if err := RestartService(); err != nil {
|
||||||
|
log.Printf("Service restart failed: %v", err)
|
||||||
|
log.Println("Please restart the service manually: sudo systemctl restart sub2api")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -244,23 +243,6 @@ func (s *UpdateService) Rollback() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RestartService triggers a service restart via systemd
|
|
||||||
func (s *UpdateService) RestartService() error {
|
|
||||||
if runtime.GOOS != "linux" {
|
|
||||||
return fmt.Errorf("systemd restart only available on Linux")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try direct systemctl first (works if running as root or with proper permissions)
|
|
||||||
cmd := exec.Command("systemctl", "restart", "sub2api")
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
// Try with sudo (requires NOPASSWD sudoers entry)
|
|
||||||
sudoCmd := exec.Command("sudo", "systemctl", "restart", "sub2api")
|
|
||||||
if sudoErr := sudoCmd.Run(); sudoErr != nil {
|
|
||||||
return fmt.Errorf("systemctl restart failed: %w (sudo also failed: %v)", err, sudoErr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *UpdateService) fetchLatestRelease(ctx context.Context) (*UpdateInfo, error) {
|
func (s *UpdateService) fetchLatestRelease(ctx context.Context) (*UpdateInfo, error) {
|
||||||
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", githubRepo)
|
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", githubRepo)
|
||||||
|
|||||||
@@ -7,8 +7,10 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"sub2api/internal/pkg/response"
|
"sub2api/internal/pkg/response"
|
||||||
|
"sub2api/internal/pkg/sysutil"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -337,8 +339,17 @@ func install(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Schedule service restart in background after sending response
|
||||||
|
// This ensures the client receives the success response before the service restarts
|
||||||
|
go func() {
|
||||||
|
// Wait a moment to ensure the response is sent
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
sysutil.RestartServiceAsync()
|
||||||
|
}()
|
||||||
|
|
||||||
response.Success(c, gin.H{
|
response.Success(c, gin.H{
|
||||||
"message": "Installation completed successfully",
|
"message": "Installation completed successfully. Service will restart automatically.",
|
||||||
"restart": true,
|
"restart": true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed dist/*
|
//go:embed all:dist
|
||||||
var frontendFS embed.FS
|
var frontendFS embed.FS
|
||||||
|
|
||||||
// ServeEmbeddedFrontend returns a Gin handler that serves embedded frontend assets
|
// ServeEmbeddedFrontend returns a Gin handler that serves embedded frontend assets
|
||||||
|
|||||||
@@ -683,7 +683,7 @@ uninstall() {
|
|||||||
# If not interactive (piped), require -y flag or skip confirmation
|
# If not interactive (piped), require -y flag or skip confirmation
|
||||||
if ! is_interactive; then
|
if ! is_interactive; then
|
||||||
if [ "${FORCE_YES:-}" != "true" ]; then
|
if [ "${FORCE_YES:-}" != "true" ]; then
|
||||||
print_error "Non-interactive mode detected. Use 'bash -s -- uninstall -y' to confirm."
|
print_error "Non-interactive mode detected. Use 'curl ... | bash -s -- uninstall -y' to confirm."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -214,12 +214,18 @@
|
|||||||
<!-- Success Message -->
|
<!-- Success Message -->
|
||||||
<div v-if="installSuccess" class="mt-6 p-4 bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800/50 rounded-xl">
|
<div v-if="installSuccess" class="mt-6 p-4 bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800/50 rounded-xl">
|
||||||
<div class="flex items-start gap-3">
|
<div class="flex items-start gap-3">
|
||||||
<svg class="w-5 h-5 text-green-500 flex-shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
|
<svg v-if="!serviceReady" class="animate-spin w-5 h-5 text-green-500 flex-shrink-0" fill="none" viewBox="0 0 24 24">
|
||||||
|
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
|
||||||
|
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
||||||
|
</svg>
|
||||||
|
<svg v-else class="w-5 h-5 text-green-500 flex-shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
|
||||||
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
|
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
|
||||||
</svg>
|
</svg>
|
||||||
<div>
|
<div>
|
||||||
<p class="text-sm font-medium text-green-700 dark:text-green-400">Installation completed!</p>
|
<p class="text-sm font-medium text-green-700 dark:text-green-400">Installation completed!</p>
|
||||||
<p class="text-sm text-green-600 dark:text-green-500 mt-1">Please restart the service to apply changes.</p>
|
<p class="text-sm text-green-600 dark:text-green-500 mt-1">
|
||||||
|
{{ serviceReady ? 'Redirecting to login page...' : 'Service is restarting, please wait...' }}
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -290,6 +296,7 @@ const dbConnected = ref(false);
|
|||||||
const redisConnected = ref(false);
|
const redisConnected = ref(false);
|
||||||
const installing = ref(false);
|
const installing = ref(false);
|
||||||
const confirmPassword = ref('');
|
const confirmPassword = ref('');
|
||||||
|
const serviceReady = ref(false);
|
||||||
|
|
||||||
// Get current server port from browser location (set by install.sh)
|
// Get current server port from browser location (set by install.sh)
|
||||||
const getCurrentPort = (): number => {
|
const getCurrentPort = (): number => {
|
||||||
@@ -390,6 +397,8 @@ async function performInstall() {
|
|||||||
try {
|
try {
|
||||||
await install(formData);
|
await install(formData);
|
||||||
installSuccess.value = true;
|
installSuccess.value = true;
|
||||||
|
// Start polling for service restart
|
||||||
|
waitForServiceRestart();
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
const err = error as { response?: { data?: { detail?: string } }; message?: string };
|
const err = error as { response?: { data?: { detail?: string } }; message?: string };
|
||||||
errorMessage.value = err.response?.data?.detail || err.message || 'Installation failed';
|
errorMessage.value = err.response?.data?.detail || err.message || 'Installation failed';
|
||||||
@@ -397,4 +406,52 @@ async function performInstall() {
|
|||||||
installing.value = false;
|
installing.value = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wait for service to restart and become available
|
||||||
|
async function waitForServiceRestart() {
|
||||||
|
const maxAttempts = 30; // 30 attempts, ~30 seconds max
|
||||||
|
const interval = 1000; // 1 second between attempts
|
||||||
|
|
||||||
|
// Wait a moment for the service to start restarting
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||||
|
|
||||||
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
||||||
|
try {
|
||||||
|
// Try to access the health endpoint
|
||||||
|
const response = await fetch('/health', {
|
||||||
|
method: 'GET',
|
||||||
|
cache: 'no-store'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
// Service is up, check if setup is no longer needed
|
||||||
|
const statusResponse = await fetch('/setup/status', {
|
||||||
|
method: 'GET',
|
||||||
|
cache: 'no-store'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (statusResponse.ok) {
|
||||||
|
const data = await statusResponse.json();
|
||||||
|
// If needs_setup is false, service has restarted in normal mode
|
||||||
|
if (data.data && !data.data.needs_setup) {
|
||||||
|
serviceReady.value = true;
|
||||||
|
// Redirect to login page after a short delay
|
||||||
|
setTimeout(() => {
|
||||||
|
window.location.href = '/login';
|
||||||
|
}, 1500);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Service not ready yet, continue polling
|
||||||
|
}
|
||||||
|
|
||||||
|
await new Promise(resolve => setTimeout(resolve, interval));
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we reach here, service didn't restart in time
|
||||||
|
// Show a message to refresh manually
|
||||||
|
errorMessage.value = 'Service restart is taking longer than expected. Please refresh the page manually.';
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user