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

56 lines
1.2 KiB
Go

package main
//go:generate sqlc generate
import (
"context"
"html/template"
"log"
"net/http"
"path/filepath"
"git.netflux.io/rob/elon-eats-my-tweets/config"
"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/twitter"
"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)
}
templates, err := template.ParseGlob(filepath.Join(cfg.PublicPath, "views", "*.html"))
if err != nil {
log.Fatalf("error loading templates: %s", 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)
handler := httpserver.NewHandler(
cfg,
templates,
store,
func(c *http.Client) httpserver.TwitterAPIClient { return twitter.NewAPIClient(c) },
sessions.NewCookieStore([]byte(cfg.SessionKey)),
httpserver.RandomTokenGenerator{},
logger,
)
log.Fatal(http.ListenAndServe(cfg.ListenAddr, handler))
}