97 lines
3.3 KiB
Go
97 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/golang-migrate/migrate/v4"
|
|
"github.com/golang-migrate/migrate/v4/database/postgres"
|
|
_ "github.com/golang-migrate/migrate/v4/source/file"
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/pkg/errors"
|
|
"github.com/urfave/negroni"
|
|
"gitlab.com/sparetimecoders/goamqp"
|
|
"gitlab.com/unboundsoftware/dancefinder/dancefinder_go/pkg/auth"
|
|
"gitlab.com/unboundsoftware/dancefinder/dancefinder_go/pkg/geo"
|
|
"log"
|
|
"net/http"
|
|
"reflect"
|
|
"time"
|
|
|
|
"github.com/99designs/gqlgen/handler"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"gitlab.com/unboundsoftware/dancefinder/dancefinder_go"
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
|
)
|
|
|
|
func main() {
|
|
port := kingpin.Flag("port", "Port used for serving GraphQL API").Envar("PORT").Default("8080").Int()
|
|
url := kingpin.Flag("postgres-url", "URL to use to connect to Postgres").Envar("POSTGRES_URL").Default("postgres://postgres:postgres@:5432/dancefinder?sslmode=disable").String()
|
|
amqpUrl := kingpin.Flag("amqp-url", "URL to use to connect to RabbitMQ").Envar("AMQP_URL").Default("amqp://user:password@localhost:5672/").String()
|
|
geoServiceUrl := kingpin.Flag("geo-service-url", "URL where geo-service is found").Envar("GEO_SERVICE_URL").Default("http://geo-service").String()
|
|
kingpin.Parse()
|
|
|
|
db, err := sqlx.Open("postgres", *url)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
db.SetMaxOpenConns(200)
|
|
db.SetMaxIdleConns(10)
|
|
db.SetConnMaxLifetime(10 * time.Minute)
|
|
|
|
if err := runMigrations(db); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
conn, err := goamqp.NewFromURL("dancefinder", *amqpUrl)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
geoClient := geo.New(*geoServiceUrl)
|
|
resolver := &dancefinder_go.Resolver{
|
|
Db: db,
|
|
GeoClient: geoClient,
|
|
}
|
|
processor := dancefinder_go.NewEventProcessor(db, geoClient)
|
|
err = conn.Start(
|
|
goamqp.EventStreamPublisher("Band.Created", processor.BandPublisher),
|
|
goamqp.EventStreamPublisher("DanceHall.Created", processor.DanceHallPublisher),
|
|
goamqp.EventStreamPublisher("Event.Created", processor.EventPublisher),
|
|
goamqp.EventStreamListener("Band.Created", processor.Process, reflect.TypeOf(dancefinder_go.BandCreated{})),
|
|
goamqp.EventStreamListener("DanceHall.Created", processor.Process, reflect.TypeOf(dancefinder_go.DanceHallCreated{})),
|
|
goamqp.EventStreamListener("Event.Created", processor.Process, reflect.TypeOf(dancefinder_go.EventCreated{})),
|
|
)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/", handler.Playground("GraphQL playground", "/query"))
|
|
mux.HandleFunc("/query", handler.GraphQL(dancefinder_go.NewExecutableSchema(dancefinder_go.Config{Resolvers: resolver})))
|
|
mux.HandleFunc("/event", processor.ProcessREST)
|
|
|
|
n := negroni.Classic().With(negroni.HandlerFunc(auth.Middleware().HandlerWithNext))
|
|
n.UseHandler(mux)
|
|
|
|
log.Printf("connect to http://localhost:%d/ for GraphQL playground", *port)
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), n))
|
|
}
|
|
|
|
func runMigrations(db *sqlx.DB) error {
|
|
driver, err := postgres.WithInstance(db.DB, &postgres.Config{})
|
|
if err != nil {
|
|
return errors.Wrap(err, "Could not create the driver")
|
|
}
|
|
|
|
m, err := migrate.NewWithDatabaseInstance("file://migrations", "postgres", driver)
|
|
if err != nil {
|
|
return errors.Wrap(err, "Could not initiate the migrations")
|
|
}
|
|
|
|
if err := m.Up(); err != nil {
|
|
if err != migrate.ErrNoChange {
|
|
return errors.Wrap(err, "Could not migrate")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|