feat: app proxy

This commit is contained in:
Simon Ding
2024-07-27 17:18:38 +08:00
parent 5175e651ee
commit feecc9f983
8 changed files with 52 additions and 14 deletions

View File

@@ -8,6 +8,7 @@ import (
func main() {
log.Infof("------------------- Starting Polaris ---------------------")
dbClient, err := db.Open()
if err != nil {
log.Panicf("init db error: %v", err)

View File

@@ -9,6 +9,7 @@ const (
SettingJacketApiKey = "jacket_api_key"
SettingDownloadDir = "download_dir"
SettingLogLevel = "log_level"
SettingProxy = "proxy"
)
const (

View File

@@ -43,6 +43,8 @@ type Server struct {
func (s *Server) Serve() error {
s.scheduler()
s.reloadTasks()
s.restoreProxy()
s.jwtSerect = s.db.GetSetting(db.JwtSerectKey)
//st, _ := fs.Sub(ui.Web, "build/web")
s.r.Use(static.Serve("/", static.EmbedFolder(ui.Web, "build/web")))

View File

@@ -2,6 +2,8 @@ package server
import (
"fmt"
"net/http"
"net/url"
"polaris/db"
"polaris/log"
"polaris/pkg/transmission"
@@ -15,6 +17,7 @@ type GeneralSettings struct {
TmdbApiKey string `json:"tmdb_api_key"`
DownloadDir string `json:"download_dir"`
LogLevel string `json:"log_level"`
Proxy string `json:"proxy"`
}
func (s *Server) SetSetting(c *gin.Context) (interface{}, error) {
@@ -29,6 +32,7 @@ func (s *Server) SetSetting(c *gin.Context) (interface{}, error) {
}
}
if in.DownloadDir != "" {
log.Info("set download dir to %s", in.DownloadDir)
if err := s.db.SetSetting(db.SettingDownloadDir, in.DownloadDir); err != nil {
return nil, errors.Wrap(err, "save download dir")
}
@@ -40,9 +44,30 @@ func (s *Server) SetSetting(c *gin.Context) (interface{}, error) {
}
}
s.setProxy(in.Proxy)
return nil, nil
}
func (s *Server) setProxy(proxy string) {
proxyUrl, err := url.Parse(proxy)
tp := http.DefaultTransport.(*http.Transport)
if proxy == "" || err != nil {
log.Warnf("proxy url not valid, disabling: %v", proxy)
tp.Proxy = nil
s.db.SetSetting(db.SettingProxy, "")
} else {
log.Infof("set proxy to %v", proxy)
tp.Proxy = http.ProxyURL(proxyUrl)
s.db.SetSetting(db.SettingProxy, proxy)
}
}
func (s *Server) restoreProxy() {
p := s.db.GetSetting(db.SettingProxy)
s.setProxy(p)
}
func (s *Server) GetSetting(c *gin.Context) (interface{}, error) {
tmdb := s.db.GetSetting(db.SettingTmdbApiKey)
downloadDir := s.db.GetSetting(db.SettingDownloadDir)
@@ -51,6 +76,7 @@ func (s *Server) GetSetting(c *gin.Context) (interface{}, error) {
TmdbApiKey: tmdb,
DownloadDir: downloadDir,
LogLevel: logLevel,
Proxy: s.db.GetSetting(db.SettingProxy),
}, nil
}

View File

@@ -50,14 +50,17 @@ class GeneralSetting {
String? tmdbApiKey;
String? downloadDIr;
String? logLevel;
String? proxy;
GeneralSetting({this.tmdbApiKey, this.downloadDIr, this.logLevel});
GeneralSetting(
{this.tmdbApiKey, this.downloadDIr, this.logLevel, this.proxy});
factory GeneralSetting.fromJson(Map<String, dynamic> json) {
return GeneralSetting(
tmdbApiKey: json["tmdb_api_key"],
downloadDIr: json["download_dir"],
logLevel: json["log_level"]);
logLevel: json["log_level"],
proxy: json["proxy"]);
}
Map<String, dynamic> toJson() {
@@ -65,6 +68,7 @@ class GeneralSetting {
data['tmdb_api_key'] = tmdbApiKey;
data['download_dir'] = downloadDIr;
data["log_level"] = logLevel;
data["proxy"] = proxy;
return data;
}
}

View File

@@ -36,14 +36,14 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
initialValue: {
"tmdb_api": v.tmdbApiKey,
"download_dir": v.downloadDIr,
"log_level": v.logLevel
"log_level": v.logLevel,
"proxy": v.proxy,
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
FormBuilderTextField(
name: "tmdb_api",
autofocus: true,
decoration: Commons.requiredTextFieldStyle(
text: "TMDB Api Key", icon: const Icon(Icons.key)),
//
@@ -51,12 +51,16 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
),
FormBuilderTextField(
name: "download_dir",
autofocus: true,
decoration: Commons.requiredTextFieldStyle(
text: "下载路径", icon: const Icon(Icons.folder)),
text: "下载路径", icon: const Icon(Icons.folder), helperText: "媒体文件临时下载路径,非最终存储路径"),
//
validator: FormBuilderValidators.required(),
),
FormBuilderTextField(
name: "proxy",
decoration: const InputDecoration(
labelText: "代理地址", icon: Icon(Icons.folder), helperText: "后台联网代理地址,留空表示不启用代理"),
),
SizedBox(
width: 300,
child: FormBuilderDropdown(
@@ -66,13 +70,10 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
icon: Icon(Icons.file_present_rounded),
),
items: const [
DropdownMenuItem(
value: "debug", child: Text("DEBUG")),
DropdownMenuItem(value: "debug", child: Text("DEBUG")),
DropdownMenuItem(value: "info", child: Text("INFO")),
DropdownMenuItem(
value: "warn", child: Text("WARNING")),
DropdownMenuItem(
value: "error", child: Text("ERROR")),
DropdownMenuItem(value: "warn", child: Text("WARN")),
DropdownMenuItem(value: "error", child: Text("ERROR")),
],
validator: FormBuilderValidators.required(),
),
@@ -93,7 +94,8 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
.updateSettings(GeneralSetting(
tmdbApiKey: values["tmdb_api"],
downloadDIr: values["download_dir"],
logLevel: values["log_level"]));
logLevel: values["log_level"],
proxy: values["proxy"]));
f.then((v) {
Utils.showSnakeBar("更新成功");
}).onError((e, s) {

View File

@@ -37,7 +37,7 @@ class _SystemPageState extends ConsumerState<SystemPage> {
columns: const [
DataColumn(label: Text("日志")),
DataColumn(label: Text("大小")),
DataColumn(label: Text("*"))
DataColumn(label: Text("下载"))
],
rows: List.generate(list.length, (i) {
final item = list[i];

View File

@@ -4,8 +4,10 @@ class Commons {
static InputDecoration requiredTextFieldStyle({
required String text,
Widget? icon,
String? helperText,
}) {
return InputDecoration(
helperText: helperText,
label: Row(
children: [
Text(text),