ch3.3
This commit is contained in:
@@ -3,12 +3,11 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func home(w http.ResponseWriter, r *http.Request) {
|
func (app *application) home(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path != "/" {
|
if r.URL.Path != "/" {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
@@ -20,18 +19,18 @@ func home(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
ts, err := template.ParseFiles(files...)
|
ts, err := template.ParseFiles(files...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err.Error())
|
app.errorLog.Print(err.Error())
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = ts.ExecuteTemplate(w, "base", nil)
|
err = ts.ExecuteTemplate(w, "base", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err.Error())
|
app.errorLog.Print(err.Error())
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func snippetView(w http.ResponseWriter, r *http.Request) {
|
func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.Atoi(r.URL.Query().Get("id"))
|
id, err := strconv.Atoi(r.URL.Query().Get("id"))
|
||||||
if err != nil || id < 1 {
|
if err != nil || id < 1 {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
@@ -40,7 +39,7 @@ func snippetView(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Fprintf(w, "Display a specific snippet with ID %d...", id)
|
fmt.Fprintf(w, "Display a specific snippet with ID %d...", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func snippetCreate(w http.ResponseWriter, r *http.Request) {
|
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
w.Header().Set("Allow", http.MethodPost)
|
w.Header().Set("Allow", http.MethodPost)
|
||||||
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||||
|
|||||||
@@ -7,6 +7,11 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type application struct {
|
||||||
|
errorLog *log.Logger
|
||||||
|
infoLog *log.Logger
|
||||||
|
}
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
addr string
|
addr string
|
||||||
staticDir string
|
staticDir string
|
||||||
@@ -18,15 +23,20 @@ func main() {
|
|||||||
flag.StringVar(&cfg.staticDir, "static-dir", "./ui/static", "Path to static assets")
|
flag.StringVar(&cfg.staticDir, "static-dir", "./ui/static", "Path to static assets")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
|
|
||||||
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
|
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
|
||||||
|
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
|
||||||
|
|
||||||
|
app := &application{
|
||||||
|
errorLog: errorLog,
|
||||||
|
infoLog: infoLog,
|
||||||
|
}
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
fileServer := http.FileServer(http.Dir("./ui/static/"))
|
fileServer := http.FileServer(http.Dir("./ui/static/"))
|
||||||
|
|
||||||
mux.HandleFunc("/", home)
|
mux.HandleFunc("/", app.home)
|
||||||
mux.HandleFunc("/snippet/view", snippetView)
|
mux.HandleFunc("/snippet/view", app.snippetView)
|
||||||
mux.HandleFunc("/snippet/create", snippetCreate)
|
mux.HandleFunc("/snippet/create", app.snippetCreate)
|
||||||
mux.Handle("/static/", http.StripPrefix("/static", fileServer))
|
mux.Handle("/static/", http.StripPrefix("/static", fileServer))
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
|
|||||||
Reference in New Issue
Block a user