Compare commits

...

6 Commits

Author SHA1 Message Date
8b1468fc1f fix logging
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/dryrun Pipeline was successful
ci/woodpecker/push/publish-tag Pipeline was successful
ci/woodpecker/push/publish-latest Pipeline was successful
2024-06-26 22:57:53 -04:00
c98fc938c9 middleware with status codes
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/dryrun Pipeline was successful
ci/woodpecker/push/publish-tag Pipeline was successful
ci/woodpecker/push/publish-latest Pipeline was successful
2024-06-19 22:16:45 -04:00
025596162c
fix address binding
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/dryrun Pipeline was successful
ci/woodpecker/push/publish-tag Pipeline was successful
ci/woodpecker/push/publish-latest Pipeline was successful
2024-06-19 14:57:41 -04:00
399cfd827d
logging
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/dryrun Pipeline was successful
ci/woodpecker/push/publish-tag Pipeline was successful
ci/woodpecker/push/publish-latest Pipeline was successful
2024-06-19 11:46:22 -04:00
1b528a85c8 Merge branch 'feature/database'
All checks were successful
ci/woodpecker/push/dryrun Pipeline was successful
ci/woodpecker/push/publish-tag Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/publish-latest Pipeline was successful
2024-06-18 15:15:43 -04:00
3cc4cbe069 Merge pull request 'Postgres Database Support' (#2) from feature/database into main
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/dryrun Pipeline was successful
ci/woodpecker/push/publish-tag Pipeline was successful
ci/woodpecker/push/publish-latest Pipeline was successful
Reviewed-on: #2
2024-06-18 18:40:37 +00:00
4 changed files with 42 additions and 11 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/joho/godotenv"
"michaelthomson.dev/mthomson/go-todos-app/handlers"
"michaelthomson.dev/mthomson/go-todos-app/middleware"
"michaelthomson.dev/mthomson/go-todos-app/repositories"
"michaelthomson.dev/mthomson/go-todos-app/services"
)
@ -129,8 +130,8 @@ func main() {
router.HandleFunc("PATCH /todos/{id}/undone", todoHandler.Undone)
server := http.Server{
Addr: "localhost:3000",
Handler: router,
Addr: ":3000",
Handler: middleware.LoggingMiddleware(router),
}
log.Fatal(server.ListenAndServe())

31
middleware/logging.go Normal file
View File

@ -0,0 +1,31 @@
package middleware
import (
"log"
"net/http"
"time"
)
type wrappedWriter struct {
http.ResponseWriter
statusCode int
}
func (w * wrappedWriter) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
w.statusCode = statusCode
}
func LoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
wrapped := &wrappedWriter{
ResponseWriter: w,
statusCode: http.StatusOK,
}
next.ServeHTTP(wrapped, r)
log.Println(wrapped.statusCode, r.Method, r.URL.Path, time.Since(start))
})
}

View File

@ -18,7 +18,7 @@ func (ts *PostgresTodoRepository) List() ([]models.Todo, error) {
rows, err := ts.db.Query(context.Background(), "SELECT id, name, done FROM todo")
if err != nil {
log.Fatal(err)
log.Printf("Database error: %v", err)
return todos, err
}
@ -26,7 +26,6 @@ func (ts *PostgresTodoRepository) List() ([]models.Todo, error) {
var r models.Todo
err := rows.Scan(&r.Id, &r.Name, &r.Done)
if err != nil {
log.Fatal(err)
return todos, err
}
todos = append(todos, r)
@ -40,7 +39,7 @@ func (ts *PostgresTodoRepository) Get(id uuid.UUID) (models.Todo, error) {
err := ts.db.QueryRow(context.Background(), "SELECT id, name, done FROM todo WHERE id=$1", id).Scan(&todo.Id, &todo.Name, &todo.Done)
if err != nil {
log.Fatal(err)
log.Printf("Database error: %v", err)
return todo, err
}
@ -51,7 +50,7 @@ func (ts *PostgresTodoRepository) Update(id uuid.UUID, name string, done bool) e
_, err := ts.db.Exec(context.Background(), "UPDATE todo SET name=$1, done=$2 WHERE id=$3", name, done, id)
if err != nil {
log.Fatal(err)
log.Printf("Database error: %v", err)
}
return err
@ -61,7 +60,7 @@ func (ts *PostgresTodoRepository) Add(todo models.Todo) error {
_, err := ts.db.Exec(context.Background(), "INSERT INTO todo(id, name, done) values($1, $2, $3)", todo.Id, todo.Name, todo.Done)
if err != nil {
log.Fatal(err)
log.Printf("Database error: %v", err)
return err
}
@ -72,7 +71,7 @@ func (ts *PostgresTodoRepository) Delete(id uuid.UUID) error {
_, err := ts.db.Exec(context.Background(), "DELETE FROM todo where id=$1", id)
if err != nil {
log.Fatal(err)
log.Printf("Database error: %v", err)
return err
}

View File

@ -54,14 +54,14 @@ func (ts *TodoService) UpdateTodo(id uuid.UUID, name string, done bool) (models.
return ts.db.Get(id)
}
func (ts *TodoService) GetTodos() (todos []models.Todo, err error) {
func (ts *TodoService) GetTodos() ([]models.Todo, error) {
return ts.db.List()
}
func (ts *TodoService) GetTodoById(id uuid.UUID) (todo models.Todo, err error) {
func (ts *TodoService) GetTodoById(id uuid.UUID) (models.Todo, error) {
return ts.db.Get(id)
}
func (ts *TodoService) DeleteTodoById(id uuid.UUID) (err error) {
func (ts *TodoService) DeleteTodoById(id uuid.UUID) error {
return ts.db.Delete(id)
}