2022-05-19 15:07:49 +00:00
|
|
|
package main
|
|
|
|
|
2022-05-20 20:57:45 +00:00
|
|
|
//go:generate sqlc generate
|
|
|
|
|
2022-05-19 19:51:12 +00:00
|
|
|
import (
|
2022-05-20 20:57:45 +00:00
|
|
|
"context"
|
2022-05-20 19:52:54 +00:00
|
|
|
"html/template"
|
2022-05-19 19:51:12 +00:00
|
|
|
"log"
|
2022-05-20 19:52:54 +00:00
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
2022-05-19 19:51:12 +00:00
|
|
|
|
|
|
|
"git.netflux.io/rob/elon-eats-my-tweets/config"
|
2022-05-20 20:57:45 +00:00
|
|
|
"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 19:52:54 +00:00
|
|
|
"github.com/gorilla/sessions"
|
2022-05-20 20:57:45 +00:00
|
|
|
"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
|
|
|
|
2022-05-20 19:52:54 +00:00
|
|
|
templates, err := template.ParseGlob(filepath.Join(cfg.PublicPath, "views", "*.html"))
|
|
|
|
if err != nil {
|
2022-05-20 20:57:45 +00:00
|
|
|
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)
|
2022-05-20 19:52:54 +00:00
|
|
|
}
|
2022-05-20 20:57:45 +00:00
|
|
|
defer dbconn.Close()
|
|
|
|
store := store.New(dbconn)
|
2022-05-20 19:52:54 +00:00
|
|
|
|
|
|
|
handler := httpserver.NewHandler(
|
|
|
|
cfg,
|
|
|
|
templates,
|
2022-05-20 20:57:45 +00:00
|
|
|
store,
|
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
|
|
|
}
|