本文整理汇总了Golang中github.com/pbberlin/tools/util_err.Err_http函数的典型用法代码示例。如果您正苦于以下问题:Golang Err_http函数的具体用法?Golang Err_http怎么用?Golang Err_http使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Err_http函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SaveEntry
func SaveEntry(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
contnt, ok := m["content"].(string)
util_err.Err_http(w, r, ok, false, "need a key 'content' with a string")
c := appengine.NewContext(r)
g := GbsaveEntry{
Content: contnt,
Date: time.Now(),
TypeShiftingField: 2,
}
if u := user.Current(c); u != nil {
g.Author = u.String()
}
g.Comment1 = "comment"
/* We set the same parent key on every GB entry entity
to ensure each GB entry is in the same entity group.
Queries across the single entity group will be consistent.
However, we should limit write rate to single entity group ~1/sec.
NewIncompleteKey(appengine.Context, kind string , parent *Key)
has neither a string key, nor an integer key
only a "kind" (classname) and a parent
Upon usage the datastore generates an integer key
*/
incomplete := ds.NewIncompleteKey(c, DSKindGBEntry, key_entity_group_key_parent(c))
concreteNewKey, err := ds.Put(c, incomplete, &g)
util_err.Err_http(w, r, err, false)
_ = concreteNewKey // we query entries via key_entity_group_key_parent - via parent
}
示例2: templatesExtend
// m contains defaults or overwritten template contents
func templatesExtend(w http.ResponseWriter, r *http.Request, subtemplates map[string]string) *tt.Template {
var err error = nil
tder := cloneFromBase(w, r)
//for k,v := range furtherDefinitions{
// tder, err = tder.Parse( `{{define "` + k +`"}}` + v + `{{end}}` )
// util_err.Err_http(w,r,err,false)
//}
for k, v := range subtemplates {
if len(v) > lenLff && v[0:lenLff] == PrefixLff {
fileName := v[lenLff:]
fcontent, err := ioutil.ReadFile("templates/" + fileName + ".html")
util_err.Err_http(w, r, err, false, "could not open static template file")
v = string(fcontent)
}
tder, err = tder.Parse(`{{define "` + k + `"}}` + v + `{{end}}`)
util_err.Err_http(w, r, err, false)
}
return tder
}
示例3: imagefileAsBase64
// output static file as base64 string
// image must exist as file in /static
func imagefileAsBase64(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
c := appengine.NewContext(r)
p := r.FormValue("p")
if p == "" {
p = "static/chartbg_400x960__480x1040__12x10.png"
}
f, err := os.Open(p)
util_err.Err_http(w, r, err, false)
defer f.Close()
img, whichFormat, err := image.Decode(f)
util_err.Err_http(w, r, err, false)
c.Infof("format %v - subtype %T\n", whichFormat, img)
imgRGBA, ok := img.(*image.RGBA)
util_err.Err_http(w, r, ok, true, "source image was not convertible to image.RGBA - gifs or jpeg?")
// => header with mime prefix always prepended
// and its always image/PNG
str_b64 := conv.Rgba_img_to_base64_str(imgRGBA)
w.Header().Set("Content-Type", "text/html")
io.WriteString(w, "<p>Image embedded in HTML as Base64:</p><img width=200px src=\"")
io.WriteString(w, str_b64)
io.WriteString(w, "\"> ")
}
示例4: templatesCompileDemo
func templatesCompileDemo(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
w.Header().Set("Content-Type", "text/html")
funcMap := tt.FuncMap{
"unescape": html.UnescapeString,
"escape": html.EscapeString,
}
var t_base *tt.Template
var err error = nil
// creating T0 - naming it - adding func map
t_base = tt.Must(tt.New("str_T0_outmost").Funcs(funcMap).Parse(T0))
util_err.Err_http(w, r, err, false)
// adding the definition of T1 - introducing reference to T2 - undefined yet
t_base, err = t_base.Parse(T1) // definitions must appear at top level - but not at the start
util_err.Err_http(w, r, err, false)
// create two clones
// now both containing T0 and T1
tc_1, err := t_base.Clone()
util_err.Err_http(w, r, err, false)
tc_2, err := t_base.Clone()
util_err.Err_http(w, r, err, false)
// adding different T2 definitions
s_if := "{{if .}}{{.}}{{else}}no dyn data{{end}}"
tc_1, err = tc_1.Parse("{{define `T2`}}T2-A <br>--" + s_if + "-- {{end}}")
util_err.Err_http(w, r, err, false)
tc_2, err = tc_2.Parse("{{define `T2`}}T2-B <br>--" + s_if + "-- {{end}}")
util_err.Err_http(w, r, err, false)
// writing both clones to the response writer
err = tc_1.ExecuteTemplate(w, "str_T0_outmost", nil)
util_err.Err_http(w, r, err, false)
// second clone is written with dynamic data on two levels
dyndata := map[string]string{"key1": "dyn val 1", "key2": "dyn val 2"}
err = tc_2.ExecuteTemplate(w, "str_T0_outmost", dyndata)
util_err.Err_http(w, r, err, false)
// Note: it is important to pass the DOT
// {{template "T1" .}}
// {{template "T2" .key2 }}
// ^
// otherwise "dyndata" can not be accessed by the inner templates...
// leaving T2 undefined => error
tc_3, err := t_base.Clone()
util_err.Err_http(w, r, err, false)
err = tc_3.ExecuteTemplate(w, "str_T0_outmost", dyndata)
util_err.Err_http(w, r, err, false)
}
示例5: submitUpload
func submitUpload(c appengine.Context, w http.ResponseWriter, r *http.Request) {
// c := appengine.NewContext(r)
uploadURL, err := blobstore.UploadURL(c, "/blob2/processing-new-upload", nil)
util_err.Err_http(w, r, err, false)
w.Header().Set("Content-type", "text/html; charset=utf-8")
err = upload2.Execute(w, uploadURL)
util_err.Err_http(w, r, err, false)
}
示例6: cloneFromBase
// a clone factory
// as t_base is a "app scope" variable
// it will live as long as the application runs
func cloneFromBase(w http.ResponseWriter, r *http.Request) *tt.Template {
funcMap := tt.FuncMap{
"unescape": html.UnescapeString,
"escape": html.EscapeString,
"fMult": fMult,
"fAdd": fAdd,
"fChop": fChop,
"fAccessElement": fAccessElement,
"fAccessElementB2": fAccessElementB2,
"fMakeRange": fMakeRange,
"fNumCols": fNumCols,
"df": func(g time.Time) string {
return g.Format("2006-01-02 (Jan 02)")
},
}
if t_base == nil {
t_base = tt.Must(tt.New("n_page_scaffold_01").Funcs(funcMap).Parse(c_page_scaffold_01))
}
t_derived, err := t_base.Clone()
util_err.Err_http(w, r, err, false)
return t_derived
}
示例7: emailSend
func emailSend(w http.ResponseWriter, r *http.Request, m map[string]string) {
c := appengine.NewContext(r)
//addr := r.FormValue("email")
if _, ok := m["subject"]; !ok {
m["subject"] = "empty subject line"
}
email_thread_id := []string{"3223"}
msg := &ae_mail.Message{
//Sender: "Peter Buchmann <[email protected]",
// Sender: "[email protected]",
Sender: "[email protected]",
//To: []string{addr},
To: []string{"[email protected]"},
Subject: m["subject"],
Body: "some_body some_body2",
Headers: go_mail.Header{"References": email_thread_id},
}
err := ae_mail.Send(c, msg)
util_err.Err_http(w, r, err, false, "could not send the email")
}
示例8: guestEntry
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)
}
示例9: imgServingExample3
func imgServingExample3(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
c := appengine.NewContext(r)
p := r.FormValue("p")
if p == "" {
p = "static/chartbg_400x960__480x1040__12x10.png"
}
if p == "" {
p = "static/pberg1.png"
}
// try p=static/unmodifiable_format.jpg
// prepare a cutout rect
var p1, p2 image.Point
p1.X, p1.Y = 10, 60
p2.X, p2.Y = 400, 255
var rec image.Rectangle = image.Rectangle{Min: p1, Max: p2}
f, err := os.Open(p)
util_err.Err_http(w, r, err, false)
defer f.Close()
img, whichFormat, err := image.Decode(f)
util_err.Err_http(w, r, err, false, "only jpeg and png are 'activated' ")
c.Infof("serving format %v %T\n", whichFormat, img)
switch t := img.(type) {
default:
util_err.Err_http(w, r, false, false, "internal color formats image.YCbCr and image.RGBA are understood")
case *image.RGBA, *image.YCbCr:
imgXFull, ok := t.(*image.RGBA)
util_err.Err_http(w, r, ok, false, "image.YCbCr can not be typed to image.RGBA - this will panic")
imgXCutout, ok := imgXFull.SubImage(rec).(*image.RGBA)
util_err.Err_http(w, r, ok, false, "cutout operation failed")
// we serve it as JPEG
w.Header().Set("Content-Type", "image/jpeg")
jpeg.Encode(w, imgXCutout, &jpeg.Options{Quality: jpeg.DefaultQuality})
}
}
示例10: GetImageFromDatastore
func GetImageFromDatastore(w http.ResponseWriter, r *http.Request, key string) *image.RGBA {
c := appengine.NewContext(r)
dsObj, err := dsu.BufGet(w, r, "dsu.WrapBlob__"+key)
util_err.Err_http(w, r, err, false)
s := string(dsObj.VByte)
img, whichFormat := conv.Base64_str_to_img(s)
c.Infof("retrieved img from base64: format %v - subtype %T\n", whichFormat, img)
i, ok := img.(*image.RGBA)
util_err.Err_http(w, r, ok, false, "saved image needs to be reconstructible into a format png of subtype *image.RGBA")
return i
}
示例11: call
func call(w http.ResponseWriter, r *http.Request) {
// params trial: https://www.twilio.com/user/account/developer-tools/api-explorer/call-create
// params docu: https://www.twilio.com/docs/api/rest/making-calls
v := url.Values{}
v.Set("To", phoneTo)
v.Set("From", phoneFrom)
v.Set("Url", callbackUrl)
v.Set("Method", "GET")
v.Set("FallbackMethod", "GET")
v.Set("StatusCallbackMethod", "GET")
// v.Set("SendDigits", "32168")
v.Set("Timeout", "4")
v.Set("Record", "false")
rb := *strings.NewReader(v.Encode())
// Create Client
// client := &http.Client{} // local, not on appengine
c := appengine.NewContext(r)
// following appengine method, derived from big-query is non working:
// config := oauth2_google.NewAppEngineConfig(c, []string{twilioURLPrefix})
// client := &http.Client{Transport: config.NewTransport()}
clientClassic := urlfetch.Client(c)
//clientTwilio := twilio.NewClient(accountSid, authToken, clientClassic) // not needed
req, _ := http.NewRequest("POST", twilioFullURL, &rb)
req.SetBasicAuth(accountSid, authToken)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := clientClassic.Do(req)
util_err.Err_http(w, r, err, false, "something wrong with the http client")
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var data map[string]interface{}
bodyBytes, _ := ioutil.ReadAll(resp.Body)
err := json.Unmarshal(bodyBytes, &data)
if err == nil {
fmt.Println(data["sid"])
}
fmt.Fprintf(w, "%#v", resp)
} else {
util_err.Err_http(w, r, err, false, "twilio response not ok", resp.Status)
}
}
示例12: imgServingExample1
func imgServingExample1(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
p := r.FormValue("p")
if p == "" {
p = "static/chartbg_400x960__480x1040__12x10.png"
}
if p == "" {
p = "static/pberg1.png"
}
f, err := os.Open(p)
util_err.Err_http(w, r, err, false)
defer f.Close()
mode := r.FormValue("mode")
if mode == "" {
mode = "direct"
}
if mode == "direct" {
// file reader directly to http writer
w.Header().Set("Content-Type", "image/png")
c.Infof("serving directly %v \n", p)
io.Copy(w, f)
} else {
// file to memory image - memory image to http writer
img, whichFormat, err := image.Decode(f)
util_err.Err_http(w, r, err, false)
c.Infof("serving as memory image %v - format %v - type %T\n", p, whichFormat, img)
// initially a png - we now encode it to jpg
w.Header().Set("Content-Type", "image/jpeg")
jpeg.Encode(w, img, &jpeg.Options{Quality: jpeg.DefaultQuality})
}
}
示例13: SaveImageToDatastore
func SaveImageToDatastore(w http.ResponseWriter, r *http.Request, i *image.RGBA, key string) string {
s := conv.Rgba_img_to_base64_str(i)
internalType := fmt.Sprintf("%T", i)
//buffBytes, _ := StringToVByte(s) // instead of []byte(s)
key_combi, err := dsu.BufPut(w, r, dsu.WrapBlob{Name: key, VByte: []byte(s), S: internalType}, key)
util_err.Err_http(w, r, err, false)
return key_combi
}
示例14: ListEntries
func ListEntries(w http.ResponseWriter, r *http.Request) (gbEntries []GbEntryRetr, report string) {
c := appengine.NewContext(r)
/* High Replication Datastore:
Ancestor queries are strongly consistent.
Queries spanning MULTIPLE entity groups are EVENTUALLY consistent.
If .Ancestor was omitted from this query, there would be slight chance
that recent GB entry would not show up in a query.
*/
q := ds.NewQuery(DSKindGBEntry).Ancestor(key_entity_group_key_parent(c)).Order("-Date").Limit(10)
gbEntries = make([]GbEntryRetr, 0, 10)
keys, err := q.GetAll(c, &gbEntries)
if fmt.Sprintf("%T", err) == fmt.Sprintf("%T", new(ds.ErrFieldMismatch)) {
//s := fmt.Sprintf("%v %T vs %v %T <br>\n",err,err,ds.ErrFieldMismatch{},ds.ErrFieldMismatch{})
util_err.Err_http(w, r, err, true)
err = nil // ignore this one - it's caused by our deliberate differences between gbsaveEntry and gbEntrieRetr
}
util_err.Err_http(w, r, err, false)
// for investigative purposes,
// we
var b1 bytes.Buffer
var sw string
var descrip []string = []string{"class", "path", "key_int_guestbk"}
for i0, v0 := range keys {
sKey := fmt.Sprintf("%v", v0)
v1 := strings.Split(sKey, ",")
sw = fmt.Sprintf("key %v", i0)
b1.WriteString(sw)
for i2, v2 := range v1 {
d := descrip[i2]
sw = fmt.Sprintf(" \t %v: %q ", d, v2)
b1.WriteString(sw)
}
b1.WriteString("\n")
}
report = b1.String()
return
}
示例15: guestView
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)
}