本文整理匯總了Golang中github.com/gin-gonic/gin.Context.Get方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Get方法的具體用法?Golang Context.Get怎麽用?Golang Context.Get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gin-gonic/gin.Context
的用法示例。
在下文中一共展示了Context.Get方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: PostChirp
func PostChirp(c *gin.Context) {
var chirp model.Chirp
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.JSON(400, gin.H{"message": sys.ERR_READ_BODY, "status": sys.FAIL})
return
}
err = json.Unmarshal(body, &chirp)
if err != nil {
c.JSON(500, gin.H{"message": sys.ERR_UNMARSHAL_BODY, "status": sys.FAIL, "payload": body})
return
}
uid, _ := c.Get("userid")
chirp.Userid, _ = Atoi(uid.(string))
row := util.DB.QueryRow(chirp.StmtInsert())
err = row.Scan(&chirp.Id)
if err != nil {
c.JSON(500, gin.H{"message": sys.ERR_DB_INSERT, "status": sys.FAIL, "payload": body})
return
}
c.JSON(200, gin.H{"payload": chirp.Id, "status": sys.SUCCESS})
}
示例2: getContext
func getContext(c *gin.Context) (tmpl string, context map[string]interface{}, err error) {
tmplName, tmplNameExists := c.Get("tmpl")
tmplNameValue, isString := tmplName.(string)
tmplData, tmplDataExists := c.Get("data")
// 模板未定義
if !tmplNameExists || !isString {
return "", nil, errors.New("No tmpl defined!")
}
// 公共模板數據
commonDatas := getCommonContext(c)
// 模板數據
if tmplData != nil && tmplDataExists {
contextData, isMap := tmplData.(map[string]interface{})
if isMap {
for key, value := range commonDatas {
contextData[key] = value
}
return tmplNameValue, contextData, nil
}
}
return tmplNameValue, commonDatas, nil
}
示例3: NewMessage
func NewMessage(c *gin.Context) {
user, _ := c.Get("User")
username := user.(middleware.Userinfo).Username
sid, _ := c.Get("Sid")
fmt.Println("sid--->", sid)
var msg Message
err := c.BindJSON(&msg)
if err == nil {
//fmt.Printf("%#v\n", msg)
conn := db.GetConnection()
//fmt.Println("--->", username)
if msg.Body == "" {
return
}
if username == "" {
c.JSON(400, gin.H{"error": "session expired"})
return
}
insert_message(conn, msg.Body, username)
if msg.To != "" {
//fmt.Println("go to:", msg.To)
c.JSON(http.StatusOK, gin.H{"status": "have to", "body": "you say: " + msg.Body})
} else {
//fmt.Println("to all")
c.JSON(http.StatusOK, gin.H{"status": "to all", "body": "you say: " + msg.Body})
}
return
}
fmt.Println("err===", err)
c.JSON(400, gin.H{"error": err})
}
示例4: HandleRun
func HandleRun(c *gin.Context) {
req, err := ParseRequest(c.Request)
if err != nil {
errorResponse(400, err, c)
return
}
config, exists := c.Get("config")
if !exists {
errorResponse(400, fmt.Errorf("Cant get config"), c)
return
}
client, exists := c.Get("client")
if !exists {
errorResponse(400, fmt.Errorf("Cant get client"), c)
return
}
run := NewRun(config.(*Config), client.(*docker.Client), req)
defer run.Destroy()
result, err := performRun(run)
if err != nil {
errorResponse(400, err, c)
return
}
c.Header("X-Run-Command", req.Command)
c.Header("X-Run-ExitCode", strconv.Itoa(result.ExitCode))
c.Header("X-Run-Duration", result.Duration)
c.Data(200, req.Format, result.Output)
}
示例5: IndexHandler
func IndexHandler(c *gin.Context) {
var listTmpl = template.Must(template.ParseFiles("templates/base.html",
"apps/message/templates/index.html"))
User, _ := c.Get("User")
Sid, _ := c.Get("Sid")
sid := Sid.(string)
//fmt.Println("User+++==>", Sid)
bapi := auth.GetBackendApi2(c)
data := gin.H{}
if err := bapi.Get(&data, "http://127.0.0.1:8080/messages"); err != nil {
fmt.Println("bapi failed:", err)
return
}
if e, exist := data["error"]; exist {
fmt.Println("get messages failed:", e.(string))
if e.(string) == "session expired" {
conn := utils.OpenDB()
auth.Signout_del_session(conn, sid)
c.Redirect(302, "/auth/signin")
return
}
// TODO: else
}
data["User"] = User
if err := listTmpl.Execute(c.Writer, data); err != nil {
fmt.Println(err.Error())
}
}
示例6: Hostname
// Hostname returns the hostname associated with
// the current context.
func Hostname(c *gin.Context) (host string) {
v, ok := c.Get("host")
if ok {
host = v.(string)
}
return
}
示例7: IsTatSystem
// IsTatSystem return true if user is a system user. Get value in gin.Context
func IsTatSystem(ctx *gin.Context) bool {
value, exist := ctx.Get(TatCtxIsSystem)
if value != nil && exist && value.(bool) == true {
return true
}
return false
}
示例8: convertByURLHandler
// convertByURLHandler is the main v1 API handler for converting a HTML to a PDF
// via a GET request. It can either return a JSON string indicating that the
// output of the conversion has been uploaded or it can return the output of
// the conversion to the client (raw bytes).
func convertByURLHandler(c *gin.Context) {
s := c.MustGet("statsd").(*statsd.Client)
r, ravenOk := c.Get("sentry")
url := c.Query("url")
if url == "" {
c.AbortWithError(http.StatusBadRequest, ErrURLInvalid).SetType(gin.ErrorTypePublic)
s.Increment("invalid_url")
return
}
ext := c.Query("ext")
source, err := converter.NewConversionSource(url, nil, ext)
if err != nil {
s.Increment("conversion_error")
if ravenOk {
r.(*raven.Client).CaptureError(err, map[string]string{"url": url})
}
c.Error(err)
return
}
conversionHandler(c, *source)
}
示例9: convertByFileHandler
func convertByFileHandler(c *gin.Context) {
s := c.MustGet("statsd").(*statsd.Client)
r, ravenOk := c.Get("sentry")
file, header, err := c.Request.FormFile("file")
if err != nil {
c.AbortWithError(http.StatusBadRequest, ErrFileInvalid).SetType(gin.ErrorTypePublic)
s.Increment("invalid_file")
return
}
ext := c.Query("ext")
source, err := converter.NewConversionSource("", file, ext)
if err != nil {
s.Increment("conversion_error")
if ravenOk {
r.(*raven.Client).CaptureError(err, map[string]string{"url": header.Filename})
}
c.Error(err)
return
}
conversionHandler(c, *source)
}
示例10: ReadSeriesOfUserHandler
func ReadSeriesOfUserHandler(c *gin.Context, app AppContext) error {
tmp, err := c.Get("Session")
if err != nil {
return err
}
s, ok := tmp.(aauth.Session)
if !ok {
errors.New("Cannot find session")
}
userID := c.Params.ByName("id")
if s.UserID == userID {
errors.New("Wrong user")
}
db := app.DB()
defer db.Session.Close()
sList, err := ReadSeriesOfUser(db, bson.ObjectIdHex(s.UserID))
if err != nil {
return err
}
resp := NewSuccessResponse(sList)
c.JSON(http.StatusOK, resp)
return nil
}
示例11: DB
func DB(ctx *gin.Context) *gorm.DB {
newDB, exist := ctx.Get("DB")
if exist {
return newDB.(*gorm.DB)
}
return db.DB
}
示例12: getDB
func getDB(c *gin.Context) *sql.DB {
db, ok := c.Get("DB")
if ok {
return db.(*sql.DB)
}
return nil
}
示例13: AuthCheck
func AuthCheck(c *gin.Context) {
id, _ := c.Get("request_id")
log.InfoFields("Checking auth token", log.Fields{
"request_id": id,
})
token := c.Query("token")
if token == "" {
tokenHeaders := c.Request.Header["Token"]
if len(tokenHeaders) == 0 {
c.AbortWithError(http.StatusUnauthorized, fmt.Errorf("No notion access token provided"))
return
} else {
token = tokenHeaders[0]
}
}
in, user, err := db.GetUserByToken(token)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("Internal server error"))
return
}
if !in {
c.AbortWithError(http.StatusUnauthorized, fmt.Errorf("Notion access token provided is not currently valid"))
return
}
c.Set("request_user_id", user.Id)
}
示例14: DeleteChirp
func DeleteChirp(c *gin.Context) {
var id = c.Params.ByName("id")
var chirp model.Chirp
uid, _ := c.Get("userid")
userid, _ := Atoi(uid.(string))
chirp.Id, _ = Atoi(id)
row := util.DB.QueryRow(chirp.StmtGetById())
err := row.Scan(&chirp.Id, &chirp.Userid, &chirp.Type, &chirp.Message, &chirp.Create_date, &chirp.Active)
if err != nil {
c.JSON(400, sys.JSON_NOACCESS)
return
}
// Check if chirp belongs to requester's account
if chirp.Userid != userid {
c.JSON(403, sys.JSON_NOACCESS)
return
}
row = util.DB.QueryRow(chirp.StmtSetActive(false))
err = row.Scan(&chirp.Id)
if err != nil {
c.JSON(500, gin.H{"message": sys.ERR_DB_DELETE, "status": sys.FAIL, "payload": id})
return
}
c.JSON(200, gin.H{"payload": "Deleted Chirp " + Itoa(chirp.Id), "status": sys.SUCCESS})
}
示例15: GetToken
func GetToken(c *gin.Context) *tokenModel {
token, exists := c.Get("token")
if !exists {
panic("token not exists")
}
return token.(*tokenModel)
}