go-on-rails aims at three scenarios:
- Integrate some APIs written in Golang to existed Rails app for high performance
- Use your farmiliar Rails tools to develope and manage a Golang app project
- Convert a not very complicated Rails app to Golang equivalent
Here's a simple example(tutorial) shows the basic usage of go-on-rails generator, and some real-world examples are coming soon.
- Rails development environment
- Golang development environment
Add this line to your application's Gemfile:
gem 'go-on-rails', '~> 0.0.9'
And then execute:
$ bundle
Or install it yourself as:
$ gem install go-on-rails
You must have a Rails app or to create one before you can try go-on-rails generator to convert a Rails app to Golang codes.
You can just run the command to convert the application:
rails g gor [dev(elopment) | pro(duction) | test] [-m model_a model_b model_c ...]
Then a directory named "go_app" with Golang codes will be generated under your Rails app root path.
Install the dependent Golang packages:
rails gor:deps
Then change to the go_app
directory and run:
go run main.go
You can visit the page in http://localhost:3000 by default.
More command details about go-on-rails generator:
rails g gor --help
- Go project directory layout (all under the
go_app
directory, likeviews
,controllers
,public
) - A Go data struct corresponding to each activerecord model
- And each struct related CRUD functions/methods like FindModel, UpdateModel, DestroyModle etc. All these models related program files under the
go_app/models
directory - We use Gin as the default web framework, but you can change it to anyone that you favor in
main.go
andcontrollers
programs
And the gem is still under development, so there're a lot of known issues.
- the generated Golang codes includes the association between models, basic relations like has_many, has_one, belongs_to have been supported
- so some association functions is available
- and the :dependent action is triggered when destroying some related model
- some validation like presense, length restriction and format are supported
- databases specific functions between mysql, postgres are not covered yet
- model callbacks are not available
- 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.
Really a lot...
github.com/jmoiron/sqlx
: an extension on the standarddatabase/sql
database API librarygithub.com/mattn/go-sqlite3
: a SQLite drivergithub.com/go-sql-driver/mysql
: a MySQL drivergithub.com/lib/pq
: a PostgreSQL drivergithub.com/asaskevich/govalidator
: for the struct validationgopkg.in/gin-gonic/gin.v1
: a HTTP web framework
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 convertion and models association, it make my idea come true faster than I imagined.
There're two branches at present: master
and dev
.
The dev
branch has a whole Rails environment for development: models, seeds for testing, and under go_app
directory there's a file named modles_test.go
used to test generated Golang codes.
- Fork the project switch to branch
dev
. - Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a future version unintentionally.
- Commit, do not mess with Rakefile or version (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
- Send me a pull request. Bonus points for topic branches.
See the LICENSE file.