<p>After doing a nice part of this realtime Golang API (Postgresql backed, say 1k lines) I want to write an admin page and I'm finding it a big hassle in Go, even with <a href="https://github.com/marmelab/admin-on-rest" rel="nofollow">https://github.com/marmelab/admin-on-rest</a> - and I only have 5 tables or so.</p>
<p>I have to write a lot of repetitive code for each HTTP operation and for each table... I also have to duplicate my "models" (structs) since the keys I want to JSON encode aren't the same as in the "main application" (cf Golang type tags).</p>
<p>Should I just suck it up and write the code, or throw Django together, redefine the models there and present that as the admin (operating on the same database as the main app) ?</p>
<hr/>**评论:**<br/><br/>ZetaHunter: <pre><p>I am currently building a project that requires an admin frontend of sorts, what I ended up doing is writing backend API in Go, using go-chi/chi & go-chi/render.</p>
<p>So a typical scenario for me similar to yours right now is managing storage mounts via frontend, I wrote a middleware for it that retrieves the mount, puts it into context, and passes it on.
The actual http method handlers only handle IO (actually just calling go-chi/render stuff) and perform specific operation on mount in context that implements my own interface. </p>
<p>ie. I use <code>render.Bind</code> to process request body into my struct with all the fields checked in the Bind function the struct implements, then I change the mount how I want to, finally call the <code>render.Render</code> (which again, my structs implement) to send the response back to user/client.</p>
<p><a href="https://github.com/go-chi/chi/blob/master/_examples/rest/main.go" rel="nofollow">chi/_examples/rest</a> shows a good example of how to build a nice API</p></pre>HugoWeb: <pre><p>Yes, I have something like that, its the admin views that I wanted to auto generate somehow. </p>
<p>Qor actually worked quite well - I don't use Gorm on the main app so there is some "concept" duplication now..
(SQL to generate my tables on my main app, same tables represented as Gorm models on the admin app).</p>
<p>I'd have to choose between keeping it this way or migrating the main app to Gorm as well ....</p></pre>danilobuerger: <pre><p><a href="https://postgrest.com/en/v4.1/" rel="nofollow">https://postgrest.com/en/v4.1/</a> and then use something like <a href="https://github.com/marmelab/admin-on-rest" rel="nofollow">https://github.com/marmelab/admin-on-rest</a> </p></pre>HugoWeb: <pre><p>Thanks - will try that on future projects. BTW are you brazilian?</p></pre>joncalhoun: <pre><p>I would suck it up and write all the code in Go. At the very least some of your validation and other logic like this could be reused and it will be easier to keep the app in sync. </p>
<p>Btw can you use something like the "Using embedded data and an alias type to reduce code" section of this post - <a href="https://blog.gopheracademy.com/advent-2016/advanced-encoding-decoding/" rel="nofollow">https://blog.gopheracademy.com/advent-2016/advanced-encoding-decoding/</a> - to reduce your code duplication on the JSON front?</p></pre>HugoWeb: <pre><p>Had not seen that, thanks!</p></pre>tzcczt: <pre><p>I used protobuf, grpc, grpc-gateway to generate server side models and plumbing as well as a swagger file. Then I generate a TypeScript client from the swagger file giving me a JS API and models that match my grpc models. Then all I need to do is write proto files and everything except for business logic is generated code. </p></pre>nemith: <pre><p>With protobufs are you able to define nullable fields. With thrift there is no way to define something all nullable which makes some database operations interesting.</p></pre>egonelbre: <pre><p>It depends on several things:</p>
<ol>
<li>Does it have huge value to the end-user?</li>
<li>How critical is it in the big-picture?</li>
<li>Do you have to decide this now?</li>
<li>...</li>
</ol>
<p>I would use the one that I can get up and running fastest... and revisit the question when there's some significant problem with the solution.</p>
<p>You don't have to use multiple structs as long as you can store the same fields in the struct. <em><a href="https://medium.com/@egonelbre/specification-types-ed9ddf35ec8f" rel="nofollow">See composed serialization</a></em>.</p></pre>gnu-user: <pre><p>Take a look at the gin framework and write everything as a REST API, then create examples of all the JSON response data you will return. There are tools for Go such as JSON-to-Go that will create all the struct declarations for you.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传