:bullettrain_side: Use Rails to Develop or Generate a Golang Application.
Use Rails to generate Golang code or manage Go app development
go-on-railsis a Rails generator aims to:
Here's some examples: * A simple example(tutorial) on the basic usage of go-on-rails generator * An advanced example shows how to integrate Go APIs in a Rails project * Another example shows how to handle a Rails session to get a user's info in a go-on-rails generated Go API
Add this line to your application's Gemfile:
gem 'go-on-rails', '~> 0.4.0'
And then execute:
bash $ bundle
Or install it yourself as:
bash $ gem install go-on-rails
You must have an existing Rails app or to create a new one before you try go-on-rails to generate Golang codes.
After that you can run the command just as other Rails generators:
rails g gor [dev(elopment) | pro(duction) | test | ...] [-m model_a model_b model_c ...]
OR (on rails version < 5.0)
rake g gor ...
here we take generating all models for the
developmentenvironment for example:
rails g gor dev
Then a folder named
go_appthat includes Golang codes will be generated under your Rails app root path.
Install the dependent Golang packages for this Go project:
Then change to the
go_appdirectory and run:
go run main.go
You can visit the page in http://localhost:4000 by default.
More command details about go-on-rails generator:
rails g gor --help
You can view the godoc page of all functions in http://localhost:7979/doc/models.html after run:
The gem is still under development, so there're some known issues. You're welcomed to contribute. 👏
sql.NullType not supported yet, so you'd better in the migrations set those columns "not null" with a default value that's consistent with Golang's zero value specification, such as "" default for string and text typed column, and 0 default for int, etc. And now we have an alternative approach for manipulating the database nullable fields, see the wiki Working with database nullable fields
[x] Pagination(details see wiki)
[ ] Callbacks
[ ] Transactions
database/sqldatabase API library
When I had the idea to convert Rails app or build Golang app with Rails tools, I searched github and found the project: https://github.com/t-k/ar2gostruct. And from ar2gostruct I copied some codes on handling data structure conversion and models association, it made my idea come true faster than I imagined.
There're two branches at present:
devbranch has a whole Rails environment for development: models, seeds for testing, and under
go_appdirectory there's a file named
models_test.goused to test generated Golang codes.
We create four models for testing:
rails db:seedto use the data defined in
db/seeds.rb. And change to
go_app/modelsdirectory to run
go testto test generated models-related functions. The test covers a small part of the functions currently. More will be added later on.
See the LICENSE file.