|
| 1 | +/* |
| 2 | +Copyright 2016 Google Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Command template is a trivial web server that uses the text/template (and |
| 18 | +// html/template) package's "block" feature to implement a kind of template |
| 19 | +// inheritance. |
| 20 | +// |
| 21 | +// It should be executed from the directory in which the source resides, |
| 22 | +// as it will look for its template files in the current directory. |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "html/template" |
| 27 | + "log" |
| 28 | + "net/http" |
| 29 | + "strings" |
| 30 | +) |
| 31 | + |
| 32 | +func main() { |
| 33 | + http.HandleFunc("/", indexHandler) |
| 34 | + http.HandleFunc("/image/", imageHandler) |
| 35 | + log.Fatal(http.ListenAndServe("localhost:8080", nil)) |
| 36 | +} |
| 37 | + |
| 38 | +// indexTemplate is the main site template. |
| 39 | +// The default template includes two template blocks ("sidebar" and "content") |
| 40 | +// that may be replaced in templates derived from this one. |
| 41 | +var indexTemplate = template.Must(template.ParseFiles("index.tmpl")) |
| 42 | + |
| 43 | +// Index is a data structure used to populate an indexTemplate. |
| 44 | +type Index struct { |
| 45 | + Title string |
| 46 | + Body string |
| 47 | + Links []Link |
| 48 | +} |
| 49 | + |
| 50 | +type Link struct { |
| 51 | + URL, Title string |
| 52 | +} |
| 53 | + |
| 54 | +// indexHandler is an HTTP handler that serves the index page. |
| 55 | +func indexHandler(w http.ResponseWriter, r *http.Request) { |
| 56 | + data := &Index{ |
| 57 | + Title: "Image gallery", |
| 58 | + Body: "Welcome to the image gallery.", |
| 59 | + } |
| 60 | + for name, img := range images { |
| 61 | + data.Links = append(data.Links, Link{ |
| 62 | + URL: "/image/" + name, |
| 63 | + Title: img.Title, |
| 64 | + }) |
| 65 | + } |
| 66 | + if err := indexTemplate.Execute(w, data); err != nil { |
| 67 | + log.Println(err) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// imageTemplate is a clone of indexTemplate that provides |
| 72 | +// alternate "sidebar" and "content" templates. |
| 73 | +var imageTemplate = template.Must(template.Must(indexTemplate.Clone()).ParseFiles("image.tmpl")) |
| 74 | + |
| 75 | +// Image is a data structure used to populate an imageTemplate. |
| 76 | +type Image struct { |
| 77 | + Title string |
| 78 | + URL string |
| 79 | +} |
| 80 | + |
| 81 | +// imageHandler is an HTTP handler that serves the image pages. |
| 82 | +func imageHandler(w http.ResponseWriter, r *http.Request) { |
| 83 | + data, ok := images[strings.TrimPrefix(r.URL.Path, "/image/")] |
| 84 | + if !ok { |
| 85 | + http.NotFound(w, r) |
| 86 | + return |
| 87 | + } |
| 88 | + if err := imageTemplate.Execute(w, data); err != nil { |
| 89 | + log.Println(err) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// images specifies the site content: a collection of images. |
| 94 | +var images = map[string]*Image{ |
| 95 | + "go": {"The Go Gopher", "https://golang.org/doc/gopher/frontpage.png"}, |
| 96 | + "google": {"The Google Logo", "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"}, |
| 97 | +} |
0 commit comments