From 2227a2473da0c9645932648c51ce30257f361f92 Mon Sep 17 00:00:00 2001 From: rawleyIfowler Date: Sun, 13 Feb 2022 14:04:31 -0600 Subject: [PATCH] implemented database connection for blog, added blog templates, and cresated a bootstrap submodule --- :w | 45 ++++++++++++++++++++++++++++++++++ {utils => bootstrap}/routes.go | 6 +++-- controllers/blog.go | 27 +++++++++++++++++--- go.mod | 6 ++++- go.sum | 12 +++++++++ main.go | 7 +++--- models/blog_post.go | 14 +++++------ models/comment.go | 5 ++++ static/index.css | 6 ++--- templates/blog.tmpl | 14 +++++++++++ utils/dsn.go | 33 +++++++++++++++++++++++++ utils/migrations.go | 27 ++++++++++++++++++++ 12 files changed, 182 insertions(+), 20 deletions(-) create mode 100644 :w rename {utils => bootstrap}/routes.go (89%) create mode 100644 templates/blog.tmpl create mode 100644 utils/dsn.go create mode 100644 utils/migrations.go diff --git a/:w b/:w new file mode 100644 index 0000000..802d7c7 --- /dev/null +++ b/:w @@ -0,0 +1,45 @@ +package init + +/* +Copyright (C) 2022 Rawley Fowler + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see .Rawley Fowler, 2022 +*/ + +import ( + "github.com/gin-gonic/gin" + "gitlab.com/rawleyifowler/site-rework/controllers" + "net/http" +) + +// Initializes all routes from the controller package +func InitializeRoutes(router *gin.Engine) { + router.NoRoute(ServePage("not_found.tmpl")) + blogGroup := router.Group("/blog") + controllers.RegisterBlogGroup(blogGroup) + router.GET("/", ServePage("index.tmpl")) + router.GET("/resume", ServePage("resume.tmpl")) + router.GET("/contact", ServePage("contact.tmpl")) +} + +// Returns a handler function to serve a page based on the template that is inputted +func ServePage(temp string) gin.HandlerFunc { + return func(c *gin.Context) { + c.HTML( + http.StatusOK, + temp, + gin.H{}, + ) + } +} diff --git a/utils/routes.go b/bootstrap/routes.go similarity index 89% rename from utils/routes.go rename to bootstrap/routes.go index 79f968c..a4af90f 100644 --- a/utils/routes.go +++ b/bootstrap/routes.go @@ -1,4 +1,4 @@ -package utils +package bootstrap /* Copyright (C) 2022 Rawley Fowler @@ -19,16 +19,18 @@ along with this program. If not, see .Rawley Fow import ( "github.com/gin-gonic/gin" + "gitlab.com/rawleyifowler/site-rework/controllers" "net/http" ) // Initializes all routes from the controller package func InitializeRoutes(router *gin.Engine) { router.NoRoute(ServePage("not_found.tmpl")) + blogGroup := router.Group("/blog") + controllers.RegisterBlogGroup(blogGroup) router.GET("/", ServePage("index.tmpl")) router.GET("/resume", ServePage("resume.tmpl")) router.GET("/contact", ServePage("contact.tmpl")) - router.GET("/blog", ServePage("wip.tmpl")); } // Returns a handler function to serve a page based on the template that is inputted diff --git a/controllers/blog.go b/controllers/blog.go index c7521c4..e2905e2 100644 --- a/controllers/blog.go +++ b/controllers/blog.go @@ -21,21 +21,42 @@ import ( "github.com/gin-gonic/gin" "net/http" "gitlab.com/rawleyifowler/site-rework/models" + "gitlab.com/rawleyifowler/site-rework/utils" + "gorm.io/driver/mysql" + "gorm.io/gorm" ) +var db *gorm.DB + func RegisterBlogGroup(r *gin.RouterGroup) { + // Load dsn and initialize database + dsn := utils.LoadDSN("dsn") + var err error + db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) + if err != nil { + panic("Database connection failed...") + } + utils.PerformMigrations(db) r.GET("/", RenderBlogPage) r.GET("/post/:url", RenderIndividualBlogPost) } func RenderBlogPage(c *gin.Context) { - + c.HTML(http.StatusOK, "blog.tmpl", *GetAllBlogPosts()) } func RenderIndividualBlogPost(c *gin.Context) { - + c.HTML(http.StatusOK, "blog_post.tmpl", GetBlogPostById(c.Param("url"))) } -func GetAllBlogPosts() { +func GetAllBlogPosts() *[]models.BlogPost { + var posts []models.BlogPost + db.Find(&posts) + return &posts +} +func GetBlogPostById(id string) *models.BlogPost { + var post models.BlogPost + db.Where(&models.BlogPost{ Url: id }).First(&post) + return &post } diff --git a/go.mod b/go.mod index aeda7ed..aced78f 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,8 @@ module gitlab.com/rawleyifowler/site-rework go 1.15 -require github.com/gin-gonic/gin v1.7.7 +require ( + github.com/gin-gonic/gin v1.7.7 + gorm.io/driver/mysql v1.2.3 + gorm.io/gorm v1.22.5 +) diff --git a/go.sum b/go.sum index abee8fc..6946f07 100644 --- a/go.sum +++ b/go.sum @@ -11,9 +11,16 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87 github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= +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/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.3/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas= +github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= @@ -47,3 +54,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gorm.io/driver/mysql v1.2.3 h1:cZqzlOfg5Kf1VIdLC1D9hT6Cy9BgxhExLj/2tIgUe7Y= +gorm.io/driver/mysql v1.2.3/go.mod h1:qsiz+XcAyMrS6QY+X3M9R6b/lKM1imKmcuK9kac5LTo= +gorm.io/gorm v1.22.4/go.mod h1:1aeVC+pe9ZmvKZban/gW4QPra7PRoTEssyc922qCAkk= +gorm.io/gorm v1.22.5 h1:lYREBgc02Be/5lSCTuysZZDb6ffL2qrat6fg9CFbvXU= +gorm.io/gorm v1.22.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= diff --git a/main.go b/main.go index 893bf95..22912c4 100644 --- a/main.go +++ b/main.go @@ -19,14 +19,15 @@ along with this program. If not, see .Rawley Fow import ( "github.com/gin-gonic/gin" - "gitlab.com/rawleyifowler/site-rework/utils" + "gitlab.com/rawleyifowler/site-rework/bootstrap" ) var router *gin.Engine +var dsn string -func main() { +func main() { router = gin.Default() router.LoadHTMLGlob("templates/*.tmpl") - utils.InitializeRoutes(router) + bootstrap.InitializeRoutes(router) router.Run() } diff --git a/models/blog_post.go b/models/blog_post.go index 3653a46..a11cf5d 100644 --- a/models/blog_post.go +++ b/models/blog_post.go @@ -16,16 +16,14 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see .Rawley Fowler, 2022 */ +import ( + "gorm.io/gorm" +) type BlogPost struct { - Url string `json:"url"` + gorm.Model + Url string `json:"url" gorm:"primaryKey"` Title string `json:"title"` Date string `json:"date"` - Comments []Comment -} - -// Load comments here -func (b *BlogPost) LoadComments() bool { - - return false + Comments []Comment `gorm:"foreignKey:Id"` } diff --git a/models/comment.go b/models/comment.go index b960053..ca67103 100644 --- a/models/comment.go +++ b/models/comment.go @@ -16,8 +16,13 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see .Rawley Fowler, 2022 */ +import ( + "gorm.io/gorm" +) type Comment struct { + gorm.Model + Id uint `json:"id"` Date string `json:"date"` Author string `json:"author"` Content string `json:"content"` diff --git a/static/index.css b/static/index.css index 28dfd9a..0df6f92 100644 --- a/static/index.css +++ b/static/index.css @@ -121,13 +121,13 @@ a:hover:after { } .post-record { - padding-left: 12px; - padding-right: 12px; + padding-left: 6px; + padding-right: 6px; width: 200px + 2vw; display: flex; flex-direction: row; flex-wrap: wrap; - margin-top: 9px; + margin-top: 12px; } .skill-keywords { diff --git a/templates/blog.tmpl b/templates/blog.tmpl new file mode 100644 index 0000000..24168fb --- /dev/null +++ b/templates/blog.tmpl @@ -0,0 +1,14 @@ +{{ template "header.tmpl" . }} +

