本文整理汇总了Golang中code/google/com/p/rsc/appfs/fs.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: loadPost
// ☻ Parse a post file
func loadPost(c *fs.Context, name string, req *http.Request) (meta *PostData, article string, err error) {
meta = &PostData{
Name: name,
Title: "¿Title?",
PlusAuthor: config.PlusID,
PlusAPIKey: config.PlusKey,
HostURL: hostURL(req),
}
art, fi, err := c.Read(name)
if err != nil {
return nil, "", err
}
if bytes.HasPrefix(art, []byte("{\n")) {
i := bytes.Index(art, []byte("\n}\n"))
if i < 0 {
panic("cannot find end of json metadata")
}
hdr, rest := art[:i+3], art[i+3:]
if err := json.Unmarshal(hdr, meta); err != nil {
panic(fmt.Sprintf("loading %s: %s", name, err))
}
art = rest
}
meta.FileModTime = fi.ModTime
meta.FileSize = fi.Size
return meta, replacer.Replace(string(art)), nil
}
示例2: loadSize
func loadSize(ctxt *fs.Context, name string, max int) *image.RGBA {
data, _, err := ctxt.Read("qr/upload/" + name + ".png")
if err != nil {
panic(err)
}
i, _, err := image.Decode(bytes.NewBuffer(data))
if err != nil {
panic(err)
}
b := i.Bounds()
dx, dy := max, max
if b.Dx() > b.Dy() {
dy = b.Dy() * dx / b.Dx()
} else {
dx = b.Dx() * dy / b.Dy()
}
var irgba *image.RGBA
switch i := i.(type) {
case *image.RGBA:
irgba = resize.ResizeRGBA(i, i.Bounds(), dx, dy)
case *image.NRGBA:
irgba = resize.ResizeNRGBA(i, i.Bounds(), dx, dy)
}
return irgba
}
示例3: flag
func flag(w http.ResponseWriter, req *http.Request, img string, ctxt *fs.Context) {
if !isImgName(img) && !isTagName(img) {
fmt.Fprintf(w, "Invalid image.\n")
return
}
data, _, _ := ctxt.Read("qr/flag/" + img)
data = append(data, '!')
ctxt.Write("qr/flag/"+img, data)
fmt.Fprintf(w, "Thank you. The image has been reported.\n")
}
示例4: mainTemplate
func mainTemplate(c *fs.Context) *template.Template {
t := template.New("main")
t.Funcs(funcMap)
main, _, err := c.Read("blog/main.html")
if err != nil {
panic(err)
}
style, _, _ := c.Read("blog/style.html")
main = append(main, style...)
_, err = t.Parse(string(main))
if err != nil {
panic(err)
}
return t
}
示例5: runTemplate
func runTemplate(c *fs.Context, w http.ResponseWriter, name string, data interface{}) {
t := template.New("main")
main, _, err := c.Read(name)
if err != nil {
panic(err)
}
style, _, _ := c.Read("style.html")
main = append(main, style...)
_, err = t.Parse(string(main))
if err != nil {
panic(err)
}
var buf bytes.Buffer
if err := t.Execute(&buf, &data); err != nil {
panic(err)
}
w.Write(buf.Bytes())
}
示例6: oldRedirect
func oldRedirect(ctxt *fs.Context, w http.ResponseWriter, req *http.Request, p string) {
m := map[string]string{}
if key, ok := ctxt.CacheLoad("blog:oldRedirectMap", "blog/post", &m); !ok {
dir, err := ctxt.ReadDir("blog/post")
if err != nil {
panic(err)
}
for _, d := range dir {
meta, _, err := loadPost(ctxt, d.Name, req)
if err != nil {
// Should not happen: we just listed the directory.
panic(err)
}
m[meta.OldURL] = "/" + d.Name
}
ctxt.CacheStore(key, m)
}
if url, ok := m[p]; ok {
http.Redirect(w, req, url, http.StatusFound)
return
}
notfound(ctxt, w, req)
}
示例7: readDir
func readDir(c *fs.Context, root string) ([]proto.FileInfo, error) {
return c.ReadDir(root)
}
示例8: Update
func Update(ctxt *fs.Context, client *http.Client, version string) error {
prefix := strings.Map(func(r rune) rune {
if 'A' <= r && r <= 'Z' {
return r - 'A' + 'a'
}
if 'a' <= r && r <= 'z' || '0' <= r && r <= '9' {
return r
}
return -1
}, version)
label := strings.Map(func(r rune) rune {
if r == ' ' {
return -1
}
return r
}, version)
graphFile := "/issue-dashboard/" + prefix + ".graph"
htmlFile := "/issue-dashboard/" + label
var graph []Point
data, _, err := ctxt.Read(graphFile)
if err == nil {
if err := json.Unmarshal(data, &graph); err != nil {
return fmt.Errorf("unmarshal dashboard graph: %v", err)
}
}
yes, err := issue.Search("go", "open", "label:"+label, false, client)
if err != nil {
return fmt.Errorf("searching for %s issues: %v", version, err)
}
maybe, err := issue.Search("go", "open", "label:"+label+"Maybe", false, client)
if err != nil {
return fmt.Errorf("searching for %sMaybe issues: %v", label, err)
}
graph = append(graph, Point{time.Now(), len(yes), len(maybe)})
data, err = json.Marshal(graph)
if err != nil {
return fmt.Errorf("marshal dashboard graph: %v", err)
}
if err := ctxt.Write(graphFile, data); err != nil {
return fmt.Errorf("writing dashboard graph: %v", err)
}
byDir := map[string][]*issue.Issue{}
for _, p := range append(yes, maybe...) {
dir := p.Summary
if i := strings.Index(dir, ":"); i >= 0 {
dir = dir[:i]
}
if i := strings.Index(dir, ","); i >= 0 {
dir = dir[:i]
}
byDir[dir] = append(byDir[dir], p)
}
var small []Point
now := time.Now()
day := -1
for _, p := range graph {
if p.Maybe == 0 {
continue
}
d := p.Time.Day()
if d != day || now.Sub(p.Time) < 3*24*time.Hour {
day = d
small = append(small, p)
}
}
tmplData := struct {
Version string
Label string
Graph []Point
Issues map[string][]*issue.Issue
}{
Version: version,
Label: label,
Graph: small,
Issues: byDir,
}
var buf bytes.Buffer
tmpl, err := template.New("main").
Funcs(template.FuncMap{
"hasLabel": hasLabel,
"hasStatus": hasStatus,
}).
Parse(dashTemplate)
if err != nil {
return fmt.Errorf("parsing template: %v", err)
}
if err := tmpl.Execute(&buf, &tmplData); err != nil {
return fmt.Errorf("executing template: %v", err)
}
if err := ctxt.Write(htmlFile, buf.Bytes()); err != nil {
return fmt.Errorf("writing html: %v", err)
//.........这里部分代码省略.........