-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
149 lines (113 loc) · 2.69 KB
/
app.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"errors"
"log"
"os"
"runtime"
"strings"
"github.com/gocolly/colly/v2"
)
const (
SJS = "sjs"
ASB = "asb"
HNZ = "hnz"
MTUP = "mtup"
PROD = "prod"
DEV = "dev"
METHOD_POST = "POST"
METHOD_GET = "GET"
)
type ISource interface {
GetRequestData() []byte
GetRequestMethod() string
GetQuery() string
GetOnScrapedHandler(data []byte) []IEntity
}
type IJsonSource interface {
ISource
GetRequestData() []byte
}
type IHtmlSource interface {
ISource
GetContainer() string
GetOnHtmlHandler(e *colly.HTMLElement)
}
type ISourceCollector interface {
Collect()
GetEntities() []IEntity
}
type IEntity interface {
GetLink() string
GetTitle() string
GetDescription() string
GetType() string
}
func runDomain(collector ISourceCollector, channel chan<- []IEntity) {
runtime.LockOSThread()
defer wg.Done()
collector.Collect()
entities := collector.GetEntities()
channel <- entities
runtime.UnlockOSThread()
}
func getDomainConfig(domain string) CollectorConfig {
prefix := strings.ToUpper(domain)
config := CollectorConfig{
AllowedDomains: os.Getenv(prefix + "_ALLOWED_DOMAINS"),
CacheDir: os.Getenv(prefix + "_CACHE_DIR"),
Env: os.Getenv("APP_ENV"),
Route: os.Getenv(prefix + "_ROUTE"),
Domain: domain,
}
if config.Route == "" {
log.Printf("Route for domain %s is empty", domain)
}
return config
}
func run() []IEntity {
domains, workers := getDomains()
channel := make(chan []IEntity, workers)
wg.Add(workers)
for _, domain := range domains {
config := getDomainConfig(domain)
collector, err := CollectorResolver(&config)
if err != nil {
log.Panic(err)
continue
}
if collector == nil {
log.Panic("Unsupported source")
continue
}
go runDomain(collector, channel)
}
wg.Wait()
close(channel)
entities := make([]IEntity, 0)
for j := range channel {
entities = append(entities, j...)
}
return entities
}
func getDomains() ([]string, int) {
activeDomains := os.Getenv("ACTIVE_DOMAINS")
domains := strings.Split(activeDomains, ",")
return domains, len(domains)
}
func CollectorResolver(collectorConfig *CollectorConfig) (ISourceCollector, error) {
switch collectorConfig.Domain {
case MTUP:
source := MtapMakeSource()
return MakeSourceJsonCollector(source, collectorConfig), nil
case SJS:
source := SjsMakeSource()
return MakeSourceJsonCollector(source, collectorConfig), nil
case HNZ:
source := HnzMakeSource()
return MakeSourceJsonCollector(source, collectorConfig), nil
case ASB:
source := AsbMakeSource()
return MakeSourcePaginationalHtmlCollector(source, collectorConfig), nil
}
return nil, errors.New("unsupported source")
}