diff --git a/api.go b/api.go index 6a5833e..f08096a 100644 --- a/api.go +++ b/api.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" "net/http" "os" "path/filepath" @@ -103,7 +102,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) { // Read sources.json file and return a list of SourceItem. func readSources() []MusicItem { - data, err := ioutil.ReadFile("./sources.json") + data, err := os.ReadFile("./sources.json") fmt.Println("[Info] Reading local sources.json") if err != nil { fmt.Println("[Error] Failed to read sources.json:", err) @@ -124,7 +123,7 @@ func readSources() []MusicItem { func getLocalMusicItem(song, singer string) MusicItem { musicDir := "./files/music" fmt.Println("[Info] Reading local folder music.") - files, err := ioutil.ReadDir(musicDir) + files, err := os.ReadDir(musicDir) if err != nil { fmt.Println("[Error] Failed to read local music directory:", err) return MusicItem{} diff --git a/files/.DS_Store b/files/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/files/.DS_Store differ diff --git a/files/background.webp b/files/background.webp new file mode 100644 index 0000000..6124d9d Binary files /dev/null and b/files/background.webp differ diff --git a/helper.go b/helper.go index f66517c..249c8fb 100644 --- a/helper.go +++ b/helper.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "mime" "net/http" "os" @@ -67,7 +66,7 @@ func createM3U8Playlist(outputDir string) error { } chunkDir := filepath.Join(outputDir, "chunk") - files, err := ioutil.ReadDir(chunkDir) + files, err := os.ReadDir(chunkDir) if err != nil { return err } @@ -223,7 +222,7 @@ func requestAndCacheMusic(song, singer string) { // If no valid music item was found, return an empty MusicItem if musicItem.Title == "" { - fmt.Println("[Warning] No valid music item retrieved.") + fmt.Printf("[Warning] No valid music item retrieved.\n") return } @@ -236,7 +235,7 @@ func requestAndCacheMusic(song, singer string) { fmt.Println("[Error] Error marshalling cache data:", err) return } - err = ioutil.WriteFile(cacheFile, cacheData, 0644) + err = os.WriteFile(cacheFile, cacheData, 0644) if err != nil { fmt.Println("[Error] Error writing cache file:", err) return @@ -247,7 +246,7 @@ func requestAndCacheMusic(song, singer string) { // Helper function to read music data from cache file func readFromCache(filePath string) (MusicItem, bool) { - data, err := ioutil.ReadFile(filePath) + data, err := os.ReadFile(filePath) if err != nil { fmt.Println("[Error] Failed to read cache file:", err) return MusicItem{}, false diff --git a/index.go b/index.go index 0227c13..3daed7c 100644 --- a/index.go +++ b/index.go @@ -3,6 +3,8 @@ package main import ( "fmt" "net/http" + "os" + "path/filepath" ) func indexHandler(w http.ResponseWriter, r *http.Request) { @@ -13,5 +15,361 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { fileHandler(w, r) return } - fmt.Fprintf(w, "

音乐服务器

") + // Serve index.html in theme directory + indexPath := filepath.Join("theme", "index.html") + + // Check if index.html exists in theme directory + if _, err := os.Stat(indexPath); os.IsNotExist(err) { + defaultIndexPage(w) + } else if err != nil { + defaultIndexPage(w) + } else { + http.ServeFile(w, r, indexPath) + } +} + +func defaultIndexPage(w http.ResponseWriter) { + websiteVersion := "0.0.1 alpha 20250909" + websiteNameCN := os.Getenv("WEBSITE_NAME_CN") + if websiteNameCN == "" { + websiteNameCN = "🎵 音乐搜索" + } + websiteNameEN := os.Getenv("WEBSITE_NAME_EN") + if websiteNameEN == "" { + websiteNameEN = "🎵 Music Search" + } + websiteTitleCN := os.Getenv("WEBSITE_TITLE_CN") + if websiteTitleCN == "" { + websiteTitleCN = "为嵌入式设备设计的音乐搜索服务器" + } + websiteTitleEN := os.Getenv("WEBSITE_TITLE_EN") + if websiteTitleEN == "" { + websiteTitleEN = "Music Search Server for Embedded Devices" + } + websiteDescCN := os.Getenv("WEBSITE_DESC_CN") + if websiteDescCN == "" { + websiteDescCN = "搜索并播放您喜爱的音乐" + } + websiteDescEN := os.Getenv("WEBSITE_DESC_EN") + if websiteDescEN == "" { + websiteDescEN = "Search and play your favorite music" + } + websiteKeywordsCN := os.Getenv("WEBSITE_KEYWORDS_CN") + if websiteKeywordsCN == "" { + websiteKeywordsCN = "音乐, 搜索, 嵌入式" + } + websiteKeywordsEN := os.Getenv("WEBSITE_KEYWORDS_EN") + if websiteKeywordsEN == "" { + websiteKeywordsEN = "music, search, embedded" + } + websiteFavicon := os.Getenv("WEBSITE_FAVICON") + if websiteFavicon == "" { + websiteFavicon = "/favicon.ico" + } + websiteBackground := os.Getenv("WEBSITE_BACKGROUND") + if websiteBackground == "" { + websiteBackground = "/background.webp" + } + fontawesomeCDN := os.Getenv("FONTAWESOME_CDN") + if fontawesomeCDN == "" { + fontawesomeCDN = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" + } + + // Build HTML + fmt.Fprintf(w, "") + fmt.Fprintf(w, "") + fmt.Fprintf(w, "") + fmt.Fprintf(w, "") + fmt.Fprintf(w, "") + fmt.Fprintf(w, "", websiteFavicon) + fmt.Fprintf(w, "", fontawesomeCDN) + fmt.Fprintf(w, "") + // Build body + fmt.Fprintf(w, "
") + fmt.Fprintf(w, "
") + fmt.Fprintf(w, "
") + fmt.Fprintf(w, "
") + fmt.Fprintf(w, "
") + fmt.Fprintf(w, "
1

") + fmt.Fprintf(w, "
") + fmt.Fprintf(w, "") } diff --git a/files/.gitignore b/themes/.gitignore similarity index 100% rename from files/.gitignore rename to themes/.gitignore diff --git a/yuafengfreeapi.go b/yuafengfreeapi.go index bd981e1..6e663a1 100644 --- a/yuafengfreeapi.go +++ b/yuafengfreeapi.go @@ -3,7 +3,7 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "os" "path/filepath" @@ -43,7 +43,7 @@ func YuafengAPIResponseHandler(sources, song, singer string) MusicItem { return MusicItem{} } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { fmt.Println("[Error] Error reading the response body from Yuafeng free API:", err) return MusicItem{}