This commit is contained in:
2025-03-26 00:42:53 +08:00
commit 10e8512723
8 changed files with 705 additions and 0 deletions

12
Makefile Normal file
View File

@ -0,0 +1,12 @@
.PHONY: build run clean
build:
cd backend && \
GOOS=linux GOARCH=amd64 go build -o ../bin/navigation-server
run: build
./bin/navigation-server
clean:
rm -f bin/navigation-server
rm -f data/navigation.db

187
backend/main.go Normal file
View File

@ -0,0 +1,187 @@
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
"gopkg.in/yaml.v2"
)
type Config struct {
Server struct {
Port string `yaml:"port"`
} `yaml:"server"`
Database struct {
Path string `yaml:"path"`
} `yaml:"database"`
}
type Site struct {
ID int `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
Icon string `json:"icon,omitempty"`
Category string `json:"category"`
}
var db *sql.DB
func main() {
// 加载配置
config, err := loadConfig("config/config.yaml")
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
// 初始化数据库
err = initDB(config.Database.Path)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// 设置路由
http.HandleFunc("/api/sites", sitesHandler)
http.HandleFunc("/api/add", addSiteHandler)
http.HandleFunc("/api/delete", deleteSiteHandler)
// 静态文件服务
fs := http.FileServer(http.Dir("../frontend"))
http.Handle("/", fs)
// 启动服务器
log.Printf("Starting server on :%s", config.Server.Port)
log.Fatal(http.ListenAndServe(":"+config.Server.Port, nil))
}
func loadConfig(path string) (*Config, error) {
config := &Config{}
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
d := yaml.NewDecoder(file)
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}
func initDB(dbPath string) error {
var err error
// 确保数据库目录存在
os.MkdirAll(filepath.Dir(dbPath), 0755)
db, err = sql.Open("sqlite3", dbPath)
if err != nil {
return err
}
// 创建表
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS sites (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
url TEXT NOT NULL,
icon TEXT,
category TEXT NOT NULL
)`)
return err
}
func sitesHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
getSites(w, r)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func getSites(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query("SELECT id, name, url, icon, category FROM sites")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer rows.Close()
var sites []Site
for rows.Next() {
var s Site
if err := rows.Scan(&s.ID, &s.Name, &s.URL, &s.Icon, &s.Category); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sites = append(sites, s)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(sites)
}
func addSiteHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var s Site
if err := json.NewDecoder(r.Body).Decode(&s); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// 验证数据
if s.Name == "" || s.URL == "" || s.Category == "" {
http.Error(w, "Name, URL and Category are required", http.StatusBadRequest)
return
}
// 插入数据库
res, err := db.Exec("INSERT INTO sites (name, url, icon, category) VALUES (?, ?, ?, ?)",
s.Name, s.URL, s.Icon, s.Category)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
id, _ := res.LastInsertId()
s.ID = int(id)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(s)
}
func deleteSiteHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var request struct {
ID int `json:"id"`
}
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
_, err := db.Exec("DELETE FROM sites WHERE id = ?", request.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"status": "success"}`)
}

5
config/config.yaml Normal file
View File

@ -0,0 +1,5 @@
server:
port: "8080" # 服务端口
database:
path: "data/navigation.db" # 数据库文件路径

86
frontend/index.html Normal file
View File

@ -0,0 +1,86 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>我的导航网站</title>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
</head>
<body>
<div class="container">
<header>
<div class="header-left">
<h1>我的导航网站</h1>
<div class="search-box">
<input type="text" id="searchInput" placeholder="搜索网站...">
<i class="fas fa-search"></i>
</div>
</div>
<div class="header-right">
<div class="theme-switcher">
<button id="themeBtn"><i class="fas fa-moon"></i></button>
</div>
<div class="actions">
<button id="addSiteBtn"><i class="fas fa-plus"></i> 添加网站</button>
<button id="manageCategoriesBtn"><i class="fas fa-tags"></i> 管理分类</button>
<button id="exportBtn"><i class="fas fa-file-export"></i> 导出</button>
<button id="importBtn"><i class="fas fa-file-import"></i> 导入</button>
<input type="file" id="importFile" style="display: none;" accept=".json">
</div>
</div>
</header>
<div class="categories" id="categories">
<!-- 分类将通过JavaScript动态生成 -->
</div>
<!-- 添加网站的模态框 -->
<div class="modal" id="addSiteModal">
<div class="modal-content">
<span class="close">&times;</span>
<h2><i class="fas fa-plus-circle"></i> 添加新网站</h2>
<form id="addSiteForm">
<div class="form-group">
<label for="siteName"><i class="fas fa-heading"></i> 网站名称</label>
<input type="text" id="siteName" required>
</div>
<div class="form-group">
<label for="siteUrl"><i class="fas fa-link"></i> 网站URL</label>
<input type="url" id="siteUrl" required>
</div>
<div class="form-group">
<label for="siteCategory"><i class="fas fa-tag"></i> 分类</label>
<select id="siteCategory" required>
<!-- 分类选项将通过JavaScript动态生成 -->
</select>
</div>
<div class="form-group">
<label for="siteIcon"><i class="fas fa-image"></i> 图标URL (可选)</label>
<input type="url" id="siteIcon" placeholder="https://example.com/favicon.ico">
</div>
<button type="submit"><i class="fas fa-save"></i> 保存</button>
</form>
</div>
</div>
<!-- 管理分类的模态框 -->
<div class="modal" id="manageCategoriesModal">
<div class="modal-content">
<span class="close">&times;</span>
<h2><i class="fas fa-tags"></i> 管理分类</h2>
<div class="category-list" id="categoryList">
<!-- 分类列表将通过JavaScript动态生成 -->
</div>
<div class="add-category">
<input type="text" id="newCategoryName" placeholder="新分类名称">
<button id="addCategoryBtn"><i class="fas fa-plus"></i> 添加分类</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/sortablejs@1.14.0/Sortable.min.js"></script>
<script src="script.js"></script>
</body>
</html>

0
frontend/script.js Normal file
View File

401
frontend/styles.css Normal file
View File

@ -0,0 +1,401 @@
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--danger-color: #e74c3c;
--text-color: #333;
--bg-color: #f5f5f5;
--card-color: #fff;
--border-color: #ddd;
--shadow-color: rgba(0, 0, 0, 0.1);
}
[data-theme="dark"] {
--primary-color: #2980b9;
--secondary-color: #27ae60;
--danger-color: #c0392b;
--text-color: #f5f5f5;
--bg-color: #121212;
--card-color: #1e1e1e;
--border-color: #333;
--shadow-color: rgba(0, 0, 0, 0.3);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Arial', sans-serif;
transition: background-color 0.3s, color 0.3s;
}
body {
background-color: var(--bg-color);
color: var(--text-color);
line-height: 1.6;
}
.container {
max-width: 1400px;
margin: 0 auto;
padding: 20px;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
padding-bottom: 20px;
border-bottom: 1px solid var(--border-color);
flex-wrap: wrap;
}
.header-left, .header-right {
display: flex;
align-items: center;
gap: 20px;
}
h1 {
font-size: 2rem;
color: var(--primary-color);
}
.search-box {
position: relative;
min-width: 250px;
}
.search-box input {
width: 100%;
padding: 8px 15px 8px 35px;
border: 1px solid var(--border-color);
border-radius: 20px;
background-color: var(--card-color);
color: var(--text-color);
}
.search-box i {
position: absolute;
left: 12px;
top: 50%;
transform: translateY(-50%);
color: var(--text-color);
opacity: 0.7;
}
.actions {
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.actions button {
padding: 8px 16px;
background-color: var(--primary-color);
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
display: flex;
align-items: center;
gap: 5px;
}
.actions button:hover {
opacity: 0.9;
}
.actions button i {
font-size: 0.9rem;
}
.theme-switcher button {
background: none;
border: none;
color: var(--text-color);
font-size: 1.2rem;
cursor: pointer;
padding: 5px;
}
.categories {
display: flex;
flex-direction: column;
gap: 30px;
}
.category {
background-color: var(--card-color);
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 5px var(--shadow-color);
}
.category h2 {
margin-bottom: 15px;
color: var(--primary-color);
padding-bottom: 10px;
border-bottom: 1px solid var(--border-color);
display: flex;
align-items: center;
gap: 10px;
}
.sites {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
gap: 15px;
}
.site {
display: flex;
flex-direction: column;
align-items: center;
text-decoration: none;
color: var(--text-color);
transition: transform 0.2s;
position: relative;
padding: 10px;
border-radius: 8px;
background-color: var(--bg-color);
}
.site:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px var(--shadow-color);
}
.site-icon {
width: 40px;
height: 40px;
margin-bottom: 8px;
border-radius: 50%;
object-fit: cover;
background-color: var(--border-color);
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
color: var(--primary-color);
}
.site-name {
text-align: center;
font-size: 0.9rem;
word-break: break-word;
width: 100%;
}
.delete-btn {
position: absolute;
top: -8px;
right: -8px;
background-color: var(--danger-color);
color: white;
border: none;
border-radius: 50%;
width: 20px;
height: 20px;
font-size: 12px;
cursor: pointer;
display: none;
}
.site:hover .delete-btn {
display: flex;
align-items: center;
justify-content: center;
}
/* 模态框样式 */
.modal {
display: none;
position: fixed;
z-index: 100;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
}
.modal-content {
background-color: var(--card-color);
margin: 10% auto;
padding: 25px;
border-radius: 8px;
width: 90%;
max-width: 500px;
box-shadow: 0 4px 8px var(--shadow-color);
position: relative;
max-height: 80vh;
overflow-y: auto;
}
.modal h2 {
margin-bottom: 20px;
color: var(--primary-color);
display: flex;
align-items: center;
gap: 10px;
}
.close {
position: absolute;
top: 15px;
right: 20px;
font-size: 28px;
font-weight: bold;
color: var(--text-color);
cursor: pointer;
opacity: 0.7;
}
.close:hover {
opacity: 1;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
display: flex;
align-items: center;
gap: 8px;
}
.form-group input,
.form-group select {
width: 100%;
padding: 10px;
border: 1px solid var(--border-color);
border-radius: 4px;
background-color: var(--bg-color);
color: var(--text-color);
}
form button {
background-color: var(--secondary-color);
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
}
form button:hover {
opacity: 0.9;
}
/* 分类管理样式 */
.category-list {
margin-bottom: 20px;
max-height: 300px;
overflow-y: auto;
}
.category-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
margin-bottom: 10px;
background-color: var(--bg-color);
border-radius: 4px;
}
.category-item span {
flex-grow: 1;
}
.category-item button {
background-color: var(--danger-color);
color: white;
border: none;
border-radius: 4px;
padding: 5px 10px;
cursor: pointer;
}
.add-category {
display: flex;
gap: 10px;
}
.add-category input {
flex-grow: 1;
padding: 10px;
border: 1px solid var(--border-color);
border-radius: 4px;
background-color: var(--bg-color);
color: var(--text-color);
}
.add-category button {
background-color: var(--secondary-color);
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
display: flex;
align-items: center;
gap: 5px;
}
/* 拖拽样式 */
.sortable-ghost {
opacity: 0.5;
background: var(--primary-color);
}
.sortable-chosen {
box-shadow: 0 0 10px var(--shadow-color);
}
/* 响应式设计 */
@media (max-width: 768px) {
header {
flex-direction: column;
align-items: stretch;
gap: 15px;
}
.header-left, .header-right {
flex-direction: column;
align-items: stretch;
gap: 15px;
}
.search-box {
width: 100%;
}
.actions {
justify-content: space-between;
}
.actions button {
flex-grow: 1;
justify-content: center;
}
.sites {
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
}
.add-category {
flex-direction: column;
}
}

8
go.mod Normal file
View File

@ -0,0 +1,8 @@
module navigation/backend
go 1.24.1
require (
github.com/mattn/go-sqlite3 v1.14.24
gopkg.in/yaml.v2 v2.4.0
)

6
go.sum Normal file
View File

@ -0,0 +1,6 @@
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=