forked from golang/example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (74 loc) · 2.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Template is a trivial web server that uses the text/template (and
// html/template) package's "block" feature to implement a kind of template
// inheritance.
//
// It should be executed from the directory in which the source resides,
// as it will look for its template files in the current directory.
package main
import (
"html/template"
"log"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/image/", imageHandler)
log.Fatal(http.ListenAndServe("localhost:8080", nil))
}
// indexTemplate is the main site template.
// The default template includes two template blocks ("sidebar" and "content")
// that may be replaced in templates derived from this one.
var indexTemplate = template.Must(template.ParseFiles("index.tmpl"))
// Index is a data structure used to populate an indexTemplate.
type Index struct {
Title string
Body string
Links []Link
}
type Link struct {
URL, Title string
}
// indexHandler is an HTTP handler that serves the index page.
func indexHandler(w http.ResponseWriter, r *http.Request) {
data := &Index{
Title: "Image gallery",
Body: "Welcome to the image gallery.",
}
for name, img := range images {
data.Links = append(data.Links, Link{
URL: "/image/" + name,
Title: img.Title,
})
}
if err := indexTemplate.Execute(w, data); err != nil {
log.Println(err)
}
}
// imageTemplate is a clone of indexTemplate that provides
// alternate "sidebar" and "content" templates.
var imageTemplate = template.Must(template.Must(indexTemplate.Clone()).ParseFiles("image.tmpl"))
// Image is a data structure used to populate an imageTemplate.
type Image struct {
Title string
URL string
}
// imageHandler is an HTTP handler that serves the image pages.
func imageHandler(w http.ResponseWriter, r *http.Request) {
data, ok := images[strings.TrimPrefix(r.URL.Path, "/image/")]
if !ok {
http.NotFound(w, r)
return
}
if err := imageTemplate.Execute(w, data); err != nil {
log.Println(err)
}
}
// images specifies the site content: a collection of images.
var images = map[string]*Image{
"go": {"The Go Gopher", "https://golang.org/doc/gopher/frontpage.png"},
"google": {"The Google Logo", "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"},
}