-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
superhighway84.go
165 lines (137 loc) · 3.71 KB
/
superhighway84.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"context"
"embed"
"encoding/json"
"net/http"
"net/url"
"os"
"runtime"
"strings"
"time"
"log"
"github.com/mrusme/superhighway84/cache"
"github.com/mrusme/superhighway84/config"
"github.com/mrusme/superhighway84/database"
"github.com/mrusme/superhighway84/models"
"github.com/mrusme/superhighway84/tui"
"go.uber.org/zap"
)
//go:embed superhighway84.jpeg
var EMBEDFS embed.FS
var version = "v0.0.0"
func NewLogger(filename string) (*zap.Logger, error) {
if runtime.GOOS == "windows" {
zap.RegisterSink("winfile", func(u *url.URL) (zap.Sink, error) {
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
})
}
cfg := zap.NewDevelopmentConfig()
if runtime.GOOS == "windows" {
cfg.OutputPaths = []string{
"stdout",
"winfile:///" + filename,
}
} else {
cfg.OutputPaths = []string{
filename,
}
}
return cfg.Build()
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.Println("loading configuration ...")
cfg, err := config.LoadConfig()
if err != nil {
log.Panicln(err)
}
if cfg.WasSetup() == false {
cfg.Setup()
}
log.Println("initializing logger ...")
logger, err := NewLogger(cfg.Logfile)
if err != nil {
log.Panicln(err)
}
log.Println("initializing cache ...")
cch, err := cache.NewCache(cfg.ProgramCachePath)
if err != nil {
log.Panicln(err)
}
defer cch.Close()
var articles []*models.Article
var articlesRoots []*models.Article
log.Println("initializing TUI and loading database, please wait ...")
TUI := tui.Init(&EMBEDFS, cfg, cch, logger)
TUI.SetVersion(strings.TrimLeft(version, "v"), strings.TrimLeft(getLatestVersion(), "v") )
TUI.ArticlesDatasource = &articles
TUI.ArticlesRoots = &articlesRoots
log.Println("initializing database ...")
db, err := database.NewDatabase(ctx, cfg.ConnectionString, cfg.DatabaseCachePath, cch, logger)
if err != nil {
log.Panicln(err)
}
defer db.Disconnect()
TUI.CallbackRefreshArticles = func() (error) {
articles, articlesRoots, err = db.ListArticles()
if err != nil {
logger.Error("%s", zap.Error(err))
}
return err
}
TUI.CallbackSubmitArticle = func(article *models.Article) (error) {
err := db.SubmitArticle(article)
if err != nil {
logger.Error("%s", zap.Error(err))
}
return err
}
log.Println("connecting database ...")
err = db.Connect(func(address string) {
TUI.Meta["myID"] = db.GetOwnID()
TUI.Meta["myPubKey"] = db.GetOwnPubKey()
TUI.Views["mainscreen"].(*tui.Mainscreen).SetFooter(address)
articles, articlesRoots, _ = db.ListArticles()
time.Sleep(time.Second * 2)
TUI.SetView("mainscreen", true)
TUI.RefreshData()
TUI.Refresh()
TUI.App.Draw()
})
if err != nil {
log.Panicln(err)
}
go func() {
peers := 0
for {
bw := db.IPFSNode.Reporter.GetBandwidthTotals()
connections, err := db.IPFSCoreAPI.Swarm().Peers(context.Background())
if err == nil {
peers = len(connections)
} else {
logger.Error("%s", zap.Error(err))
}
TUI.SetStats(int64(peers), int64(bw.RateIn), int64(bw.RateOut), bw.TotalIn , bw.TotalOut)
time.Sleep(time.Second * 5)
}
}()
TUI.Launch()
}
func getLatestVersion() (string) {
var client = &http.Client{Timeout: 10 * time.Second}
r, err := client.Get(
"https://api.github.com/repos/mrusme/superhighway84/releases/latest",
)
if err != nil {
return version
}
defer r.Body.Close()
var result map[string]interface{}
json.NewDecoder(r.Body).Decode(&result)
if val, exist := result["tag_name"]; exist == true {
return val.(string)
}
return version
}