github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/net/http/dedup/register_handlers.go (about) 1 package dedup 2 3 import ( 4 "bytes" 5 "fmt" 6 "net/http" 7 8 "github.com/pbberlin/tools/net/http/domclean2" 9 "github.com/pbberlin/tools/net/http/fetch" 10 "github.com/pbberlin/tools/net/http/htmlfrag" 11 "github.com/pbberlin/tools/net/http/loghttp" 12 "github.com/pbberlin/tools/net/http/routes" 13 "github.com/pbberlin/tools/net/http/tplx" 14 "golang.org/x/net/html" 15 "google.golang.org/appengine" 16 ) 17 18 // InitHandlers is called from outside, 19 // and makes the EndPoints available. 20 func InitHandlers() { 21 http.HandleFunc(routes.DedupURI, loghttp.Adapter(dedupHTTP)) 22 } 23 24 // BackendUIRendered returns a userinterface rendered to HTML 25 func BackendUIRendered() *bytes.Buffer { 26 var b1 = new(bytes.Buffer) 27 // htmlfrag.Wb(b1, "Deduplicate", "") 28 29 fullURL := fmt.Sprintf("%s?%s=%s&cnt=%v", routes.DedupURI, routes.URLParamKey, URLs[0], numTotal-1) 30 htmlfrag.Wb(b1, "Deduplicate", fullURL) 31 32 return b1 33 } 34 35 // dedupHTTP wraps Dedup() 36 func dedupHTTP(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 37 38 lg, b := loghttp.BuffLoggerUniversal(w, r) 39 closureOverBuf := func(bUnused *bytes.Buffer) { 40 loghttp.Pf(w, r, b.String()) 41 } 42 defer closureOverBuf(b) // the argument is ignored, 43 44 r.Header.Set("X-Custom-Header-Counter", "nocounter") 45 46 wpf(b, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "Deduplicating redundant stuff"})) 47 defer wpf(b, tplx.Foot) 48 49 wpf(b, "<pre>") 50 defer wpf(b, "</pre>") 51 52 err := r.ParseForm() 53 lg(err) 54 55 surl := r.FormValue(routes.URLParamKey) 56 ourl, err := fetch.URLFromString(surl) 57 lg(err) 58 if err != nil { 59 return 60 } 61 if ourl.Host == "" { 62 lg("host is empty (%v)", surl) 63 return 64 } 65 66 knownProtocol := "" 67 if r.FormValue("prot") != "" { 68 knownProtocol = r.FormValue("prot") 69 } 70 71 lg("Host %q, Path %q", ourl.Host, ourl.Path) 72 73 fs := GetFS(appengine.NewContext(r), 0) 74 75 least3Files := FetchAndDecodeJSON(r, ourl.String(), knownProtocol, lg, fs) 76 77 lg("Fetched and decoded; found %v", len(least3Files)) 78 if len(least3Files) > 0 { 79 doc := Dedup(ourl, least3Files, lg, fs) 80 81 fNamer := domclean2.FileNamer(logDir, 0) 82 fNamer() // first call yields key 83 fsPerm := GetFS(appengine.NewContext(r), 0) 84 fileDump(lg, fsPerm, doc, fNamer, "_fin.html") 85 86 lg("MapSimiliarCompares: %v SimpleCompares: %v LevenstheinComp: %v\n", breakMapsTooDistinct, appliedLevenshtein, appliedCompare) 87 lg("Finish\n") 88 89 var b2 bytes.Buffer 90 err := html.Render(&b2, doc) 91 lg(err) 92 if err != nil { 93 return 94 } 95 96 b = new(bytes.Buffer) 97 // w.Write([]byte("aa")) 98 w.Header().Set("Content-type", "text/html; charset=utf-8") 99 w.Write(b2.Bytes()) 100 101 } 102 103 }