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.
116 lines
3.5 KiB
Go
116 lines
3.5 KiB
Go
6 days ago
|
package server
|
||
7 days ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"html/template"
|
||
7 days ago
|
"log/slog"
|
||
7 days ago
|
"net/http"
|
||
|
"strconv"
|
||
6 days ago
|
|
||
|
"git.runcible.io/learning/ratchet/internal/domain/user"
|
||
7 days ago
|
)
|
||
|
|
||
|
type RatchetServer struct {
|
||
|
http.Handler
|
||
|
|
||
6 days ago
|
logger *slog.Logger
|
||
7 days ago
|
//Services used by HTTP routes
|
||
6 days ago
|
UserService user.UserService
|
||
7 days ago
|
}
|
||
|
|
||
6 days ago
|
func NewRatchetServer(logger *slog.Logger) *RatchetServer {
|
||
|
rs := new(RatchetServer)
|
||
|
rs.logger = logger
|
||
7 days ago
|
// TODO implement middleware that disables directory listings
|
||
|
fileServer := http.FileServer(http.Dir("./ui/static/"))
|
||
7 days ago
|
router := http.NewServeMux()
|
||
7 days ago
|
|
||
|
// Subtree pattern for static assets
|
||
|
router.Handle("GET /static/", http.StripPrefix("/static/", fileServer))
|
||
|
|
||
7 days ago
|
// /{$} 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
|
||
6 days ago
|
router.HandleFunc("GET /{$}", rs.home)
|
||
|
router.HandleFunc("GET /snippet/view/{id}", rs.snippetView)
|
||
|
router.HandleFunc("GET /snippet/create", rs.snippetCreate)
|
||
7 days ago
|
|
||
|
// FYI The http.HandlerFunc() adapter works by automatically adding a ServeHTTP() method to
|
||
|
// the passed function
|
||
6 days ago
|
router.HandleFunc("POST /snippet/create", rs.snippetCreatePost)
|
||
7 days ago
|
|
||
|
// Mux Router implements the Handler interface. AKA it has a ServeHTTP receiver.
|
||
6 days ago
|
rs.Handler = router
|
||
|
return rs
|
||
7 days ago
|
}
|
||
|
|
||
6 days ago
|
func (rs *RatchetServer) home(w http.ResponseWriter, r *http.Request) {
|
||
7 days ago
|
// TODO middleware should be able to print out these lines for all routes
|
||
6 days ago
|
rs.logger.Info("request received", "method", "GET", "path", "/")
|
||
7 days ago
|
|
||
7 days ago
|
w.Header().Add("Server", "Go")
|
||
|
|
||
|
// Initialize a slice containing the paths to the two files. It's important
|
||
|
// to note that the file containing our base template must be the *first*
|
||
|
// file in the slice.
|
||
|
files := []string{
|
||
|
"./ui/html/base.go.tmpl",
|
||
|
"./ui/html/partials/nav.go.tmpl",
|
||
|
"./ui/html/pages/home.go.tmpl",
|
||
|
}
|
||
|
|
||
|
// read template file into template set.
|
||
|
ts, err := template.ParseFiles(files...)
|
||
|
if err != nil {
|
||
6 days ago
|
rs.serverError(w, r, err)
|
||
7 days ago
|
return
|
||
|
}
|
||
|
// Write template content to response body
|
||
|
err = ts.ExecuteTemplate(w, "base", nil)
|
||
|
if err != nil {
|
||
6 days ago
|
// This is the older more verbose way of doing what RatchetServer.serverError does
|
||
|
// rs.logger.Error(err.Error())
|
||
|
// http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||
|
rs.serverError(w, r, err)
|
||
7 days ago
|
}
|
||
|
}
|
||
|
|
||
6 days ago
|
func (rs *RatchetServer) snippetView(w http.ResponseWriter, r *http.Request) {
|
||
7 days ago
|
|
||
|
id, err := strconv.Atoi(r.PathValue("id"))
|
||
|
if err != nil || id < 1 {
|
||
6 days ago
|
// http.NotFound(w, r)
|
||
|
rs.clientError(w, http.StatusNotFound)
|
||
7 days ago
|
return
|
||
|
}
|
||
|
|
||
|
// Set a new cache-control header. If an existing "Cache-Control" header exists
|
||
|
// it will be overwritten.
|
||
|
w.Header().Set("Cache-Control", "public, max-age=31536000")
|
||
|
|
||
|
// msg := fmt.Sprintf("Snippet %d...", id)
|
||
|
|
||
|
// w.Write([]byte(msg))
|
||
|
|
||
|
// we can rely on the Write() interface to use a differnent
|
||
|
// function to write out our response
|
||
|
|
||
|
fmt.Fprintf(w, "Snippet %d...", id)
|
||
|
}
|
||
|
|
||
|
// snippetCreate handles display of the form used to create snippets
|
||
6 days ago
|
func (rs *RatchetServer) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
||
7 days ago
|
w.Write([]byte("Displaying snippetCreate form..."))
|
||
|
}
|
||
|
|
||
|
// snippetCreate handles display of the form used to create snippets
|
||
6 days ago
|
func (rs *RatchetServer) snippetCreatePost(w http.ResponseWriter, r *http.Request) {
|
||
7 days ago
|
// example of a custom header. Must be done before calling WriteHeader
|
||
|
// or they will fail to take effect.
|
||
|
w.Header().Add("Server", "Dirp")
|
||
|
w.WriteHeader(http.StatusCreated)
|
||
|
|
||
|
w.Write([]byte("Created snippet..."))
|
||
|
}
|