Blog posts

+{{ range $val := . }} +
+ +
+ {{ $val.Date }} +
+
+{{ end }} + +{{ template "footer.tmpl" }} diff --git a/utils/dsn.go b/utils/dsn.go new file mode 100644 index 0000000..4c3e7cd --- /dev/null +++ b/utils/dsn.go @@ -0,0 +1,33 @@ +package utils +/* +Copyright (C) 2022 Rawley Fowler + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see .Rawley Fowler, 2022 +*/ +import ( + "os" + "bufio" +) + +// Loads the dsn via OS file system and then returns the value as a string +func LoadDSN(dsnPath string) string { + file, err := os.Open(dsnPath) + if err != nil { + panic("Could not find DSN for database...") + } + defer file.Close() + reader := bufio.NewScanner(file) + // The dsn should be the first line of the file + return reader.Text() +} diff --git a/utils/migrations.go b/utils/migrations.go new file mode 100644 index 0000000..be17039 --- /dev/null +++ b/utils/migrations.go @@ -0,0 +1,27 @@ +package utils +/* +Copyright (C) 2022 Rawley Fowler + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see .Rawley Fowler, 2022 +*/ +import ( + "gitlab.com/rawleyifowler/site-rework/models" + "gorm.io/gorm" +) + +// Performs migrations of all models +func PerformMigrations(db *gorm.DB) { + db.AutoMigrate(&models.BlogPost{}) + db.AutoMigrate(&models.Comment{}) +}