Skip to content

Commit

Permalink
Changes from the last stream
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgomes28 committed Oct 20, 2024
1 parent feec646 commit a9cee7b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
12 changes: 10 additions & 2 deletions admin-app/app.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package admin_app

import (
"log"

"github.com/gin-gonic/gin"
"github.com/matheusgomes28/urchin/common"
"github.com/matheusgomes28/urchin/database"
"github.com/matheusgomes28/urchin/plugins"
lua "github.com/yuin/gopher-lua"
)

func SetupRoutes(app_settings common.AppSettings, database database.Database, shortcode_handlers map[string]*lua.LState) *gin.Engine {
func SetupRoutes(app_settings common.AppSettings, database database.Database, shortcode_handlers map[string]*lua.LState, hooks map[string]plugins.Hook) *gin.Engine {

r := gin.Default()
r.MaxMultipartMemory = 1

post_hook, ok := hooks["add_post"]
if !ok {
log.Fatalf("could not find add_post hook")
}

r.GET("/posts/:id", getPostHandler(database))
r.POST("/posts", postPostHandler(database, shortcode_handlers))
r.POST("/posts", postPostHandler(database, shortcode_handlers, post_hook.(plugins.PostHook)))
r.PUT("/posts", putPostHandler(database))
r.DELETE("/posts/:id", deletePostHandler(database))

Expand Down
54 changes: 29 additions & 25 deletions admin-app/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/matheusgomes28/urchin/common"
"github.com/matheusgomes28/urchin/database"
"github.com/matheusgomes28/urchin/plugins"
"github.com/rs/zerolog/log"
lua "github.com/yuin/gopher-lua"
)
Expand Down Expand Up @@ -39,7 +40,7 @@ func getPostHandler(database database.Database) func(*gin.Context) {
}
}

func postPostHandler(database database.Database, shortcode_handlers map[string]*lua.LState) func(*gin.Context) {
func postPostHandler(database database.Database, shortcode_handlers map[string]*lua.LState, post_hook plugins.PostHook) func(*gin.Context) {
return func(c *gin.Context) {
var add_post_request AddPostRequest
if c.Request.Body == nil {
Expand All @@ -59,32 +60,35 @@ func postPostHandler(database database.Database, shortcode_handlers map[string]*
if err != nil {
log.Error().Msgf("failed to add post required data is missing: %v", err)
c.JSON(http.StatusBadRequest, common.ErrorRes("missing required data", err))
}

transformed_content, err := transformContent(add_post_request.Content, shortcode_handlers)
if err != nil {
log.Warn().Msgf("could not transform post: %v", err)
c.JSON(http.StatusBadRequest, gin.H{
"error": "invalid request body",
"msg": err.Error(),
})
return
}

id, err := database.AddPost(
add_post_request.Title,
add_post_request.Excerpt,
transformed_content,
)
if err != nil {
log.Error().Msgf("failed to add post: %v", err)
c.JSON(http.StatusBadRequest, common.ErrorRes("could not add post", err))
return
}

c.JSON(http.StatusOK, PostIdResponse{
id,
})
// TODO : Move this into the plugin
altered_post := post_hook.UpdatePost(add_post_request.Title, add_post_request.Content, add_post_request.Excerpt, shortcode_handlers)

// transformed_content, err := transformContent(add_post_request.Content, shortcode_handlers)
// if err != nil {
// log.Warn().Msgf("could not transform post: %v", err)
// c.JSON(http.StatusBadRequest, gin.H{
// "error": "invalid request body",
// "msg": err.Error(),
// })
// return
// }

id, err := database.AddPost(
altered_post.Title,
altered_post.Excerpt,
altered_post.Content,
)
if err != nil {
log.Error().Msgf("failed to add post: %v", err)
c.JSON(http.StatusBadRequest, common.ErrorRes("could not add post", err))
return
}

c.JSON(http.StatusOK, PostIdResponse{
id,
})
}
}

Expand Down
18 changes: 17 additions & 1 deletion cmd/urchin-admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
admin_app "github.com/matheusgomes28/urchin/admin-app"
"github.com/matheusgomes28/urchin/common"
"github.com/matheusgomes28/urchin/database"
"github.com/matheusgomes28/urchin/plugins"
"github.com/rs/zerolog/log"
lua "github.com/yuin/gopher-lua"
)
Expand Down Expand Up @@ -70,7 +71,22 @@ func main() {
os.Exit(-1)
}

r := admin_app.SetupRoutes(app_settings, database, shortcode_handlers)
// TODO : we probably want to refactor loadShortcodeHandler
// TODO : into loadPluginHandlers instead

post_hook := plugins.PostHook{}
image_plugin := plugins.Plugin{
ScriptName: "img",
Id: "img-plugin",
}
post_hook.Register(image_plugin)

// img, _ := shortcode_handlers["img"]
hooks_map := map[string]plugins.Hook{
"add_post": post_hook,
}

r := admin_app.SetupRoutes(app_settings, database, shortcode_handlers, hooks_map)
err = r.Run(fmt.Sprintf(":%d", app_settings.WebserverPort))
if err != nil {
log.Error().Msgf("could not run app: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions urchin_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ database_port = 3306
database_name = "gocms"

# port to run the webserver on
webserver_port = 8080
webserver_port = 8082

# port to run the admin app on
admin_port = 8081
admin_port = 8083

# Directory to use for storing uploaded images.
image_dir = "./images"
Expand Down

0 comments on commit a9cee7b

Please sign in to comment.