This repository has been archived on 2022-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
elon-eats-my-tweets/main.go

48 lines
1.0 KiB
Go
Raw Normal View History

2022-05-19 15:07:49 +00:00
package main
//go:generate sqlc generate
2022-05-19 19:51:12 +00:00
import (
"context"
2022-05-19 19:51:12 +00:00
"log"
2022-05-20 19:52:54 +00:00
"net/http"
2022-05-19 19:51:12 +00:00
"git.netflux.io/rob/elon-eats-my-tweets/config"
"git.netflux.io/rob/elon-eats-my-tweets/generated/store"
2022-05-19 19:51:12 +00:00
"git.netflux.io/rob/elon-eats-my-tweets/httpserver"
2022-05-20 22:34:20 +00:00
"git.netflux.io/rob/elon-eats-my-tweets/twitter"
2022-05-20 19:52:54 +00:00
"github.com/gorilla/sessions"
"github.com/jackc/pgx/v4/pgxpool"
2022-05-20 19:52:54 +00:00
"go.uber.org/zap"
2022-05-19 19:51:12 +00:00
)
2022-05-19 15:07:49 +00:00
func main() {
2022-05-20 19:52:54 +00:00
cfg, err := config.NewFromEnv()
if err != nil {
log.Fatal(err)
}
2022-05-20 00:26:42 +00:00
2022-05-20 19:52:54 +00:00
logger, err := zap.NewDevelopment()
2022-05-20 00:26:42 +00:00
if err != nil {
log.Fatal(err)
}
2022-05-19 19:51:12 +00:00
ctx := context.Background()
dbconn, err := pgxpool.Connect(ctx, cfg.DatabaseURL)
if err != nil {
log.Fatalf("error connecting to database: %s", err)
2022-05-20 19:52:54 +00:00
}
defer dbconn.Close()
store := store.New(dbconn)
2022-05-20 19:52:54 +00:00
handler := httpserver.NewHandler(
cfg,
store,
2022-05-20 22:34:20 +00:00
func(c *http.Client) httpserver.TwitterAPIClient { return twitter.NewAPIClient(c) },
2022-05-20 19:52:54 +00:00
sessions.NewCookieStore([]byte(cfg.SessionKey)),
httpserver.RandomTokenGenerator{},
logger,
)
log.Fatal(http.ListenAndServe(cfg.ListenAddr, handler))
2022-05-19 15:07:49 +00:00
}