Exemplo n.º 1
0
func renderItemHandler(db *db.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		code := c.Param("shortcode")
		if code == "" {
			c.String(http.StatusOK, "Nothing to see here. Move along now, people.")
			return
		}

		item, _ := db.GetItem(code)
		if item == nil {
			c.String(http.StatusNotFound, "")
			return
		}

		switch item.Type {
		case model.URLItemType:
			c.Redirect(http.StatusMovedPermanently, item.Content)
			return
		case model.TextItemType:
			var output bytes.Buffer
			textTmpl.Execute(&output, item)
			if output.Len() == 0 {
				c.String(http.StatusNotFound, "Something went wrong rendering")
				return
			}
			c.Data(http.StatusOK, "text/html; charset=utf-8", output.Bytes())
			return
		default:
			c.String(http.StatusNotFound, "Not found")
			return
		}
	}
}
Exemplo n.º 2
0
func itemsShowHandler(db *db.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		code := c.Param("code")
		item, err := db.GetItem(code)

		if err != nil {
			c.String(http.StatusNotFound, "Item not found.")
			return
		}

		c.JSON(http.StatusOK, gin.H{
			"item": item,
		})
	}
}