(w http.ResponseWriter, r *http.Request, b *rod.Browser)
| 110 | } |
| 111 | |
| 112 | func (s *Server) handleSVG(w http.ResponseWriter, r *http.Request, b *rod.Browser) { |
| 113 | if svgContent, found := s.c.Get("cachedSvgContent:" + r.URL.Query().Encode()); found { |
| 114 | log.Println("SVG found in cache.") |
| 115 | |
| 116 | s.sendSVG(svgContent.(string), w) |
| 117 | |
| 118 | return |
| 119 | } |
| 120 | |
| 121 | if svgContent, found := s.c.Get("cachedSvgContent:old:" + r.URL.Query().Encode()); found { |
| 122 | log.Println("SVG found in old cache.") |
| 123 | |
| 124 | s.c.Delete("cachedSvgContent:old:" + r.URL.Query().Encode()) |
| 125 | s.sendSVG(svgContent.(string), w) |
| 126 | |
| 127 | go s.handleSVGInBackground(r, b) |
| 128 | |
| 129 | return |
| 130 | } |
| 131 | |
| 132 | log.Println("New SVG request.") |
| 133 | |
| 134 | page := b.MustPage(fmt.Sprintf("http://localhost:8687/?%s", r.URL.Query().Encode())) |
| 135 | defer page.Close() |
| 136 | page.MustWaitLoad() |
| 137 | |
| 138 | if page.MustHas("pre") { |
| 139 | pre := page.MustElement("pre") |
| 140 | |
| 141 | w.Header().Set("Content-Type", "text/plain") |
| 142 | w.Header().Set("Cache-Control", "no-store, max-age=0") |
| 143 | w.WriteHeader(http.StatusInternalServerError) |
| 144 | |
| 145 | fmt.Fprintln(w, pre.MustText()) |
| 146 | |
| 147 | return |
| 148 | } else if page.MustHas("#svg-container") { |
| 149 | svg := page.MustElement("#svg-container") |
| 150 | svgContent := svg.MustHTML() |
| 151 | |
| 152 | s.sendSVG(svgContent, w) |
| 153 | |
| 154 | s.c.Set("cachedSvgContent:"+r.URL.Query().Encode(), svgContent, cache.DefaultExpiration) |
| 155 | s.c.Set("cachedSvgContent:old:"+r.URL.Query().Encode(), svgContent, 15778463000000000) |
| 156 | |
| 157 | return |
| 158 | } |
| 159 | |
| 160 | w.Header().Set("Content-Type", "text/plain") |
| 161 | w.Header().Set("Cache-Control", "no-store, max-age=0") |
| 162 | w.WriteHeader(http.StatusInternalServerError) |
| 163 | |
| 164 | fmt.Fprintln(w, "Error while parsing SVG.") |
| 165 | } |
| 166 | |
| 167 | func (s *Server) Start() { |
| 168 | s.c = cache.New(30*time.Minute, 1*time.Minute) |
no test coverage detected