From d56e24ea9fb7a40d4b0a72a30e6550939e993863 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 18 May 2021 14:21:29 +0100 Subject: [PATCH] just make proxy a handler --- url/proxy/LICENSE | 104 -------------------------------------------- url/proxy/README.md | 15 ------- url/proxy/main.go | 71 ------------------------------ url/proxy/proxy.go | 67 ++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 190 deletions(-) delete mode 100644 url/proxy/LICENSE delete mode 100644 url/proxy/README.md delete mode 100644 url/proxy/main.go create mode 100644 url/proxy/proxy.go diff --git a/url/proxy/LICENSE b/url/proxy/LICENSE deleted file mode 100644 index 062a8f0..0000000 --- a/url/proxy/LICENSE +++ /dev/null @@ -1,104 +0,0 @@ -# PolyForm Strict License 1.0.0 - - - -## Acceptance - -In order to get any license under these terms, you must agree -to them as both strict obligations and conditions to all -your licenses. - -## Copyright License - -The licensor grants you a copyright license for the software -to do everything you might do with the software that would -otherwise infringe the licensor's copyright in it for any -permitted purpose, other than distributing the software or -making changes or new works based on the software. - -## Patent License - -The licensor grants you a patent license for the software that -covers patent claims the licensor can license, or becomes able -to license, that you would infringe by using the software. - -## Noncommercial Purposes - -Any noncommercial purpose is a permitted purpose. - -## Personal Uses - -Personal use for research, experiment, and testing for -the benefit of public knowledge, personal study, private -entertainment, hobby projects, amateur pursuits, or religious -observance, without any anticipated commercial application, -is use for a permitted purpose. - -## Noncommercial Organizations - -Use by any charitable organization, educational institution, -public research organization, public safety or health -organization, environmental protection organization, -or government institution is use for a permitted purpose -regardless of the source of funding or obligations resulting -from the funding. - -## Fair Use - -You may have "fair use" rights for the software under the -law. These terms do not limit them. - -## No Other Rights - -These terms do not allow you to sublicense or transfer any of -your licenses to anyone else, or prevent the licensor from -granting licenses to anyone else. These terms do not imply -any other licenses. - -## Patent Defense - -If you make any written claim that the software infringes or -contributes to infringement of any patent, your patent license -for the software granted under these terms ends immediately. If -your company makes such a claim, your patent license ends -immediately for work on behalf of your company. - -## Violations - -The first time you are notified in writing that you have -violated any of these terms, or done anything with the software -not covered by your licenses, your licenses can nonetheless -continue if you come into full compliance with these terms, -and take practical steps to correct past violations, within -32 days of receiving notice. Otherwise, all your licenses -end immediately. - -## No Liability - -***As far as the law allows, the software comes as is, without -any warranty or condition, and the licensor will not be liable -to you for any damages arising out of these terms or the use -or nature of the software, under any kind of legal claim.*** - -## Definitions - -The **licensor** is the individual or entity offering these -terms, and the **software** is the software the licensor makes -available under these terms. - -**You** refers to the individual or entity agreeing to these -terms. - -**Your company** is any legal entity, sole proprietorship, -or other kind of organization that you work for, plus all -organizations that have control over, are under the control of, -or are under common control with that organization. **Control** -means ownership of substantially all the assets of an entity, -or the power to direct its management and policies by vote, -contract, or otherwise. Control can be direct or indirect. - -**Your licenses** are all the licenses granted to you for the -software under these terms. - -**Use** means anything you do with the software requiring one -of your licenses. diff --git a/url/proxy/README.md b/url/proxy/README.md deleted file mode 100644 index 9e1395b..0000000 --- a/url/proxy/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# URL Proxy - -The URL proxy provides the single entrypoint for m3o.one - -## Overview - -The [url](https://github.com/micro/services) service provides link shortening and sharing. The URL Proxy fronts those urls -as a single entrypoint at https://m3o.one. We don't serve directly because of time wasted on ssl certs, etc. - -## Usage - -- Assumes url is of format `https://m3o.one/u/AArfeZE` -- Will call `https://api.m3o.com/url/proxy?shortURL=AArfeZE` -- URL service should return `destinationURL=https://foobar.com/example` -- Proxy will issue a 301 redirect diff --git a/url/proxy/main.go b/url/proxy/main.go deleted file mode 100644 index a640223..0000000 --- a/url/proxy/main.go +++ /dev/null @@ -1,71 +0,0 @@ -package main - -import ( - "encoding/json" - "io/ioutil" - "net/http" - "net/url" - "strings" -) - -func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - r.ParseForm() - - // assuming /u/short-id - parts := strings.Split(r.URL.Path, "/") - if len(parts) < 3 { - return - } - // assuming its /u/ for url - if parts[1] != "u" { - return - } - - // get the url id - //id := parts[2] - - uri := url.URL{ - Scheme: r.URL.Scheme, - Host: r.URL.Host, - Path: r.URL.Path, - } - - // call the backend for the url - rsp, err := http.Get("https://api.m3o.com/url/proxy?shortURL=" + uri.String()) - if err != nil { - http.Error(w, err.Error(), 500) - return - } - defer rsp.Body.Close() - - if rsp.StatusCode != 200 { - http.Error(w, "unexpected error", 500) - return - } - - result := map[string]interface{}{} - - b, err := ioutil.ReadAll(rsp.Body) - if err != nil { - http.Error(w, err.Error(), 500) - return - } - - if err := json.Unmarshal(b, &result); err != nil { - http.Error(w, err.Error(), 500) - return - } - - // get the destination url - url, _ := result["destinationURL"].(string) - if len(url) == 0 { - return - } - - // return the redirect url to caller - http.Redirect(w, r, url, 301) - }) - - http.ListenAndServe(":8080", nil) -} diff --git a/url/proxy/proxy.go b/url/proxy/proxy.go new file mode 100644 index 0000000..b3824a0 --- /dev/null +++ b/url/proxy/proxy.go @@ -0,0 +1,67 @@ +package proxy + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "net/url" + "strings" +) + +func Handler(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + + // assuming /u/short-id + parts := strings.Split(r.URL.Path, "/") + if len(parts) < 3 { + return + } + // assuming its /u/ for url + if parts[1] != "u" { + return + } + + // get the url id + //id := parts[2] + + uri := url.URL{ + Scheme: r.URL.Scheme, + Host: r.URL.Host, + Path: r.URL.Path, + } + + // call the backend for the url + rsp, err := http.Get("https://api.m3o.com/url/proxy?shortURL=" + uri.String()) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + defer rsp.Body.Close() + + if rsp.StatusCode != 200 { + http.Error(w, "unexpected error", 500) + return + } + + result := map[string]interface{}{} + + b, err := ioutil.ReadAll(rsp.Body) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + if err := json.Unmarshal(b, &result); err != nil { + http.Error(w, err.Error(), 500) + return + } + + // get the destination url + url, _ := result["destinationURL"].(string) + if len(url) == 0 { + return + } + + // return the redirect url to caller + http.Redirect(w, r, url, 301) +}