server checks if current device is mobile device, and let browser navigate to specific pages

This commit is contained in:
MaysWind
2020-11-20 22:52:44 +08:00
parent 80ce5a7f73
commit 1ba9790a42
5 changed files with 23 additions and 3 deletions
+8 -1
View File
@@ -9,6 +9,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"github.com/mssola/user_agent"
"github.com/urfave/cli/v2"
"github.com/mayswind/lab/pkg/api"
@@ -81,7 +82,13 @@ func startWebServer(c *cli.Context) error {
router.NoMethod(bindApi(api.Default.MethodNotAllowed))
router.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/mobile/")
ua := user_agent.New(c.GetHeader("User-Agent"))
if ua.Mobile() {
c.Redirect(http.StatusMovedPermanently, "/mobile/")
} else {
c.Redirect(http.StatusMovedPermanently, "/desktop/")
}
})
router.StaticFile("robots.txt", filepath.Join(config.StaticRootPath, "robots.txt"))