update: service, handlers, and db separation
This commit is contained in:
24
main.go
24
main.go
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user