Golang Context.Get Examples

Golang Context.Get - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.Get extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
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})
}
Example #2
0
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

}
Example #3
0
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})
}
Example #4
0
File: api.go Project: P0rtk3y/api
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)
}
Example #5
0
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())
	}

}
Example #6
0
// 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
}