本文整理汇总了Golang中github.com/insionng/vodka.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: login
func login(c vodka.Context) error {
username := c.FormValue("username")
password := c.FormValue("password")
if username == "jon" && password == "shhh!" {
// Create token
token := jwt.New(jwt.SigningMethodHS256)
// Set claims
claims := token.Claims.(jwt.MapClaims)
claims["name"] = "Jon Snow"
claims["admin"] = true
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
// Generate encoded token and send it as response.
t, err := token.SignedString([]byte("secret"))
if err != nil {
return err
}
return c.JSON(http.StatusOK, map[string]string{
"token": t,
})
}
return vodka.ErrUnauthorized
}
示例2: Render
// Render 渲染
func (r *Renderer) Render(w io.Writer, name string, ctx vodka.Context) error {
template, err := r.getTemplate(name)
if err != nil {
return err
}
err = template.ExecuteWriter(ctx.GetStore(), w)
return err
}
示例3: createUser
func createUser(c vodka.Context) error {
u := new(user)
if err := c.Bind(u); err != nil {
return err
}
users[u.ID] = *u
return c.JSON(http.StatusCreated, u)
}
示例4: NodeEditGetHandler
func NodeEditGetHandler(self *vodka.Context) error {
data := make(map[string]interface{})
nid, _ := self.ParamInt64(":nid")
nid_handler := models.GetNode(nid)
data["inode"] = nid_handler
data["icategory"] = models.GetCategory(nid_handler.Pid)
return self.Render(http.StatusOK, "node_edit.html", data)
}
示例5: TopicEditGetHandler
func TopicEditGetHandler(self *vodka.Context) error {
data := make(map[string]interface{})
tid, _ := self.ParamInt64(":tid")
tid_handler := models.GetTopic(tid)
data["topic"] = tid_handler
data["inode"] = models.GetNode(tid_handler.Nid)
return self.Render(http.StatusOK, "topic_edit.html", data)
}
示例6: upload
func upload(c *vodka.Context) error {
mr, err := c.Request().MultipartReader()
if err != nil {
return err
}
// Read form field `name`
part, err := mr.NextPart()
if err != nil {
return err
}
defer part.Close()
b, err := ioutil.ReadAll(part)
if err != nil {
return err
}
name := string(b)
// Read form field `email`
part, err = mr.NextPart()
if err != nil {
return err
}
defer part.Close()
b, err = ioutil.ReadAll(part)
if err != nil {
return err
}
email := string(b)
// Read files
i := 0
for {
part, err := mr.NextPart()
if err != nil {
if err == io.EOF {
break
}
return err
}
defer part.Close()
file, err := os.Create(part.FileName())
if err != nil {
return err
}
defer file.Close()
if _, err := io.Copy(file, part); err != nil {
return err
}
i++
}
return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.",
name, email, i)
}
示例7: mustWrapHandleFunc
func (ceh *customVodkaHandler) mustWrapHandleFunc(c vodka.Context) vodka.HandlerFunc {
if _, ok := c.Request().(*standard.Request); ok {
return standard.WrapHandler(ceh.httpHandler)
} else if _, ok = c.Request().(*fasthttp.Request); ok {
return NewFastHTTPVodkaAdaptor(ceh.httpHandler)
}
log.Fatal("Unknown HTTP implementation")
return nil
}
示例8: TokenHandler
func TokenHandler(self *vodka.Context) error {
var claims = map[string]interface{}{}
claims["username"] = "Insion"
token, err := jwt.NewToken(key, claims)
if err != nil {
return err
}
// show the token use for test
return self.String(http.StatusOK, "%s", token)
}
示例9: createUser
func createUser(c vodka.Context) error {
u := &user{
ID: seq,
}
if err := c.Bind(u); err != nil {
return err
}
users[u.ID] = u
seq++
return c.JSON(http.StatusCreated, u)
}
示例10: TokenHandler
func TokenHandler(self *vodka.Context) error {
var claims = map[string]interface{}{}
claims["token"] = "insion's-Token"
token, err := NewToken(JWTContextKey, claims)
if err != nil {
return err
}
// show the token use for test
return self.String(http.StatusOK, "%s", token)
}
示例11: SigninGetHandler
func SigninGetHandler(self *vodka.Context) error {
session := sessions.Default(self)
val := session.Get("user")
if val != nil {
return self.Redirect(302, "/")
} else {
return self.Render(http.StatusOK, "signin.html", nil)
}
}
示例12: upload
func upload(c vodka.Context) error {
// Read form fields
name := c.FormValue("name")
email := c.FormValue("email")
//-----------
// Read file
//-----------
// Source
file, err := c.FormFile("file")
if err != nil {
return err
}
src, err := file.Open()
if err != nil {
return err
}
defer src.Close()
// Destination
dst, err := os.Create(file.Filename)
if err != nil {
return err
}
defer dst.Close()
// Copy
if _, err = io.Copy(dst, src); err != nil {
return err
}
return c.HTML(http.StatusOK, fmt.Sprintf("<p>File %s uploaded successfully with fields name=%s and email=%s.</p>", file.Filename, name, email))
}
示例13: ModifyCatPostHandler
func ModifyCatPostHandler(self *vodka.Context) error {
cid, _ := self.ParamInt64("categoryid")
cat_title := self.ParamEscape("title")
cat_content := self.ParamEscape("content")
if cid != 0 && cat_title != "" && cat_content != "" {
var cat models.Category
cat.Id = int64(cid)
cat.Title = cat_title
cat.Content = cat_content
cat.Created = time.Now()
models.UpdateCategory(cat.Id, cat)
return self.Redirect(302, fmt.Sprintf("/category/%v/", cid))
} else {
return self.Redirect(302, "/")
}
}
示例14: stream
func stream(c vodka.Context) error {
res := c.Response().(*standard.Response).ResponseWriter
gone := res.(http.CloseNotifier).CloseNotify()
res.Header().Set(vodka.HeaderContentType, vodka.MIMETextHTMLCharsetUTF8)
res.WriteHeader(http.StatusOK)
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
fmt.Fprint(res, "<pre><strong>Clock Stream</strong>\n\n<code>")
for {
fmt.Fprintf(res, "%v\n", time.Now())
res.(http.Flusher).Flush()
select {
case <-ticker.C:
case <-gone:
break
}
}
}
示例15: NewTopicPostHandler
func NewTopicPostHandler(self *vodka.Context) error {
nid, _ := self.ParamInt64("nodeid")
cid := models.GetNode(nid).Pid
session := sessions.Default(self)
var user models.User
val := session.Get("user")
if val != nil {
user = val.(models.User)
}
uid := user.Id
author := user.Nickname
tid_title := self.FormEscape("title")
tid_content := self.FormEscape("content")
if tid_title != "" && tid_content != "" {
models.AddTopic(tid_title, tid_content, cid, nid, uid, author)
return self.Redirect(302, fmt.Sprintf("/node/", nid))
} else {
return self.Redirect(302, "/")
}
}