You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.6 KiB
Go

package server
import (
"database/sql"
"log/slog"
"net/http"
"git.runcible.io/learning/ratchet/internal/model"
)
type RatchetServer struct {
http.Handler
logger *slog.Logger
//Services used by HTTP routes
snippetService *model.SnippetService
UserService model.UserService
}
func NewRatchetServer(logger *slog.Logger, db *sql.DB) *RatchetServer {
rs := new(RatchetServer)
rs.logger = logger
rs.snippetService = &model.SnippetService{DB: db}
// TODO implement middleware that disables directory listings
fileServer := http.FileServer(http.Dir("./ui/static/"))
router := http.NewServeMux()
// Subtree pattern for static assets
router.Handle("GET /static/", http.StripPrefix("/static/", fileServer))
// /{$} is used to prevent subtree path patterns from acting like a wildcard
// resulting in this route requiring an exact match on "/" only
// You can only include one HTTP method in a route pattern if you choose
// GET will match GET & HEAD http request methods
// router.HandleFunc("GET /{$}", rs.home)
// router.HandleFunc("GET /snippet/view/{id}", rs.snippetView)
// router.HandleFunc("GET /snippet/create", rs.snippetCreate)
// FYI The http.HandlerFunc() adapter works by automatically adding a ServeHTTP() method to
// the passed function
// router.HandleFunc("POST /snippet/create", rs.snippetCreatePost)
// Mux Router implements the Handler interface. AKA it has a ServeHTTP receiver.
// SEE we can really clean things up by moving this into routes.go and handlers.go
rs.Handler = addRoutes(router, rs.logger, db, rs.snippetService)
return rs
}