DATABASE!

This commit is contained in:
2025-03-11 18:13:22 +02:00
parent 5141fa8047
commit 7eb464272e
10 changed files with 134 additions and 1 deletions

21
main.go
View File

@@ -1,15 +1,22 @@
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"github.com/rdarius/go-http-server/internal/database"
"log"
"net/http"
"os"
"regexp"
"sync/atomic"
)
type apiConfig struct {
fileserverHits atomic.Int32
db database.Queries
}
func (cfg *apiConfig) middlewareMetricsInc(next http.Handler) http.Handler {
@@ -108,9 +115,23 @@ func validateChirpHandler(w http.ResponseWriter, r *http.Request) {
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
dbURL := os.Getenv("DB_URL")
db, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatal(err)
}
apiCfg := &apiConfig{}
dbQueries := database.New(db)
apiCfg.db = *dbQueries
mux := http.NewServeMux()
mux.HandleFunc("GET /api/healthz", readinessHandler)