Skip to content

Commit b29b495

Browse files
authored
Merge pull request #525 from goblogplatform/fix/head-request-support
Support HEAD requests on all public routes
2 parents f8be04f + 1f2859c commit b29b495

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

goblog.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"log"
2222
"net/http"
2323
"os"
24+
"sync"
2425
"path/filepath"
2526
"strings"
2627

@@ -42,7 +43,7 @@ type goblog struct {
4243
_registry *gplugin.Registry
4344
sessionKey string
4445
router *gin.Engine
45-
handlersRegistered bool
46+
routesOnce sync.Once
4647
}
4748

4849
func envFilePresent() bool {
@@ -106,7 +107,7 @@ func attemptConnectDb() *gorm.DB {
106107
}
107108

108109
// depending on if the env file is present or not, we will show the wizard or the main site
109-
func (g goblog) rootHandler(c *gin.Context) {
110+
func (g *goblog) rootHandler(c *gin.Context) {
110111
if !envFilePresent() {
111112
log.Println("Root handler: No .env file found")
112113
c.HTML(http.StatusOK, "wizard_db.html", gin.H{
@@ -186,7 +187,7 @@ func (g goblog) rootHandler(c *gin.Context) {
186187
}
187188
}
188189

189-
func (g goblog) loginHandler(c *gin.Context) {
190+
func (g *goblog) loginHandler(c *gin.Context) {
190191
if !envFilePresent() {
191192
log.Println("Root handler: No .env file found")
192193
c.HTML(http.StatusOK, "wizard_db.html", gin.H{
@@ -316,7 +317,7 @@ func main() {
316317
registry.StartScheduledJobs()
317318
}
318319

319-
goblog := goblog{
320+
goblog := &goblog{
320321
_wizard: &_wizard,
321322
_blog: &_blog,
322323
_auth: &_auth,
@@ -402,8 +403,8 @@ func main() {
402403
c.File(filepath.Join("themes", activeTheme, "static", fp))
403404
})
404405

405-
router.GET("/", goblog.rootHandler)
406-
router.GET("/login", goblog.loginHandler)
406+
getAndHead(router, "/", goblog.rootHandler)
407+
getAndHead(router, "/login", goblog.loginHandler)
407408
router.GET("/wizard", goblog._wizard.SaveToken)
408409
router.POST("/wizard_db", updateDB)
409410
router.POST("/test_db", testDB)
@@ -427,12 +428,13 @@ func main() {
427428
}
428429
}
429430

430-
func (g goblog) addRoutes() {
431-
if g.handlersRegistered {
432-
log.Println("Handlers already registered")
433-
return
434-
}
435-
g.handlersRegistered = true
431+
func (g *goblog) addRoutes() {
432+
g.routesOnce.Do(func() {
433+
g.addRoutesInner()
434+
})
435+
}
436+
437+
func (g *goblog) addRoutesInner() {
436438
log.Println("Adding main blog routes")
437439
//all of this is the json api
438440
g.router.MaxMultipartMemory = 50 << 20
@@ -468,16 +470,16 @@ func (g goblog) addRoutes() {
468470
//the json API. The json API is tested more easily. Also javascript can
469471
//served in the html can be used to create and update posts by directly
470472
//working with the json API.
471-
g.router.GET("/index.php", g._blog.Home)
472-
g.router.GET("/posts/:yyyy/:mm/:dd/:slug", g._blog.Post)
473+
getAndHead(g.router, "/index.php", g._blog.Home)
474+
getAndHead(g.router, "/posts/:yyyy/:mm/:dd/:slug", g._blog.Post)
473475
// lets posts work with our without the word posts in front
474-
g.router.GET("/:yyyy/:mm/:dd/:slug", g._blog.Post)
476+
getAndHead(g.router, "/:yyyy/:mm/:dd/:slug", g._blog.Post)
475477
g.router.GET("/admin/posts/:yyyy/:mm/:dd/:slug", g._admin.Post)
476-
g.router.GET("/tag/*name", g._blog.Tag)
478+
getAndHead(g.router, "/tag/*name", g._blog.Tag)
477479
g.router.GET("/logout", g._blog.Logout)
478480

479-
g.router.GET("/search", g._blog.Search)
480-
g.router.GET("/sitemap.xml", g._blog.Sitemap)
481+
getAndHead(g.router, "/search", g._blog.Search)
482+
getAndHead(g.router, "/sitemap.xml", g._blog.Sitemap)
481483
// lets old WordPress stuff stored at wp-content/uploads work
482484
g.router.Use(static.Serve("/wp-content", static.LocalFile("www", false)))
483485

@@ -494,6 +496,13 @@ func (g goblog) addRoutes() {
494496
g.router.NoRoute(g._blog.NoRoute)
495497
}
496498

499+
// getAndHead registers a handler for both GET and HEAD on the given path.
500+
// HEAD is required by the HTTP spec for any resource that supports GET.
501+
func getAndHead(router gin.IRoutes, path string, handler gin.HandlerFunc) {
502+
router.GET(path, handler)
503+
router.HEAD(path, handler)
504+
}
505+
497506
func CORS() gin.HandlerFunc {
498507
return func(c *gin.Context) {
499508
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")

0 commit comments

Comments
 (0)