-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (93 loc) · 3.08 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
105
106
package main
import ("os"
"log"
"time"
"fmt"
"main/conf"
"main/web/vk"
"main/web/rss"
"main/kernel/cache"
"main/kernel/interception"
"strconv"
"main/kernel/performer/functions"
"main/kernel/performer"
)
//Is log will be written
const isLogFileWritten = false
var (
//New log file name
logFilePath = fmt.Sprintf("%v/%v.log", conf.LogDirPath, strconv.FormatInt(time.Now().Unix(), 10))
//Mandatory files list
pathWays = []struct{
path string
isDir bool
}{
{conf.DataDirPath, true}, // data
{conf.DataDirPath + "/dict.aiml.xml", false}, // data/dict.aiml.xml
{conf.LogDirPath, true}, // data/log
{logFilePath, false}, // data/log/xxxxxxxxxx.log
{conf.CommandsDirPath, true}, // data/functions
{conf.CommandsDirPath + "/help.xml", false}, // data/functions/help.xml
{conf.CommandsDirPath + "/cities.xml", false}, // data/functions/cities.xml
}
)
func main() {
log.Println("[INFO] main.go started")
//Checking files
for _, path := range pathWays {
if _, err := os.Stat(path.path); os.IsNotExist(err) {
if path.isDir { //Create dir if not exist
os.Mkdir(path.path, conf.DataFilePermission)
} else { //Create path if not exist
os.OpenFile(path.path, os.O_CREATE, conf.DataFilePermission)
}
log.Print("[INFO] ", path.path, " has been created") //Log path/dir creation
}
}
logFile, fileOpenError := os.OpenFile(logFilePath, os.O_RDWR, conf.DataFilePermission)
if fileOpenError != nil {
log.Print("[ERROR] [main::main()] Failed to open log path: ", fileOpenError)
}
//noinspection ALL
if isLogFileWritten {
log.Println("[INFO] Output will be redirected to a log path.")
log.SetOutput(logFile) //Redirecting log output
}
//vk API initialization
log.Println("[INFO] Initializing vk api...")
var api vk.Api
api.AccessToken = conf.VkToken
var dataCache cache.DataCache
//Chan initialization
log.Println("[INFO] Initializating chan kit...")
api.InitChanKit()
//RSS path (news, bash, etc) initialization
log.Println("[INFO] Initializing cache...")
dataCache.InitCache()
rss.UpdateRss(&dataCache.RssCache)
//Intercept indications init
indications := interception.Indications{}
indications.Init()
//Runs long poll checking
var lp vk.LongPoll
go lp.Start(api.ChanKit)
//Chan checking
for {
select {
case message := <- lp.NewMessageChan: //New message
log.Println("[INFO] New message detected: ", message)
args := functions.FuncArgs{
ApiChan: api.ChanKit, Message: message,
DataCache: dataCache, InterceptIndications: indications,
} //Creating func params
go performer.Perform(args)
case request := <- api.ChanKit.RequestChan: //New api request
out, err := api.Request(request.Name, request.Params) //Request API method
api.ChanKit.AnswerChan <- vk.Answer{out, err} //Sending API answer back
time.Sleep(time.Second / conf.MaxRequestPerSecond) //Delay
case <- time.After(conf.RssUpdateDelay): //Time to update cache
log.Println("[INFO] Time to update RSS files")
go rss.UpdateRss(&dataCache.RssCache) //Updating RSS in new thread
}
}
}