mirror of
https://github.com/nkanaev/yarr.git
synced 2025-09-15 02:40:15 +00:00
reorganizing server-related packages
This commit is contained in:
61
src/server/router/context.go
Normal file
61
src/server/router/context.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Req *http.Request
|
||||
Out http.ResponseWriter
|
||||
|
||||
Vars map[string]string
|
||||
|
||||
chain []Handler
|
||||
index int
|
||||
}
|
||||
|
||||
func (c *Context) Next() {
|
||||
c.index++
|
||||
c.chain[c.index](c)
|
||||
}
|
||||
|
||||
func (c *Context) JSON(status int, data interface{}) {
|
||||
body, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
c.Out.WriteHeader(status)
|
||||
c.Out.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
c.Out.Write(body)
|
||||
c.Out.Write([]byte("\n"))
|
||||
}
|
||||
|
||||
func (c *Context) HTML(status int, tmpl *template.Template, data interface{}) {
|
||||
c.Out.WriteHeader(status)
|
||||
c.Out.Header().Set("Content-Type", "text/html")
|
||||
tmpl.Execute(c.Out, data)
|
||||
}
|
||||
|
||||
func (c *Context) VarInt64(key string) (int64, error) {
|
||||
if val, ok := c.Vars[key]; ok {
|
||||
return strconv.ParseInt(val, 10, 64)
|
||||
}
|
||||
return 0, fmt.Errorf("no such var: %s", key)
|
||||
}
|
||||
|
||||
func (c *Context) QueryInt64(key string) (int64, error) {
|
||||
query := c.Req.URL.Query()
|
||||
return strconv.ParseInt(query.Get(key), 10, 64)
|
||||
}
|
||||
|
||||
func (c *Context) Redirect(url string) {
|
||||
if url == "" {
|
||||
url = "/"
|
||||
}
|
||||
http.Redirect(c.Out, c.Req, url, http.StatusFound)
|
||||
}
|
Reference in New Issue
Block a user