forked from pilgrim2go/aws-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-proxy.go
70 lines (55 loc) · 1.7 KB
/
aws-proxy.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
package main
import (
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/acquia/aws-proxy/proxy"
"github.com/gorilla/handlers"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var conf *viper.Viper
func init() {
conf = viper.New()
conf.SetEnvPrefix("AWS_PROXY")
conf.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
conf.AutomaticEnv()
pflag.StringP("endpoint", "e", "", "The entry point for the web service, e.g. https://dynamodb.us-west-2.amazonaws.com")
conf.BindPFlag("endpoint", pflag.Lookup("endpoint"))
conf.SetDefault("endpoint", "")
pflag.IntP("port", "p", 3000, "The port that the reverse proxy binds to")
conf.BindPFlag("port", pflag.Lookup("port"))
conf.SetDefault("port", 3000)
pflag.BoolP("behind-reverse-proxy", "b", false, "Set this flag if the proxy is being run behind another")
conf.BindPFlag("behind-reverse-proxy", pflag.Lookup("behind-reverse-proxy"))
conf.SetDefault("behind-reverse-proxy", false)
pflag.Parse()
}
func main() {
endpoint := conf.GetString("endpoint")
if endpoint == "" {
log.Fatal("missing required option --endpoint")
}
url, err := url.Parse(endpoint)
if err != nil {
log.Fatal(err)
}
region, service, err := proxy.ParseEndpointUrl(url)
if err != nil {
log.Fatal(err)
}
// Build the reverse proxy handler chain.
var handler http.Handler
proxy_handler := proxy.ReverseProxy(url, region, service)
if conf.GetBool("behind-reverse-proxy") {
handler = handlers.CombinedLoggingHandler(os.Stdout, handlers.ProxyHeaders(proxy_handler))
} else {
handler = handlers.CombinedLoggingHandler(os.Stdout, proxy_handler)
}
// Run the reverse proxy.
port := strconv.Itoa(conf.GetInt("port"))
http.ListenAndServe(":"+port, handler)
}