forked from updevru/polkovnik-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (105 loc) · 2.64 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"embed"
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"os/signal"
"polkovnik/adapter/storage"
"polkovnik/api"
"polkovnik/app"
"polkovnik/job"
"polkovnik/repository"
"syscall"
"time"
)
//go:generate swagger generate spec -o ./ui/public/doc/api/swagger.json
var stdout *bool
var configFile *string
var dbFile *string
var httpPort *string
//go:embed templates
var templates embed.FS
//go:embed ui/build
var UIFiles embed.FS
func init() {
stdout = flag.Bool("o", false, "Send logs to stdout")
configFile = flag.String("c", "var/config.json", "Config file")
dbFile = flag.String("db", "var/data.db", "Database file")
httpPort = flag.String("p", "8080", "HTTP port for UI")
flag.Parse()
log.SetFormatter(&log.TextFormatter{})
if *stdout == true {
log.SetOutput(os.Stdout)
}
}
func runWebServer(port string, repository *repository.Repository, history *repository.HistoryRepository, processor *job.Processor) {
API := api.NewApiHandler(repository, history, processor)
server := http.Server{
Addr: ":" + port,
Handler: api.CreateRouter(API, UIFiles),
}
fmt.Println("Starting server at", port)
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}
func main() {
fmt.Println("Config file: ", *configFile)
configStorage := storage.NewConfigFile(*configFile)
dataRepository := repository.NewRepository(configStorage)
err := dataRepository.Load()
if err != nil {
log.Fatal(err)
return
}
fmt.Println("Database file: ", *dbFile)
historyStorage, err := repository.CreateHistoryRepository(*dbFile)
if err != nil {
log.Fatal(err)
return
}
defer historyStorage.Close()
err = app.Migrate(dataRepository)
if err != nil {
log.Fatal(err)
return
}
processor := job.NewProcessor(app.NewTemplateEngine("templates", templates), dataRepository, historyStorage)
signals := make(chan os.Signal, 1)
exit := make(chan bool, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signals
processor.Stop()
exit <- true
}()
fmt.Println("Run scheduler")
go processor.StartScheduler()
fmt.Println("Run worker")
go processor.StartWorker()
fmt.Println("Run http server")
go runWebServer(*httpPort, dataRepository, historyStorage, processor)
go func() {
ticker := time.NewTicker(time.Minute)
for range ticker.C {
result, err := dataRepository.Flush()
if err != nil {
log.Error(fmt.Sprintf("Save config... Error: %s", err.Error()))
} else if result {
log.Info("Save config... Ok")
}
}
}()
<-exit
fmt.Print("Save config...")
_, err = dataRepository.Flush()
if err != nil {
fmt.Println(err)
}
fmt.Println("OK")
fmt.Println("Buy.")
}