diff --git a/admin-app/app.go b/admin-app/app.go index 0cf968b..436dab5 100644 --- a/admin-app/app.go +++ b/admin-app/app.go @@ -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)) diff --git a/admin-app/post.go b/admin-app/post.go index 67a4ae7..197a981 100644 --- a/admin-app/post.go +++ b/admin-app/post.go @@ -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" ) @@ -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 { @@ -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, + }) } } diff --git a/cmd/urchin-admin/main.go b/cmd/urchin-admin/main.go index c168d54..5dea17b 100644 --- a/cmd/urchin-admin/main.go +++ b/cmd/urchin-admin/main.go @@ -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" ) @@ -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) diff --git a/urchin_config.toml b/urchin_config.toml index 6ed6b1c..6e89007 100644 --- a/urchin_config.toml +++ b/urchin_config.toml @@ -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"