2021-12-23 23:54:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-12-26 07:02:15 +00:00
|
|
|
"context"
|
2021-12-25 16:19:41 +00:00
|
|
|
"embed"
|
2021-12-29 21:41:29 +00:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2021-12-29 03:48:28 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2021-12-25 21:01:42 +00:00
|
|
|
"time"
|
2021-12-25 16:19:41 +00:00
|
|
|
|
2021-12-23 23:54:03 +00:00
|
|
|
"log"
|
|
|
|
|
2021-12-30 05:14:18 +00:00
|
|
|
"github.com/mrusme/superhighway84/cache"
|
2021-12-26 20:51:44 +00:00
|
|
|
"github.com/mrusme/superhighway84/config"
|
2021-12-26 07:02:15 +00:00
|
|
|
"github.com/mrusme/superhighway84/database"
|
2021-12-25 21:37:23 +00:00
|
|
|
"github.com/mrusme/superhighway84/models"
|
2021-12-25 16:19:41 +00:00
|
|
|
"github.com/mrusme/superhighway84/tui"
|
2021-12-26 07:02:15 +00:00
|
|
|
"go.uber.org/zap"
|
2021-12-23 23:54:03 +00:00
|
|
|
)
|
|
|
|
|
2021-12-25 18:33:33 +00:00
|
|
|
//go:embed superhighway84.jpeg
|
2021-12-25 16:19:41 +00:00
|
|
|
var EMBEDFS embed.FS
|
2021-12-29 04:42:50 +00:00
|
|
|
var version = "v0.0.0"
|
2021-12-23 23:54:03 +00:00
|
|
|
|
2021-12-26 07:02:15 +00:00
|
|
|
func NewLogger(filename string) (*zap.Logger, error) {
|
2021-12-29 03:48:28 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-26 07:02:15 +00:00
|
|
|
cfg := zap.NewProductionConfig()
|
2021-12-29 03:48:28 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
cfg.OutputPaths = []string{
|
|
|
|
"stdout",
|
|
|
|
"winfile:///" + filename,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cfg.OutputPaths = []string{
|
|
|
|
filename,
|
|
|
|
}
|
2021-12-26 07:02:15 +00:00
|
|
|
}
|
|
|
|
return cfg.Build()
|
|
|
|
}
|
|
|
|
|
2021-12-23 23:54:03 +00:00
|
|
|
func main() {
|
2021-12-26 07:02:15 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2021-12-23 23:54:03 +00:00
|
|
|
|
2021-12-26 20:51:44 +00:00
|
|
|
cfg, err := config.LoadConfig()
|
2021-12-26 15:55:37 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
2021-12-23 23:54:03 +00:00
|
|
|
}
|
2021-12-26 15:55:37 +00:00
|
|
|
if cfg.WasSetup() == false {
|
|
|
|
cfg.Setup()
|
2021-12-23 23:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 15:55:37 +00:00
|
|
|
logger, err := NewLogger(cfg.Logfile)
|
2021-12-26 07:02:15 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2021-12-30 05:56:51 +00:00
|
|
|
cch, err := cache.NewCache(cfg.ProgramCachePath)
|
2021-12-30 05:14:18 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
defer cch.Close()
|
|
|
|
|
2021-12-29 00:25:24 +00:00
|
|
|
var articles []*models.Article
|
|
|
|
var articlesRoots []*models.Article
|
2021-12-26 07:02:15 +00:00
|
|
|
|
2021-12-30 05:14:18 +00:00
|
|
|
TUI := tui.Init(&EMBEDFS, cfg, cch, logger)
|
2021-12-29 21:41:29 +00:00
|
|
|
TUI.SetVersion(version, getLatestVersion())
|
2021-12-29 04:42:50 +00:00
|
|
|
|
2021-12-26 07:02:15 +00:00
|
|
|
TUI.ArticlesDatasource = &articles
|
2021-12-29 00:25:24 +00:00
|
|
|
TUI.ArticlesRoots = &articlesRoots
|
2021-12-26 07:02:15 +00:00
|
|
|
|
2021-12-30 05:53:48 +00:00
|
|
|
db, err := database.NewDatabase(ctx, cfg.ConnectionString, cfg.DatabaseCachePath, cch, logger)
|
2021-12-26 07:02:15 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
defer db.Disconnect()
|
2021-12-27 01:01:05 +00:00
|
|
|
|
|
|
|
TUI.CallbackRefreshArticles = func() (error) {
|
2021-12-29 00:25:24 +00:00
|
|
|
articles, articlesRoots, err = db.ListArticles()
|
2021-12-27 01:01:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
TUI.CallbackSubmitArticle = func(article *models.Article) (error) {
|
|
|
|
return db.SubmitArticle(article)
|
|
|
|
}
|
|
|
|
|
2021-12-26 07:02:15 +00:00
|
|
|
err = db.Connect(func(address string) {
|
|
|
|
TUI.Views["mainscreen"].(*tui.Mainscreen).SetFooter(address)
|
2021-12-29 00:25:24 +00:00
|
|
|
articles, articlesRoots, _ = db.ListArticles()
|
2021-12-27 01:01:05 +00:00
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
TUI.SetView("mainscreen", true)
|
|
|
|
|
|
|
|
TUI.RefreshData()
|
|
|
|
TUI.Refresh()
|
|
|
|
TUI.App.Draw()
|
2021-12-26 07:02:15 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
2021-12-25 23:54:19 +00:00
|
|
|
}
|
2021-12-25 21:37:23 +00:00
|
|
|
|
2021-12-26 07:02:15 +00:00
|
|
|
|
2021-12-27 05:03:38 +00:00
|
|
|
go func() {
|
|
|
|
peers := 0
|
|
|
|
for {
|
|
|
|
bw := db.IPFSNode.Reporter.GetBandwidthTotals()
|
|
|
|
connections, err := db.IPFSCoreAPI.Swarm().Peers(context.Background())
|
|
|
|
if err == nil {
|
|
|
|
peers = len(connections)
|
|
|
|
}
|
|
|
|
TUI.SetStats(int64(peers), int64(bw.RateIn), int64(bw.RateOut), bw.TotalIn , bw.TotalOut)
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-12-25 18:33:33 +00:00
|
|
|
TUI.Launch()
|
2021-12-23 23:54:03 +00:00
|
|
|
}
|
2021-12-24 02:49:45 +00:00
|
|
|
|
2021-12-29 21:41:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|