package main import ( "fmt" "html/template" "log/slog" "net/http" "strconv" ) // home ... func (app *application) home(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } files := []string{ "./ui/html/base.tmpl", "./ui/html/partials/nav.tmpl", "./ui/html/pages/home.tmpl", } ts, err := template.ParseFiles(files...) if err != nil { app.logger.Error(err.Error(), slog.String("method", r.Method), slog.String("uri", r.URL.RequestURI())) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } err = ts.ExecuteTemplate(w, "base", nil) if err != nil { app.logger.Error(err.Error(), slog.String("method", r.Method), slog.String("uri", r.URL.RequestURI())) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } } // snippetView ... func (app *application) snippetView(w http.ResponseWriter, r *http.Request) { id, err := strconv.Atoi(r.URL.Query().Get("id")) if err != nil || id < 1 { http.NotFound(w, r) return } fmt.Fprintf(w, "It's snippet id: %d", id) } // snippetCreate ... func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) { w.Header().Set("Allow", "POST") if r.Method != http.MethodPost { http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed) return } w.Write([]byte("Creating a snippet!")) }