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

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
.idea
out
out
.env

6
go.mod
View File

@@ -1,3 +1,9 @@
module github.com/rdarius/go-http-server
go 1.23.6
require (
github.com/google/uuid v1.6.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/lib/pq v1.10.9 // indirect
)

6
go.sum Normal file
View File

@@ -0,0 +1,6 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=

View File

@@ -0,0 +1,28 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.28.0
// source: CreateUser.sql
package database
import (
"context"
)
const createUser = `-- name: CreateUser :one
INSERT INTO users (id, created_at, updated_at, email)
VALUES (gen_random_uuid(), NOW(), NOW(), $1)
RETURNING id, created_at, updated_at, email
`
func (q *Queries) CreateUser(ctx context.Context, email string) (User, error) {
row := q.db.QueryRowContext(ctx, createUser, email)
var i User
err := row.Scan(
&i.ID,
&i.CreatedAt,
&i.UpdatedAt,
&i.Email,
)
return i, err
}

31
internal/database/db.go Normal file
View File

@@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.28.0
package database
import (
"context"
"database/sql"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}

View File

@@ -0,0 +1,18 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.28.0
package database
import (
"time"
"github.com/google/uuid"
)
type User struct {
ID uuid.UUID
CreatedAt time.Time
UpdatedAt time.Time
Email string
}

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)

View File

@@ -0,0 +1,4 @@
-- name: CreateUser :one
INSERT INTO users (id, created_at, updated_at, email)
VALUES (gen_random_uuid(), NOW(), NOW(), $1)
RETURNING *;

10
sql/schema/001_users.sql Normal file
View File

@@ -0,0 +1,10 @@
-- +goose Up
CREATE TABLE users(
id UUID PRIMARY KEY,
created_at TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP,
email TEXT not null
);
-- +goose Down
DROP TABLE users;

8
sqlc.yaml Normal file
View File

@@ -0,0 +1,8 @@
version: "2"
sql:
- schema: "sql/schema"
queries: "sql/queries"
engine: "postgresql"
gen:
go:
out: "internal/database"