-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
65 lines (53 loc) · 1.34 KB
/
utils.go
File metadata and controls
65 lines (53 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"encoding/json"
"errors"
"log"
"net/http"
"os"
"time"
"github.com/gocolly/colly/v2"
)
// Return files for Logging or dumping
func GetFileWrite(fileName string) *os.File {
if fileName == "" {
log.Fatalf("errors.New(\"\"): %v\n", errors.New("WRITE FILE ERROR"))
return nil
}
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatalf("errors.New(\"\"): %v\n", err)
return nil
}
return file
}
func GetCollector() *colly.Collector {
// Instantiate default collector
return colly.NewCollector(
//colly.AllowedDomains("www.oca.org", "https://news.ycombinator.com/"),
// Cache responses to prevent multiple download of pages
// even if the collector is restarted
colly.CacheDir("./cache"),
// Cached responses older than the specified duration will be refreshed
colly.CacheExpiration(24*time.Hour),
)
}
func GetURL(r *http.Request) string {
URL := r.URL.Query().Get("url")
if URL == "" {
missingMsg := "missing URL argument"
log.Println(missingMsg)
return errors.New(missingMsg).Error()
}
log.Println("visiting", URL)
return URL
}
func WriteHttpJson(v any, w http.ResponseWriter) {
b, err := json.Marshal(v)
if err != nil {
log.Println("failed to serialize response:", err)
return
}
w.Header().Add("Content-Type", "application/json")
w.Write(b)
}