-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.go
87 lines (73 loc) · 2.24 KB
/
editor.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
86
87
package www
import (
"errors"
"html/template"
"net/http"
"strings"
)
type EditorHandlerOptions struct {
Templates *template.Template
InitialLatitude float64
InitialLongitude float64
InitialZoom int
EnablePlaceholder bool
PlaceholderEndpoint string
EnableOEmbed bool
OEmbedEndpoints []string
EnableWriter bool
WriterPath string
MapRenderer string
EnablePointInPolygon bool
PointInPolygonEndpoint string
PointInPolygonDataEndpoint string
}
type EditorHandlerVars struct {
PageContext string
InitialLatitude float64
InitialLongitude float64
InitialZoom int
EnablePlaceholder bool
PlaceholderEndpoint string
EnableOEmbed bool
OEmbedEndpoints string
EnableWriter bool
WriterPath string
MapRenderer string
EnablePointInPolygon bool
PointInPolygonEndpoint string
PointInPolygonDataEndpoint string
}
func EditorHandler(opts *EditorHandlerOptions) (http.Handler, error) {
t := opts.Templates.Lookup("editor")
if t == nil {
return nil, errors.New("Missing 'editor' template")
}
oembed_endpoints := strings.Join(opts.OEmbedEndpoints, ",")
fn := func(rsp http.ResponseWriter, req *http.Request) {
vars := EditorHandlerVars{
PageContext: "Editor",
InitialLatitude: opts.InitialLatitude,
InitialLongitude: opts.InitialLongitude,
InitialZoom: opts.InitialZoom,
EnablePlaceholder: opts.EnablePlaceholder,
PlaceholderEndpoint: opts.PlaceholderEndpoint,
EnableOEmbed: opts.EnableOEmbed,
OEmbedEndpoints: oembed_endpoints,
EnablePointInPolygon: opts.EnablePointInPolygon,
PointInPolygonEndpoint: opts.PointInPolygonEndpoint,
PointInPolygonDataEndpoint: opts.PointInPolygonDataEndpoint,
EnableWriter: opts.EnableWriter,
WriterPath: opts.WriterPath,
MapRenderer: opts.MapRenderer,
}
rsp.Header().Set("Content-Type", "text/html; charset=utf-8")
err := t.Execute(rsp, vars)
if err != nil {
http.Error(rsp, err.Error(), http.StatusInternalServerError)
return
}
return
}
h := http.HandlerFunc(fn)
return h, nil
}