package main

import (
	"crowsnest/internal/model/database"
	"database/sql"
	"log"
	"net/http"
	"os"

	_ "github.com/lib/pq"
)

type App struct {
	articles *database.ArticleModel
}

func main() {
	// collect environement variables
	databaseURL := os.Getenv("DB_URL")

	// connect to database
	db, err := sql.Open("postgres", databaseURL)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// define app
	app := &App{
		articles: &database.ArticleModel{DB: db},
	}

	// start web server
	server := http.Server{
		Addr:    ":8080",
		Handler: app.routes(),
	}

	log.Println("server started, listening on :8080")
	server.ListenAndServe()
}
