Skip to content

Instantly share code, notes, and snippets.

@p4tin
Last active February 24, 2018 03:07
Show Gist options
  • Save p4tin/a81644a8b7f8a7f2302e11635e911c71 to your computer and use it in GitHub Desktop.
Save p4tin/a81644a8b7f8a7f2302e11635e911c71 to your computer and use it in GitHub Desktop.
Proxying a request to a backend
package main
import (
"net/http"
"net/http/httputil"
"net/url"
"github.com/gorilla/mux"
)
func report(r *http.Request) {
r.Host = "localhost:8888"
r.URL.Host = r.Host
r.URL.Scheme = "http"
}
func main() {
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: "http",
Host: "localhost:8888",
})
proxy.Director = report
r := mux.NewRouter()
r.Handle("/hello", proxy)
r.Handle("/hello/{user}", proxy)
r.Handle("/bye", proxy)
http.ListenAndServe(":8181", r)
}
#!/usr/bin/env python
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
class MainHandler(tornado.web.RequestHandler):
def get(self, name=None):
print("Header")
print(self.request.headers.get('X-MyHeader'))
self.set_header('User', 'Paul')
if name:
self.write("Hello, {}".format(name))
else:
self.write("Hello, world")
class ByeHandler(tornado.web.RequestHandler):
def get(self):
self.write("Bye Bye, world")
def main():
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/hello", MainHandler),
(r"/hello/([^/]+)", MainHandler),
(r'/bye', ByeHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment