57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package main
|
|
|
|
//go:generate sqlc generate
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
|
|
"git.netflux.io/rob/elon-eats-my-tweets/config"
|
|
"git.netflux.io/rob/elon-eats-my-tweets/daemon"
|
|
"git.netflux.io/rob/elon-eats-my-tweets/generated/store"
|
|
"git.netflux.io/rob/elon-eats-my-tweets/httpserver"
|
|
"git.netflux.io/rob/elon-eats-my-tweets/twitterapi"
|
|
"github.com/gorilla/sessions"
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func main() {
|
|
cfg, err := config.NewFromEnv()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
logger, err := zap.NewDevelopment()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
dbconn, err := pgxpool.Connect(ctx, cfg.DatabaseURL)
|
|
if err != nil {
|
|
log.Fatalf("error connecting to database: %s", err)
|
|
}
|
|
defer dbconn.Close()
|
|
store := store.New(dbconn)
|
|
|
|
// TODO: separate daemon and webserver in separate binaries (or via flags).
|
|
go daemon.Run(
|
|
context.Background(),
|
|
store,
|
|
twitterapi.NewClientWithBearerToken(cfg.Twitter.BearerToken),
|
|
logger.Sugar().Named("daemon"),
|
|
)
|
|
|
|
handler := httpserver.NewHandler(
|
|
cfg,
|
|
store,
|
|
func(c *http.Client) twitterapi.APIClient { return twitterapi.NewClient(c) },
|
|
sessions.NewCookieStore([]byte(cfg.SessionKey)),
|
|
httpserver.RandomTokenGenerator{},
|
|
logger,
|
|
)
|
|
log.Fatal(http.ListenAndServe(cfg.ListenAddr, handler))
|
|
}
|