How would you implement a general web app using Go?

blov · · 528 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Whenever I want to make a web applications, I always get stuck writing the API endpoints. It seems that writing APIs involves writing a wholelot of boilerplate, which I hate to write. A lot of functionality seems reusable too. What solutions do people here use?</p> <p>Generally, my web applications need the following features: authentication and authorization (login, register, account recovery, ...), rate limiting, pagination, other WHERE clauses (since data is fed from a DB).</p> <p>Would you use a RESTful API using JSON responses? Would you use a (g)RPC approach (or why not)? WebSocket is only useful if there is not a request-response type of communication, but streaming or message passing. The authentication part could be done using <a href="https://github.com/go-authboss/authboss" rel="nofollow">https://github.com/go-authboss/authboss</a>. But what about all these endpoints for all your methods? So much boilerplate, also clientside. I quite like how in gRPC you can define a protocol and reuse those client side (I don&#39;t get why browser support was considered so late though).</p> <p>Concluding, it would be nice to have a standard solution for a web application that could be reused. Does anybody have examples?</p> <p>PS: I quite like the idea of having a static website that uses AJAX to load in data. This way the website can be fully cached and nicely separates the server and the client. You could also use the classical approach of generating webpages using templates. That would eliminate the need for an API. What do you prefer?</p> <hr/>**评论:**<br/><br/>tdewolff: <pre><p>Apparently it&#39;s called JAMstack when you have a static site with AJAX to a serverside API: <a href="https://jamstack.org/" rel="nofollow">https://jamstack.org/</a></p> <p>But would you use that when you have fairly tight coupling, such as a simple blog CMS?</p></pre>jackmcmorrow: <pre><p>I don&#39;t really understand why generating a static site if you need anything more than a landing page. </p></pre>Redundancy_: <pre><p>There&#39;s nothing to attack or defend, it caches exceedingly well, you don&#39;t need to pay for instances.</p></pre>jackmcmorrow: <pre><p>You also can&#39;t do searches, can&#39;t give your client a backend (that I know of) can&#39;t have user interaction outside third party services and have to deploy every time you want to update the site.</p> <p>Again, can&#39;t see that much advantages over a simple CMS. Unless you&#39;re a Dev, and it&#39;s your personal site, then fuck user experience</p></pre>Redundancy_: <pre><p>We saved thousands of dollars (more if you consider the sysadmin, update and other costs).</p> <p>There are a large number of sites that are largely static, except when a admin/dev posts new content to them. You don&#39;t need databases, nginx or instances for all of that - you can use S3 and Cloudfront, or your favourite alternative.</p> <p>Then you consolidate the things you actually need to be dynamic based on user input down to an API.</p></pre>jackmcmorrow: <pre><p>If you say so. I&#39;m still skeptical, the use case is really important, are you developing a site for someone or you have a team behind the site?</p></pre>Redundancy_: <pre><p>These were frequently web sites where customer relations people would periodically post new content. We had a CMS on the backend, but pretty much just ended up baking out a static version of the site from staging and uploading it. Previously there were maybe 6 machines which were spending a large amount of their time doing nothing much interesting at all - checking databases for content that wasn&#39;t changed.</p> <p>The traffic to our origin servers was massively offloaded, and we didn&#39;t need to worry about DDOS attacks or most other things that previously would also have affected our websites. QA was simplified, because if you checked what you baked, you&#39;d get exactly that in prod.</p> <p>Something like gohugo.io could get you a very long way, very cheaply.</p></pre>tdewolff: <pre><p>Of course you can, do an AJAX request with the search terms, send back the results in JSON and render them using HTML templates using JS. This way the search functionality is completely modularized and could be reused in multiple spots.</p> <p>I wouldn&#39;t mind a backend that has dynamic pages, but it&#39;s the frontend that could be completely decoupled using an API.</p></pre>shovelpost: <pre><blockquote> <p>It seems that writing APIs involves writing a wholelot of boilerplate, which I hate to write.</p> </blockquote> <p>You might have chosen the wrong tool for the job then. Have you considered Ruby?</p></pre>kaeshiwaza: <pre><p>The &#34;classical approach of generating webpages using templates&#34; is also fine with Go...</p></pre>shovelpost: <pre><p>I don&#39;t disagree but that requires even more boilerplate which apparently the OP hates.</p></pre>cameronjerrellnewton: <pre><p>I like to think of go as a pragmatists language. It was made for specific solutions (hence the lack of generics), while still allowing moderate flexibility when it&#39;s called for. </p> <p>As it turns out, a lot of these generic solutions never live up to their promises. There are always subtle differences between implementation to implementation that makes or breaks an application. If there werent, someone would have automated it by now and we&#39;d all be out of work.</p> <p>I have embraced the fact that today&#39;s business problems might not be tomorrow&#39;s, and I&#39;m probably going to throw away an implementation or two (Fred Brooks anyone?) until the problem is complete solved, at which point it only needs maintanence.</p></pre>

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

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