implemented database connection for blog, added blog templates, and cresated a bootstrap submodule
This commit is contained in:
45
:w
Normal file
45
:w
Normal file
@@ -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 <https://www.gnu.org/licenses/>.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{},
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package bootstrap
|
||||
|
||||
/*
|
||||
Copyright (C) 2022 Rawley Fowler
|
||||
@@ -19,16 +19,18 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.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
|
||||
@@ -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
|
||||
}
|
||||
|
||||
6
go.mod
6
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
|
||||
)
|
||||
|
||||
12
go.sum
12
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=
|
||||
|
||||
7
main.go
7
main.go
@@ -19,14 +19,15 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.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()
|
||||
}
|
||||
|
||||
@@ -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 <https://www.gnu.org/licenses/>.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"`
|
||||
}
|
||||
|
||||
@@ -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 <https://www.gnu.org/licenses/>.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"`
|
||||
|
||||
@@ -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 {
|
||||
|
||||
14
templates/blog.tmpl
Normal file
14
templates/blog.tmpl
Normal file
@@ -0,0 +1,14 @@
|
||||
{{ template "header.tmpl" . }}
|
||||
<h2>Blog posts</h2>
|
||||
{{ range $val := . }}
|
||||
<div class="post-record">
|
||||
<div>
|
||||
<a href="/blog/post/{{ $val.Url }}">{{ $val.Title }}</a>
|
||||
</div>
|
||||
<div>
|
||||
{{ $val.Date }}
|
||||
</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
{{ template "footer.tmpl" }}
|
||||
33
utils/dsn.go
Normal file
33
utils/dsn.go
Normal file
@@ -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 <https://www.gnu.org/licenses/>.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()
|
||||
}
|
||||
27
utils/migrations.go
Normal file
27
utils/migrations.go
Normal file
@@ -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 <https://www.gnu.org/licenses/>.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{})
|
||||
}
|
||||
Reference in New Issue
Block a user