-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
120 lines (104 loc) · 2.89 KB
/
Copy pathmain.go
File metadata and controls
120 lines (104 loc) · 2.89 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
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
package main
import (
"io"
"log/slog"
"net/http"
"os"
"strings"
)
func proxyUrl(url string, removePrefixPath string) func(http.ResponseWriter, *http.Request) {
// final url = url - remove
return func(w http.ResponseWriter, r *http.Request) {
client := &http.Client{}
// Normalize removePrefixPath (remove trailing slash)
removePrefixPath = strings.TrimSuffix(removePrefixPath, "/")
// Trim the prefix from the request path
urlPath := strings.TrimPrefix(r.URL.Path, removePrefixPath)
if !strings.HasPrefix(urlPath, "/") && urlPath != "" {
urlPath = "/" + urlPath
}
finalURL := url + urlPath
if r.URL.RawQuery != "" {
finalURL += "?" + r.URL.RawQuery
}
slog.Info("proxying request", "path", r.URL.Path, "trimmed", urlPath, "target", finalURL)
req, err := http.NewRequest(r.Method, url+urlPath+"?"+r.URL.RawQuery, r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Copy headers
for name, values := range r.Header {
for _, value := range values {
req.Header.Add(name, value)
}
}
// Make the request
resp, err := client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// Copy response headers
for name, values := range resp.Header {
for _, value := range values {
w.Header().Add(name, value)
}
}
w.WriteHeader(resp.StatusCode)
_, err = io.Copy(w, resp.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func handler(w http.ResponseWriter, r *http.Request) {
client := &http.Client{}
req, err := http.NewRequest(r.Method, "https://api.themoviedb.org/3"+r.URL.Path+"?"+r.URL.RawQuery, r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Copy headers
for name, values := range r.Header {
for _, value := range values {
req.Header.Add(name, value)
}
}
// Make the request
resp, err := client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// Copy response headers
for name, values := range resp.Header {
for _, value := range values {
w.Header().Add(name, value)
}
}
w.WriteHeader(resp.StatusCode)
_, err = io.Copy(w, resp.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func main() {
port := "3000"
// read port from environment variable if needed
if envPort := os.Getenv("PORT"); envPort != "" {
port = envPort
}
slog.Info("Starting proxy server", "port", port)
http.HandleFunc("/3k-image/", proxyUrl("https://image.tmdb.org", "/3k-image"))
http.HandleFunc("/live", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
http.HandleFunc("/", proxyUrl("https://api.themoviedb.org/3", ""))
http.ListenAndServe(":"+port, nil)
}