How to handle http request internally to call urls from same server code?

xuanbao · · 743 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am planning to bring my main url inside another url and want to get output of main url internally. For example: my urls is : <a href="http://www.abc.com/url?url=%22http://www.abc.com/mainurl">http://www.abc.com/url?url=&#34;http://www.abc.com/mainurl</a>&#34;</p> <p>Will send response from this url to user but internally call other url specified in parameter to get output.</p> <p>Now my router will route me to a function where i am handling url(<a href="http://www.abc.com/url">http://www.abc.com/url</a>). In that function, i want to reroute and get output of url <a href="http://www.abc.com/mainurl">http://www.abc.com/mainurl</a>. One option is to use curl, but that again need to go to DNS and do complete url hitting process which will nullify my motive. Another option is to call each function by using some switch case which will parse internal url and call desired function. Is there any better way to internally route this url and get output in main function of original url. I want to make it generic. I am currently using mux library for routing of gorillatoolkit. Any suggestions on how to do will be great.</p> <p>I am new to golang. Was using request factory which execute request internally in Kohana Php to give output.</p> <p>EDIT: Just read that we can do it from meta programming in other languages, trying to understand how we can do it in go.</p> <hr/>**评论:**<br/><br/>anoobisus: <pre><p>Um, this isn&#39;t meta programming, and you&#39;re way, way, way off course if you&#39;re trying to optimize a DNS lookup out of a curl request. I don&#39;t even know where else to begin.</p> <p>This is definitely one of those times that you should take a step back and explain what you&#39;re really trying to do, because I think you&#39;re making it way, way, way more complicated than it needs to be.</p> <p>If you just want to take that route and match it against the existing router, that should be .... relatively trivial.</p> <p>Create a request object and call router.ServeHTTP(), done.</p></pre>myth007: <pre><p>ahh, I was trying to expose only one url to user and internally route all urls (as all urls comes as parameter in that main url). That way i can do all validation at a single place and no one can hack the system as there is only one entry point and it is secure. Once i get a url as parameter, now i want to call function which belongs to that. This is motivation behind trying to do what i mentioned.</p> <p>Your valuable comments on this?</p></pre>anoobisus: <pre><p>That&#39;s a complexly normal thing and requires none of the weird stuff you&#39;re doing. It&#39;s hard to explain how wrong it is but that description could go on dailywtf. I&#39;m not trying to be a dick but every web framework ever written ever allows for common validation easily.</p> <p>I mean. Wrap each handler with a validation function and you&#39;ve achieve the same thing</p></pre>myth007: <pre><p>Thanks. Yes added a middleware using negroni which validate every url before passing it to next main url handler.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

743 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传