DATABASE!
This commit is contained in:
21
main.go
21
main.go
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user