本文整理匯總了Golang中web.Context類的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Context類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: RSS
func (c *Index) RSS(ctx *web.Context) string {
p := PostModelInit()
results := p.RSS()
ctx.ContentType("xml")
return mustache.RenderFile("templates/rss.mustache", map[string][]map[string]string{"posts": results})
}
示例2: Start
func Start(ctx *web.Context, handler *MHandler) *Session {
session := new(Session)
session.handler = handler
session.handler.Clean()
old := false
if ctx.Cookies != nil {
if id, exists := ctx.Cookies["bloody_sess"]; exists {
session.id = id
old = true
}
}
if !old {
// Starts new session
session.generateId()
session.handler.Store(session.GetID(), nil)
}
rt := session.handler.Retrieve(session.GetID())
json.Unmarshal(rt.SessionData, &session.Data)
if session.Data == nil {
t := make(map[string]interface{})
session.Data = t
}
ctx.SetCookie("bloody_sess", session.GetID(), time.Now().Unix()+3600)
return session
}
示例3: index
func index(ctx *web.Context, val string) string {
switch val {
case "Liberator":
ctx.Redirect(302, util.Settings.WebHome())
}
return val
}
示例4: LoadPost
func LoadPost(ctx *web.Context, val string) {
username := ctx.Params["username"]
password := ctx.Params["password"]
salt := strconv.Itoa64(time.Nanoseconds()) + username
var h hash.Hash = sha256.New()
h.Write([]byte(password + salt))
s, _err := conn.Prepare("INSERT INTO users VALUES(NULL, ?, ?, ?)")
utils.ReportErr(_err)
s.Exec(username, string(h.Sum()), salt)
s.Finalize()
conn.Close()
sidebar := utils.Loadmustache("admin.mustache", &map[string]string{})
//TESTING, REMOVE LATER
script := "<script type=\"text/javascript\" src=\"../inc/adminref.js\"></script>"
content := "Welcome to the admin panel, use the control box on your right to control the site content"
//ENDTESTING
mapping := map[string]string{"css": "../inc/site.css",
"title": "Proggin: Admin panel",
"sidebar": sidebar,
"content": content,
"script": script}
output := utils.Loadmustache("frame.mustache", &mapping)
ctx.WriteString(output)
}
示例5: get_delete
func get_delete(ctx *web.Context, id string) {
log.Printf("get_delete %s\n", id)
if e, err := Load(id); err == nil {
ctx.WriteString(page(edit_form("/delete", e.Id, e.Date, e.Body, "Really delete")))
} else {
ctx.WriteString(page("<p>Invalid ID</p>"))
}
}
示例6: checkGodLevel
func checkGodLevel(ctx *web.Context) bool {
godlevel, _ := ctx.GetSecureCookie("godlevel")
godlevel = godHash(godlevel)
if godlevel == admin_pass {
return true
}
return false
}
示例7: create_person
func create_person(ctx *web.Context) {
var p Personne
Personnes := gouda.M(p)
p.Id = 0
p.Nom = ctx.Request.Params["nom"][0]
p.Age, _ = strconv.Atoi(ctx.Request.Params["age"][0])
Personnes.Save(p)
ctx.Redirect(302, "/")
}
示例8: ReadPost
func (c *Index) ReadPost(ctx *web.Context, postId string) string {
p := PostModelInit()
result := p.RenderPost(postId)
viewVars := make(map[string]interface{})
viewVars["Title"] = result.Title
viewVars["Content"] = result.Content
viewVars["Date"] = time.Unix(result.Created, 0).Format(blogConfig.Get("dateFormat"))
viewVars["Id"] = objectIdHex(result.Id.String())
// To be used within the {{Comments}} blog
viewVars["PostId"] = objectIdHex(result.Id.String())
if result.Status == 0 {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
ctx.Redirect(302, "/")
return ""
}
}
if blogConfig.Get("enableComment") != "" {
viewVars["EnableComment"] = true
} else {
viewVars["EnableComment"] = false
}
// Render comments
comments := make([]map[string]string, 0)
for i, v := range result.Comments {
comments = append(comments, map[string]string{
"Number": strconv.Itoa(i + 1),
"Date": time.Unix(v.Created, 0).Format(blogConfig.Get("dateFormat")),
"Id": v.Id[0:9],
"RealId": v.Id,
"Content": v.Content,
"Author": v.Author})
}
viewVars["Comments"] = comments
if next, exists := p.GetNextId(objectIdHex(result.Id.String())); exists {
viewVars["Next"] = next
}
if last, exists := p.GetLastId(objectIdHex(result.Id.String())); exists {
viewVars["Last"] = last
}
sessionH := session.Start(ctx, h)
defer sessionH.Save()
viewVars["Admin"] = false
if sessionH.Data["logged"] != nil {
viewVars["Admin"] = true
}
output := mustache.RenderFile("templates/view-post.mustache", viewVars)
return render(output, result.Title)
}
示例9: adminIndexGet
func adminIndexGet(ctx *web.Context) string {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] != nil {
ctx.Redirect(302, "/admin/post/list")
return ""
}
ctx.Redirect(302, "/admin/login")
return ""
}
示例10: saveHandler
func saveHandler(ctx *web.Context, title string) {
body, ok := ctx.Request.Params["body"]
if !ok {
ctx.Abort(500, "No body supplied.")
return
}
page := makePage(title, string(body))
page.save()
redirect(ctx, "view", title)
}
示例11: redirect
/*redirects the shortened URL to the real URL*/
func redirect(ctx *web.Context, key string) {
/*fetch our URL*/
url, _ := redisClient.Get(getRedisKey(key))
if url == nil {
printError(ctx, "I can't find that URL")
} else {
/*redirect*/
ctx.Redirect(301, string(url))
}
}
示例12: update_person
func update_person(ctx *web.Context) {
var p Personne
Personnes := gouda.M(p)
i, _ := strconv.Atoi(ctx.Request.Params["id"][0])
p = Personnes.Where(gouda.F("id").Eq(i)).First().(Personne)
p.Nom = ctx.Request.Params["nom"][0]
p.Age, _ = strconv.Atoi(ctx.Request.Params["age"][0])
Personnes.Save(p)
ctx.Redirect(302, "/person/"+fmt.Sprint(p.Id))
}
示例13: adminLoginGet
func adminLoginGet(ctx *web.Context) string {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] != nil {
ctx.Redirect(302, "/admin/post/list")
return ""
}
output := mustache.RenderFile("templates/admin-login.mustache")
return render(output, "Login")
}
示例14: newPostGet
func newPostGet(ctx *web.Context) string {
sessionH := session.Start(ctx, h)
defer sessionH.Save()
if sessionH.Data["logged"] == nil {
ctx.Redirect(302, "/admin/login")
return ""
}
output := mustache.RenderFile("templates/new-post.mustache")
return render(output, "New Post")
}
示例15: Get
func Get(ctx *web.Context, val string) {
v := strings.Split(val, "/", 2)
controllerName := ""
actionName := ""
if len(v) == 2 {
controllerName, actionName = v[0], v[1]
} else if len(v) == 1 {
controllerName = v[0]
actionName = "index"
}
if conType, ok := C.Controllers[controllerName]; ok {
conTypePtr := reflect.PtrTo(conType)
actionMethName := strings.ToUpper(string(actionName[0:1])) + actionName[1:]
var actionMeth reflect.Method
found := false
for i := 0; i < conTypePtr.NumMethod(); i++ {
if conTypePtr.Method(i).Name == actionMethName {
actionMeth = conTypePtr.Method(i)
found = true
break
}
}
if !found {
return
}
conValue := reflect.New(conType)
conIndirect := reflect.Indirect(conValue)
// Inject Params
conIndirect.FieldByName("Params").Set(reflect.ValueOf(ctx.Request.Params))
// Inject beans
for beanName, setterFunc := range bean.Registry() {
if _, ok := conType.FieldByName(beanName); ok {
if f := conIndirect.FieldByName(beanName); f.IsValid() {
f.Set(reflect.ValueOf(setterFunc()))
}
}
}
action := actionMeth.Func
ret := action.Call([]reflect.Value{conValue})
if len(ret) == 2 {
m := ret[0].Interface().(mv.Model)
v := ret[1].Interface().(mv.View)
controllerName = v.String()
ctx.WriteString(mustache.RenderFile("app/view/"+controllerName+"/index.m", m))
} else if len(ret) == 1 {
m := ret[0].Interface().(mv.Model)
ctx.WriteString(mustache.RenderFile("app/view/"+controllerName+"/"+actionName+".m", m))
}
}
return
}