|
4 | 4 | "encoding/json" |
5 | 5 | "fmt" |
6 | 6 | "net/http" |
7 | | - "net/url" |
8 | 7 | "sync" |
9 | 8 |
|
10 | 9 | "github.com/pico-cs/go-client/client" |
@@ -52,27 +51,79 @@ func (s *CSSet) Close() error { |
52 | 51 | return lastErr |
53 | 52 | } |
54 | 53 |
|
55 | | -// HandleFunc returns a http.HandleFunc handler. |
56 | | -func (s *CSSet) HandleFunc(addr string) func(w http.ResponseWriter, r *http.Request) { |
57 | | - return func(w http.ResponseWriter, r *http.Request) { |
58 | | - type tpldata struct { |
59 | | - Title string |
60 | | - Items map[string]*url.URL |
| 54 | +/* |
| 55 | +func (s *CSSet) idxTplExecute(w io.Writer) error { |
| 56 | + var b bytes.Buffer |
| 57 | +
|
| 58 | + renderLocos := func(title string, locos []*Loco) { |
| 59 | + if len(locos) > 0 { |
| 60 | + b.WriteString(`<!DOCTYPE html> |
| 61 | + <html> |
| 62 | + <head> |
| 63 | + <meta charset="UTF-8"> |
| 64 | + <title>command stations</title> |
| 65 | + </head> |
| 66 | + <body> |
| 67 | + `) |
| 68 | +
|
| 69 | + for _, loco := range locos { |
| 70 | + link := &url.URL{Path: fmt.Sprintf("/loco/%s", loco.name())} |
| 71 | + fmt.Fprintf(&b, "<div><a href='%s'>%s</a></div>\n", link, html.EscapeString(loco.name())) |
| 72 | + } |
61 | 73 | } |
| 74 | + } |
62 | 75 |
|
63 | | - data := &tpldata{Items: map[string]*url.URL{}} |
| 76 | + b.WriteString(`<!DOCTYPE html> |
| 77 | +<html> |
| 78 | + <head> |
| 79 | + <meta charset="UTF-8"> |
| 80 | + <title>command stations</title> |
| 81 | + </head> |
| 82 | + <body> |
| 83 | + <ul> |
| 84 | +`) |
| 85 | + for name, cs := range s.csMap { |
| 86 | + link := &url.URL{Path: fmt.Sprintf("/cs/%s", name)} |
| 87 | + fmt.Fprintf(&b, "<div><a href='%s'>%s</a></div>\n", link, html.EscapeString(name)) |
64 | 88 |
|
65 | | - data.Title = "command stations" |
66 | | - for name := range s.csMap { |
67 | | - data.Items[name] = &url.URL{Scheme: "http", Host: addr, Path: fmt.Sprintf("/cs/%s", name)} |
68 | | - } |
| 89 | + b.WriteString(`<li>primary locos |
69 | 90 |
|
70 | | - w.Header().Set("Access-Control-Allow-Origin", "*") |
71 | | - if err := idxTpl.Execute(w, data); err != nil { |
72 | | - http.Error(w, err.Error(), http.StatusNotFound) |
73 | | - return |
| 91 | +`) |
| 92 | +
|
| 93 | +
|
| 94 | +
|
| 95 | +
|
| 96 | + ") |
| 97 | + fmt.Fprintf(&b, "<ul><a href='%s'>%s</a></div>\n", link, html.EscapeString(name)) |
| 98 | +
|
| 99 | +
|
| 100 | +
|
| 101 | + renderLocos("primary locos", cs.filterLocos(func(loco *Loco) bool { return loco.isPrimary(cs) })) |
| 102 | + renderLocos("secondary locos", cs.filterLocos(func(loco *Loco) bool { return loco.isSecondary(cs) })) |
| 103 | + } |
| 104 | + b.WriteString(`</ul> |
| 105 | +</body> |
| 106 | +</html>`) |
| 107 | + _, err := w.Write(b.Bytes()) |
| 108 | + return err |
| 109 | +} |
| 110 | +*/ |
| 111 | + |
| 112 | +// ServeHTTP implements the http.Handler interface. |
| 113 | +func (s *CSSet) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 114 | + data := csTplData{CSMap: map[string]csTpl{}} |
| 115 | + for name, cs := range s.csMap { |
| 116 | + data.CSMap[name] = csTpl{ |
| 117 | + Primaries: cs.filterLocos(func(loco *Loco) bool { return loco.isPrimary(cs) }), |
| 118 | + Secondaries: cs.filterLocos(func(loco *Loco) bool { return loco.isSecondary(cs) }), |
74 | 119 | } |
75 | 120 | } |
| 121 | + |
| 122 | + w.Header().Set("Access-Control-Allow-Origin", "*") |
| 123 | + if err := csIdxTpl.Execute(w, data); err != nil { |
| 124 | + http.Error(w, err.Error(), http.StatusNotFound) |
| 125 | + return |
| 126 | + } |
76 | 127 | } |
77 | 128 |
|
78 | 129 | // A CS represents a command station. |
@@ -132,6 +183,17 @@ func newCS(lg logger.Logger, config *CSConfig, gw *gateway.Gateway) (*CS, error) |
132 | 183 |
|
133 | 184 | func (cs *CS) name() string { return cs.config.Name } |
134 | 185 |
|
| 186 | +// filterLocos returns a map of locos filtered by function filter. |
| 187 | +func (cs *CS) filterLocos(filter func(loco *Loco) bool) map[string]*Loco { |
| 188 | + locos := map[string]*Loco{} |
| 189 | + for name, loco := range cs.locos { |
| 190 | + if filter(loco) { |
| 191 | + locos[name] = loco |
| 192 | + } |
| 193 | + } |
| 194 | + return locos |
| 195 | +} |
| 196 | + |
135 | 197 | // close closes the command station and the underlying client connection. |
136 | 198 | func (cs *CS) close() error { |
137 | 199 | cs.lg.Printf("close command station %s", cs.name()) |
|
0 commit comments