Esempio n. 1
0
func seedData(db *db.DB) {
	var textContent = `
 This is a **sample text in markdown**!

 It contains:

  * lists
  * bold text
  * [links](http://example.com)

 Also, it features blocks of code

     func main() {
 	   fmt.Println("Here be fire dragons!")
     }
 	`
	var items = []*model.Item{
		&model.Item{Code: "url", Type: model.URLItemType, Content: "https://ariejan.net", CreatedAt: time.Now()},
		&model.Item{Code: "txt", Type: model.TextItemType, Content: textContent, CreatedAt: time.Now()},
	}

	for _, item := range items {
		db.SaveItem(item)
	}
}
Esempio n. 2
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
		}
	}
}
Esempio n. 3
0
func itemsDestroyHandler(db *db.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		code := c.Param("code")
		err := db.DeleteItem(code)

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

		c.JSON(http.StatusOK, gin.H{})
	}
}
Esempio n. 4
0
func itemsCreateHandler(db *db.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		var item *model.Item
		if c.BindJSON(&item) == nil {
			if storedItem, err := db.SaveItem(item); err == nil {
				c.JSON(http.StatusCreated, gin.H{
					"item": storedItem,
				})
			} else {
				c.String(http.StatusInternalServerError, "Could not create item.")
			}
		}
	}
}
Esempio n. 5
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,
		})
	}
}
Esempio n. 6
0
func itemsIndexHandler(db *db.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		items, err := db.GetItems()

		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{
				"items": []string{},
			})
			return
		}

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