增加Vue包管理界面与更新接口

This commit is contained in:
Scout开发者 committed 2026-08-30 21:50:03 +08:00
1 parent 5ae932f5db
commit 15f4f9ef86
14 files changed
+2199 -8

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
node_modules/
dist/
.env
.env.*
!.env.example
.DS_Store
+13
View File
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#0b1220" />
<title>Scout 包管理中心</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
+1392
View File
File diff suppressed because it is too large. Load diff
+18
View File
@@ -0,0 +1,18 @@
{
"name": "scout-frontend",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.5.18"
},
"devDependencies": {
"@vitejs/plugin-vue": "^6.0.1",
"vite": "^7.1.3"
}
}
+7
View File
@@ -0,0 +1,7 @@
<script setup>
import Home from './views/Home/Home.vue'
</script>
<template>
<Home />
</template>
+5
View File
@@ -0,0 +1,5 @@
import { createApp } from 'vue'
import App from './App.vue'
import './styles.css'
createApp(App).mount('#app')
+21
View File
@@ -0,0 +1,21 @@
:root {
font-family: Inter, "Microsoft YaHei", "微软雅黑", sans-serif;
color: #dce4f2;
background: #0b1220;
font-synthesis: none;
text-rendering: optimizeLegibility;
}
* {
box-sizing: border-box;
}
body {
min-width: 320px;
min-height: 100vh;
margin: 0;
background:
radial-gradient(circle at 12% 5%, rgba(48, 105, 112, 0.28), transparent 30%),
radial-gradient(circle at 90% 30%, rgba(51, 63, 130, 0.2), transparent 34%),
#0b1220;
}
+378
View File
@@ -0,0 +1,378 @@
.page-shell {
width: min(1180px, calc(100% - 40px));
margin: 0 auto;
padding: 48px 0 72px;
}
.topbar {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 24px;
margin-bottom: 28px;
}
.eyebrow,
.section-kicker {
margin: 0 0 10px;
color: #65e6c2;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
}
h1,
h2,
h3,
p {
margin-top: 0;
}
h1 {
max-width: 680px;
margin-bottom: 10px;
color: #f6f7fb;
font-size: clamp(32px, 5vw, 58px);
line-height: 1.06;
letter-spacing: -0.05em;
}
h2 {
margin-bottom: 0;
color: #f7f8fc;
font-size: 22px;
}
h3 {
margin: 28px 0 12px;
color: #d7dfef;
font-size: 14px;
}
.subtitle {
margin-bottom: 0;
color: #8c9ab4;
font-size: 16px;
}
.connection-badge,
.security-label,
.platform-label,
.count-label {
border: 1px solid #27334b;
border-radius: 999px;
color: #9aa8c1;
font-size: 12px;
padding: 8px 12px;
white-space: nowrap;
}
.connection-badge {
display: flex;
align-items: center;
gap: 8px;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #65e6c2;
box-shadow: 0 0 14px #65e6c2;
}
.panel {
border: 1px solid #202c43;
border-radius: 20px;
background: rgba(19, 28, 47, 0.82);
box-shadow: 0 18px 60px rgba(3, 8, 20, 0.22);
}
.toolbar {
display: flex;
align-items: flex-end;
gap: 14px;
margin-bottom: 18px;
padding: 18px;
}
.toolbar label {
flex: 1;
}
label {
display: grid;
gap: 8px;
color: #8c9ab4;
font-size: 12px;
font-weight: 700;
}
input {
width: 100%;
box-sizing: border-box;
border: 1px solid #2a3751;
border-radius: 10px;
background: #10192b;
color: #eaf0fa;
font: inherit;
padding: 12px 13px;
outline: none;
}
input:focus {
border-color: #65e6c2;
box-shadow: 0 0 0 3px rgba(101, 230, 194, 0.12);
}
button,
a {
font: inherit;
}
button {
cursor: pointer;
}
button:disabled {
cursor: wait;
opacity: 0.65;
}
.primary-button,
.secondary-button {
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 10px;
padding: 12px 16px;
text-decoration: none;
font-size: 13px;
font-weight: 800;
transition: transform 160ms ease, opacity 160ms ease;
}
.primary-button:hover,
.secondary-button:hover {
transform: translateY(-1px);
}
.primary-button {
border: 0;
background: #65e6c2;
color: #07151b;
}
.secondary-button {
border: 1px solid #33435f;
background: #17233a;
color: #d8e1f0;
}
.notice {
border-radius: 10px;
margin: 0 0 18px;
padding: 12px 14px;
font-size: 13px;
}
.notice.success {
background: rgba(101, 230, 194, 0.1);
color: #8ff1d6;
}
.notice.error {
background: rgba(255, 122, 144, 0.1);
color: #ff9cac;
}
.content-grid {
display: grid;
grid-template-columns: minmax(280px, 0.82fr) minmax(420px, 1.18fr);
gap: 18px;
}
.package-panel,
.detail-panel,
.upload-panel {
padding: 24px;
}
.panel-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
margin-bottom: 22px;
}
.package-list,
.version-list {
display: grid;
gap: 9px;
}
.package-card {
display: flex;
align-items: center;
gap: 12px;
width: 100%;
border: 1px solid transparent;
border-radius: 13px;
background: #151f34;
color: #e9eff9;
padding: 12px;
text-align: left;
}
.package-card:hover,
.package-card.active {
border-color: #3f806f;
background: #192b3a;
}
.package-icon {
display: grid;
width: 38px;
height: 38px;
flex: 0 0 38px;
place-items: center;
border-radius: 10px;
background: #263c4d;
color: #65e6c2;
font-size: 11px;
font-weight: 900;
}
.package-copy {
display: grid;
min-width: 0;
gap: 5px;
flex: 1;
}
.package-copy strong {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.package-copy small,
.checksum,
.version-row small,
.muted-label {
color: #7f8da7;
font-size: 11px;
}
.version-tag {
color: #65e6c2;
font-size: 12px;
font-weight: 800;
}
.empty-state {
min-height: 160px;
display: grid;
place-items: center;
border: 1px dashed #2b3952;
border-radius: 13px;
color: #7686a1;
font-size: 13px;
text-align: center;
}
.latest-card {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
border-radius: 14px;
background: #17243a;
padding: 18px;
}
.latest-card > div {
display: grid;
gap: 8px;
min-width: 0;
}
.latest-card strong {
color: #f7f8fc;
font-size: 26px;
}
.checksum {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.version-row {
display: grid;
grid-template-columns: 1fr 1.4fr auto;
align-items: center;
gap: 12px;
border-bottom: 1px solid #222e45;
padding: 10px 0;
color: #dce4f2;
font-size: 13px;
}
.version-row a {
color: #65e6c2;
font-size: 12px;
font-weight: 800;
text-decoration: none;
}
.upload-panel {
margin-top: 18px;
}
.upload-layout {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}
.wide-field {
grid-column: span 2;
}
.file-field {
grid-column: span 1;
}
.upload-button {
margin-top: 20px;
}
@media (max-width: 820px) {
.page-shell {
width: min(100% - 24px, 620px);
padding-top: 28px;
}
.topbar,
.toolbar {
align-items: stretch;
flex-direction: column;
}
.content-grid,
.upload-layout {
grid-template-columns: 1fr;
}
.wide-field,
.file-field {
grid-column: span 1;
}
.latest-card {
align-items: stretch;
flex-direction: column;
}
}
+146
View File
@@ -0,0 +1,146 @@
import { ref } from 'vue'
const defaultApi = import.meta.env.VITE_API_BASE || 'http://127.0.0.1:8080'
export function useHome() {
const apiBase = ref(defaultApi)
const developerToken = ref('')
const packages = ref([])
const selectedPackage = ref(null)
const versions = ref([])
const uploadForm = ref({
name: '',
version: '',
description: '',
platform: '',
architecture: '',
file: null,
})
const loading = ref(false)
const uploading = ref(false)
const message = ref('')
const error = ref('')
let eventSource = null
function endpoint(path) {
return `${apiBase.value.replace(/\/$/, '')}${path}`
}
async function readResponse(response) {
const contentType = response.headers.get('content-type') || ''
const payload = contentType.includes('application/json')
? await response.json()
: await response.text()
if (!response.ok) {
throw new Error(payload?.error || payload || `请求失败:${response.status}`)
}
return payload
}
async function loadPackages() {
loading.value = true
error.value = ''
try {
const response = await fetch(endpoint('/api/v1/packages'))
const payload = await readResponse(response)
packages.value = payload.packages || []
} catch (reason) {
error.value = reason.message
} finally {
loading.value = false
}
}
async function selectPackage(item) {
selectedPackage.value = item
error.value = ''
try {
const response = await fetch(endpoint(`/api/v1/packages/${encodeURIComponent(item.name)}/versions`))
const payload = await readResponse(response)
versions.value = payload.versions || []
} catch (reason) {
error.value = reason.message
}
}
function onFileChange(event) {
uploadForm.value.file = event.target.files?.[0] || null
}
async function uploadPackage() {
if (!developerToken.value) {
error.value = '请先填写开发者令牌'
return
}
if (!uploadForm.value.name || !uploadForm.value.version || !uploadForm.value.file) {
error.value = '请填写包名、版本并选择包文件'
return
}
uploading.value = true
error.value = ''
message.value = ''
const formData = new FormData()
Object.entries(uploadForm.value).forEach(([key, value]) => {
if (value) formData.append(key, value)
})
try {
const response = await fetch(endpoint('/api/v1/packages/upload'), {
method: 'POST',
headers: { Authorization: `Bearer ${developerToken.value}` },
body: formData,
})
const payload = await readResponse(response)
message.value = `上传成功:${payload.name} ${payload.version}`
uploadForm.value.version = ''
uploadForm.value.file = null
document.querySelector('#package-file').value = ''
await loadPackages()
} catch (reason) {
error.value = reason.message
} finally {
uploading.value = false
}
}
function downloadUrl(item) {
return endpoint(`/api/v1/packages/${encodeURIComponent(item.name)}/${encodeURIComponent(item.version)}/download`)
}
function startNotifications() {
stopNotifications()
eventSource = new EventSource(endpoint('/api/v1/updates/stream'))
eventSource.addEventListener('package_updated', async (event) => {
const payload = JSON.parse(event.data)
message.value = `收到更新通知:${payload.package.name} ${payload.package.version}`
await loadPackages()
})
eventSource.onerror = () => {
message.value = '更新通知连接暂时中断,页面会继续提供手动刷新'
}
}
function stopNotifications() {
eventSource?.close()
eventSource = null
}
return {
apiBase,
developerToken,
packages,
selectedPackage,
versions,
uploadForm,
loading,
uploading,
message,
error,
loadPackages,
selectPackage,
onFileChange,
uploadPackage,
downloadUrl,
startNotifications,
stopNotifications,
}
}
+164
View File
@@ -0,0 +1,164 @@
<script setup>
import { onMounted, onUnmounted } from 'vue'
import { useHome } from './Home.js'
import './Home.css'
const {
apiBase,
developerToken,
packages,
selectedPackage,
versions,
uploadForm,
loading,
uploading,
message,
error,
loadPackages,
selectPackage,
onFileChange,
uploadPackage,
downloadUrl,
startNotifications,
stopNotifications,
} = useHome()
onMounted(async () => {
await loadPackages()
startNotifications()
})
onUnmounted(stopNotifications)
</script>
<template>
<main class="page-shell">
<header class="topbar">
<div>
<p class="eyebrow">SCOUT PACKAGE SERVICE</p>
<h1>轻量化包管理中心</h1>
<p class="subtitle">开发者上传,客户端发现更新并安全下载。</p>
</div>
<div class="connection-badge">
<span class="status-dot"></span>
<span>实时通知已启用</span>
</div>
</header>
<section class="toolbar panel">
<label>
<span>服务端地址</span>
<input v-model="apiBase" type="url" placeholder="http://127.0.0.1:8080" />
</label>
<button class="secondary-button" type="button" @click="loadPackages">刷新包列表</button>
</section>
<p v-if="message" class="notice success">{{ message }}</p>
<p v-if="error" class="notice error">{{ error }}</p>
<section class="content-grid">
<div class="panel package-panel">
<div class="panel-heading">
<div>
<p class="section-kicker">PACKAGE CATALOG</p>
<h2>包目录</h2>
</div>
<span class="count-label">{{ packages.length }} 个包</span>
</div>
<div v-if="loading" class="empty-state">正在读取包目录……</div>
<div v-else-if="packages.length === 0" class="empty-state">还没有包,上传第一个版本吧。</div>
<div v-else class="package-list">
<button
v-for="item in packages"
:key="item.name"
class="package-card"
:class="{ active: selectedPackage?.name === item.name }"
type="button"
@click="selectPackage(item)"
>
<span class="package-icon">PK</span>
<span class="package-copy">
<strong>{{ item.name }}</strong>
<small>{{ item.description || '暂无描述' }}</small>
</span>
<span class="version-tag">v{{ item.version }}</span>
</button>
</div>
</div>
<div class="panel detail-panel">
<div class="panel-heading">
<div>
<p class="section-kicker">PACKAGE DETAIL</p>
<h2>{{ selectedPackage?.name || '选择一个包' }}</h2>
</div>
<span v-if="selectedPackage" class="platform-label">
{{ selectedPackage.platform || '通用' }} / {{ selectedPackage.architecture || '通用' }}
</span>
</div>
<div v-if="!selectedPackage" class="empty-state">从左侧选择包,查看版本并下载。</div>
<div v-else>
<div class="latest-card">
<div>
<span class="muted-label">最新版本</span>
<strong>v{{ selectedPackage.version }}</strong>
<span class="checksum">SHA-256 {{ selectedPackage.sha256 }}</span>
</div>
<a class="primary-button" :href="downloadUrl(selectedPackage)">下载最新版本</a>
</div>
<h3>历史版本</h3>
<div class="version-list">
<div v-for="item in versions" :key="item.version" class="version-row">
<span>v{{ item.version }}</span>
<small>{{ new Date(item.uploaded_at).toLocaleString('zh-CN') }}</small>
<a :href="downloadUrl(item)">下载</a>
</div>
</div>
</div>
</div>
</section>
<section class="panel upload-panel">
<div class="panel-heading">
<div>
<p class="section-kicker">DEVELOPER UPLOAD</p>
<h2>上传新包</h2>
</div>
<span class="security-label">需要开发者令牌</span>
</div>
<div class="upload-layout">
<label>
<span>开发者令牌</span>
<input v-model="developerToken" type="password" placeholder="输入服务端配置的令牌" />
</label>
<label>
<span>包名</span>
<input v-model="uploadForm.name" type="text" placeholder="例如:demo-core" />
</label>
<label>
<span>版本</span>
<input v-model="uploadForm.version" type="text" placeholder="例如:1.0.0" />
</label>
<label>
<span>平台</span>
<input v-model="uploadForm.platform" type="text" placeholder="例如:windows" />
</label>
<label>
<span>架构</span>
<input v-model="uploadForm.architecture" type="text" placeholder="例如:amd64" />
</label>
<label class="wide-field">
<span>描述</span>
<input v-model="uploadForm.description" type="text" placeholder="简要说明这个包的用途" />
</label>
<label class="file-field wide-field">
<span>包文件</span>
<input id="package-file" type="file" @change="onFileChange" />
</label>
</div>
<button class="primary-button upload-button" type="button" :disabled="uploading" @click="uploadPackage">
{{ uploading ? '正在上传……' : '上传并通知客户端' }}
</button>
</section>
</main>
</template>
+10
View File
@@ -0,0 +1,10 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
server: {
port: 5173,
host: '127.0.0.1',
},
})
+1 -1
View File
@@ -1,3 +1,3 @@
module scout module scout
go 1.26 go 1.26
+16 -7
View File
@@ -2,7 +2,6 @@ package httpapi
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@@ -23,14 +22,19 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("GET /healthz", s.health) mux.HandleFunc("GET /healthz", s.health)
mux.HandleFunc("POST /api/v1/packages/upload", s.upload) mux.HandleFunc("POST /api/v1/packages/upload", s.upload)
mux.HandleFunc("GET /api/v1/updates/stream", s.stream) mux.HandleFunc("GET /api/v1/updates/stream", s.stream)
mux.HandleFunc("GET /api/v1/packages", s.packageList)
mux.HandleFunc("GET /api/v1/packages/", s.packageRoute) mux.HandleFunc("GET /api/v1/packages/", s.packageRoute)
return logging(mux) return cors(logging(mux))
} }
func (s *Server) health(writer http.ResponseWriter, _ *http.Request) { func (s *Server) health(writer http.ResponseWriter, _ *http.Request) {
writeJSON(writer, http.StatusOK, map[string]string{"status": "ok"}) writeJSON(writer, http.StatusOK, map[string]string{"status": "ok"})
} }
func (s *Server) packageList(writer http.ResponseWriter, _ *http.Request) {
writeJSON(writer, http.StatusOK, map[string]any{"packages": s.Registry.List()})
}
func (s *Server) upload(writer http.ResponseWriter, request *http.Request) { func (s *Server) upload(writer http.ResponseWriter, request *http.Request) {
if !s.authorized(request) { if !s.authorized(request) {
writeError(writer, http.StatusUnauthorized, "开发者令牌无效") writeError(writer, http.StatusUnauthorized, "开发者令牌无效")
@@ -111,9 +115,7 @@ func (s *Server) download(writer http.ResponseWriter, request *http.Request, nam
if request.Method == http.MethodHead { if request.Method == http.MethodHead {
return return
} }
if _, err := io.Copy(writer, file); err != nil { _, _ = io.Copy(writer, file)
return
}
} }
func (s *Server) stream(writer http.ResponseWriter, request *http.Request) { func (s *Server) stream(writer http.ResponseWriter, request *http.Request) {
@@ -174,8 +176,11 @@ func writeError(writer http.ResponseWriter, status int, message string) {
writeJSON(writer, status, map[string]string{"error": message}) writeJSON(writer, status, map[string]string{"error": message})
} }
func logging(next http.Handler) http.Handler { func cors(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Access-Control-Allow-Origin", "*")
writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Developer-Token")
writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
if request.Method == http.MethodOptions { if request.Method == http.MethodOptions {
writer.WriteHeader(http.StatusNoContent) writer.WriteHeader(http.StatusNoContent)
return return
@@ -184,4 +189,8 @@ func logging(next http.Handler) http.Handler {
}) })
} }
var _ = errors.New func logging(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
next.ServeHTTP(writer, request)
})
}
+22
View File
@@ -0,0 +1,22 @@
package registry
import "sort"
func (r *Registry) List() []Package {
r.mu.RLock()
defer r.mu.RUnlock()
result := make([]Package, 0, len(r.packages))
for _, versions := range r.packages {
if len(versions) == 0 {
continue
}
keys := make([]string, 0, len(versions))
for version := range versions {
keys = append(keys, version)
}
sort.Slice(keys, func(i, j int) bool { return CompareVersions(keys[i], keys[j]) > 0 })
result = append(result, versions[keys[0]])
}
sort.Slice(result, func(i, j int) bool { return result[i].Name < result[j].Name })
return result
}