move the book api to the top v1 package
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/v1/book.go Mon Jan 25 04:36:48 2021 -0600
@@ -0,0 +1,56 @@
+ "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" + "keep.imfreedom.org/grim/devweb/db" +func getBook(c *gin.Context) { + bookID := c.Params.ByName("id") + book, err := db.FindBook(bookID) + c.AbortWithStatus(http.StatusNotFound) + if c.Request.Method == http.MethodHead { + c.Status(http.StatusOK) + c.JSON(http.StatusOK, book) +func updateBook(c *gin.Context) { + book.ID = c.Params.ByName("id") + log.Errorf("failed to update book %q: %v", book.ID, err) + c.AbortWithStatus(http.StatusBadRequest) + c.JSON(http.StatusOK, book) +func deleteBook(c *gin.Context) { + bookID := c.Params.ByName("id") + book, err := db.FindBook(bookID) + log.Errorf("failed to delete book %q: %v", bookID, err) + c.AbortWithStatus(http.StatusNotFound) + if err := book.Delete(); err != nil { + log.Errorf("failed to delete book %q: %v", bookID, err) + c.AbortWithStatus(http.StatusInternalServerError) + c.Status(http.StatusNoContent) --- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/v1/books.go Mon Jan 25 04:36:48 2021 -0600
@@ -0,0 +1,40 @@
+ "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" + "keep.imfreedom.org/grim/devweb/db" +func getBooks(c *gin.Context) { + books, err := db.ListBooks() + log.Errorf("failed to get books: %v", err) + c.AbortWithStatus(http.StatusInternalServerError) + Books []db.Book `json:"books"` + c.JSON(http.StatusOK, d) +func createBook(c *gin.Context) { + if err := book.Create(); err != nil { + c.AbortWithStatus(http.StatusBadRequest) + c.Redirect(http.StatusFound, book.ID) --- a/v1/books/book.go Mon Jan 25 04:16:42 2021 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
- "github.com/gin-gonic/gin"
- log "github.com/sirupsen/logrus"
- "keep.imfreedom.org/grim/devweb/db"
-func getBook(c *gin.Context) {
- bookID := c.Params.ByName("id")
- book, err := db.FindBook(bookID)
- c.AbortWithStatus(http.StatusNotFound)
- if c.Request.Method == http.MethodHead {
- c.Status(http.StatusOK)
- c.JSON(http.StatusOK, book)
-func updateBook(c *gin.Context) {
- book.ID = c.Params.ByName("id")
- log.Errorf("failed to update book %q: %v", book.ID, err)
- c.AbortWithStatus(http.StatusBadRequest)
- c.JSON(http.StatusOK, book)
-func deleteBook(c *gin.Context) {
- bookID := c.Params.ByName("id")
- book, err := db.FindBook(bookID)
- log.Errorf("failed to delete book %q: %v", bookID, err)
- c.AbortWithStatus(http.StatusNotFound)
- if err := book.Delete(); err != nil {
- log.Errorf("failed to delete book %q: %v", bookID, err)
- c.AbortWithStatus(http.StatusInternalServerError)
- c.Status(http.StatusNoContent)
--- a/v1/books/books.go Mon Jan 25 04:16:42 2021 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
- "github.com/gin-gonic/gin"
- log "github.com/sirupsen/logrus"
- "keep.imfreedom.org/grim/devweb/db"
-func getBooks(c *gin.Context) {
- books, err := db.ListBooks()
- log.Errorf("failed to get books: %v", err)
- c.AbortWithStatus(http.StatusInternalServerError)
- Books []db.Book `json:"books"`
- c.JSON(http.StatusOK, d)
-func createBook(c *gin.Context) {
- if err := book.Create(); err != nil {
- c.AbortWithStatus(http.StatusBadRequest)
- c.Redirect(http.StatusFound, book.ID)
--- a/v1/books/routes.go Mon Jan 25 04:16:42 2021 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
- "github.com/gin-gonic/gin"
- "keep.imfreedom.org/grim/devweb/access"
-func Routes(router *gin.RouterGroup) {
- books := router.Group("/books")
- books.GET("/:id", getBook)
- books.HEAD("/:id", getBook)
- books.GET("/", getBooks)
- authed := books.Group("", access.Middleware())
- authed.PUT("/:id", updateBook)
- authed.DELETE("/:id", deleteBook)
- authed.POST("/", createBook)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/v1/routes.go Mon Jan 25 04:36:48 2021 -0600
@@ -0,0 +1,20 @@
+ "github.com/gin-gonic/gin" + "keep.imfreedom.org/grim/devweb/access" +func Routes(router *gin.RouterGroup) { + v1 := router.Group("/v1") + v1.GET("/books/", getBooks) + v1.GET("/books/:id", getBook) + v1.HEAD("/books/:id", getBook) + authed := v1.Group("", access.Middleware()) + authed.PUT("/books/:id", updateBook) + authed.DELETE("/books/:id", deleteBook) + authed.POST("/books/", createBook) --- a/v1/v1.go Mon Jan 25 04:16:42 2021 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,13 +0,0 @@
- "github.com/gin-gonic/gin"
- "keep.imfreedom.org/grim/devweb/v1/books"
-func Routes(router *gin.RouterGroup) {
- v1 := router.Group("/v1")