From 451fd8452bc5b484dff07863dc057693cf79c8be Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Tue, 27 Dec 2022 21:00:19 +0100 Subject: [PATCH 01/35] Complete file tree and packages managment rework --- cmd/main.go | 21 ++-- go.mod | 8 +- go.sum | 49 ++++------ internal/{ => api}/auth.go | 7 +- internal/{answers.go => api/messages.go} | 2 +- internal/{ => api}/ping.go | 2 +- .../business.go} | 5 +- .../business_test.go} | 2 +- internal/articles/handlers.go | 98 +++++++++++++++++++ internal/articlesHandlers.go | 96 ------------------ internal/{ => database}/database.go | 24 ++--- 11 files changed, 153 insertions(+), 161 deletions(-) rename internal/{ => api}/auth.go (80%) rename internal/{answers.go => api/messages.go} (95%) rename internal/{ => api}/ping.go (88%) rename internal/{articlesBusiness.go => articles/business.go} (82%) rename internal/{articles_test.go => articles/business_test.go} (98%) create mode 100644 internal/articles/handlers.go delete mode 100644 internal/articlesHandlers.go rename internal/{ => database}/database.go (81%) diff --git a/cmd/main.go b/cmd/main.go index d2f2a4d..36095ac 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -4,8 +4,9 @@ import ( "fmt" "os" + "github.com/Floriansylvain/GohCMS/internal/api" + "github.com/Floriansylvain/GohCMS/internal/articles" "github.com/gin-gonic/gin" - "github.com/gotest/internal" "github.com/joho/godotenv" ) @@ -21,15 +22,15 @@ func initEnvVariables() { } func initJWT() { - errInit := internal.AuthMiddleware.MiddlewareInit() + errInit := api.AuthMiddleware.MiddlewareInit() if errInit != nil { fmt.Printf(errInit.Error()) } } func initBasicRoutes(r *gin.Engine) { - r.POST("/login/", internal.AuthMiddleware.LoginHandler) - r.GET("/ping/", internal.Ping) + r.POST("/login/", api.AuthMiddleware.LoginHandler) + r.GET("/ping/", api.Ping) } func corsMiddleware(c *gin.Context) { @@ -46,13 +47,13 @@ func corsMiddleware(c *gin.Context) { func initArticlesRoutes(r *gin.Engine) { articlesRouter := r.Group("/articles") - articlesRouter.Use(corsMiddleware, internal.AuthMiddleware.MiddlewareFunc()) + articlesRouter.Use(corsMiddleware, api.AuthMiddleware.MiddlewareFunc()) - articlesRouter.GET("/", internal.GetAllArticlesHandler) - articlesRouter.GET("/:id", internal.GetArticleHandler) - articlesRouter.POST("/:id", internal.AddArticleHandler) - articlesRouter.PATCH("/:id", internal.EditArticleHandler) - articlesRouter.DELETE("/:id", internal.DeleteArticleHandler) + articlesRouter.GET("/", articles.GetAllArticlesHandler) + articlesRouter.GET("/:id", articles.GetArticleHandler) + articlesRouter.POST("/:id", articles.AddArticleHandler) + articlesRouter.PATCH("/:id", articles.EditArticleHandler) + articlesRouter.DELETE("/:id", articles.DeleteArticleHandler) } func initGin() { diff --git a/go.mod b/go.mod index 9310f0b..6ba95bd 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,10 @@ -module github.com/gotest +module github.com/Floriansylvain/GohCMS go 1.19 require ( github.com/appleboy/gin-jwt/v2 v2.9.1 - github.com/gin-gonic/gin v1.8.1 + github.com/gin-gonic/gin v1.8.2 github.com/joho/godotenv v1.4.0 go.mongodb.org/mongo-driver v1.11.1 ) @@ -14,7 +14,6 @@ require ( github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect github.com/go-playground/validator/v10 v10.11.1 // indirect - github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/goccy/go-json v0.10.0 // indirect github.com/golang-jwt/jwt/v4 v4.4.3 // indirect github.com/golang/snappy v0.0.4 // indirect @@ -22,13 +21,12 @@ require ( github.com/klauspost/compress v1.15.13 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/mattn/go-isatty v0.0.16 // indirect - github.com/mattn/go-sqlite3 v1.14.15 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/montanaflynn/stats v0.6.6 // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/ugorji/go/codec v1.2.7 // indirect + github.com/ugorji/go/codec v1.2.8 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/scram v1.1.2 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect diff --git a/go.sum b/go.sum index fe94b9d..379070f 100644 --- a/go.sum +++ b/go.sum @@ -1,79 +1,76 @@ github.com/appleboy/gin-jwt/v2 v2.9.1 h1:l29et8iLW6omcHltsOP6LLk4s3v4g2FbFs0koxGWVZs= github.com/appleboy/gin-jwt/v2 v2.9.1/go.mod h1:jwcPZJ92uoC9nOUTOKWoN/f6JZOgMSKlFSHw5/FrRUk= +github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4= github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8= github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk= +github.com/gin-gonic/gin v1.8.2 h1:UzKToD9/PoFj/V4rvlKqTRKnQYyz8Sc1MJlv4JHPtvY= +github.com/gin-gonic/gin v1.8.2/go.mod h1:qw5AYuDrzRTnhvusDsrov+fDIxp9Dleuu12h8nfB398= +github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU= github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs= github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= -github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0= github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos= github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ= github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU= -github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= -github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM= github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA= github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang-jwt/jwt/v4 v4.4.3 h1:Hxl6lhQFj4AnOX6MLrsCb/+7tCj7DxP7VA+2rDIq5AU= github.com/golang-jwt/jwt/v4 v4.4.3/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/compress v1.15.13 h1:NFn1Wr8cfnenSJSA46lLq4wHCcBzKTSjnBIexDMMOV0= github.com/klauspost/compress v1.15.13/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= -github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= -github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= -github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/montanaflynn/stats v0.6.6 h1:Duep6KMIDpY4Yo11iFsvyqJDyfzLF9+sndUKT+v64GQ= github.com/montanaflynn/stats v0.6.6/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= -github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU= github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= @@ -83,59 +80,52 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw= github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= -github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo= github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= -github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= +github.com/ugorji/go/codec v1.2.8 h1:sgBJS6COt0b/P40VouWKdseidkDgHxYGm0SAglUHfP0= +github.com/ugorji/go/codec v1.2.8/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= -github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E= github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g= github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= -github.com/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs= github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8= github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= -github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk= github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.mongodb.org/mongo-driver v1.10.3 h1:XDQEvmh6z1EUsXuIkXE9TaVeqHw6SwS1uf93jFs0HBA= -go.mongodb.org/mongo-driver v1.10.3/go.mod h1:z4XpeoU6w+9Vht+jAFyLgVrD+jGSQQe0+CBWFHNiHt8= go.mongodb.org/mongo-driver v1.11.1 h1:QP0znIRTuL0jf1oBQoAoM0C6ZJfBK4kx0Uumtv1A7w8= go.mongodb.org/mongo-driver v1.11.1/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8= golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU= golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -145,7 +135,6 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 h1:siQdpVirKtzPhKl3lZWozZraCFObP8S1v6PRp0bLrtU= golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -157,9 +146,7 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= @@ -168,18 +155,20 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/auth.go b/internal/api/auth.go similarity index 80% rename from internal/auth.go rename to internal/api/auth.go index 934d0ee..5e44944 100644 --- a/internal/auth.go +++ b/internal/api/auth.go @@ -1,10 +1,11 @@ -package internal +package api import ( "errors" "os" "time" + "github.com/Floriansylvain/GohCMS/internal/database" jwt "github.com/appleboy/gin-jwt/v2" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" @@ -15,7 +16,7 @@ type User struct { Password string `json:"password" bson:"password"` } -var UsersLocation = Location{Database: "gohcms", Collection: "users"} +var UsersLocation = database.Location{Database: "gohcms", Collection: "users"} var AuthMiddleware, _ = jwt.New(&jwt.GinJWTMiddleware{ Realm: "GohCMS", @@ -32,7 +33,7 @@ func JWTAuthenticator(c *gin.Context) (interface{}, error) { return nil, errors.New("wrong credentials json format.") } - _, err = getUniqueDocument(UsersLocation, bson.D{ + _, err = database.GetUniqueDocument(UsersLocation, bson.D{ {Key: "email", Value: user.Email}, {Key: "password", Value: user.Password}, }) diff --git a/internal/answers.go b/internal/api/messages.go similarity index 95% rename from internal/answers.go rename to internal/api/messages.go index c57e592..301f7c7 100644 --- a/internal/answers.go +++ b/internal/api/messages.go @@ -1,4 +1,4 @@ -package internal +package api import ( "net/http" diff --git a/internal/ping.go b/internal/api/ping.go similarity index 88% rename from internal/ping.go rename to internal/api/ping.go index 42606c8..eda7355 100644 --- a/internal/ping.go +++ b/internal/api/ping.go @@ -1,4 +1,4 @@ -package internal +package api import ( "net/http" diff --git a/internal/articlesBusiness.go b/internal/articles/business.go similarity index 82% rename from internal/articlesBusiness.go rename to internal/articles/business.go index 4366153..66d56aa 100644 --- a/internal/articlesBusiness.go +++ b/internal/articles/business.go @@ -1,6 +1,7 @@ -package internal +package articles import ( + "github.com/Floriansylvain/GohCMS/internal/database" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" ) @@ -11,7 +12,7 @@ type Article struct { Content gin.H `json:"content" bson:"content"` } -var articlesLocation = Location{Database: "gohcms", Collection: "articles"} +var articlesLocation = database.Location{Database: "gohcms", Collection: "articles"} func GetAllArticlesBusiness(documents [][]byte) []Article { var articles = []Article{} diff --git a/internal/articles_test.go b/internal/articles/business_test.go similarity index 98% rename from internal/articles_test.go rename to internal/articles/business_test.go index 2bcb9f5..24a8c1b 100644 --- a/internal/articles_test.go +++ b/internal/articles/business_test.go @@ -1,4 +1,4 @@ -package internal +package articles import ( "log" diff --git a/internal/articles/handlers.go b/internal/articles/handlers.go new file mode 100644 index 0000000..f02d96c --- /dev/null +++ b/internal/articles/handlers.go @@ -0,0 +1,98 @@ +package articles + +import ( + "fmt" + "net/http" + + "github.com/Floriansylvain/GohCMS/internal/api" + "github.com/Floriansylvain/GohCMS/internal/database" + "github.com/gin-gonic/gin" + "go.mongodb.org/mongo-driver/bson" +) + +func GetAllArticlesHandler(c *gin.Context) { + documents, err := database.GetDocuments(articlesLocation, gin.H{}) + if err != nil { + api.SendBadRequest(c, err.Error()) + return + } + c.JSON(http.StatusOK, GetAllArticlesBusiness(documents)) +} + +func GetArticleHandler(c *gin.Context) { + articleID := c.Params.ByName("id") + article, err := database.GetUniqueDocument(articlesLocation, + gin.H{"id_name": articleID}) + if err != nil { + api.SendBadRequest(c, "The ID provided doesn't match any article.") + return + } + var parsedArticle Article + bson.Unmarshal(article, &parsedArticle) + c.JSON(http.StatusOK, parsedArticle) +} + +func AddArticleHandler(c *gin.Context) { + var article Article + article.IdName = c.Params.ByName("id") + if c.BindJSON(&article) != nil { + api.SendBadRequest(c, "Could not correctly parse the article.") + return + } + + document, err := bson.Marshal(article) + if err != nil { + api.SendBadRequest(c, "Could not correctly marshal the article.") + return + } + + documents, _ := database.GetDocuments(articlesLocation, gin.H{}) + if IsArticleIdAlreadyUsed(article.IdName, documents) { + api.SendBadRequest(c, "Article ID already used.") + return + } + + err = database.PushDocument(articlesLocation, document) + if err != nil { + api.SendBadRequest(c, fmt.Sprintf(`Could not insert document into DB: %v`, err.Error())) + return + } + + api.SendOk(c, "Article successfully added!") +} + +func DeleteArticleHandler(c *gin.Context) { + id := c.Params.ByName("id") + + deleteCount, err := database.DeleteDocument(articlesLocation, gin.H{"id_name": id}) + if err != nil { + api.SendBadRequest(c, "Could not delete document into DB.") + return + } + + if deleteCount != 0 { + api.SendOk(c, fmt.Sprintf("%d articles were successfully deleted!", deleteCount)) + } else { + api.SendOk(c, "No articles were deleted.") + } +} + +func EditArticleHandler(c *gin.Context) { + id := c.Params.ByName("id") + + var articleUpdate database.DocumentUpdate + articleUpdate.Filter = gin.H{"id_name": id} + c.BindJSON(&articleUpdate.Update) + + editCount, err := database.EditDocument(articlesLocation, articleUpdate) + if err != nil { + api.SendBadRequest(c, err.Error()) + return + } + + if editCount != 0 { + api.SendOk(c, fmt.Sprintf("%d articles were successfully edited!", editCount)) + } else { + api.SendOk(c, "No articles were edited.") + } +} diff --git a/internal/articlesHandlers.go b/internal/articlesHandlers.go deleted file mode 100644 index dff2757..0000000 --- a/internal/articlesHandlers.go +++ /dev/null @@ -1,96 +0,0 @@ -package internal - -import ( - "fmt" - "net/http" - - "github.com/gin-gonic/gin" - "go.mongodb.org/mongo-driver/bson" -) - -func GetAllArticlesHandler(c *gin.Context) { - documents, err := getDocuments(articlesLocation, gin.H{}) - if err != nil { - SendBadRequest(c, err.Error()) - return - } - c.JSON(http.StatusOK, GetAllArticlesBusiness(documents)) -} - -func GetArticleHandler(c *gin.Context) { - articleID := c.Params.ByName("id") - article, err := getUniqueDocument(articlesLocation, - gin.H{"id_name": articleID}) - if err != nil { - SendBadRequest(c, "The ID provided doesn't match any article.") - return - } - var parsedArticle Article - bson.Unmarshal(article, &parsedArticle) - c.JSON(http.StatusOK, parsedArticle) -} - -func AddArticleHandler(c *gin.Context) { - var article Article - article.IdName = c.Params.ByName("id") - if c.BindJSON(&article) != nil { - SendBadRequest(c, "Could not correctly parse the article.") - return - } - - document, err := bson.Marshal(article) - if err != nil { - SendBadRequest(c, "Could not correctly marshal the article.") - return - } - - documents, _ := getDocuments(articlesLocation, gin.H{}) - if IsArticleIdAlreadyUsed(article.IdName, documents) { - SendBadRequest(c, "Article ID already used.") - return - } - - err = pushDocument(articlesLocation, document) - if err != nil { - SendBadRequest(c, fmt.Sprintf(`Could not insert document into DB: %v`, err.Error())) - return - } - - SendOk(c, "Article successfully added!") -} - -func DeleteArticleHandler(c *gin.Context) { - id := c.Params.ByName("id") - - deleteCount, err := deleteDocument(articlesLocation, gin.H{"id_name": id}) - if err != nil { - SendBadRequest(c, "Could not delete document into DB.") - return - } - - if deleteCount != 0 { - SendOk(c, fmt.Sprintf("%d articles were successfully deleted!", deleteCount)) - } else { - SendOk(c, "No articles were deleted.") - } -} - -func EditArticleHandler(c *gin.Context) { - id := c.Params.ByName("id") - - var articleUpdate DocumentUpdate - articleUpdate.Filter = gin.H{"id_name": id} - c.BindJSON(&articleUpdate.Update) - - editCount, err := editDocument(articlesLocation, articleUpdate) - if err != nil { - SendBadRequest(c, err.Error()) - return - } - - if editCount != 0 { - SendOk(c, fmt.Sprintf("%d articles were successfully edited!", editCount)) - } else { - SendOk(c, "No articles were edited.") - } -} diff --git a/internal/database.go b/internal/database/database.go similarity index 81% rename from internal/database.go rename to internal/database/database.go index b190406..0ad9853 100644 --- a/internal/database.go +++ b/internal/database/database.go @@ -1,4 +1,4 @@ -package internal +package database import ( "context" @@ -19,7 +19,7 @@ type DocumentUpdate struct { Update gin.H `json:"update"` } -func getNewClient() *mongo.Client { +func GetNewClient() *mongo.Client { client, err := mongo.Connect( context.TODO(), options.Client().ApplyURI("mongodb://db:27017/")) @@ -29,8 +29,8 @@ func getNewClient() *mongo.Client { return client } -func pushDocument(location Location, document interface{}) error { - client := getNewClient() +func PushDocument(location Location, document interface{}) error { + client := GetNewClient() collection := client.Database(location.Database).Collection(location.Collection) defer client.Disconnect(context.TODO()) @@ -41,8 +41,8 @@ func pushDocument(location Location, document interface{}) error { return nil } -func getDocuments(location Location, filter interface{}) ([][]byte, error) { - client := getNewClient() +func GetDocuments(location Location, filter interface{}) ([][]byte, error) { + client := GetNewClient() collection := client.Database(location.Database).Collection(location.Collection) defer client.Disconnect(context.TODO()) @@ -58,8 +58,8 @@ func getDocuments(location Location, filter interface{}) ([][]byte, error) { return results, nil } -func getUniqueDocument(location Location, filter interface{}) ([]byte, error) { - client := getNewClient() +func GetUniqueDocument(location Location, filter interface{}) ([]byte, error) { + client := GetNewClient() collection := client.Database(location.Database).Collection(location.Collection) defer client.Disconnect(context.TODO()) @@ -70,8 +70,8 @@ func getUniqueDocument(location Location, filter interface{}) ([]byte, error) { return singleResult.DecodeBytes() } -func deleteDocument(location Location, filter interface{}) (int64, error) { - client := getNewClient() +func DeleteDocument(location Location, filter interface{}) (int64, error) { + client := GetNewClient() collection := client.Database(location.Database).Collection(location.Collection) defer client.Disconnect(context.TODO()) @@ -83,8 +83,8 @@ func deleteDocument(location Location, filter interface{}) (int64, error) { return result.DeletedCount, nil } -func editDocument(location Location, jsons DocumentUpdate) (int64, error) { - client := getNewClient() +func EditDocument(location Location, jsons DocumentUpdate) (int64, error) { + client := GetNewClient() collection := client.Database(location.Database).Collection(location.Collection) defer client.Disconnect(context.TODO()) From f4908900b42996d6655d4321a00aeb43adf5284c Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Tue, 27 Dec 2022 21:07:12 +0100 Subject: [PATCH 02/35] Fixed env vars init emplacement --- cmd/main.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 36095ac..03d51a0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -10,15 +10,20 @@ import ( "github.com/joho/godotenv" ) -var ginMode = os.Getenv("APP_GIN_MODE") -var apiPort = os.Getenv("APP_API_PORT") -var frontPort = os.Getenv("APP_FRONT_PORT") -var hostAddress = os.Getenv("APP_HOST_ADDRESS") +var ginMode string +var apiPort string +var frontPort string +var hostAddress string func initEnvVariables() { if godotenv.Load() != nil { panic("Error loading .env file.") } + + ginMode = os.Getenv("APP_GIN_MODE") + apiPort = os.Getenv("APP_API_PORT") + frontPort = os.Getenv("APP_FRONT_PORT") + hostAddress = os.Getenv("APP_HOST_ADDRESS") } func initJWT() { From c950e2cde1ce97d6db13d82185b5d1773f3377b7 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Tue, 27 Dec 2022 21:07:21 +0100 Subject: [PATCH 03/35] Fixed build command --- Dockerfile.backend | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile.backend b/Dockerfile.backend index 172cde3..c1a2903 100644 --- a/Dockerfile.backend +++ b/Dockerfile.backend @@ -9,6 +9,6 @@ RUN go mod download ADD cmd ./cmd/ ADD internal ./internal/ COPY .env ./ -RUN go build -o ./GohCMS/ ./cmd ./internal +RUN go build -o ./GohCMS/ ./... CMD ["./GohCMS/cmd"] \ No newline at end of file From 948243525e32245418697b08bbd49304dcc147f6 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Fri, 30 Dec 2022 02:02:38 +0100 Subject: [PATCH 04/35] Added page_id field to Articles --- internal/articles/business.go | 1 + internal/articles/business_test.go | 2 ++ mongo-init.js | 5 ++++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/articles/business.go b/internal/articles/business.go index 66d56aa..fb4dfef 100644 --- a/internal/articles/business.go +++ b/internal/articles/business.go @@ -10,6 +10,7 @@ type Article struct { IdName string `json:"id_name" bson:"id_name"` Date int64 `json:"date" bson:"date"` Content gin.H `json:"content" bson:"content"` + PageID string `json:"page_id" bson:"page_id"` } var articlesLocation = database.Location{Database: "gohcms", Collection: "articles"} diff --git a/internal/articles/business_test.go b/internal/articles/business_test.go index 24a8c1b..3286bf0 100644 --- a/internal/articles/business_test.go +++ b/internal/articles/business_test.go @@ -12,12 +12,14 @@ var Article1, _ = bson.Marshal(Article{ IdName: "test_article_1", Date: 1671482492, Content: gin.H{}, + PageID: "blog", }) var Article2, _ = bson.Marshal(Article{ IdName: "test_article_2", Date: 1671899112, Content: gin.H{}, + PageID: "blog", }) var BSONConvertedArticles = [][]byte{Article1, Article2} diff --git a/mongo-init.js b/mongo-init.js index 4fdf5de..186f791 100644 --- a/mongo-init.js +++ b/mongo-init.js @@ -4,7 +4,7 @@ const db = conn.getDB("gohcms") db.createCollection('articles', { validator: { $jsonSchema: { - required: ['content', 'date', 'id_name'], + required: ['content', 'date', 'id_name', 'page_id'], properties: { _id: { bsonType: 'objectId' @@ -18,6 +18,9 @@ db.createCollection('articles', { id_name: { bsonType: 'string', pattern: '^.+$' + }, + page_id: { + bsonType: 'string' } } } From 7107e4e867d1ae388a96e296f2f10cf7fc9fc042 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Fri, 30 Dec 2022 02:40:36 +0100 Subject: [PATCH 05/35] Formatting cleaning --- web/admin-gui/src/views/Login.vue | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/web/admin-gui/src/views/Login.vue b/web/admin-gui/src/views/Login.vue index 2feecea..64c925c 100644 --- a/web/admin-gui/src/views/Login.vue +++ b/web/admin-gui/src/views/Login.vue @@ -5,13 +5,13 @@ import { setCookie } from '@/utils/cookies'; import { ref, type Ref } from 'vue'; import { useRouter } from 'vue-router'; -const isTokenOK: Ref = ref(undefined) +const isTokenOK: Ref = ref(undefined) const router = useRouter() const authStore = useAuthStore() const email = ref('') const password = ref('') -const isFormEmpty: ()=>boolean = () => { +const isFormEmpty: () => boolean = () => { return email.value === '' || password.value === '' } @@ -59,7 +59,7 @@ function login(email: string, password: string): void { .then(response => response.json()) .then(result => { jwtHandler(result) - if (isTokenOK.value === true) { + if (isTokenOK.value === true) { router.push('/home') } }) @@ -70,7 +70,7 @@ function login(email: string, password: string): void {