Exemple #1
0
func view(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	cntr := 1

	tplAdder, tplExec := tplx.FuncTplBuilder(w, r)
	tplAdder("n_html_title", "Application, Module and Instance Info", nil)
	tplAdder("n_cont_1", "<pre>{{.}}</pre>", instance_mgt.GetStatic().String())
	tplAdder("n_cont_2", "<p>{{.}} views</p>", cntr)
	tplAdder("n_cont_0", `
		<p>AppID is `+appengine.AppID(appengine.NewContext(r))+`</p>
		<p>On the development server, call 
		<a href='/instance-info/collect' 
		target='collect' >collect</a> first.</p>

		<p><a href='/instance-info/`+instance_mgt.GetStatic().InstanceID+`'>specific url</a></p>
		
		`, "")

	tplExec(w, r)

	/*
	 Requests are routed randomly accross instances

	 Following is just a futile try to register
	 an instance specific handler.
	 It is only useful, when we request an instance
	 specifically via specific hostname
	*/
	SuppressPanicUponDoubleRegistration(
		w, r, "/instance-info/"+instance_mgt.GetStatic().InstanceID, loghttp.Adapter(view))

}
Exemple #2
0
func backend(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	add, tplExec := tplx.FuncTplBuilder(w, r)

	add("n_html_title", "Saving an URL into the datastore", "")
	//add("n_cont_0", tplx.PrefixLff+"body_dsu_ancestored_urls", "")
	add("n_cont_0", tplx.PrefixLff+"body_last_url", "")

	tplExec(w, r)

}
Exemple #3
0
func guestEntry(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	c := appengine.NewContext(r)
	path := m["dir"].(string) + m["base"].(string)

	cntr, _ := sc.Count(c, path)

	tplAdder, tplExec := tplx.FuncTplBuilder(w, r)
	tplAdder("n_html_title", "New guest book entry", nil)
	tplAdder("n_cont_0", c_new_gbe, cntr)
	tplExec(w, r)

}
Exemple #4
0
func guestView(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	c := appengine.NewContext(r)
	path := m["dir"].(string) + m["base"].(string)

	cntr, _ := sc.Count(c, path)

	gbEntries, report := gbp.ListEntries(w, r)

	tplAdder, tplExec := tplx.FuncTplBuilder(w, r)
	tplAdder("n_html_title", "List of guest book entries", nil)
	tplAdder("n_cont_0", c_view_gbe, gbEntries)
	tplAdder("n_cont_1", "<pre>{{.}}</pre>", report)
	tplAdder("n_cont_2", "Visitors: {{.}}", cntr)
	tplExec(w, r)

}
Exemple #5
0
func backend2(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	c := appengine.NewContext(r)

	path := m["dir"].(string) + m["base"].(string)
	cntr, _ := sc.Count(c, path)

	add, tplExec := tplx.FuncTplBuilder(w, r)
	add("n_html_title", "Backend", nil)
	//add("n_cont_0", c_link, links)
	add("n_cont_0", tplx.PrefixLff+"backend_body", blocks2)
	add("tpl_legend", tplx.PrefixLff+"backend_body_embed01", "")

	//add("n_cont_1", "<pre>{{.}}</pre>", "pure text")
	add("n_cont_2", "<p>{{.}} views</p>", cntr)

	tplExec(w, r)

}
Exemple #6
0
func ViewHTML(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	b1, ml := disLegend(w, r)
	_ = b1

	add, tplExec := tplx.FuncTplBuilder(w, r)

	add("n_html_title", "The Battle of Computer Languages", "")

	add("n_cont_0", tplx.PrefixLff+"chart_body", map[string]map[string]string{"legend": ml})
	add("tpl_legend", tplx.PrefixLff+"chart_body_embed01", "")

	add("n_cont_1", `<a 
			target='openhub'
			href='https://www.openhub.net/languages/compare?measure=loc_changed&percent=true&l0=-1&l1=golang&l2=php&l3=python&l4=ruby&l5=-1&commit=Update' 
			>Here is a good comparison</a>`, "")

	tplExec(w, r)

}
Exemple #7
0
func backend3(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	c := appengine.NewContext(r)

	var nColsBlock = 4
	if r.FormValue("nColsBlock") != "" {
		nColsBlock = util.Stoi(r.FormValue("nColsBlock"))
	}
	var nColsViewport = 6
	if r.FormValue("nColsViewport") != "" {
		nColsViewport = util.Stoi(r.FormValue("nColsViewport"))
	}

	myB0.VB1 = X
	myB0.NumB1 = len(myB0.VB1)
	myB0.NumB2 = 0
	myB0.NColsViewport = nColsViewport

	// compute basic meta data
	for i1, _ := range myB0.VB1 {
		myB0.NumB2 += len(myB0.VB1[i1].VB2)
		for i2, _ := range myB0.VB1[i1].VB2 {
			// number of chars
			ro := myB0.VB1[i1].VB2[i2] // read only
			myB0.VB1[i1].VB2[i2].Size = len(ro.Linktext) + len(ro.Description)
			myB0.VB1[i1].VB2[i2].EditorialIndex = i2
		}
	}

	// compute NCols - NRows for the block
	for i1, _ := range myB0.VB1 {
		myB0.VB1[i1].NCols = nColsBlock
		if myB0.VB1[i1].NColsEditorial > 0 {
			myB0.VB1[i1].NCols = myB0.VB1[i1].NColsEditorial
		}
		if len(myB0.VB1[i1].VB2) < nColsBlock && len(myB0.VB1[i1].VB2) > 0 {
			myB0.VB1[i1].NCols = len(myB0.VB1[i1].VB2)
		}
		myB0.VB1[i1].NRows = complementRowsOrCols(len(myB0.VB1[i1].VB2), myB0.VB1[i1].NCols)
		myB0.VB1[i1].Discrepancy = myB0.VB1[i1].NCols*myB0.VB1[i1].NRows - len(myB0.VB1[i1].VB2)

		myB0.MaxNCols = util.Max(myB0.MaxNCols, myB0.VB1[i1].NCols)
		myB0.MaxNRows = util.Max(myB0.MaxNRows, myB0.VB1[i1].NRows)
	}

	// compute NCols - NRows - sizeup to MaxNRows
	for i1, _ := range myB0.VB1 {
		if myB0.VB1[i1].NRows < myB0.MaxNRows {
			myB0.VB1[i1].NRows = myB0.MaxNRows
			myB0.VB1[i1].NCols = complementRowsOrCols(len(myB0.VB1[i1].VB2), myB0.VB1[i1].NRows)
			myB0.VB1[i1].Discrepancy = myB0.VB1[i1].NCols*myB0.VB1[i1].NRows - len(myB0.VB1[i1].VB2)
		}
	}

	// is first or last
	for i1, _ := range myB0.VB1 {
		for i2, _ := range myB0.VB1[i1].VB2 {
			myB0.VB1[i1].VB2[i2].IsFirst = false
			myB0.VB1[i1].VB2[i2].IsLast = false
			if i2%myB0.VB1[i1].NCols == 0 {
				myB0.VB1[i1].VB2[i2].IsFirst = true
			}
			if i2%myB0.VB1[i1].NCols == (myB0.VB1[i1].NCols - 1) {
				myB0.VB1[i1].VB2[i2].IsLast = true
			}
			//aelog.Infof(c,"first-last %v %v \n", i2, i2%myB0.VB1[i1].NCols)
		}
	}

	// create slices with the data to be sorted
	for i1, _ := range myB0.VB1 {
		sh1 := make([]Order, len(myB0.VB1[i1].VB2))
		myB0.VB1[i1].BySize = ByInt(sh1)
		sh2 := make([]Order, len(myB0.VB1[i1].VB2))
		myB0.VB1[i1].ByHeading = ByStr(sh2)
		// fill in the data - to be sorted later
		for i2, _ := range myB0.VB1[i1].VB2 {
			ro := myB0.VB1[i1].VB2[i2] // read only
			myB0.VB1[i1].BySize[i2].IdxSrc = i2
			myB0.VB1[i1].BySize[i2].ByI = len(ro.Linktext) + len(ro.Description)
			myB0.VB1[i1].ByHeading[i2].IdxSrc = i2
			myB0.VB1[i1].ByHeading[i2].ByS = strings.ToLower(ro.Linktext)
		}
	}

	// actual rearranging of the sorting date
	for i1, _ := range myB0.VB1 {
		sort.Sort(myB0.VB1[i1].BySize)
		sort.Sort(myB0.VB1[i1].ByHeading)
		aelog.Infof(c, "-- Sorting %v", myB0.VB1[i1].Heading)
		// for i, v := range myB0.VB1[i1].BySize {
		// 	aelog.Infof(c,"---- %v %v %v", i, v.IdxSrc, v.ByI)
		// }
		// for i, v := range myB0.VB1[i1].ByHeading {
		// 	aelog.Infof(c,"---- %v %v %v", i, v.IdxSrc, v.ByS)
		// }
	}

	path := m["dir"].(string) + m["base"].(string)

	cntr, _ := sc.Count(c, path)

	add, tplExec := tplx.FuncTplBuilder(w, r)
	add("n_html_title", "Backend", nil)

	add("n_cont_0", "<style>"+htmlfrag.CSSColumnsWidth(nColsViewport)+"</style>", "")
	add("n_cont_1", tplx.PrefixLff+"backend3_body", myB0)
	add("tpl_legend", tplx.PrefixLff+"backend3_body_embed01", "")
	add("n_cont_2", "<p>{{.}} views</p>", cntr)

	sDumped := ""
	//sDumped = spew.Sdump(myB0)
	add("n_cont_3", "<pre>{{.}} </pre>", sDumped)

	tplExec(w, r)

}
Exemple #8
0
// handleFetchURL either displays a form for requesting an url
// or it returns the URL´s contents.
func handleFetchURL(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	lg, b := loghttp.BuffLoggerUniversal(w, r)
	_ = b

	// on live server => always use https
	if r.URL.Scheme != "https" && !util_appengine.IsLocalEnviron() {
		r.URL.Scheme = "https"
		r.URL.Host = r.Host
		lg("lo - redirect %v", r.URL.String())
		http.Redirect(w, r, r.URL.String(), http.StatusFound)
	}

	/*
		To distinguish between posted and getted value,
		we check the "post-only" slice of values first.
		If nothing's there, but FormValue *has* a value,
		then it was "getted", otherwise "posted"
	*/
	rURL := ""
	urlAs := ""
	err := r.ParseForm()
	lg(err)
	if r.PostFormValue(routes.URLParamKey) != "" {
		urlAs += "url posted "
		rURL = r.PostFormValue(routes.URLParamKey)
	}

	if r.FormValue(routes.URLParamKey) != "" {
		if rURL == "" {
			urlAs += "url getted "
			rURL = r.FormValue(routes.URLParamKey)
		}
	}
	// lg("received %v:  %q", urlAs, rURL)

	if len(rURL) == 0 {

		tplAdder, tplExec := tplx.FuncTplBuilder(w, r)
		tplAdder("n_html_title", "Fetch some http data", nil)

		m := map[string]string{
			"protocol": "https",
			"host":     r.Host, // not  fetch.HostFromReq(r)
			"path":     routes.ProxifyURI,
			"name":     routes.URLParamKey,
			"val":      "google.com",
		}
		if util_appengine.IsLocalEnviron() {
			m["protocol"] = "http"
		}
		tplAdder("n_cont_0", c_formFetchUrl, m)
		tplExec(w, r)

	} else {

		r.Header.Set("X-Custom-Header-Counter", "nocounter")

		bts, inf, err := fetch.UrlGetter(r, fetch.Options{URL: rURL})
		lg(err)

		tp := mime.TypeByExtension(path.Ext(inf.URL.Path))
		if false {
			ext := path.Ext(rURL)
			ext = strings.ToLower(ext)
			tp = mime.TypeByExtension(ext)
		}
		w.Header().Set("Content-Type", tp)
		// w.Header().Set("Content-type", "text/html; charset=latin-1")

		if r.FormValue("dbg") != "" {
			w.Header().Set("Content-type", "text/html; charset=utf-8")
			fmt.Fprintf(w, "%s<br>\n  %s<br>\n %v", inf.URL.Path, tp, inf.URL.String())
			return
		}

		opts := domclean2.CleaningOptions{Proxify: true}
		opts.Beautify = true // "<a> Linktext without trailing space"
		opts.RemoteHost = fetch.HostFromStringUrl(rURL)

		// opts.ProxyHost = routes.AppHost()
		opts.ProxyHost = fetch.HostFromReq(r)
		if !util_appengine.IsLocalEnviron() {
			opts.ProxyHost = fetch.HostFromReq(r)
		}

		doc, err := domclean2.DomClean(bts, opts)

		var bufRend bytes.Buffer
		err = html.Render(&bufRend, doc)
		lg(err)
		w.Write(bufRend.Bytes())

	}

}