code reorg, comments etc

This commit is contained in:
Antoni Sawicki 2024-07-08 21:36:32 -07:00
parent 2f667e447c
commit db4ed0d811
4 changed files with 18 additions and 23 deletions

11
cdp.go
View File

@ -1,3 +1,4 @@
// WRP ISMAP / ChromeDP routines
package main package main
import ( import (
@ -111,7 +112,7 @@ func ctxErr(err error, w io.Writer) {
// https://github.com/chromedp/chromedp/issues/979 // https://github.com/chromedp/chromedp/issues/979
func chromedpCaptureScreenshot(res *[]byte, h int64) chromedp.Action { func chromedpCaptureScreenshot(res *[]byte, h int64) chromedp.Action {
if res == nil { if res == nil {
panic("res cannot be nil") panic("res cannot be nil") // TODO: do not panic here, return error
} }
if h == 0 { if h == 0 {
return chromedp.CaptureScreenshot(res) return chromedp.CaptureScreenshot(res)
@ -223,7 +224,7 @@ func (rq *wrpReq) captureScreenshot() {
iH = i.Bounds().Max.Y iH = i.Bounds().Max.Y
log.Printf("%s Encoded JPG image: %s, Size: %s, Quality: %d, Res: %dx%d, Time: %vms\n", rq.r.RemoteAddr, imgPath, sSize, *jpgQual, iW, iH, time.Since(st).Milliseconds()) log.Printf("%s Encoded JPG image: %s, Size: %s, Quality: %d, Res: %dx%d, Time: %vms\n", rq.r.RemoteAddr, imgPath, sSize, *jpgQual, iW, iH, time.Since(st).Milliseconds())
} }
rq.printHTML(printParams{ rq.printUI(uiParams{
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,
@ -235,7 +236,6 @@ func (rq *wrpReq) captureScreenshot() {
log.Printf("%s Done with capture for %s\n", rq.r.RemoteAddr, rq.url) log.Printf("%s Done with capture for %s\n", rq.r.RemoteAddr, rq.url)
} }
// Process HTTP requests to ISMAP '/map/' url
func mapServer(w http.ResponseWriter, r *http.Request) { func mapServer(w http.ResponseWriter, r *http.Request) {
log.Printf("%s ISMAP Request for %s [%+v]\n", r.RemoteAddr, r.URL.Path, r.URL.RawQuery) log.Printf("%s ISMAP Request for %s [%+v]\n", r.RemoteAddr, r.URL.Path, r.URL.RawQuery)
rq, ok := ismap[r.URL.Path] rq, ok := ismap[r.URL.Path]
@ -257,16 +257,15 @@ func mapServer(w http.ResponseWriter, r *http.Request) {
} }
log.Printf("%s WrpReq from ISMAP: %+v\n", r.RemoteAddr, rq) log.Printf("%s WrpReq from ISMAP: %+v\n", r.RemoteAddr, rq)
if len(rq.url) < 4 { if len(rq.url) < 4 {
rq.printHTML(printParams{bgColor: "#FFFFFF"}) rq.printUI(uiParams{bgColor: "#FFFFFF"})
return return
} }
rq.navigate() // TODO: if error from navigate do not capture rq.navigate() // TODO: if error from navigate do not capture
rq.captureScreenshot() rq.captureScreenshot()
} }
// Process HTTP requests for images '/img/' url
// TODO: merge this with html mode IMGZ // TODO: merge this with html mode IMGZ
func imgServer(w http.ResponseWriter, r *http.Request) { func imgServerMap(w http.ResponseWriter, r *http.Request) {
log.Printf("%s IMG Request for %s\n", r.RemoteAddr, r.URL.Path) log.Printf("%s IMG Request for %s\n", r.RemoteAddr, r.URL.Path)
imgBuf, ok := img[r.URL.Path] imgBuf, ok := img[r.URL.Path]
if !ok || imgBuf.Bytes() == nil { if !ok || imgBuf.Bytes() == nil {

6
txt.go
View File

@ -1,8 +1,10 @@
// WRP TXT / Simple HTML Mode Routines
package main package main
// TODO: // TODO:
// - image type based on form value // - image type based on form value
// - also size and quality // - also size and quality
// - imgOpt image quality
// - non overlaping image names atomic.int etc // - non overlaping image names atomic.int etc
// - garbage collector / delete old images from map // - garbage collector / delete old images from map
// - add referer header // - add referer header
@ -210,13 +212,13 @@ func (rq *wrpReq) captureMarkdown() {
return return
} }
log.Printf("Rendered %v bytes html for %v", len(ht.String()), rq.url) log.Printf("Rendered %v bytes html for %v", len(ht.String()), rq.url)
rq.printHTML(printParams{ rq.printUI(uiParams{
text: string(asciify([]byte(ht.String()))), text: string(asciify([]byte(ht.String()))),
bgColor: "#FFFFFF", bgColor: "#FFFFFF",
}) })
} }
func imgServerZ(w http.ResponseWriter, r *http.Request) { func imgServerTxt(w http.ResponseWriter, r *http.Request) {
log.Printf("%s IMGZ Request for %s", r.RemoteAddr, r.URL.Path) log.Printf("%s IMGZ Request for %s", r.RemoteAddr, r.URL.Path)
id := strings.Replace(r.URL.Path, imgZpfx, "", 1) id := strings.Replace(r.URL.Path, imgZpfx, "", 1)
img, err := imgStor.get(id) img, err := imgStor.get(id)

View File

@ -12,7 +12,7 @@ import (
"github.com/soniakeys/quant/median" "github.com/soniakeys/quant/median"
) )
func printIPs(b string) { func printMyIPs(b string) {
ap := strings.Split(b, ":") ap := strings.Split(b, ":")
if len(ap) < 1 { if len(ap) < 1 {
log.Fatal("Wrong format of ipaddress:port") log.Fatal("Wrong format of ipaddress:port")

22
wrp.go
View File

@ -87,7 +87,7 @@ type uiData struct {
} }
// Parameters for HTML print function // Parameters for HTML print function
type printParams struct { type uiParams struct {
bgColor string bgColor string
pageHeight string pageHeight string
imgSize string imgSize string
@ -117,7 +117,6 @@ type wrpReq struct {
r *http.Request r *http.Request
} }
// Parse HTML Form, Process Input Boxes, Etc.
func (rq *wrpReq) parseForm() { func (rq *wrpReq) parseForm() {
rq.r.ParseForm() rq.r.ParseForm()
rq.wrpMode = rq.r.FormValue("m") rq.wrpMode = rq.r.FormValue("m")
@ -163,8 +162,7 @@ func (rq *wrpReq) parseForm() {
log.Printf("%s WrpReq from UI Form: %+v\n", rq.r.RemoteAddr, rq) log.Printf("%s WrpReq from UI Form: %+v\n", rq.r.RemoteAddr, rq)
} }
// Display WP UI func (rq *wrpReq) printUI(p uiParams) {
func (rq *wrpReq) printHTML(p printParams) {
rq.w.Header().Set("Cache-Control", "max-age=0") rq.w.Header().Set("Cache-Control", "max-age=0")
rq.w.Header().Set("Expires", "-1") rq.w.Header().Set("Expires", "-1")
rq.w.Header().Set("Pragma", "no-cache") rq.w.Header().Set("Pragma", "no-cache")
@ -197,7 +195,6 @@ func (rq *wrpReq) printHTML(p printParams) {
} }
} }
// Process HTTP requests to WRP '/' url
func pageServer(w http.ResponseWriter, r *http.Request) { func pageServer(w http.ResponseWriter, r *http.Request) {
log.Printf("%s Page Request for %s [%+v]\n", r.RemoteAddr, r.URL.Path, r.URL.RawQuery) log.Printf("%s Page Request for %s [%+v]\n", r.RemoteAddr, r.URL.Path, r.URL.RawQuery)
rq := wrpReq{ rq := wrpReq{
@ -206,7 +203,7 @@ func pageServer(w http.ResponseWriter, r *http.Request) {
} }
rq.parseForm() rq.parseForm()
if len(rq.url) < 4 { if len(rq.url) < 4 {
rq.printHTML(printParams{bgColor: "#FFFFFF"}) rq.printUI(uiParams{bgColor: "#FFFFFF"})
return return
} }
rq.navigate() // TODO: if error from navigate do not capture rq.navigate() // TODO: if error from navigate do not capture
@ -217,7 +214,6 @@ func pageServer(w http.ResponseWriter, r *http.Request) {
rq.captureScreenshot() rq.captureScreenshot()
} }
// Process HTTP requests for Shutdown via '/shutdown/' url
func haltServer(w http.ResponseWriter, r *http.Request) { func haltServer(w http.ResponseWriter, r *http.Request) {
log.Printf("%s Shutdown Request for %s\n", r.RemoteAddr, r.URL.Path) log.Printf("%s Shutdown Request for %s\n", r.RemoteAddr, r.URL.Path)
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
@ -230,8 +226,7 @@ func haltServer(w http.ResponseWriter, r *http.Request) {
os.Exit(1) os.Exit(1)
} }
// returns html template, either from html file or built-in func wrpTemplate(t string) string {
func tmpl(t string) string {
var tmpl []byte var tmpl []byte
fh, err := os.Open(t) fh, err := os.Open(t)
if err != nil { if err != nil {
@ -261,7 +256,6 @@ builtin:
return string(tmpl) return string(tmpl)
} }
// Main
func main() { func main() {
var err error var err error
log.SetFlags(log.LstdFlags | log.Lshortfile) log.SetFlags(log.LstdFlags | log.Lshortfile)
@ -271,7 +265,7 @@ func main() {
if len(os.Getenv("PORT")) > 0 { if len(os.Getenv("PORT")) > 0 {
*addr = ":" + os.Getenv(("PORT")) *addr = ":" + os.Getenv(("PORT"))
} }
printIPs(*addr) printMyIPs(*addr)
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)
@ -294,14 +288,14 @@ func main() {
http.HandleFunc("/", pageServer) http.HandleFunc("/", pageServer)
http.HandleFunc("/map/", mapServer) http.HandleFunc("/map/", mapServer)
http.HandleFunc("/img/", imgServer) http.HandleFunc("/img/", imgServerMap)
http.HandleFunc(imgZpfx, imgServerZ) http.HandleFunc(imgZpfx, imgServerTxt)
http.HandleFunc("/shutdown/", haltServer) http.HandleFunc("/shutdown/", haltServer)
http.HandleFunc("/favicon.ico", http.NotFound) http.HandleFunc("/favicon.ico", http.NotFound)
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(*htmFnam)) htmlTmpl, err = template.New("wrp.html").Parse(wrpTemplate(*htmFnam))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }