update: service, handlers, and db separation
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

This commit is contained in:
2024-06-14 17:49:56 -04:00
parent 44feca12d2
commit 3d29dadaf3
14 changed files with 364 additions and 29 deletions

24
main.go
View File

@@ -3,24 +3,28 @@ package main
import (
"log"
"net/http"
"path/filepath"
"github.com/a-h/templ"
"michaelthomson.dev/mthomson/go-todos-app/views"
"michaelthomson.dev/mthomson/go-todos-app/db"
"michaelthomson.dev/mthomson/go-todos-app/handlers"
"michaelthomson.dev/mthomson/go-todos-app/services"
)
func main() {
todosStore := db.NewTodoStore()
ts := services.NewTodoService(&todosStore)
homeHandler := handlers.NewHomeHandler(*ts)
todoHandler := handlers.NewTodoHandler(*ts)
router := http.NewServeMux()
// Serve static files
router.HandleFunc("GET /assets/", func(w http.ResponseWriter, r *http.Request) {
filePath := r.URL.Path[len("/assets/"):]
fullPath := filepath.Join(".", "assets", filePath)
http.ServeFile(w, r, fullPath)
})
fs := http.FileServer(http.Dir("./assets"))
router.Handle("GET /assets/", http.StripPrefix("/assets/", fs))
home := views.Home()
router.Handle("GET /", templ.Handler(home))
router.HandleFunc("GET /{$}", homeHandler.Home)
router.HandleFunc("POST /todos", todoHandler.Create)
router.HandleFunc("DELETE /todos/{id}", todoHandler.Delete)
server := http.Server{
Addr: "localhost:3000",