add more camels

This commit is contained in:
Antoni Sawicki 2020-10-31 08:51:20 -07:00
parent 56ac414405
commit 2a22cfd755
2 changed files with 45 additions and 46 deletions

83
wrp.go
View File

@ -48,9 +48,9 @@ var (
cancel context.CancelFunc cancel context.CancelFunc
img = make(map[string]bytes.Buffer) img = make(map[string]bytes.Buffer)
ismap = make(map[string]wrpReq) ismap = make(map[string]wrpReq)
nodel bool noDel bool
deftype string defType string
defgeom geom defGeom geom
htmlTmpl *template.Template htmlTmpl *template.Template
) )
@ -64,29 +64,29 @@ type geom struct {
type uiData struct { type uiData struct {
Version string Version string
URL string URL string
Bgcolor string BgColor string
NColors int64 NColors int64
Width int64 Width int64
Height int64 Height int64
Scale float64 Scale float64
ImgType string ImgType string
ImgUrl string ImgURL string
ImgSize string ImgSize string
ImgWidth int ImgWidth int
ImgHeight int ImgHeight int
MapUrl string MapURL string
PageHeight string PageHeight string
} }
// Parameters for HTML print function // Parameters for HTML print function
type printParams struct { type printParams struct {
bgcolor string bgColor string
pageheight string pageHeight string
imgsize string imgSize string
imgurl string imgURL string
mapurl string mapURL string
imgwidth int imgWidth int
imgheight int imgHeight int
} }
// WRP Request // WRP Request
@ -115,8 +115,8 @@ func (w *wrpReq) parseForm() {
w.width, _ = strconv.ParseInt(w.req.FormValue("w"), 10, 64) w.width, _ = strconv.ParseInt(w.req.FormValue("w"), 10, 64)
w.height, _ = strconv.ParseInt(w.req.FormValue("h"), 10, 64) w.height, _ = strconv.ParseInt(w.req.FormValue("h"), 10, 64)
if w.width < 10 && w.height < 10 { if w.width < 10 && w.height < 10 {
w.width = defgeom.w w.width = defGeom.w
w.height = defgeom.h w.height = defGeom.h
} }
w.scale, _ = strconv.ParseFloat(w.req.FormValue("s"), 64) w.scale, _ = strconv.ParseFloat(w.req.FormValue("s"), 64)
if w.scale < 0.1 { if w.scale < 0.1 {
@ -124,20 +124,19 @@ func (w *wrpReq) parseForm() {
} }
w.colors, _ = strconv.ParseInt(w.req.FormValue("c"), 10, 64) w.colors, _ = strconv.ParseInt(w.req.FormValue("c"), 10, 64)
if w.colors < 2 || w.colors > 256 { if w.colors < 2 || w.colors > 256 {
w.colors = defgeom.c w.colors = defGeom.c
} }
w.keys = w.req.FormValue("k") w.keys = w.req.FormValue("k")
w.buttons = w.req.FormValue("Fn") w.buttons = w.req.FormValue("Fn")
w.imgType = w.req.FormValue("t") w.imgType = w.req.FormValue("t")
if w.imgType != "gif" && w.imgType != "png" { if w.imgType != "gif" && w.imgType != "png" {
w.imgType = deftype w.imgType = defType
} }
log.Printf("%s WrpReq from UI Form: %+v\n", w.req.RemoteAddr, w) log.Printf("%s WrpReq from UI Form: %+v\n", w.req.RemoteAddr, w)
} }
// Display WP UI // Display WP UI
// TODO: make this in to an external template func (w wrpReq) printHTML(p printParams) {
func (w wrpReq) printHtml(p printParams) {
w.out.Header().Set("Cache-Control", "max-age=0") w.out.Header().Set("Cache-Control", "max-age=0")
w.out.Header().Set("Expires", "-1") w.out.Header().Set("Expires", "-1")
w.out.Header().Set("Pragma", "no-cache") w.out.Header().Set("Pragma", "no-cache")
@ -145,18 +144,18 @@ func (w wrpReq) printHtml(p printParams) {
data := uiData{ data := uiData{
Version: version, Version: version,
URL: w.url, URL: w.url,
Bgcolor: p.bgcolor, BgColor: p.bgColor,
Width: w.width, Width: w.width,
Height: w.height, Height: w.height,
NColors: w.colors, NColors: w.colors,
Scale: w.scale, Scale: w.scale,
ImgType: w.imgType, ImgType: w.imgType,
ImgSize: p.imgsize, ImgSize: p.imgSize,
ImgWidth: p.imgwidth, ImgWidth: p.imgWidth,
ImgHeight: p.imgheight, ImgHeight: p.imgHeight,
ImgUrl: p.imgurl, ImgURL: p.imgURL,
MapUrl: p.mapurl, MapURL: p.mapURL,
PageHeight: p.pageheight, PageHeight: p.pageHeight,
} }
err := htmlTmpl.Execute(w.out, data) err := htmlTmpl.Execute(w.out, data)
if err != nil { if err != nil {
@ -172,7 +171,7 @@ func pageServer(out http.ResponseWriter, req *http.Request) {
w.out = out w.out = out
w.parseForm() w.parseForm()
if len(w.url) < 4 { if len(w.url) < 4 {
w.printHtml(printParams{bgcolor: "#FFFFFF"}) w.printHTML(printParams{bgColor: "#FFFFFF"})
return return
} }
w.navigate() w.navigate()
@ -190,7 +189,7 @@ func mapServer(out http.ResponseWriter, req *http.Request) {
log.Printf("Unable to find map %s\n", req.URL.Path) log.Printf("Unable to find map %s\n", req.URL.Path)
return return
} }
if !nodel { if !noDel {
defer delete(ismap, req.URL.Path) defer delete(ismap, req.URL.Path)
} }
n, err := fmt.Sscanf(req.URL.RawQuery, "%d,%d", &w.mouseX, &w.mouseY) n, err := fmt.Sscanf(req.URL.RawQuery, "%d,%d", &w.mouseX, &w.mouseY)
@ -201,7 +200,7 @@ func mapServer(out http.ResponseWriter, req *http.Request) {
} }
log.Printf("%s WrpReq from ISMAP: %+v\n", req.RemoteAddr, w) log.Printf("%s WrpReq from ISMAP: %+v\n", req.RemoteAddr, w)
if len(w.url) < 4 { if len(w.url) < 4 {
w.printHtml(printParams{bgcolor: "#FFFFFF"}) w.printHTML(printParams{bgColor: "#FFFFFF"})
return return
} }
w.navigate() w.navigate()
@ -217,7 +216,7 @@ func imgServer(out http.ResponseWriter, req *http.Request) {
log.Printf("%s Unable to find image %s\n", req.RemoteAddr, req.URL.Path) log.Printf("%s Unable to find image %s\n", req.RemoteAddr, req.URL.Path)
return return
} }
if !nodel { if !noDel {
defer delete(img, req.URL.Path) defer delete(img, req.URL.Path)
} }
switch { switch {
@ -386,14 +385,14 @@ func (w wrpReq) capture() {
ih = cfg.Height ih = cfg.Height
log.Printf("%s Got PNG image: %s, Size: %s, %dx%d\n", w.req.RemoteAddr, imgpath, ssize, iw, ih) log.Printf("%s Got PNG image: %s, Size: %s, %dx%d\n", w.req.RemoteAddr, imgpath, ssize, iw, ih)
} }
w.printHtml(printParams{ w.printHTML(printParams{
bgcolor: fmt.Sprintf("#%02X%02X%02X", r, g, b), bgColor: fmt.Sprintf("#%02X%02X%02X", r, g, b),
pageheight: fmt.Sprintf("%d PX", h), pageHeight: fmt.Sprintf("%d PX", h),
imgsize: ssize, imgSize: ssize,
imgurl: imgpath, imgURL: imgpath,
mapurl: mappath, mapURL: mappath,
imgwidth: iw, imgWidth: iw,
imgheight: ih, imgHeight: ih,
}) })
log.Printf("%s Done with capture for %s\n", w.req.RemoteAddr, w.url) log.Printf("%s Done with capture for %s\n", w.req.RemoteAddr, w.url)
} }
@ -438,15 +437,15 @@ func main() {
flag.StringVar(&addr, "l", ":8080", "Listen address:port, default :8080") flag.StringVar(&addr, "l", ":8080", "Listen address:port, default :8080")
flag.BoolVar(&headless, "h", true, "Headless mode - hide browser window") flag.BoolVar(&headless, "h", true, "Headless mode - hide browser window")
flag.BoolVar(&debug, "d", false, "Debug ChromeDP") flag.BoolVar(&debug, "d", false, "Debug ChromeDP")
flag.BoolVar(&nodel, "n", false, "Do not free maps and images after use") flag.BoolVar(&noDel, "n", false, "Do not free maps and images after use")
flag.StringVar(&deftype, "t", "gif", "Image type: gif|png") flag.StringVar(&defType, "t", "gif", "Image type: gif|png")
flag.StringVar(&fgeom, "g", "1152x600x256", "Geometry: width x height x colors, height can be 0 for unlimited") flag.StringVar(&fgeom, "g", "1152x600x256", "Geometry: width x height x colors, height can be 0 for unlimited")
flag.StringVar(&tHtml, "ui", "wrp.html", "HTML template file for the UI") flag.StringVar(&tHtml, "ui", "wrp.html", "HTML template file for the UI")
flag.Parse() flag.Parse()
if len(os.Getenv("PORT")) > 0 { if len(os.Getenv("PORT")) > 0 {
addr = ":" + os.Getenv(("PORT")) addr = ":" + os.Getenv(("PORT"))
} }
n, err := fmt.Sscanf(fgeom, "%dx%dx%d", &defgeom.w, &defgeom.h, &defgeom.c) n, err := fmt.Sscanf(fgeom, "%dx%dx%d", &defGeom.w, &defGeom.h, &defGeom.c)
if err != nil || n != 3 { if err != nil || n != 3 {
log.Fatalf("Unable to parse -g geometry flag / %s", err) log.Fatalf("Unable to parse -g geometry flag / %s", err)
} }
@ -479,7 +478,7 @@ func main() {
http.HandleFunc("/favicon.ico", http.NotFound) http.HandleFunc("/favicon.ico", http.NotFound)
log.Printf("Web Rendering Proxy Version %s\n", version) log.Printf("Web Rendering Proxy Version %s\n", version)
log.Printf("Args: %q", os.Args) log.Printf("Args: %q", os.Args)
log.Printf("Default Img Type: %v, Geometry: %+v", deftype, defgeom) log.Printf("Default Img Type: %v, Geometry: %+v", defType, defGeom)
htmlTmpl, err = template.New("wrp.html").Parse(tmpl(tHtml)) htmlTmpl, err = template.New("wrp.html").Parse(tmpl(tHtml))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -2,7 +2,7 @@
<HEAD> <HEAD>
<TITLE>WRP {{.URL}}</TITLE> <TITLE>WRP {{.URL}}</TITLE>
</HEAD> </HEAD>
<BODY BGCOLOR="{{.Bgcolor}}"> <BODY BGCOLOR="{{.BgColor}}">
<FORM ACTION="/" METHOD="POST"> <FORM ACTION="/" METHOD="POST">
<INPUT TYPE="TEXT" NAME="url" VALUE="{{.URL}}" SIZE="20"> <INPUT TYPE="TEXT" NAME="url" VALUE="{{.URL}}" SIZE="20">
<INPUT TYPE="SUBMIT" VALUE="Go"> <INPUT TYPE="SUBMIT" VALUE="Go">
@ -38,9 +38,9 @@
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="&gt;" SIZE="1"> <INPUT TYPE="SUBMIT" NAME="Fn" VALUE="&gt;" SIZE="1">
</FORM> </FORM>
<BR> <BR>
{{if .ImgUrl}} {{if .ImgURL}}
<A HREF="{{.MapUrl}}"> <A HREF="{{.MapURL}}">
<IMG SRC="{{.ImgUrl}}" BORDER="0" ALT="Url: {{.URL}}, Size: {{.ImgSize}} PageHeight: {{.PageHeight}}" WIDTH="{{.ImgWidth}}" HEIGHT="{{.ImgHeight}}" ISMAP> <IMG SRC="{{.ImgURL}}" BORDER="0" ALT="Url: {{.URL}}, Size: {{.ImgSize}} PageHeight: {{.PageHeight}}" WIDTH="{{.ImgWidth}}" HEIGHT="{{.ImgHeight}}" ISMAP>
</A> </A>
<P> <P>
{{end}} {{end}}