automated setup

This commit is contained in:
Cadey Ratio 2020-01-11 16:06:44 -05:00
parent 280f09d287
commit dfd57bd581
7 changed files with 138 additions and 27 deletions

13
Dockerfile Normal file
View File

@ -0,0 +1,13 @@
FROM xena/go:1.13.5 AS build
WORKDIR /mi
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN GOBIN=/mi/bin go install ./cmd/...
FROM xena/alpine
COPY --from=build /mi/bin /usr/local/bin
COPY ./run /run
RUN mkdir -p /mi
CMD ["/bin/sh", "/run/start.sh"]

55
cmd/mi-init/main.go Normal file
View File

@ -0,0 +1,55 @@
package main
import (
"flag"
"log"
"github.com/facebookgo/flagenv"
"github.com/hashicorp/go-multierror"
_ "github.com/joho/godotenv/autoload"
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
"within.website/mi/rethink"
)
var (
rethinkDBURL = flag.String("rethinkdb-url", "rethinkdb2://admin@127.0.0.1:28015/mi", "RethinkDB URL")
)
func main() {
flagenv.Parse()
flag.Parse()
session, err := rethink.GetSession(*rethinkDBURL)
if err != nil {
log.Fatal(err)
}
err = r.DB("mi").Info().Exec(session)
if err == nil {
return
}
if err := r.DBCreate("mi").Exec(session); err != nil {
log.Fatal(err)
}
tables := []string{
"mastodon",
"blogposts",
"reddit_comments",
"reddit_posts",
"twitter",
}
var result error
for _, tbl := range tables {
if err := r.DB("mi").TableCreate(tbl).Exec(session); err != nil {
result = multierror.Append(result, err)
}
}
if result != nil {
log.Fatal(result)
}
}

View File

@ -5,16 +5,15 @@ import (
"flag"
"fmt"
"net/http"
"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"
"within.website/ln/ex"
"within.website/mi/rethink"
"within.website/x/web/useragent"
)
@ -31,9 +30,6 @@ var (
// 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")
domain = flag.String("domain", "mi.within.website", "domain this is being served on")
@ -43,29 +39,11 @@ func main() {
flagenv.Parse()
flag.Parse()
http.DefaultTransport = useragent.Transport("mi-posse", "https://" + *domain + "/.within/botinfo", http.DefaultTransport)
http.DefaultTransport = useragent.Transport("mi-posse", "https://"+*domain+"/.within/botinfo", http.DefaultTransport)
ctx := context.Background()
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)
}
session, err := rethink.GetSession(*rethinkDBURL)
mastodonClient, err := makeMastodon()
if err != nil {
@ -82,6 +60,11 @@ func main() {
ln.FatalErr(ctx, err)
}
patreonClient, err := makePatreon()
if err != nil {
ln.FatalErr(ctx, err)
}
mux := http.NewServeMux()
pm, err := MakeMiddleware(*pasetoPublicKey, *pasetoPrivateKey, mux)
if err != nil {
@ -104,6 +87,7 @@ func main() {
session: session,
mastodonClient: mastodonClient,
twitterClient: twitterClient,
patreonClient: patreonClient,
redditBot: redditBot,
mux: mux,
}

View File

@ -83,8 +83,9 @@ type MastodonStatus struct {
Account struct {
Acct string `json:"acct"`
} `json:"account"`
URL string `json:"url"`
Content string `json:"content"`
URL string `json:"url"`
Content string `json:"content"`
Sensitive bool `json:"sensitive"`
}
type NewStatus struct {
@ -128,6 +129,10 @@ func (mi *Mi) StreamMastodonToTwitter(ctx context.Context) {
}
st := ns.NewVal
if st.Sensitive {
continue
}
if st.Account.Acct != *mastodonAccount {
continue
}

17
docker-compose.yml Normal file
View File

@ -0,0 +1,17 @@
version: '3.7'
services:
mi:
restart: always
build: .
env_file: .env
depends_on: rethinkdb
ports: "127.0.0.1:39243:5000"
rethinkdb:
image: "rethinkdb:2.4.0"
volumes:
- "rethink-data:/data"
volumes:
rethink-data:

31
rethink/session.go Normal file
View File

@ -0,0 +1,31 @@
package rethink
import (
"net/url"
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
)
func GetSession(dbURL string) (*r.Session, error) {
r.SetTags("rethinkdb", "json")
u, err := url.Parse(dbURL)
if err != nil {
return nil, 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 {
return nil, err
}
return session, nil
}

6
run/start.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
set -e
mi-init
mi