mi-v1/cmd/mi/main.go

129 lines
2.4 KiB
Go
Raw Normal View History

2020-01-11 18:38:50 +00:00
package main
import (
"context"
"flag"
2020-01-11 19:11:30 +00:00
"fmt"
"net/http"
2020-01-11 18:38:50 +00:00
"net/url"
"os"
"os/signal"
"time"
"github.com/facebookarchive/flagenv"
_ "github.com/joho/godotenv/autoload"
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
"within.website/ln"
2020-01-11 19:11:30 +00:00
"within.website/ln/ex"
2020-01-11 18:38:50 +00:00
)
var (
/*
// Gitea
giteaInstance = flag.String("gitea-instance", "", "Gitea instance to use")
giteaAPIToken = flag.String("gitea-api-token", "", "Gitea API token")
// Github
githubToken = flag.String("github-token", "", "Github API token")
*/
// RethinkDB
rethinkDBURL = flag.String("rethinkdb-url", "rethinkdb2://admin@127.0.0.1:28015/mi", "RethinkDB URL")
// Blog
blogURL = flag.String("blog-url", "https://christine.website/blog.json", "JSONFeed to monitor for new posts")
// Port
port = flag.String("port", "5000", "HTTP port")
)
func main() {
flagenv.Parse()
flag.Parse()
ctx := context.Background()
ctx = ln.WithF(ctx, ln.F{
"mastodon_instance": *mastodonInstance,
})
r.SetTags("rethinkdb", "json")
u, err := url.Parse(*rethinkDBURL)
if err != nil {
ln.FatalErr(ctx, err)
}
pw, _ := u.User.Password()
db := u.Path[1:]
session, err := r.Connect(r.ConnectOpts{
Address: u.Host,
Database: db,
Username: u.User.Username(),
Password: pw,
})
if err != nil {
ln.FatalErr(ctx, err)
}
mastodonClient, err := makeMastodon()
if err != nil {
ln.FatalErr(ctx, err)
}
redditBot, err := makeReddit()
if err != nil {
ln.FatalErr(ctx, err)
}
twitterClient, err := makeTwitter()
if err != nil {
ln.FatalErr(ctx, err)
}
2020-01-11 19:11:30 +00:00
mux := http.NewServeMux()
pm, err := MakeMiddleware(*pasetoPublicKey, *pasetoPrivateKey, mux)
if err != nil {
ln.FatalErr(ctx, err)
}
tok, err := pm.CreateToken(nil, time.Now().AddDate(2, 0, 0))
if err != nil {
ln.FatalErr(ctx, err)
}
fmt.Printf("token: %s\n", tok)
2020-01-11 18:38:50 +00:00
mi := &Mi{
session: session,
mastodonClient: mastodonClient,
twitterClient: twitterClient,
redditBot: redditBot,
}
ctx, cancel := context.WithCancel(ctx)
go func(cf func()) {
var signalChan chan os.Signal
signalChan = make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
<-signalChan
cf()
}(cancel)
go mi.StreamMastodon(ctx)
go mi.StreamReddit(ctx)
go mi.StreamMastodonToTwitter(ctx)
2020-01-11 19:11:30 +00:00
h := &http.Server{
Addr: ":" + *port,
Handler: ex.HTTPLog(pm),
}
go func() { ln.FatalErr(ctx, h.ListenAndServe()) }()
2020-01-11 18:38:50 +00:00
<-ctx.Done()
ln.Log(ctx, ln.Info("cleaning up"))
time.Sleep(500 * time.Millisecond)
}