-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
104 lines (80 loc) · 1.82 KB
/
main.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
package main
import (
"github.com/sausozluk/sozluk-etl/helpers"
"github.com/sausozluk/sozluk-etl/models"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"log"
"io/ioutil"
"gopkg.in/yaml.v2"
"os"
"sync"
"fmt"
)
type SozlukData struct {
entries []models.Entry
topics []models.Topic
}
type Config struct {
Server string `yaml:"server"`
Database string `yaml:"database"`
}
func main() {
config := GetConfig()
s := helpers.SozlukDB{Server: config.Server, Database: config.Database}
db := s.InitMongo()
sozlukData := SozlukData{}
err := FetchFromMongoDB(db, &sozlukData)
if err != nil {
log.Fatal(err)
} else {
GoParallel(&sozlukData)
}
}
func GoParallel(sozlukData *SozlukData) {
entryLen := len((*sozlukData).entries)
var wg sync.WaitGroup
wg.Add(entryLen)
for i := 0; i < entryLen; i++ {
go ProcessData(i, sozlukData, &wg)
}
wg.Wait()
}
func ProcessData(index int, sozlukData *SozlukData, wg *sync.WaitGroup) {
defer wg.Done()
entry := sozlukData.entries[index]
fmt.Printf("Index: %v, EntryID: %v\n", index, entry.EntryID)
}
func FetchFromMongoDB(db *mgo.Database, data *SozlukData) error {
entries := db.C("entries")
topics := db.C("topics")
fetchEntriesErr := entries.Find(bson.M{}).All(&data.entries)
fetchTopicsErr := topics.Find(bson.M{}).All(&data.topics)
if fetchEntriesErr != nil {
return fetchEntriesErr
}
if fetchTopicsErr != nil {
return fetchTopicsErr
}
return nil
}
func GetConfig() Config {
config := Config{}
env := GetSozlukEnv()
yamlFile, err := ioutil.ReadFile("configs/" + env + ".yaml")
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
return config
}
func GetSozlukEnv() string {
env := os.Getenv("SOZLUK_ENV")
if env == "" {
env = "local"
}
return env
}