-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
37 lines (32 loc) · 832 Bytes
/
main.go
File metadata and controls
37 lines (32 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"context"
"fmt"
"log"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5"
)
func main() {
ctx := context.Background()
conn, err := pgx.Connect(ctx, "postgres://admin:secret@localhost:5432/todo")
if err != nil {
panic(err)
}
defer func() {
if closeErr := conn.Close(ctx); closeErr != nil {
log.Printf("ошибка закрытия БД: %v", closeErr)
}
}()
storage := &Storage{db: conn}
handler := &Handler{storage: storage}
r := chi.NewRouter()
r.Post("/tasks", handler.handlerCreateTask)
r.Get("/tasks/{id}", handler.handlerPrintTask)
r.Put("/tasks/{id}", handler.handlerUpdateTask)
r.Get("/tasks", handler.handlerPrintAllTasks)
r.Delete("/tasks/{id}", handler.handlerDeleteTask)
if err := http.ListenAndServe(":8080", r); err != nil {
fmt.Println(err)
}
}