diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..273c62a --- /dev/null +++ b/.env.example @@ -0,0 +1,39 @@ +# Twitter +TWITTER_CONSUMER_TOKEN= +TWITTER_CONSUMER_SECRET= +TWITTER_API_KEY= +TWITTER_API_SECRET= + +# Mastodon +MASTODON_INSTANCE= +MASTODON_APP_ID= +MASTODON_APP_SECRET= +MASTODON_TOKEN= +MASTODON_ACCOUNT= + +# Gitea +GITEA_INSTANCE= +GITEA_API_TOKEN= + +# Github +GITHUB_TOKEN= + +# Reddit +REDDIT_APP_ID= +REDDIT_APP_SECRET= +REDDIT_USERNAME= +REDDIT_PASSWORD= + +# Patreon +PATREON_CLIENT_ID= +PATREON_CLIENT_SECRET= +PATREON_ACCESS_TOKEN= +PATREON_REFRESH_TOKEN= + +# Paseto +# Generate with ./tools/paseto-key-gen.go +PASETO_PUBLIC_KEY= +PASETO_PRIVATE_KEY= + +# Discord +DISCORD_WEBHOOK= diff --git a/cmd/mi/logrus.go b/cmd/mi/logrus.go new file mode 100644 index 0000000..4b888bd --- /dev/null +++ b/cmd/mi/logrus.go @@ -0,0 +1,12 @@ +package main + +import ( + "os" + + r "gopkg.in/rethinkdb/rethinkdb-go.v6" +) + +func init() { + r.SetVerbose(true) + r.Log.Out = os.Stderr +} diff --git a/cmd/mi/main.go b/cmd/mi/main.go index 562081e..3b92552 100644 --- a/cmd/mi/main.go +++ b/cmd/mi/main.go @@ -12,6 +12,7 @@ import ( "github.com/facebookarchive/flagenv" _ "github.com/joho/godotenv/autoload" + "golang.org/x/net/trace" r "gopkg.in/rethinkdb/rethinkdb-go.v6" "within.website/ln" "within.website/ln/ex" @@ -47,6 +48,7 @@ var ( func main() { flagenv.Parse() flag.Parse() + ln.AddFilter(ex.NewGoTraceLogger()) http.DefaultTransport = useragent.Transport("mi-posse", "https://"+*domain+"/.within/botinfo", http.DefaultTransport) @@ -102,6 +104,14 @@ func main() { mi.RegisterRoutes() sc := switchcounter.New(session, mux) + mux.HandleFunc("/debug/requests", func(rw http.ResponseWriter, req *http.Request) { + trace.Render(rw, req, true) + }) + + mux.HandleFunc("/debug/events", func(rw http.ResponseWriter, req *http.Request) { + trace.RenderEvents(rw, req, true) + }) + mux.HandleFunc("/.within/tokeninfo", func(rw http.ResponseWriter, req *http.Request) { tokenData := req.Context().Value(tokenKey) json.NewEncoder(rw).Encode(tokenData) @@ -153,8 +163,10 @@ func main() { } h := &http.Server{ - Addr: ":" + *port, - Handler: ex.HTTPLog(pm), + Addr: ":" + *port, + Handler: GoTraceMiddleware{ + next: ex.HTTPLog(pm), + }, } go func() { ln.FatalErr(ctx, h.ListenAndServe()) }() diff --git a/cmd/mi/mastodon.go b/cmd/mi/mastodon.go index 895883d..130a82c 100644 --- a/cmd/mi/mastodon.go +++ b/cmd/mi/mastodon.go @@ -77,21 +77,9 @@ func (mi *Mi) StreamMastodon(ctx context.Context) error { } } -type MastodonStatus struct { - ID float64 `json:"id"` - InReplyToID *float64 `json:"in_reply_to_id"` - Account struct { - Acct string `json:"acct"` - } `json:"account"` - URL string `json:"url"` - Content string `json:"content"` - Sensitive bool `json:"sensitive"` - Application *madon.Application `json:"application"` -} - type NewStatus struct { - NewVal *MastodonStatus `json:"new_val"` - OldVal *MastodonStatus `json:"old_val"` + NewVal *madon.Status `json:"new_val"` + OldVal *madon.Status `json:"old_val"` } func (mi *Mi) PushMastodon(ctx context.Context, p Post) error { @@ -135,6 +123,10 @@ func (mi *Mi) StreamMastodonToTwitter(ctx context.Context) { st := ns.NewVal + if st.Visibility != "public" { + continue + } + if st.Application.Name == "mi_irl" { continue } diff --git a/cmd/mi/middleware.go b/cmd/mi/middleware.go index b1c1bf1..6161a4e 100644 --- a/cmd/mi/middleware.go +++ b/cmd/mi/middleware.go @@ -10,6 +10,7 @@ import ( "github.com/google/uuid" "github.com/o1egl/paseto/v2" "golang.org/x/crypto/ed25519" + "golang.org/x/net/trace" ) var ( @@ -18,6 +19,17 @@ var ( pasetoPrivateKey = flag.String("paseto-private-key", "", "Paseto private key") ) +type GoTraceMiddleware struct{ next http.Handler } + +func (gtm GoTraceMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + tr := trace.New("http", r.URL.Path) + ctx = trace.NewContext(ctx, tr) + r = r.WithContext(ctx) + gtm.next.ServeHTTP(w, r) + tr.Finish() +} + type PasetoMiddleware struct { next http.Handler pubKey ed25519.PublicKey @@ -74,6 +86,8 @@ func init() { "/static/gruvbox.css": true, "/static/favicon.ico": true, "/static/favicon.png": true, + "/debug/requests": true, + "/debug/events": true, } } diff --git a/go.mod b/go.mod index b573d32..263d887 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/olekukonko/tablewriter v0.0.4 // indirect github.com/turnage/graw v0.0.0-20191224200831-a592320d5bc9 golang.org/x/crypto v0.0.0-20200109152110-61a87790db17 + golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d gopkg.in/mxpv/patreon-go.v1 v1.0.0-20171031001022-1d2f253ac700 gopkg.in/rethinkdb/rethinkdb-go.v6 v6.0.0 diff --git a/switchcounter/switch.go b/switchcounter/switch.go index 4c7fc44..035a83a 100644 --- a/switchcounter/switch.go +++ b/switchcounter/switch.go @@ -36,6 +36,7 @@ func (s *Switches) Switch(ctx context.Context, who string) (Switch, Switch, erro var lastSw Switch var currentSw Switch + ln.Log(ctx, ln.Info("getting last switch")) res, err := r.Table("switches"). OrderBy(r.Desc("started_at")). Limit(1). @@ -49,6 +50,12 @@ func (s *Switches) Switch(ctx context.Context, who string) (Switch, Switch, erro return lastSw, currentSw, err } + f := ln.F{ + "from": lastSw.Who, + "to": who, + } + ln.Log(ctx, f, ln.Info("got last switch")) + now := time.Now().UTC() lastSw.EndedAt = &now lastSw.Duration = now.Sub(lastSw.StartedAt).Round(time.Second) @@ -72,6 +79,8 @@ func (s *Switches) Switch(ctx context.Context, who string) (Switch, Switch, erro return lastSw, currentSw, err } + ln.Log(ctx, f, ln.Info("switched")) + return lastSw, currentSw, nil }