forked from acheong08/ChatGPT-to-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
43 lines (36 loc) · 826 Bytes
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"os"
gin "github.com/gin-gonic/gin"
)
var ADMIN_PASSWORD string
func init() {
ADMIN_PASSWORD = os.Getenv("ADMIN_PASSWORD")
if ADMIN_PASSWORD == "" {
ADMIN_PASSWORD = "TotallySecurePassword"
}
}
func adminCheck(c *gin.Context) {
password := c.Request.Header.Get("Authorization")
if password != ADMIN_PASSWORD {
c.String(401, "Unauthorized")
c.Abort()
return
}
c.Next()
}
func cors(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "*")
c.Header("Access-Control-Allow-Headers", "*")
c.Next()
}
func Authorization(c *gin.Context) {
api_password := os.Getenv("API_PASSWORD")
if api_password != "" && c.Request.Header.Get("Authorization") != api_password {
c.String(401, "Unauthorized")
c.Abort()
return
}
c.Next()
}