Go as backend API, Polymer frontend

xuanbao · · 366 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m trying to work out the best way of getting Polymer to talk to my go app end points? I&#39;m using Go and Postgres as the backend, but would like to use Polymer as the front-end instead of the Go html/template package. Has anyone got a good suggestion on how is set this up? Sorry, struggling to get the penny dropping on this part. I&#39;ve watched Francesc Campoy&#39;s polymer-gopher video but it looks like he has the app setup already? </p> <p>Also, would you recommend having go handle the routing also, or just be the data end point for this type of setup?</p> <p>Thanks guys and gals :) </p> <hr/>**评论:**<br/><br/>ianchildress: <pre><p>If you are doing this as a personal project then I&#39;d recommend just using Go as both your API and static pages server. You can use Polymer as a Single-Page App much like AngularJS. If this is a production app, then what you use to serve the static pages would require more discussion. <em>Quick hint: If you serve any Go apps in production make sure you set your timeouts!</em> <a href="https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/">https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/</a></p></pre>darkmagician2: <pre><p>Hey it&#39;s great to see more Polymer people around! I know Captain Codeman who I see on this subreddit is very active in the Polymer slack which is where you should go with Polymer questions. In terms of your question, your Go backend would be serving JSON and on the Polymer side you grab the data using the &lt;iron-ajax&gt; element. Let us know if you have more questions :D</p></pre>CaptaincCodeman: <pre><p>Yeah, can&#39;t recommend Polymer enough for front-end work and of course Go for backend. I call it the POGO stack ... Of course there is no direct link between front-end and back-end if you do it right - ideally you&#39;ll use things like REST / JSON which both sides make extremely easy and performant.</p> <p>Although they are obviously very different technologies for different ends of the web, Polymer and Go both have a similar &#34;feel&#34; to them - well thought out, low ceremony, pragmatic and great for productivity. The tooling and ecosystems for both are flourishing and will only improve going forward. In short, both are a great bet if you want to pick technology to learn and build on for the future.</p> <p>Lots of big-name web frameworks are still in the yesteryear mindset and they are starting to show their age. You can build apps with them (you can still build apps with jQuery) but they have been overtaken somewhat as the web platform has evolved.</p> <p>Checkout the Google videos about Progressive Web Apps (PWAs) and the PRPL pattern for fast loading, mobile friendly apps. Polymer makes it much easier than some other frameworks do.</p> <p>For balance, I always recommend Go to people on the Polymer side :)</p></pre>arcagenis: <pre><blockquote> <p>I call it the POGO stack ... </p> </blockquote> <p>Nice name</p> <blockquote> <p>Checkout the Google videos about Progressive Web Apps (PWAs) and the PRPL pattern for fast loading, mobile friendly apps. Polymer makes it much easier than some other frameworks do.</p> </blockquote> <p>And more importantly, keep in mind the moto of Polymer : &#34;Use the platform&#34;</p></pre>CaptaincCodeman: <pre><p>Although you normally want to keep the frontend and backend separate, communicating over a REST API etc... there are sometimes times when they need to be more tightly integrated.</p> <p>I created an example project showing how Go can serve a Polymer app as a multi-tenancy scenario. Each site can have a customized front-end with the customization done by Go using the Polymer index.html page as a template that it modifies.</p> <p>Hopefully clearer from the repo and useful to someone:</p> <p><a href="https://github.com/CaptainCodeman/go-poly-tenant">https://github.com/CaptainCodeman/go-poly-tenant</a></p> <p>Go also works great to add static metadata that still needs to be added server-side, for things like OpenGraph:</p> <p><a href="https://github.com/Polymer/shop/issues/66">https://github.com/Polymer/shop/issues/66</a></p> <p><a href="https://github.com/Polymer/shop/pull/78">https://github.com/Polymer/shop/pull/78</a></p></pre>ianchildress: <pre><p>I did this a little over a year ago. I wrote a repo called go-polymer I think? I&#39;ll find it and update after dinner. </p></pre>RuNpiXelruN: <pre><p>Oh thanks so much @ianchildress!! That would be amazing!!</p></pre>ianchildress: <pre><p>I couldnt&#39; find the repo I was referring to. It was a boilerplate for polymer front end and Go backend. I did however find a very very old repo that uses a Go backend api and Polymer front end. It connected to the docker api and did a few things.</p> <p><a href="https://github.com/ianchildress/polydocker">https://github.com/ianchildress/polydocker</a> <strong>WARNING</strong> That is really really really old! I&#39;m sure a LOT has changed since then. Basically create your Go api endpoints to perform the operations you need irrelevant to whatever frontend you use. Then create Polymer frontend and use Polymer AJAX calls to your Go api. The repo above does this and hopefully enough is still the same that it&#39;ll point you in the right direction. I&#39;ll try to help if you get stuck.</p></pre>RuNpiXelruN: <pre><p>Oh awesome, thanks so much for sharing! I&#39;ll let you know how I get on! Nice article below too :)</p></pre>Pforce: <pre><p>I&#39;ve done a lot of projects like this. To get started quick you can do this. You can structure the repo differently, this is just an example.</p> <p><code>/public</code> folder in your repo has npm/bower configs whatever you wanna use to get Polymer etc. You&#39;ll end up with <code>/public/bower_components</code>. Just make a <code>/public/index.html</code> to run your polymer app (look at their examples). This is where you do your web dev.</p> <p>Now in the go server, just server the repo <code>./public</code> dir as the root path <code>/</code>. Then do your API handlers, these will be matched before going to the generic <code>/</code> handler.</p> <pre><code>// serves your static polymer app // if not spesific hander is found, always goes here http.Handle(&#34;/&#34;, http.FileServer(http.Dir(&#34;./public&#34;))) // v1 in the path if you one day want to change the API but keep old things working // (mainly relevant if you have an API other than your code will be calling) http.HandleFunc(&#34;/api/v1/get/x&#34;, handleGetX) http.HandleFunc(&#34;/api/v1/set/x&#34;, handleSetX) http.ListenAndServe(&#34;:8080&#34;, nil) </code></pre> <p>This should get you started. At some point you might want to make the index.html a template and run some data against it. Or check for auth in the request and redirect to a login page etc. Then you no longer use <code>FileServer</code>, but you can built those things in over time.</p></pre>CaptaincCodeman: <pre><p>You might want to look at using the http method instead of having the action in the path. The former (using GET, PUT, POST, DELETE etc...) is more REST, the latter is more RPC. It will work but is not what many expect an API to be.</p></pre>Pforce: <pre><p>Those were simple examples to drive the point home. I don&#39;t actually write code like that. He can choose what the API looks like. Granted probably could have used something more concrete.</p></pre>neoasterisk: <pre><p>Keeping Go on the backend and using Polymer (or others like React etc) is the way to go nowadays in my opinion.</p> <p>All you need to do is write the Go API that serves JSON and use AJAX in the polymer app to get the data from the Go API. You probably need to enable CORS on the Go API for that.</p> <p>All routing and UI is handled by the polymer app. All the data is served by the Go API. It might be easier to think about it as 2 separate projects but you can also keep the code in the same repo.</p></pre>RuNpiXelruN: <pre><p>Oh thanks @neoasterisk, your answer and approach makes total sense. I was wondering about routing but thought the same, that polymer should handle it and have the Go app look after serving the data only. Appreciate all the feedback by you guys

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

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