-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
44 lines (36 loc) · 1.02 KB
/
example_test.go
File metadata and controls
44 lines (36 loc) · 1.02 KB
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
package example
import (
"html/template"
"log/slog"
"net/http"
"sync"
"github.com/linkdata/jaws"
"github.com/linkdata/jaws/lib/bind"
"github.com/linkdata/jaws/lib/ui"
)
const indexhtml = `
<html>
<head>{{$.HeadHTML}}</head>
<body>{{with .Dot}}
{{$.Range .}}
{{$.TailHTML}}
{{end}}</body>
</html>
`
func Example() {
jw, err := jaws.New() // create a default JaWS instance
if err != nil {
panic(err)
}
defer jw.Close() // ensure we clean up
jw.Logger = slog.Default() // optionally set the logger to use
// parse our template and inform JaWS about it
templates := template.Must(template.New("index").Parse(indexhtml))
jw.AddTemplateLookuper(templates)
go jw.Serve() // start the JaWS processing loop
http.DefaultServeMux.Handle("GET /jaws/", jw) // ensure the JaWS routes are handled
var mu sync.Mutex
var f float64
http.DefaultServeMux.Handle("GET /", ui.Handler(jw, "index", bind.New(&mu, &f)))
slog.Error(http.ListenAndServe("localhost:8080", nil).Error())
}