package main import ( "flag" "fmt" "image" "image/color" "image/png" "log" "net" "net/http" "strconv" "amadan.net/rastrillo/aviso" "amadan.net/rastrillo/pwa" ) func main() { addr := flag.String("addr", "127.0.0.1:8080", "listen address") flag.Parse() mux := http.NewServeMux() manifest, err := (pwa.Manifest{ID: "/", Name: "PWA Example", StartURL: "/", Scope: "/", ThemeColor: "#234d45", BackgroundColor: "#ffffff", Icons: []pwa.Icon{ {Src: "/icon/192", Sizes: "192x192", Type: "image/png"}, {Src: "/icon/512", Sizes: "512x512", Type: "image/png"}, }, }).Handler() if err != nil { log.Fatal(err) } mux.Handle("/manifest.webmanifest", manifest) mux.Handle("/pwa/", http.StripPrefix("/pwa", pwa.Assets())) mux.HandleFunc("GET /aviso-sw.js", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/javascript") w.Header().Set("Cache-Control", "no-cache") _, _ = w.Write(aviso.WorkerJS()) }) mux.HandleFunc("GET /sw.js", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/javascript") w.Header().Set("Cache-Control", "no-cache") fmt.Fprint(w, `importScripts("/pwa/worker.js", "/aviso-sw.js"); RastrilloPWA.install(); self.addEventListener("push", e => e.waitUntil(AvisoSW.handlePush(e, { fallback: () => ({title: "New activity", options: {data: {url: "/"}}}) }))); self.addEventListener("notificationclick", e => e.waitUntil(AvisoSW.handleClick(e, {fallbackURL: "/"}))); `) }) mux.HandleFunc("GET /icon/{size}", func(w http.ResponseWriter, r *http.Request) { size, _ := strconv.Atoi(r.PathValue("size")) if size != 180 && size != 192 && size != 512 { http.NotFound(w, r) return } im := image.NewRGBA(image.Rect(0, 0, size, size)) for y := 0; y < size; y++ { for x := 0; x < size; x++ { im.Set(x, y, color.RGBA{35, 77, 69, 255}) } } w.Header().Set("Content-Type", "image/png") _ = png.Encode(w, im) }) mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Cache-Control", "no-store") fmt.Fprint(w, ` PWA Example

PWA Example

Add this app to your Home Screen or use your browser's install option.

Open it once online. Then disconnect and reload to see the offline page.

`) }) listener, err := net.Listen("tcp", *addr) if err != nil { log.Fatal(err) } fmt.Printf("http://%s\n", listener.Addr()) log.Fatal(http.Serve(listener, mux)) }