2024-01-22 21:25:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-22 23:07:14 +00:00
|
|
|
"fmt"
|
2024-01-23 19:19:53 +00:00
|
|
|
"html/template"
|
2024-01-24 21:32:07 +00:00
|
|
|
"log/slog"
|
2024-01-22 21:25:34 +00:00
|
|
|
"net/http"
|
2024-01-22 23:07:14 +00:00
|
|
|
"strconv"
|
2024-01-22 21:25:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// home ...
|
2024-01-24 21:32:07 +00:00
|
|
|
func (app *application) home(w http.ResponseWriter, r *http.Request) {
|
2024-01-22 21:25:34 +00:00
|
|
|
if r.URL.Path != "/" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2024-01-23 19:19:53 +00:00
|
|
|
|
|
|
|
files := []string{
|
|
|
|
"./ui/html/base.tmpl",
|
|
|
|
"./ui/html/partials/nav.tmpl",
|
|
|
|
"./ui/html/pages/home.tmpl",
|
|
|
|
}
|
|
|
|
|
|
|
|
ts, err := template.ParseFiles(files...)
|
|
|
|
if err != nil {
|
2024-01-24 21:32:07 +00:00
|
|
|
app.logger.Error(err.Error(), slog.String("method", r.Method), slog.String("uri", r.URL.RequestURI()))
|
2024-01-23 19:19:53 +00:00
|
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ts.ExecuteTemplate(w, "base", nil)
|
|
|
|
if err != nil {
|
2024-01-24 21:32:07 +00:00
|
|
|
app.logger.Error(err.Error(), slog.String("method", r.Method), slog.String("uri", r.URL.RequestURI()))
|
2024-01-23 19:19:53 +00:00
|
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-01-22 21:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// snippetView ...
|
2024-01-24 21:32:07 +00:00
|
|
|
func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
2024-01-22 23:07:14 +00:00
|
|
|
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)
|
2024-01-22 21:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// snippetCreate ...
|
2024-01-24 21:32:07 +00:00
|
|
|
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
2024-01-22 21:25:34 +00:00
|
|
|
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!"))
|
|
|
|
}
|