mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
update proxy to include api token
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user