setupMuxRequirements creates the required mux handlers for orbit, these include - fileserver for the bundle directory bound to the "/p/" directory
()
| 241 | // setupMuxRequirements creates the required mux handlers for orbit, these include |
| 242 | // - fileserver for the bundle directory bound to the "/p/" directory |
| 243 | func (s *Serve) setupMuxRequirements() *Serve { |
| 244 | pool := sync.Pool{ |
| 245 | New: func() interface{} { |
| 246 | w := gzip.NewWriter(ioutil.Discard) |
| 247 | return w |
| 248 | }, |
| 249 | } |
| 250 | s.mux.HandleFunc("/p/", func(w http.ResponseWriter, r *http.Request) { |
| 251 | // TODO(guy): allow these cache policies to be overwritten |
| 252 | switch CurrentDevMode { |
| 253 | case DevBundleMode: |
| 254 | w.Header().Set("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate") |
| 255 | case ProdBundleMode: |
| 256 | w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") |
| 257 | } |
| 258 | if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { |
| 259 | w.Header().Set("Content-Encoding", "gzip") |
| 260 | gz := pool.Get().(*gzip.Writer) |
| 261 | defer pool.Put(gz) |
| 262 | gz.Reset(w) |
| 263 | defer gz.Close() |
| 264 | http.StripPrefix("/p/", http.FileServer(http.Dir(bundleDir))).ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r) |
| 265 | return |
| 266 | } |
| 267 | http.StripPrefix("/p/", http.FileServer(http.Dir(bundleDir))).ServeHTTP(w, r) |
| 268 | }) |
| 269 | return s |
| 270 | } |
| 271 | // Serve returns the mux server |
| 272 | func (s *Serve) Serve() MuxHandler { |
| 273 | return s.mux |