Files
software-station/internal/config/config.go
Sudo-Ivan d8748bba77
All checks were successful
CI / build (push) Successful in 1m8s
renovate / renovate (push) Successful in 1m42s
Update asset caching and documentation features
- Updated the API server to support asset caching with a new flag for enabling/disabling caching.
- Implemented asset caching logic in the DownloadProxyHandler to store and retrieve assets efficiently.
- Added tests for asset caching functionality, ensuring proper behavior for cache hits and misses.
- Introduced new documentation files for software, including multi-language support.
- Enhanced the SoftwareCard component to display documentation links for software with available docs.
- Updated the Software model to include a flag indicating the presence of documentation.
- Improved the user interface for documentation navigation and search functionality.
2025-12-27 19:08:36 -06:00

95 lines
2.5 KiB
Go

package config
import (
"bufio"
"io"
"log"
"net/http"
"os"
"strings"
"software-station/internal/cache"
"software-station/internal/gitea"
"software-station/internal/models"
)
func checkDocsExist(repo string) bool {
// Check for .svx or .md files in the frontend docs directory
docsDir := "frontend/src/lib/docs/software"
_, errSvx := os.Stat(docsDir + "/" + repo + ".svx")
_, errMd := os.Stat(docsDir + "/" + repo + ".md")
return errSvx == nil || errMd == nil
}
func LoadSoftware(path, server, token string) []models.Software {
return LoadSoftwareExtended(path, server, token, true)
}
func LoadSoftwareExtended(path, server, token string, useCache bool) []models.Software {
var reader io.Reader
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
resp, err := http.Get(path) // #nosec G107
if err != nil {
log.Printf("Error fetching remote config: %v", err)
return nil
}
defer resp.Body.Close()
reader = resp.Body
} else {
file, err := os.Open(path) // #nosec G304
if err != nil {
log.Printf("Warning: config file %s not found", path)
return nil
}
defer file.Close()
reader = file
}
var softwareList []models.Software
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
parts := strings.Split(line, "/")
if len(parts) == 2 {
owner, repo := parts[0], parts[1]
if useCache {
if cached, err := cache.GetFromCache(owner, repo); err == nil {
softwareList = append(softwareList, *cached)
continue
}
}
desc, topics, license, isPrivate, avatarURL, err := gitea.FetchRepoInfo(server, token, owner, repo)
if err != nil {
log.Printf("Error fetching repo info for %s/%s: %v", owner, repo, err)
}
releases, err := gitea.FetchReleases(server, token, owner, repo)
if err != nil {
log.Printf("Error fetching releases for %s/%s: %v", owner, repo, err)
}
sw := models.Software{
Name: repo,
Owner: owner,
Description: desc,
Releases: releases,
GiteaURL: server,
Topics: topics,
License: license,
IsPrivate: isPrivate,
AvatarURL: avatarURL,
HasDocs: checkDocsExist(repo),
}
softwareList = append(softwareList, sw)
if err := cache.SaveToCache(owner, repo, sw); err != nil {
log.Printf("Error saving to cache for %s/%s: %v", owner, repo, err)
}
}
}
return softwareList
}