简体   繁体   English

Rails App / API的最佳方法

[英]Best approach for rails app/api

I'm developing a rails app, it's a regular site, but I need to develop an API, so I was reading and trying some stuff, the options that I've thought are (others are welcome): 我正在开发一个Rails应用程序,它是一个常规站点,但是我需要开发一个API,所以我正在阅读并尝试一些东西,我认为是可以的(欢迎其他选择):

  • Versioning the API and separate the API from the controller. 对API进行版本控制,并将API与控制器分离。 Pro: Clean and separate stuff. 优点:清洁并分开东西。 Cons: the need to handle 2 controllers for the same task. 缺点:同一任务需要处理2个控制器。

  • Not versioning and keep everything in the same controllers, views can be done with jbuilder. 不使用版本控制并将所有内容保留在同一控制器中,可以使用jbuilder完成视图。 Pro: not really easy way to version it. 优点:不是很简单的版本控制方法。 Cons: harder to expose only parts of the app. 缺点:更难于仅显示应用程序的一部分。 harder to route stuff like in an versioned way. 很难以版本化的方式路由内容。

I really want to avoid duplication, but I need some way to avoid that, and make some nice routes like in a versioned way, I don't want to have more than 1 controller for some type of object, in a versioned way if you have 3 versions you'll end up with 4 controllers, that is a lot to maintain. 我确实想避免重复,但是我需要某种方式来避免这种情况,并做出一些不错的路由,例如以版本控制的方式,我不想为某种类型的对象使用多个控制器,如果您使用版本控制的话有3个版本,最终会有4个控制器,这需要大量维护。

Please correct me if I'm wrong, and hope to get some good answers :) 如果我错了,请指正我,希望得到一些好的答案:)

Thanks. 谢谢。

I always code api within their own namespace because these points could be very different from the main app: 我总是在自己的命名空间中编写api,因为这些要点可能与主应用有很大不同:

  • format rules 格式规则

  • auth 认证

  • rescue 拯救

  • versioning 版本控制

  • before / after filters 过滤器之前/之后

Even if you have duplication in controllers, there should not be much code, and I cant see why it would be a big deal. 即使您在控制器中有重复项,也应该没有太多代码,而且我不明白为什么这很重要。 Actually it could even force you to abstract your code and have thin controllers. 实际上,它甚至可能迫使您抽象代码并使用瘦控制器。

Versioning your API is the best route to take. 对API进行版本控制是最好的选择。

If you have 4 versions, 1 controller, you still only need to maintain 2 controller (the one in the web facing application, and the most recent version of the API.) 如果您有4个版本,即1个控制器,则仍只需维护2个控制器(面向Web的应用程序中的一个,以及API的最新版本)。

If you have write access in your API, you would be able to take advantage of your own API in your application and not need to create a new method in both the API and in the main application (AJAX). 如果您在API中具有写访问权限,则可以在应用程序中利用自己的API,而无需在API和主应用程序(AJAX)中都创建new方法。

It is possible that your filters are different. 您的过滤器可能会有所不同。 You may need different types of authentication for the API and the main application. 您可能需要对API和主应用程序进行不同类型的身份验证。

The biggest argument for versioning would probably be that if you have anyone other than yourself consuming the API, their application would not break the day you deploy your changes. 版本控制的最大论点可能是,如果您除了使用API​​之外还拥有其他任何人,那么他们的应用程序将不会破坏您部署更改的一天。

Besides, you should keep to the 'thin controllers, fat models' mentality. 此外,您应保持“瘦身,胖模特”的心态。 That would prevent a lot of duplication with that alone. 仅此一项就可以避免很多重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Rails中创建轮询应用程序的最佳方法 - Best approach for creating a polling app in Rails Rails应用程序和会员访问选项的最佳方法 - Best approach to rails app and membership access options 在Rails应用程序中定义子菜单的最佳方法 - Best approach for defining sub menu in rails app Rails应用程序中用户配置文件图像的最佳方法 - Best approach to Users Profile Images in Rails App 为rails应用程序构建iphone客户端的最佳方法是什么? - What is the best approach for building an iphone client for a rails app? 在 Rails 应用程序中处理大文件上传的最佳方法是什么? - What is the best approach to handle large file uploads in a rails app? Rails 3最佳方法可在一个应用程序中同时使用多个应用程序 - rails 3 best approach multiple apps within one app Rails身份验证和授权-最佳方法? - Rails Authentication and Authorization - Best approach? 如何在Rails应用程序和Rails API之间最佳共享路由 - How to best share routes between Rails app and Rails API 将字符串形式的哈希类型数据从Rails / Ruby传递到C#应用程序的最佳方法? - Best approach to pass hash type data in string form from Rails/Ruby to a C# app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM