Example #1
0
func backend(w http.ResponseWriter, r *http.Request) {

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

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

	tplExec(w, r)

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

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

	err := sc.Increment(c, path)
	util_err.Err_http(w, r, err, false)

	cntr, err := sc.Count(w, r, path)
	util_err.Err_http(w, r, err, false)

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

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

	c := appengine.NewContext(r)
	_ = c

	// CIRCULAR DEPENDENCY PROBLEM

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

	// err := sc.Increment(c, path)
	// util_err.Err_http(w, r, err, false)

	// cntr, err := sc.Count(w, r, path)
	// util_err.Err_http(w, r, err, false)
	cntr := 1

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

		<p><a href='/instance-info/`+ii.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
	*/
	util_err.SuppressPanicUponDoubleRegistration(w, r, "/instance-info/"+ii.InstanceID,
		util_appengine.Adapter(view))

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

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

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

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

	add("n_cont_0", tpl_html.PrefixLff+"chart_body", map[string]map[string]string{"legend": ml})
	add("tpl_legend", tpl_html.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)

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

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

	err := sc.Increment(c, path)
	util_err.Err_http(w, r, err, false)

	cntr, err := sc.Count(w, r, path)
	util_err.Err_http(w, r, err, false)

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

	tplAdder, tplExec := tpl_html.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)

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

	c := appengine.NewContext(r)

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

	err := sc.Increment(c, path)
	util_err.Err_http(w, r, err, false)

	cntr, err := sc.Count(w, r, path)
	util_err.Err_http(w, r, err, false)

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

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

	tplExec(w, r)

}
Example #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 = ss.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
			}
			//c.Infof("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([]ss.Order, len(myB0.VB1[i1].VB2))
		myB0.VB1[i1].BySize = ss.ByInt(sh1)
		sh2 := make([]ss.Order, len(myB0.VB1[i1].VB2))
		myB0.VB1[i1].ByHeading = ss.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)
		c.Infof("-- Sorting %v", myB0.VB1[i1].Heading)
		// for i, v := range myB0.VB1[i1].BySize {
		// 	c.Infof("---- %v %v %v", i, v.IdxSrc, v.ByI)
		// }
		// for i, v := range myB0.VB1[i1].ByHeading {
		// 	c.Infof("---- %v %v %v", i, v.IdxSrc, v.ByS)
		// }
	}

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

	err := sc.Increment(c, path)
	util_err.Err_http(w, r, err, false)

	cntr, err := sc.Count(w, r, path)
	util_err.Err_http(w, r, err, false)

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

	add("n_cont_0", "<style>"+util.CSSColumnsWidth(nColsViewport)+"</style>", "")
	add("n_cont_1", tpl_html.PrefixLff+"backend3_body", myB0)
	add("tpl_legend", tpl_html.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)

}
func handleFetchURL(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)

	if r.URL.Scheme != "https" && !util_appengine.IsLocalEnviron() {
		r.URL.Scheme = "https"
		r.URL.Host = r.Host
		http.Redirect(w, r, r.URL.String(), http.StatusFound)
	}

	rURL := ""
	urlAsPost := ""

	/*
		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"
	*/
	if r.PostFormValue("url") != "" {
		urlAsPost = "url posted"
		rURL = r.PostFormValue("url")
	}

	if r.FormValue("url") != "" {
		if rURL == "" {
			urlAsPost = "url getted"
			rURL = r.FormValue("url")
		}
	}

	renderInPre := false
	if len(r.FormValue("renderInPre")) > 0 {
		renderInPre = true
	}

	elipseOutput := r.FormValue("elipseOutput")

	var msg, cntnt string

	if len(rURL) == 0 {

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

		m := map[string]string{"protocol": "https", "host": r.Host, "path": fetchURL, "val": "google.com"}
		if util_appengine.IsLocalEnviron() {
			m["protocol"] = "http"
		}
		tplAdder("n_cont_0", c_formFetchURL, m)
		tplExec(w, r)

	} else {

		w.Header().Set("Content-type", "text/html; charset=utf-8")
		// w.Header().Set("Content-type", "text/html; charset=latin-1")

		if !strings.HasPrefix(rURL, "http://") && !strings.HasPrefix(rURL, "https://") {
			rURL = "https://" + rURL
		}

		u, err := url.Parse(rURL)
		if err != nil {
			panic(err)
		}
		host, port, err = net.SplitHostPort(u.Host)
		if err != nil {
			host = u.Host
		}
		log.Println("host and port: ", host, port, "of", rURL, "standalone:", u.Host)

		client := urlfetch.Client(c)
		resp, err := client.Get(rURL)

		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		if resp.StatusCode != http.StatusOK {
			fmt.Fprintf(w, "HTTP GET returned status %v<br>\n\n", resp.Status)
			return
		}

		defer resp.Body.Close()
		byteContent, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			c.Errorf("%s", err)
			fmt.Fprintf(w, "Error %v<br>\n\n", err.Error())
			return
		} else {
			msg = fmt.Sprintf("%v bytes read<br>", len(byteContent))
			if urlAsPost != "" {
				msg += fmt.Sprintf("get overwritten by post: %v <br>", urlAsPost)
			}
			cntnt = string(byteContent)
		}

		cntnt = insertNewlines.Replace(cntnt)
		cntnt = undouble.Replace(cntnt)

		if len(elipseOutput) > 0 {
			cutoff := util.Min(100, len(cntnt))
			fmt.Fprintf(w, "content is: <pre>"+cntnt[:cutoff]+" ... "+cntnt[len(cntnt)-cutoff:]+"</pre>")
		} else {
			if renderInPre {
				fmt.Fprintf(w, "content is: <pre>"+cntnt+"</pre>")
			} else {
				cntnt = ModifyHTML(r, cntnt)
				fmt.Fprintf(w, cntnt)
			}
		}

	}

	// cntnt = html.EscapeString(cntnt)

	fmt.Fprintf(w, " %s \n\n", msg)

}