-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss.go
84 lines (59 loc) · 1.62 KB
/
css.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
package sfomuseum
/*
For example, if we are serving an application off of a "leaf" node like example.com/foo
and need to override the default font URL definitions which assume /fonts in the common
CSS files:
if uri_prefix != "" {
css_t, err := sfom_css.LoadTemplates(ctx)
if err != nil {
return fmt.Errorf("Failed to parse CSS templates, %w", err)
}
font_t := css_t.Lookup("fonts")
if font_t == nil {
return fmt.Errorf("Failed to find 'fonts' CSS template")
}
css_opts := &sfomuseum.CSSHandlerOptions{
URIPrefix: uri_prefix,
Logger: logger,
Template: font_t,
}
css_handler, err := sfomuseum.CSSHandler(css_opts)
if err != nil {
return fmt.Errorf("Failed to create css handler, %w", err)
}
fonts_uri, err := url.JoinPath(uri_prefix, "/css/fonts.css")
if err != nil {
return fmt.Errorf("Failed to derive fonts URI, %w", err)
}
mux.Handle(fonts_uri, css_handler)
}
*/
import (
"log"
"net/http"
"text/template"
)
type CSSHandlerOptions struct {
URIPrefix string
Logger *log.Logger
Template *template.Template
}
type CSSHandlerVars struct {
URIPrefix string
}
func CSSHandler(opts *CSSHandlerOptions) (http.Handler, error) {
fn := func(rsp http.ResponseWriter, req *http.Request) {
vars := CSSHandlerVars{
URIPrefix: opts.URIPrefix,
}
rsp.Header().Set("Content-type", "text/css")
err := opts.Template.Execute(rsp, vars)
if err != nil {
opts.Logger.Printf("Failed to render CSS template for %s, %v", req.URL.Path, err)
http.Error(rsp, "Internal Server Error", http.StatusInternalServerError)
return
}
return
}
return http.HandlerFunc(fn), nil
}