本文整理匯總了Golang中github.com/gin-gonic/gin.Context.Param方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Param方法的具體用法?Golang Context.Param怎麽用?Golang Context.Param使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gin-gonic/gin.Context
的用法示例。
在下文中一共展示了Context.Param方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: clientHandler
func clientHandler(c *gin.Context) {
data, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
log.Println(err)
return
}
log.Printf("[CLIENT] %s\n", string(data))
javaConn, _, err := websocket.DefaultDialer.Dial("ws://localhost:8081", http.Header{})
if err != nil {
log.Println(err)
return
}
initialJSON, _ := json.Marshal(map[string]string{"name": c.Param("name")})
javaConn.WriteMessage(websocket.TextMessage, initialJSON)
javaConn.ReadMessage()
javaConn.WriteMessage(websocket.TextMessage, data)
_, p, err := javaConn.ReadMessage()
if err != nil {
log.Println(err)
return
}
log.Printf("[SPIGOT] %s\n", string(p))
javaConn.Close()
c.Writer.Write(p)
}
示例2: ExecutionCreate
//ExecutionCreate serves the route POST /tasks/:task_id/executions
func ExecutionCreate(c *gin.Context) {
models.InTx(func(txn *gorm.DB) bool {
var task models.Task
if txn.Where("id like ? ", c.Param("task_id")).First(&task); task.ID == "" {
c.JSON(http.StatusNotFound, "")
return false
}
var execution models.Execution
if err := c.BindJSON(&execution); err != nil {
c.JSON(http.StatusBadRequest, err)
return false
}
execution.TaskID = task.ID
if valid, errMap := models.ValidStruct(&execution); !valid {
c.JSON(http.StatusConflict, errMap)
return false
}
if txn.Create(&execution).Error != nil {
c.JSON(http.StatusBadRequest, "Execution can't be saved")
return false
}
c.JSON(http.StatusOK, execution)
return true
})
}
示例3: Generate8bit
func Generate8bit(c *gin.Context) {
name := c.Param("name")
gender := c.Param("gender")
switch {
case gender == "m":
gender = "male"
case gender == "f":
gender = "female"
case gender == "male" || gender == "female":
//do nothing
default:
c.AbortWithError(http.StatusInternalServerError, errors.New("Invalid parameters"))
return
}
log.Println(name)
InitAssets()
img := GenerateIdenticon8bits(gender, []byte(name))
c.Header("Content-Type", "image/png")
c.Stream(func(w io.Writer) bool {
png.Encode(w, img)
return false
})
}
示例4: GetAdminNewsEdit
func GetAdminNewsEdit(c *gin.Context) {
nw := News{}
i, _ := strconv.Atoi(c.Param("kkk"))
NewsSession.Find(gin.H{"id": i}).One(&nw)
fmt.Println(nw.Short, nw.Id)
c.HTML(200, "editn.html", gin.H{"news": nw})
}
示例5: GetResource
func (rc *ResourceController) GetResource(ctx *gin.Context) {
var id bson.ObjectId
req := ctx.Request
resourceType := getResourceType(req.URL)
// Validate id as a bson Object ID
id, err := toBsonObjectID(ctx.Param("id"))
if err != nil {
ctx.AbortWithError(http.StatusBadRequest, err)
return
}
logger.Log.WithFields(
logrus.Fields{"resource type": resourceType, "id": id}).Info("GetResource")
resource, err := rc.LoadResource(resourceType, id)
if err != nil {
if err == mgo.ErrNotFound {
ctx.String(http.StatusNotFound, "Not Found")
ctx.Abort()
return
} else {
ctx.AbortWithError(http.StatusBadRequest, err)
return
}
}
logger.Log.WithFields(logrus.Fields{"resource": resource}).Info("GetResource")
ctx.JSON(http.StatusOK, resource)
}
示例6: fibonacci
func fibonacci(c *gin.Context) {
nParam := c.Param("n")
n, err := strconv.Atoi(nParam)
if err != nil || n == 0 {
c.JSON(http.StatusBadRequest, errorResponse{
ErrorCode: 1000,
Message: "Received inputParameter is not a correct limit value",
InputParameters: nParam,
})
return
}
fibelems, err := fibSrv.CountNValues(n)
if err != nil {
c.JSON(http.StatusInternalServerError, errorResponse{
ErrorCode: 1000,
Message: "Unexpected error occurred during fibonacci calculation",
InputParameters: nParam,
})
return
}
c.JSON(http.StatusOK, response{N: n, Elems: fibelems.ToString()})
}
示例7: update
func update(c *gin.Context) {
var input Dog
c.Bind(&input)
log.Print(c)
id := c.Param("id")
log.Print(id)
//check if valid id
if bson.IsObjectIdHex(id) == false {
c.JSON(400, gin.H{"error": "ID not valid"})
return
}
bId := bson.ObjectIdHex(id)
err := Collection.UpdateId(bId, &Dog{bId, input.Name, input.Owner})
if err != nil {
c.JSON(400, "id not found")
} else {
c.JSON(200, "dog updated")
}
}
示例8: showNote
func (p *Engine) showNote(c *gin.Context) (interface{}, error) {
u := c.MustGet("user").(*platform.User)
id := c.Param("id")
var note Note
err := p.Db.Where("id = ? AND user_id = ?", id, u.ID).First(¬e).Error
return note, err
}
示例9: UserOrgIndex
// UserOrgIndex retrieves all orgs related to a user.
func UserOrgIndex(c *gin.Context) {
records, err := store.GetUserOrgs(
c,
&model.UserOrgParams{
User: c.Param("user"),
},
)
if err != nil {
logrus.Warnf("Failed to fetch user orgs. %s", err)
c.JSON(
http.StatusInternalServerError,
gin.H{
"status": http.StatusInternalServerError,
"message": "Failed to fetch orgs",
},
)
c.Abort()
return
}
c.JSON(
http.StatusOK,
records,
)
}
示例10: New
func (self *EnvController) New(c *gin.Context) {
username := self.CurrentUser(c)
if username == "" {
c.Redirect(http.StatusFound, "/")
return
}
appName := c.Param("appName")
key := c.PostForm("key")
value := c.PostForm("value")
err := env.Create(self.etcd, username, appName, key, value)
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
c.HTML(http.StatusInternalServerError, "app.tmpl", gin.H{
"alert": true,
"error": true,
"message": "Failed to add environment variable.",
})
return
}
c.Redirect(http.StatusSeeOther, "/apps/"+appName)
}
示例11: ViewCtr
func (fc *FrontController) ViewCtr(c *gin.Context) {
id := c.Param("id")
var blog VBlogItem
CKey := fmt.Sprintf("blogitem-%d", id)
val, ok := Cache.Get(CKey)
if val != nil && ok == true {
fmt.Println("Ok, we found cache, Cache Len: ", Cache.Len())
blog = val.(VBlogItem)
} else {
rows, err := DB.Query("Select * from top_article where aid = ?", &id)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var ()
for rows.Next() {
err := rows.Scan(&blog.aid, &blog.title, &blog.content, &blog.publish_time, &blog.publish_status)
if err != nil {
log.Fatal(err)
}
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
Cache.Add(CKey, blog)
}
c.HTML(http.StatusOK, "view.html", gin.H{
"aid": blog.aid,
"title": blog.title.String,
"content": template.HTML(blog.content.String),
"publish_time": blog.publish_time.String,
})
}
示例12: editReplicationController
func editReplicationController(c *gin.Context) {
namespace := c.Param("ns")
rcname := c.Param("rc")
_, delete := c.GetQuery("delete")
rc, err := kubeclient.Get().ReplicationControllers(namespace).Get(rcname)
if err != nil {
c.HTML(http.StatusInternalServerError, "error", gin.H{"error": err.Error()})
return
}
b, err := json.Marshal(rc)
if err != nil {
c.HTML(http.StatusInternalServerError, "error", gin.H{"error": err.Error()})
return
}
var out bytes.Buffer
err = json.Indent(&out, b, "", " ")
if err != nil {
c.HTML(http.StatusInternalServerError, "error", gin.H{"error": err.Error()})
return
}
c.HTML(http.StatusOK, "replicationControllerEdit", gin.H{
"title": rcname,
"namespace": namespace,
"objname": rcname,
"json": out.String(),
"delete": strconv.FormatBool(delete),
})
}
示例13: readPodLog
func readPodLog(c *gin.Context) {
namespace := c.Param("ns")
podname := c.Param("po")
_, previous := c.GetQuery("previous")
readLog(c, namespace, podname, "", previous)
}
示例14: postDeleteNodeAction
func (pc *NodeController) postDeleteNodeAction(c *gin.Context) {
id := c.Param("id")
node, err := models.NodeMapper.FetchOneById(id)
if err != nil {
c.HTML(http.StatusInternalServerError, "error_500.html", map[string]interface{}{
"error": err,
})
return
}
if node == nil {
c.HTML(http.StatusNotFound, "error_404.html", map[string]interface{}{
"text": "Node not found",
})
return
}
if err := models.NodeMapper.Delete(node); err != nil {
c.HTML(http.StatusInternalServerError, "error_500.html", map[string]interface{}{
"error": err,
})
return
}
c.Redirect(http.StatusFound, "/nodes")
}
示例15: OpenWebsocket
// Status handler for the /status route
func OpenWebsocket(c *gin.Context) {
userId := c.MustGet("request_user_id").(string)
noteId := c.Param("note_id")
in, note, err := db.GetNoteById(noteId)
if log.Error(err) {
c.Error(errors.NewISE())
return
}
if !in {
c.Error(errors.NewHttp(http.StatusNotFound, "The requested note was not found"))
return
}
if userId != note.Owner {
c.Error(errors.NewHttp(http.StatusUnauthorized, "Only owners can open websockets into their notes"))
return
}
conn, err := websocketUpgrader.Upgrade(c.Writer, c.Request, nil)
if log.Error(err) {
c.Error(errors.NewISE())
return
}
log.Info("Opening ws for user %v on %v", userId, note.Id)
bundle := model.NewContext(userId, noteId)
WrapWebsocket(conn, bundle)
ws.ProcessMessages(bundle)
}