You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.2 KiB
54 lines
1.2 KiB
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
var (
|
|
downloadDir = flag.String("dir", "./downloads", "下载目录")
|
|
port = flag.String("port", "55830", "服务端口")
|
|
)
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
// 创建下载目录
|
|
if err := os.MkdirAll(*downloadDir, 0755); err != nil {
|
|
log.Fatal("创建下载目录失败:", err)
|
|
}
|
|
|
|
// 设置路由
|
|
http.HandleFunc("/api/save_imgs", saveImagesHandler)
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(w, r, "index.html")
|
|
})
|
|
|
|
// 允许跨域
|
|
http.HandleFunc("/api/", enableCORS(saveImagesHandler))
|
|
|
|
fmt.Printf("HD4K下载服务启动\n")
|
|
fmt.Printf("下载目录: %s\n", *downloadDir)
|
|
fmt.Printf("API地址: http://127.0.0.1:%s/api/save_imgs\n", *port)
|
|
fmt.Printf("按 Ctrl+C 停止服务\n")
|
|
|
|
log.Fatal(http.ListenAndServe(":"+*port, nil))
|
|
}
|
|
|
|
func enableCORS(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
|
|
if r.Method == "OPTIONS" {
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
|
|
next(w, r)
|
|
}
|
|
}
|
|
|