Skip to content

Commit 948ec2d

Browse files
committed
和菓子ってパンだっけ?
1 parent aa86a81 commit 948ec2d

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

_env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
POSTGRES_USER="postgres"
22
POSTGRES_PASSWORD="password"
33
POSTGRES_DB="postgres"
4+
POSTGRES_HOST="localhost"
45
API_PASSWORD="password"

handlers/home_handler.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
7+
"github.com/gin-gonic/gin"
8+
"gorm.io/gorm"
9+
)
10+
11+
func GetHome(db *gorm.DB) gin.HandlerFunc {
12+
return func(c *gin.Context) {
13+
// User-Agentを取得してブラウザかどうか判定
14+
userAgent := c.GetHeader("User-Agent")
15+
if isBrowser(userAgent) {
16+
// ブラウザの場合はリダイレクト
17+
c.Redirect(http.StatusMovedPermanently, "https://api.ayane0857.net/docs/payment-api/")
18+
return
19+
}
20+
21+
// ブラウザ以外の場合はシンプルなJSON応答
22+
c.JSON(http.StatusOK, gin.H{
23+
"content": "Hello World",
24+
})
25+
}
26+
}
27+
28+
// ブラウザかどうかを判定する関数
29+
func isBrowser(userAgent string) bool {
30+
userAgent = strings.ToLower(userAgent)
31+
32+
// 一般的なブラウザのUser-Agentパターン
33+
browserPatterns := []string{
34+
"mozilla",
35+
"chrome",
36+
"safari",
37+
"firefox",
38+
"edge",
39+
"opera",
40+
}
41+
42+
// ブラウザパターンに一致する場合はブラウザ
43+
for _, pattern := range browserPatterns {
44+
if strings.Contains(userAgent, pattern) {
45+
return true
46+
}
47+
}
48+
49+
return false
50+
}

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ func main() {
6262
AllowHeaders: []string{"Origin", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"},
6363
AllowCredentials: true,
6464
}))
65-
65+
r.GET("/", handlers.GetHome(db))
66+
6667
r.GET("/payments", handlers.GetPayments(db))
6768
r.GET("/payments/:id", handlers.GetPayment(db))
6869

0 commit comments

Comments
 (0)