-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (46 loc) · 1.46 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
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"time"
)
const port = 8080
func main() {
logger := log.New(os.Stdout, "DEBUG: ", log.Lshortfile)
// the url that we use as fallback api
fallbackURL, err := url.Parse("https://www.googleapis.com/")
if err != nil {
logger.Fatal(err)
}
revProxy := httputil.NewSingleHostReverseProxy(fallbackURL)
serverMux := http.NewServeMux()
// This is our 404 handler. It sends all requests to `fallbackURL`
serverMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
logger.Printf("request to: %s", "404")
writer.Header().Add("x-proxy", "true")
// It's important to set `Host` header.
// It's not done automatically.
// Though the documentation of `NewSingleHostReverseProxy` suggest a different solution,
// I decided to rewrite it here, because it's absolutely the same but more concise.
request.Host = fallbackURL.Host
revProxy.ServeHTTP(writer, request)
})
// This handler just replies `hello`.
serverMux.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
logger.Printf("request to: %s", "hello")
writer.Header().Add("x-proxy", "false")
writer.Write([]byte("hello"))
})
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
ReadTimeout: time.Second,
WriteTimeout: time.Second,
IdleTimeout: time.Second,
Handler: serverMux,
}
fmt.Printf("server has stopped: %+v", server.ListenAndServe())
}