This commit is contained in:
38
main.go
38
main.go
@@ -616,20 +616,28 @@ func main() {
|
|||||||
outDir = "/lists/"
|
outDir = "/lists/"
|
||||||
}
|
}
|
||||||
|
|
||||||
listFile := flag.String("list", "", "optional text file with URLs or Name|URL pairs (overrides built-in map)")
|
//listFile := flag.String("list", "", "optional text file with URLs or Name|URL pairs (overrides built-in map)")
|
||||||
listenAddr := flag.String("listen", ":8080", "HTTP listen address (e.g. :8080 or 127.0.0.1:8000)")
|
listFile := os.Getenv("LIST")
|
||||||
|
if listFile == "" {
|
||||||
|
listFile = ""
|
||||||
|
}
|
||||||
|
//listenAddr := flag.String("listen", ":8080", "HTTP listen address (e.g. :8080 or 127.0.0.1:8000)")
|
||||||
|
listenAddr := os.Getenv("LISTEN")
|
||||||
|
if listenAddr == "" {
|
||||||
|
listenAddr = ":8080"
|
||||||
|
}
|
||||||
//listenPrefix := flag.String("prefix", "http://importer:8080", "HTTP prefix for url in /lists.json")
|
//listenPrefix := flag.String("prefix", "http://importer:8080", "HTTP prefix for url in /lists.json")
|
||||||
listenPrefix := os.Getenv("PREFIX")
|
listenPrefix := os.Getenv("PREFIX")
|
||||||
if listenPrefix == "" {
|
if listenPrefix == "" {
|
||||||
listenPrefix = "http://flodimporter:8080"
|
listenPrefix = "http://flodimporter:8080"
|
||||||
}
|
}
|
||||||
flag.Parse()
|
//flag.Parse()
|
||||||
|
|
||||||
if *serveonly != "1" {
|
if serveonly != "1" {
|
||||||
if err := os.MkdirAll(*outDir, 0o755); err != nil {
|
if err := os.MkdirAll(outDir, 0o755); err != nil {
|
||||||
fatal("creating output dir", err)
|
fatal("creating output dir", err)
|
||||||
}
|
}
|
||||||
targets, err := buildTargetList(*listFile)
|
targets, err := buildTargetList(listFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatal("building target list", err)
|
fatal("building target list", err)
|
||||||
}
|
}
|
||||||
@@ -638,16 +646,16 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Will fetch %d files, one every %v\n", len(targets), *delay)
|
fmt.Printf("Will fetch %d files, one every %v\n", len(targets), delay)
|
||||||
client := &http.Client{Timeout: 60 * time.Second}
|
client := &http.Client{Timeout: 60 * time.Second}
|
||||||
ticker := time.NewTicker(*delay)
|
ticker := time.NewTicker(delay)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for i, t := range targets {
|
for i, t := range targets {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
<-ticker.C
|
<-ticker.C
|
||||||
}
|
}
|
||||||
if err := fetchAndSave(client, t, *outDir); err != nil {
|
if err := fetchAndSave(client, t, outDir); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "[ERROR] %s → %v\n", t.URL, err)
|
fmt.Fprintf(os.Stderr, "[ERROR] %s → %v\n", t.URL, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -655,12 +663,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Static file handler.
|
// Static file handler.
|
||||||
fs := http.FileServer(http.Dir(*outDir))
|
fs := http.FileServer(http.Dir(outDir))
|
||||||
http.Handle("/lists/", http.StripPrefix("/lists/", fs))
|
http.Handle("/lists/", http.StripPrefix("/lists/", fs))
|
||||||
|
|
||||||
// Custom index that shows a directory listing at root path.
|
// Custom index that shows a directory listing at root path.
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
data, err := buildIndexData(*outDir)
|
data, err := buildIndexData(outDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -674,21 +682,21 @@ func main() {
|
|||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
enc := json.NewEncoder(w)
|
enc := json.NewEncoder(w)
|
||||||
enc.SetIndent("", " ")
|
enc.SetIndent("", " ")
|
||||||
data, err := buildIndexData(*outDir)
|
data, err := buildIndexData(outDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error Build-Index")
|
fmt.Println("Error Build-Index")
|
||||||
}
|
}
|
||||||
d := map[string]string{}
|
d := map[string]string{}
|
||||||
for _, b := range data.Files {
|
for _, b := range data.Files {
|
||||||
d[b.Name] = *listenPrefix + "/lists/" + b.Name
|
d[b.Name] = listenPrefix + "/lists/" + b.Name
|
||||||
}
|
}
|
||||||
if err := enc.Encode(d); err != nil {
|
if err := enc.Encode(d); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Printf("HTTP server listening on %s, serving %s", *listenAddr, *outDir)
|
log.Printf("HTTP server listening on %s, serving %s", listenAddr, outDir)
|
||||||
log.Fatal(http.ListenAndServe(*listenAddr, nil))
|
log.Fatal(http.ListenAndServe(listenAddr, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ──────────────────────────────────────────────────────────────────────────────
|
// ──────────────────────────────────────────────────────────────────────────────
|
||||||
|
Reference in New Issue
Block a user