Compare commits
2 Commits
2e513bb91e
...
2e88f6f9d4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e88f6f9d4 | ||
|
|
e738975535 |
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -12,7 +13,22 @@ func home(w http.ResponseWriter, r *http.Request) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Write([]byte("Hello from Snippetbox"))
|
||||
files := []string{
|
||||
"./ui/html/base.tmpl",
|
||||
"./ui/html/partials/nav.tmpl",
|
||||
"./ui/html/pages/home.tmpl",
|
||||
}
|
||||
ts, err := template.ParseFiles(files...)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
err = ts.ExecuteTemplate(w, "base", nil)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func snippetView(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -32,14 +48,3 @@ func snippetCreate(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
w.Write([]byte("Create a new snippet"))
|
||||
}
|
||||
|
||||
func main() {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", home)
|
||||
mux.HandleFunc("/snippet/view", snippetView)
|
||||
mux.HandleFunc("/snippet/create", snippetCreate)
|
||||
|
||||
log.Print("Starting server on :4000")
|
||||
err := http.ListenAndServe(":4000", mux)
|
||||
log.Fatal(err)
|
||||
}
|
||||
17
cmd/web/main.go
Normal file
17
cmd/web/main.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", home)
|
||||
mux.HandleFunc("/snippet/view", snippetView)
|
||||
mux.HandleFunc("/snippet/create", snippetCreate)
|
||||
|
||||
log.Print("Starting server on :4000")
|
||||
err := http.ListenAndServe(":4000", mux)
|
||||
log.Fatal(err)
|
||||
}
|
||||
19
ui/html/base.tmpl
Normal file
19
ui/html/base.tmpl
Normal file
@@ -0,0 +1,19 @@
|
||||
{{define "base"}}
|
||||
<!doctype html>
|
||||
<html lang='en'>
|
||||
<head>
|
||||
<meta charset='utf-8'>
|
||||
<title>{{template "title" .}} - Snippetbox</title>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1><a href='/'>Snippetbox</a></h1>
|
||||
</header>
|
||||
{{template "nav" .}}
|
||||
<main>
|
||||
{{template "main" .}}
|
||||
</main>
|
||||
<footer>Powered by <a href='https://golang.org/'>Go</a></footer>
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
6
ui/html/pages/home.tmpl
Normal file
6
ui/html/pages/home.tmpl
Normal file
@@ -0,0 +1,6 @@
|
||||
{{define "title"}}Home{{end}}
|
||||
|
||||
{{define "main"}}
|
||||
<h2>Latest Snippets</h2>
|
||||
<p>There's nothing to see here yet!</p>
|
||||
{{end}}
|
||||
5
ui/html/partials/nav.tmpl
Normal file
5
ui/html/partials/nav.tmpl
Normal file
@@ -0,0 +1,5 @@
|
||||
{{define "nav"}}
|
||||
<nav>
|
||||
<a href='/'>Home</a>
|
||||
</nav>
|
||||
{{end}}
|
||||
Reference in New Issue
Block a user