-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (142 loc) · 3.85 KB
/
main.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
type Book struct {
ID int `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
ISBN string `json:"isbn"`
}
var db *sql.DB
func init() {
var err error
err = godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file")
}
connStr := os.Getenv("DATABASE_URL")
db, err = sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
}
if err := db.Ping(); err != nil {
log.Fatal(err)
}
}
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/books", getBooks)
r.Get("/books/{id}", getBook)
r.Post("/books", createBook)
r.Put("/books/{id}", updateBook)
r.Delete("/books/{id}", deleteBook)
srv := &http.Server{
Addr: ":3000",
Handler: r,
}
// Handle shutdown gracefully
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
go func() {
<-stop // wait for SIGINT (Ctrl+C)
log.Println("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("Server shutdown failed: %v", err)
}
log.Println("Server stopped")
}()
log.Println("Server started on :3000")
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Server startup failed: %v", err)
}
}
func getBooks(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query("SELECT id, title, author, isbn FROM books")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer rows.Close()
// make a slice of books
var books []Book
for rows.Next() {
var b Book
err := rows.Scan(&b.ID, &b.Title, &b.Author, &b.ISBN)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
books = append(books, b)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(books)
}
func getBook(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
row := db.QueryRow("SELECT id, title, author, isbn FROM books WHERE id = $1", id)
var book Book
err := row.Scan(&book.ID, &book.Title, &book.Author, &book.ISBN)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(book)
}
func createBook(w http.ResponseWriter, r *http.Request) {
var book Book
err := json.NewDecoder(r.Body).Decode(&book)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
_, err = db.Exec("INSERT INTO books (title, author, isbn) VALUES ($1, $2, $3)", book.Title, book.Author, book.ISBN)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
w.Write([]byte("Book created successfully"))
}
func updateBook(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
var book Book
err := json.NewDecoder(r.Body).Decode(&book)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
_, err = db.Exec("UPDATE books SET title = $1, author = $2, isbn = $3 WHERE id = $4", book.Title, book.Author, book.ISBN, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(book)
}
func deleteBook(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
_, err := db.Exec("DELETE FROM books WHERE id = $1", id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Book deleted successfully"))
}