File: restful-html-template.go

package info (click to toggle)
golang-github-emicklei-go-restful 3.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 792 kB
  • sloc: makefile: 9; sh: 6
file content (35 lines) | stat: -rw-r--r-- 750 bytes parent folder | download
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
package main

import (
	"log"
	"net/http"
	"text/template"

	restful "github.com/emicklei/go-restful/v3"
)

// This example shows how to serve a HTML page using the standard Go template engine.
//
// GET http://localhost:8080/

func main() {
	ws := new(restful.WebService)
	ws.Route(ws.GET("/").To(home))
	restful.Add(ws)
	print("open browser on http://localhost:8080/\n")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

type Message struct {
	Text string
}

func home(req *restful.Request, resp *restful.Response) {
	p := &Message{"restful-html-template demo"}
	// you might want to cache compiled templates
	t, err := template.ParseFiles("home.html")
	if err != nil {
		log.Fatalf("Template gave: %s", err)
	}
	t.Execute(resp.ResponseWriter, p)
}