Skip to content

Commit 58c198c

Browse files
compscidrclaude
andcommitted
Support HEAD requests on all public routes
Gin does not automatically handle HEAD for GET routes. Add a getAndHead helper that registers both methods, and use it for all public-facing routes (home, posts, tags, search, sitemap). Fixes curl -I and similar tools returning 404 instead of 200. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1fa7e76 commit 58c198c

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

goblog.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ func main() {
397397
c.File(filepath.Join("themes", activeTheme, "static", fp))
398398
})
399399

400-
router.GET("/", goblog.rootHandler)
401-
router.GET("/login", goblog.loginHandler)
400+
getAndHead(router, "/", goblog.rootHandler)
401+
getAndHead(router, "/login", goblog.loginHandler)
402402
router.GET("/wizard", goblog._wizard.SaveToken)
403403
router.POST("/wizard_db", updateDB)
404404
router.POST("/test_db", testDB)
@@ -463,16 +463,16 @@ func (g goblog) addRoutes() {
463463
//the json API. The json API is tested more easily. Also javascript can
464464
//served in the html can be used to create and update posts by directly
465465
//working with the json API.
466-
g.router.GET("/index.php", g._blog.Home)
467-
g.router.GET("/posts/:yyyy/:mm/:dd/:slug", g._blog.Post)
466+
getAndHead(g.router, "/index.php", g._blog.Home)
467+
getAndHead(g.router, "/posts/:yyyy/:mm/:dd/:slug", g._blog.Post)
468468
// lets posts work with our without the word posts in front
469-
g.router.GET("/:yyyy/:mm/:dd/:slug", g._blog.Post)
469+
getAndHead(g.router, "/:yyyy/:mm/:dd/:slug", g._blog.Post)
470470
g.router.GET("/admin/posts/:yyyy/:mm/:dd/:slug", g._admin.Post)
471-
g.router.GET("/tag/*name", g._blog.Tag)
471+
getAndHead(g.router, "/tag/*name", g._blog.Tag)
472472
g.router.GET("/logout", g._blog.Logout)
473473

474-
g.router.GET("/search", g._blog.Search)
475-
g.router.GET("/sitemap.xml", g._blog.Sitemap)
474+
getAndHead(g.router, "/search", g._blog.Search)
475+
getAndHead(g.router, "/sitemap.xml", g._blog.Sitemap)
476476
// lets old WordPress stuff stored at wp-content/uploads work
477477
g.router.Use(static.Serve("/wp-content", static.LocalFile("www", false)))
478478

@@ -489,6 +489,13 @@ func (g goblog) addRoutes() {
489489
g.router.NoRoute(g._blog.NoRoute)
490490
}
491491

492+
// getAndHead registers a handler for both GET and HEAD on the given path.
493+
// HEAD is required by the HTTP spec for any resource that supports GET.
494+
func getAndHead(router *gin.Engine, path string, handler gin.HandlerFunc) {
495+
router.GET(path, handler)
496+
router.HEAD(path, handler)
497+
}
498+
492499
func CORS() gin.HandlerFunc {
493500
return func(c *gin.Context) {
494501
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")

0 commit comments

Comments
 (0)