superhighway84/superhighway84.go

145 lines
3.1 KiB
Go
Raw Normal View History

2021-12-23 23:54:03 +00:00
package main
import (
"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"
"time"
2021-12-25 16:19:41 +00:00
2021-12-23 23:54:03 +00:00
"log"
"github.com/mrusme/superhighway84/cache"
2021-12-26 20:51:44 +00:00
"github.com/mrusme/superhighway84/config"
"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"
"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
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)
})
}
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,
}
}
return cfg.Build()
}
2021-12-23 23:54:03 +00:00
func main() {
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)
if err != nil {
log.Panicln(err)
}
2021-12-30 05:56:51 +00:00
cch, err := cache.NewCache(cfg.ProgramCachePath)
if err != nil {
log.Panicln(err)
}
defer cch.Close()
var articles []*models.Article
var articlesRoots []*models.Article
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
TUI.ArticlesDatasource = &articles
TUI.ArticlesRoots = &articlesRoots
2021-12-30 05:53:48 +00:00
db, err := database.NewDatabase(ctx, cfg.ConnectionString, cfg.DatabaseCachePath, cch, logger)
if err != nil {
log.Panicln(err)
}
defer db.Disconnect()
2021-12-27 01:01:05 +00:00
TUI.CallbackRefreshArticles = func() (error) {
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)
}
err = db.Connect(func(address string) {
TUI.Views["mainscreen"].(*tui.Mainscreen).SetFooter(address)
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()
})
if err != nil {
log.Panicln(err)
2021-12-25 23:54:19 +00:00
}
2021-12-25 21:37:23 +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-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
}