- Go + Gin + html/template 服务端渲染 - 主页:Google 风格搜索框 + 导航卡片 - 后台:卡片 CRUD、搜索引擎配置、主页背景/标题配置 - 图片上传:支持 jpg/jpeg/png/gif,自动压缩,缩略图参数 ?thumb=1 - 安全:登录日志、修改密码、IP 自动封禁、IP 白名单 - 访问统计:主页访问/卡片点击/搜索追踪、实时流量、IP 统计 - SQLite 存储(modernc.org/sqlite,纯 Go) - 内存 Session + bcrypt 密码哈希
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package models
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"simple_portal/database"
|
|
)
|
|
|
|
// Predefined search engine URL templates.
|
|
const (
|
|
SearchEngineGoogle = "https://www.google.com/search?q=%s"
|
|
SearchEngineBing = "https://www.bing.com/search?q=%s"
|
|
SearchEngineBaidu = "https://www.baidu.com/s?wd=%s"
|
|
)
|
|
|
|
// SettingKey constants.
|
|
const (
|
|
SettingKeySearchEngine = "search_engine"
|
|
SettingKeyHomepageTitle = "homepage_title"
|
|
SettingKeyHomepageSubtitle = "homepage_subtitle"
|
|
SettingKeyHomepageBackground = "homepage_background"
|
|
)
|
|
|
|
// Default setting values.
|
|
const (
|
|
DefaultHomepageTitle = "Portal"
|
|
DefaultHomepageSubtitle = ""
|
|
DefaultHomepageBackground = ""
|
|
)
|
|
|
|
// GetSetting retrieves a setting value by key. Returns empty string if not found.
|
|
func GetSetting(key string) (string, error) {
|
|
var value sql.NullString
|
|
err := database.DB.QueryRow("SELECT value FROM settings WHERE key = ?", key).Scan(&value)
|
|
if err == sql.ErrNoRows {
|
|
return "", nil
|
|
}
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to get setting %s: %w", key, err)
|
|
}
|
|
return value.String, nil
|
|
}
|
|
|
|
// SetSetting inserts or updates a setting value by key.
|
|
func SetSetting(key, value string) error {
|
|
_, err := database.DB.Exec(
|
|
"INSERT INTO settings (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = ?",
|
|
key, value, value,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to set setting %s: %w", key, err)
|
|
}
|
|
return nil
|
|
}
|