Go-provides cross-domain request Proxy Service

Source: Internet
Author: User
Tags oauth
This is a creation in Article, where the information may have evolved or changed.

In a single page application, the asynchronous request data is subject to the same-origin policy.

only if the protocol , Port , and domain name are the same page, then two pages have the same source. As long as the site protocol name Protocol, host hosts, port number port three of any one of the different, the data request and transmission between the site constitutes a cross-domain call, will be limited by the same- origin policy .

Not just traditional Ajax, Axios and fetch based on promise are also constrained. There are many solutions, here is a brief description of using Golang to build a simple proxy service.

Proxy

The principle of implementing cross-domain requests with proxies is

DomainA客户端(浏览器) ==> DomainA服务器 ==> DomainB服务器 ==> DomainA客户端(浏览器)

A proxy service is set up on the page's same-origin browser, and after the page request data is sent to the same-origin server, it is forwarded to the target server by the homologous server, waiting for the response and forwarding the response back to the page.

In fact, in some front-end frameworks, such as Vue.js, a simple proxy service is provided for testing. With the same configuration on the service side, it is very convenient to move from the development environment to the production environment.

Httprouter

Httprouter is a very lightweight HTTP framework that is encapsulated on top of the HTTP package provided by Golang.

Httprouter is a lightweight high performance HTTP request Router (also called multiplexer or just m UX for short) for Go.

With Httprouter, you can easily parse URLs and add handler.

go get github.com/julienschmidt/httprouter

Add the address and handler functions to be proxied within the main function.

func main() {    router := httprouter.New()    router.GET("/", index)    //以post方式访问/github为前缀的url时,完整的url会转化为参数传入函数    router.POST("/github/*proxyPath", proxy)     log.Fatal(http.ListenAndServe(serverPort, router))}
 func Proxy (w http. Responsewriter, R *http. Request, PS Httprouter. Params) {///github remote Remote: = "https://github.com"//Get full URL u: = remote + PS. ByName ("Proxypath") log. Println (r.url, "= =", u)//Create a new request Req,err with the existing request: = http. Newrequest (R.method, U, r.body)/* Method URL Body */req. Header = R.header/* Header */client: = http. Defaultclient//Initiate new request res, ERR: = client. Do (req) if err! = nil {w.writeheader (404) fmt. Fprint (W, err) return}//Get the new body bodyres,err:=ioutil. ReadAll (Res. Body) if err!=nil{w.writeheader (404) fmt. Fprint (W, err) return}//Set new header for K, vs: = Range res. Header {for _, V: = Range vs {W.header (). ADD (K,V)}}//Set new cookie for _, Value: = Range Res. Request.Cookies () {W.header (). ADD (value. Name,value. Value)}//write state W.writeheader (res. StatusCode) W.write (bodyres)}  

Special Note : You must first set the header and then write the status code and body

Access our proxy address as a post

View Log

2018/02/22 11:33:07 /github/login/oauth/access_token => https://github.com/login/oauth/access_token
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.