This commit is contained in:
Vicente Ferrari Smith 2025-10-09 07:44:38 +02:00
parent 0069aeaa24
commit 943408255c
9 changed files with 403 additions and 388 deletions

View File

@ -44,7 +44,6 @@ var upgrader = websocket.Upgrader{
func main() {
var cfg config
flag.IntVar(&cfg.port, "port", 4000, "API server port")
@ -52,7 +51,6 @@ func main() {
//addr := flag.String("addr", ":8443", "HTTP network address")
flag.Parse()
logger := log.New(os.Stdout, "", log.Ldate | log.Ltime)
app := &application{
@ -69,17 +67,9 @@ func main() {
database_init(conn)
fileServer := http.FileServer(http.Dir("ui/static"))
mux := http.NewServeMux()
mux.HandleFunc("/", home)
mux.HandleFunc("/ws", ws)
mux.Handle("/static/", http.StripPrefix("/static", fileServer))
mux.HandleFunc("/v1/healthcheck", app.healthcheckHandler)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.port),
Handler: mux,
Handler: app.routes(),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,

22
cmd/api/routes.go Normal file
View File

@ -0,0 +1,22 @@
package main
import (
"net/http"
"github.com/julienschmidt/httprouter"
)
func (app *application) routes() *httprouter.Router {
router := httprouter.New()
fileServer := http.FileServer(http.Dir("ui/static"))
router.HandlerFunc(http.MethodGet, "/", home)
router.HandlerFunc(http.MethodGet, "/ws", ws)
router.Handler(http.MethodGet, "/static/", http.StripPrefix("/static", fileServer))
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
// router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler)
// router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler)
return router
}

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.24.2
require (
github.com/gorilla/websocket v1.5.3
github.com/jackc/pgx/v5 v5.7.6
github.com/julienschmidt/httprouter v1.3.0
)
require (

2
go.sum
View File

@ -11,6 +11,8 @@ github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk=
github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=