From 878f43af75b74848edc6c76b24ded9c824640a93 Mon Sep 17 00:00:00 2001 From: Antoni Sawicki Date: Tue, 27 Oct 2020 04:35:57 -0700 Subject: [PATCH] add initial support for ui/html template --- wrp.go | 166 ++++++++++++++++++++++++++++--------------------------- wrp.html | 38 +++++++++++++ 2 files changed, 122 insertions(+), 82 deletions(-) create mode 100644 wrp.html diff --git a/wrp.go b/wrp.go index fe3298d..4f5d2cb 100644 --- a/wrp.go +++ b/wrp.go @@ -12,6 +12,7 @@ import ( "context" "flag" "fmt" + "html/template" "image" "image/gif" "image/png" @@ -36,15 +37,16 @@ import ( ) var ( - version = "4.5" - srv http.Server - ctx context.Context - cancel context.CancelFunc - img = make(map[string]bytes.Buffer) - ismap = make(map[string]wrpReq) - nodel bool - deftype string - defgeom geom + version = "4.5" + srv http.Server + ctx context.Context + cancel context.CancelFunc + img = make(map[string]bytes.Buffer) + ismap = make(map[string]wrpReq) + nodel bool + deftype string + defgeom geom + htmlTmpl *template.Template ) type geom struct { @@ -53,6 +55,35 @@ type geom struct { c int64 } +// Data for html template +type uiData struct { + Version string + URL string + Bgcolor string + NColors int64 + Width int64 + Height int64 + Scale float64 + ImgType string + ImgUrl string + ImgSize string + ImgWidth int + ImgHeight int + MapUrl string + PageHeight string +} + +// Parameters for HTML print function +type printParams struct { + bgcolor string + pageheight string + imgsize string + imgurl string + mapurl string + imgwidth int + imgheight int +} + // WRP Request type wrpReq struct { url string // url @@ -101,69 +132,31 @@ func (w *wrpReq) parseForm() { // Display WP UI // TODO: make this in to an external template -func (w wrpReq) printPage(bgcolor string) { - var s string +func (w wrpReq) printHtml(p printParams) { w.out.Header().Set("Cache-Control", "max-age=0") w.out.Header().Set("Expires", "-1") w.out.Header().Set("Pragma", "no-cache") w.out.Header().Set("Content-Type", "text/html") - fmt.Fprintf(w.out, "\n", version) - fmt.Fprintf(w.out, "\nWRP %s\n\n", w.url, bgcolor) - fmt.Fprintf(w.out, "
\n") - fmt.Fprintf(w.out, "", w.url) - fmt.Fprintf(w.out, "\n") - fmt.Fprintf(w.out, "\n") - fmt.Fprintf(w.out, "W \n", w.width) - fmt.Fprintf(w.out, "H \n", w.height) - fmt.Fprintf(w.out, "S \n") - fmt.Fprintf(w.out, "T \n") - fmt.Fprintf(w.out, "C \n", w.colors) - fmt.Fprintf(w.out, "K \n") - fmt.Fprintf(w.out, "\n") - fmt.Fprintf(w.out, "\n") - fmt.Fprintf(w.out, "\n") - fmt.Fprintf(w.out, "\n") - fmt.Fprintf(w.out, "\n") - fmt.Fprintf(w.out, "\n") - fmt.Fprintf(w.out, "

\n") -} - -// Status bar below captured image -func (w wrpReq) printFooter(h string, s string) { - fmt.Fprintf(w.out, - "\n

"+ - ""+ - "Web Rendering Proxy Version %s | "+ - "Shutdown WRP | "+ - "Page Height: %s |"+ - "Img Size: %s "+ - "\n\n", - w.width, - w.height, - w.scale, - w.colors, - w.imgType, - version, - h, - s) } // Process HTTP requests to WRP '/' url @@ -174,8 +167,7 @@ func pageServer(out http.ResponseWriter, req *http.Request) { w.out = out w.parseForm() if len(w.url) < 4 { - w.printPage("#FFFFFF") - w.printFooter("", "") + w.printHtml(printParams{bgcolor: "#FFFFFF"}) return } w.navigate() @@ -204,8 +196,7 @@ func mapServer(out http.ResponseWriter, req *http.Request) { } log.Printf("%s WrpReq from ISMAP: %+v\n", req.RemoteAddr, w) if len(w.url) < 4 { - w.printPage("#FFFFFF") - w.printFooter("", "") + w.printHtml(printParams{bgcolor: "#FFFFFF"}) return } w.navigate() @@ -314,6 +305,7 @@ func (w wrpReq) capture() { var pngcap []byte chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.scale), 10, w.scale, false), + chromedp.Sleep(time.Second*1), chromedp.Location(&w.url), chromedp.ComputedStyle("body", &styles, chromedp.ByQuery), chromedp.ActionFunc(func(ctx context.Context) error { @@ -330,7 +322,6 @@ func (w wrpReq) capture() { } } log.Printf("%s Landed on: %s, Height: %v\n", w.req.RemoteAddr, w.url, h) - w.printPage(fmt.Sprintf("#%02X%02X%02X", r, g, b)) height := int64(float64(w.height) / w.scale) if w.height == 0 && h > 0 { height = h + 30 @@ -338,7 +329,7 @@ func (w wrpReq) capture() { chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.scale), height, w.scale, false)) // Capture screenshot... err = chromedp.Run(ctx, - chromedp.Sleep(time.Second*2), + chromedp.Sleep(time.Second*1), chromedp.CaptureScreenshot(&pngcap), ) if err != nil { @@ -357,7 +348,7 @@ func (w wrpReq) capture() { mappath := fmt.Sprintf("/map/%04d.map", seq) ismap[mappath] = w var ssize string - var sw, sh int + var iw, ih int switch w.imgType { case "gif": i, err := png.Decode(bytes.NewReader(pngcap)) @@ -379,21 +370,28 @@ func (w wrpReq) capture() { } img[imgpath] = gifbuf ssize = fmt.Sprintf("%.0f KB", float32(len(gifbuf.Bytes()))/1024.0) - sw = i.Bounds().Max.X - sh = i.Bounds().Max.Y - log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, %dx%d\n", w.req.RemoteAddr, imgpath, ssize, w.colors, sw, sh) + iw = i.Bounds().Max.X + ih = i.Bounds().Max.Y + log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, %dx%d\n", w.req.RemoteAddr, imgpath, ssize, w.colors, iw, ih) case "png": pngbuf := bytes.NewBuffer(pngcap) img[imgpath] = *pngbuf cfg, _, _ := image.DecodeConfig(pngbuf) ssize = fmt.Sprintf("%.0f KB", float32(len(pngbuf.Bytes()))/1024.0) - sw = cfg.Width - sh = cfg.Height - log.Printf("%s Got PNG image: %s, Size: %s, %dx%d\n", w.req.RemoteAddr, imgpath, ssize, sw, sh) + iw = cfg.Width + ih = cfg.Height + log.Printf("%s Got PNG image: %s, Size: %s, %dx%d\n", w.req.RemoteAddr, imgpath, ssize, iw, ih) } - fmt.Fprintf(w.out, "\"Url:", mappath, imgpath, w.url, ssize, sw, sh) - w.printFooter(fmt.Sprintf("%d PX", h), ssize) - log.Printf("%s Done with caputure for %s\n", w.req.RemoteAddr, w.url) + w.printHtml(printParams{ + bgcolor: fmt.Sprintf("#%02X%02X%02X", r, g, b), + pageheight: fmt.Sprintf("%d PX", h), + imgsize: ssize, + imgurl: imgpath, + mapurl: mappath, + imgwidth: iw, + imgheight: ih, + }) + log.Printf("%s Done with capture for %s\n", w.req.RemoteAddr, w.url) } // Main... @@ -443,6 +441,10 @@ func main() { http.HandleFunc("/img/", imgServer) http.HandleFunc("/shutdown/", haltServer) http.HandleFunc("/favicon.ico", http.NotFound) + htmlTmpl, err = template.New("wrp.html").ParseFiles("wrp.html") + if err != nil { + log.Fatal(err) + } log.Printf("Web Rendering Proxy Version %s\n", version) log.Printf("Args: %q", os.Args) log.Printf("Default Img Type: %v, Geometry: %+v", deftype, defgeom) diff --git a/wrp.html b/wrp.html new file mode 100644 index 0000000..db7955d --- /dev/null +++ b/wrp.html @@ -0,0 +1,38 @@ + + + WRP {{.URL}} + + +

+ + + + W + H + S + T + C + K + + + + + + +
+
+ {{if .ImgUrl}} + Url: {{.URL}}, Size: {{.ImgSize}} PageHeight: {{.PageHeight}} +

+ {{end}} + + Web Rendering Proxy {{.Version}} | + Shutdown WRP | + Page Height: {{.PageHeight}} | + Img Size: {{.ImgSize}} + + + \ No newline at end of file