update proxy to include api token

This commit is contained in:
Asim Aslam
2021-05-18 14:47:14 +01:00
parent 7a37c8521e
commit 7dbc9323d6

View File

@@ -1,6 +1,8 @@
package proxy
import (
"os"
"encoding/json"
"io/ioutil"
"net/http"
@@ -8,6 +10,14 @@ import (
"strings"
)
var (
// API Key
APIKey = os.Getenv("MICRO_API_KEY")
// API Url
APIHost = "https://api.m3o.com"
)
func Handler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
@@ -30,8 +40,23 @@ func Handler(w http.ResponseWriter, r *http.Request) {
Path: r.URL.Path,
}
header := make(http.Header)
apiURL := APIHost + "/url/proxy"
// use /v1/
if len(APIKey) > 0 {
apiURL = APIHost + "/v1/url/proxy"
header.Set("Authorization", "Bearer "+APIKey)
}
// make new request
req, err := http.NewRequest("GET", apiURL+"?shortURL="+uri.String(), nil)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// call the backend for the url
rsp, err := http.Get("https://api.m3o.com/url/proxy?shortURL=" + uri.String())
rsp, err := http.DefaultClient.Do(req)
if err != nil {
http.Error(w, err.Error(), 500)
return