Go on Rails: Integrated development of Go API using Rails

Go on Rails: Integrated development of Go API using Rails

go-on-rails is a Rails generator that has been released as a gem to integrate or develop Golang applications in Rails projects.

Project address: github.com/goonr/go-on ...

This article will not describe the detailed usage method, so let's put a few existing sample tutorials first:

  • Simple example Imitated the introductory demo in Rails guides , demonstrating how to use go-on-rails to create and generate a simple blog Go API.
  • Advanced tutorial How to create a Golang project and integrate it with rails_admin, devise, cancancan, etc., to quickly add a management backend to the Go project. At the same time, the project demonstrates how to use the newly released webpacker tool in Rails 5.1, and use React to make an independent front-end interface to call the Go interface in Rails.
  • How to read Rails session from Go API Explains how to read Rails session in a Go interface generated by go-on-rails for user authentication

What do go-on-rails do

  1. Use Rails to create a model, and then go-on-rails generates the corresponding Go data structure and some basic CRUD methods according to the definition of the model, adds validation, and also generates paging and other methods
  2. Generate godoc documentation for these methods generated above
  3. Generate database connection configuration based on Rails database configuration
  4. Some command line tools help automate common tasks, such as installing default dependencies, files generated by format, viewing godoc documents, etc.

Why use go-on-rails

The target users of this tool are first Rails developers who want to learn Golang or want to integrate Go API in Rails projects. That is to be familiar with Rails, otherwise many people may not learn Rails just to try this tool.

Next, I will mainly talk about why there is this gem, or is there really any advantage to using Rails to develop Go projects?

  1. Configuration management. For example, by configuring the database in Rails, the generated Go project will generate the connection configuration of the corresponding environment database according to the Rails configuration. Similarly, other configurations can also be shared using Rails configurations.

  2. Use Rails' rich tool chain, such as using rails db:seed to write test data, using rails console to test data, and other operations such as rails db:* in the database management part.

  3. It is convenient for the management of various migrations of the database, without handwriting the schema, which continues the operation habits of the Rails programmers in the development process of the database.

  4. The definition of model will also make it easier for Rails developers to get used to it. Go-on-rails will generate most of the operation functions in association and validation.

  5. The release of webpacker in Rails 5.1 highlights Rails' new ability to be a "Web Integrated Development Environment". The emergence of more front-end and back-end separation systems has greatly reduced the amount of repetitive work required to integrate Go API in Rails. You only need to modify the calling link to complete a system transformation. In addition, using webpacker can easily make front-end pages to call Go API. You can see the tutorial here on how to use it .

  6. For some small and medium-sized systems, the performance of some interfaces can be improved by integrating Go API in Rails, without the need to refactor the system architecture.

go-on-rails is still in the development process, all kinds of participation and pr are welcome.