POST /codeshare/upload
(c *gin.Context)
| 18 | |
| 19 | // POST /codeshare/upload |
| 20 | func (h *CodeShareHandler) Upload(c *gin.Context) { |
| 21 | var req struct { |
| 22 | Author string `json:"author" binding:"required"` |
| 23 | Language string `json:"language" binding:"required"` |
| 24 | Content string `json:"content" binding:"required"` |
| 25 | TTL int64 `json:"ttl"` |
| 26 | } |
| 27 | |
| 28 | if err := c.ShouldBindJSON(&req); err != nil { |
| 29 | c.JSON(http.StatusBadRequest, gin.H{ |
| 30 | "error": "Invalid JSON: " + err.Error(), |
| 31 | }) |
| 32 | return |
| 33 | } |
| 34 | |
| 35 | if req.TTL == 0 { |
| 36 | req.TTL = 3600 |
| 37 | } |
| 38 | |
| 39 | code := h.cs.Upload(req.Author, req.Language, req.Content, req.TTL) |
| 40 | if code == nil { |
| 41 | c.JSON(http.StatusRequestEntityTooLarge, gin.H{ |
| 42 | "error": "Content too large", |
| 43 | }) |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | c.JSON(http.StatusOK, gin.H{ |
| 48 | "message": "success", |
| 49 | "hash": code.Hash, |
| 50 | "url": "/codeshare/code/" + code.Hash, |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | // GET /codeshare/code/:hash |
| 55 | func (h *CodeShareHandler) Get(c *gin.Context) { |
nothing calls this directly
no outgoing calls
no test coverage detected