-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (58 loc) · 1.4 KB
/
main.go
File metadata and controls
74 lines (58 loc) · 1.4 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"log"
"net/http"
"os"
"github.com/GitbookIO/diskache"
"github.com/fasttrack-solutions/reverse-proxy-cache/reverseproxy"
"github.com/gin-gonic/gin"
)
func main() {
apiKey := os.Getenv("API_KEY")
target := os.Getenv("TARGET")
bearerToken := os.Getenv("TARGET_BEARER_TOKEN")
port := os.Getenv("PORT")
removeFromPath := os.Getenv("REMOVE_FROM_PATH")
pathEncodeAfter := os.Getenv("PATH_ENCODE_AFTER")
// Create an instance
opts := diskache.Opts{
Directory: "diskcache_place",
}
dc, err := diskache.New(&opts)
if err != nil {
panic(err)
}
proxy := reverseproxy.New(target, bearerToken, dc, removeFromPath, pathEncodeAfter)
router := gin.Default()
router.Use(Auth(apiKey))
router.POST("/cache/clear", func(c *gin.Context) {
err := dc.Clean()
if err != nil {
c.AbortWithError(500, err)
}
c.Status(200)
})
router.GET("proxy/*url", gin.WrapF(proxy.HandleRequest))
router.Run(":" + port)
// start server
// http.HandleFunc("/proxy", proxy.HandleRequest)
// if err := http.ListenAndServe(":"+port, nil); err != nil {
// panic(err)
// }
log.Println("Started on " + port)
}
func Auth(secret string) gin.HandlerFunc {
return func(c *gin.Context) {
apiKey := c.GetHeader("x-api-key")
if secret == "" {
log.Print("No api key set")
c.Next()
return
}
if secret != apiKey {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
c.Next()
}
}