This repository was archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
64 lines (49 loc) · 1.35 KB
/
main.go
File metadata and controls
64 lines (49 loc) · 1.35 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"time"
"github.com/gorilla/mux"
)
const baseApiUrl = "https://api.airtable.com/"
type transport struct {
apiKey string
}
func (t *transport) RoundTrip(r *http.Request) (*http.Response, error) {
log.Printf("Sending request, %s %s", r.Method, r.URL.String())
r.Host = r.URL.Host
r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", t.apiKey))
r.Header.Del("X-Forwarded-For")
return http.DefaultTransport.RoundTrip(r)
}
func newProxyHandler(apiKey string) func(http.ResponseWriter, *http.Request) {
uri, err := url.Parse(baseApiUrl)
if err != nil {
panic(err)
}
return func(w http.ResponseWriter, r *http.Request) {
proxy := httputil.NewSingleHostReverseProxy(uri)
proxy.Transport = &transport{apiKey: apiKey}
proxy.ServeHTTP(w, r)
}
}
func main() {
port := flag.Int("port", 4242, "port to run the server on")
flag.Parse()
airTableApiKey := os.Getenv("AIR_TABLE_API_KEY")
router := mux.NewRouter()
router.HandleFunc("/{version}/{app}/{resource}", newProxyHandler(airTableApiKey)).Methods("POST", "OPTIONS")
srv := &http.Server{
Handler: router,
Addr: fmt.Sprintf(":%d", *port),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Printf("Listening on %s ...", srv.Addr)
log.Fatal(srv.ListenAndServe())
}