-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (94 loc) · 2.36 KB
/
main.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
func main() {
if os.Getenv("Origin") == "" {
log.Print("Non origin server defined")
os.Exit(1)
return
}
http.HandleFunc("/", func(responde http.ResponseWriter, request *http.Request) {
if !strings.Contains(request.URL.Path, "resource.rpf") {
log.Print(request.RemoteAddr+" --> localhost"+request.URL.Path)
io.WriteString(responde, "Caching proxy is up and running\n")
return
}
hash := request.URL.Query().Get("hash")
resource := UrlToResource(request.URL.Path)
cache := ReadCacheFile(resource, hash)
io.WriteString(responde, cache)
})
port := os.Getenv("Port")
if port == "" {
port = "80"
}
log.Print("Caching proxy started on port: ", port)
err := http.ListenAndServe(":"+port, nil)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("server closed\n")
} else if err != nil {
fmt.Printf("error starting server: %s\n", err)
os.Exit(1)
}
}
func UrlToResource(str string) string {
semiResoult := strings.Replace(str, "/", "", 1)
result := strings.Replace(semiResoult, "/resource.rpf", "", 1)
return result
}
func ReadCacheFile(folder, hash string) string {
mainPath := filepath.Join("cache", folder, hash)
folderPath := filepath.Join("cache", folder)
_, err := os.Stat(mainPath)
if os.IsNotExist(err) {
log.Print("Creating new cache row for resource: "+folder+" hash: ["+hash+"]")
// deleting folder
_, err := os.Stat(folderPath)
if err == nil {
err := os.RemoveAll(folderPath)
if err != nil {
log.Print(err)
}
}
// creating new folder
err = os.MkdirAll(folderPath, os.ModePerm)
if err != nil {
log.Print(err)
}
originIp := os.Getenv("Origin")
resp, err := http.Get("http://" + originIp + "/files/" + folder + "/resource.rpf?hash=" + hash)
if err != nil {
log.Print(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
httpErr := fmt.Sprintf("Origin server respond: %d ", resp.StatusCode)
log.Print(httpErr)
}
// creating new file
file, err := os.Create(mainPath)
if err != nil {
log.Print(err)
}
defer file.Close()
// copy content to new file
_, err = io.Copy(file, resp.Body)
if err != nil {
log.Print(err)
}
}
// sending content to client
data, err := os.ReadFile(mainPath)
if err != nil {
log.Print(err)
}
return string(data)
}