go-on-rails 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +25 -20
- data/lib/generators/gor_generator.rb +13 -8
- data/lib/generators/templates/Dockerfile.go_app.erb +20 -0
- data/lib/generators/templates/Makefile +32 -0
- data/lib/generators/templates/docker-compose.yml.erb +37 -0
- data/lib/generators/templates/main.go +2 -2
- data/lib/tasks/gor.rake +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9440dee24a0661bf808bf44778dd5d9d95671a9c
|
4
|
+
data.tar.gz: ca2e1a666a7650cb3dd695cb97ccf1e00797cf2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e69cb0445523bbb0a172b47cdf319f14d72196621b85240479727edfb416a9b2887089ee748521da03d7442ce93e332481145781db25533955a3fd75c45f4a6b
|
7
|
+
data.tar.gz: fb805efaf9e5ad7b66d523f0c4b303e99f09f1ffa6cea6b6e94b6d5510c521348b2f507d39d1c14bd3660a1726fbbcbf731a8d87e6a1dee4cfe38097e49c8c09
|
data/README.md
CHANGED
@@ -1,36 +1,35 @@
|
|
1
1
|
[](https://badge.fury.io/rb/go-on-rails)
|
2
2
|
[](https://travis-ci.org/goonr/go-on-rails)
|
3
|
-
[](https://codeclimate.com/github/goonr/go-on-rails/maintainability)
|
4
4
|
|
5
5
|
<img align="right" width="260" height="260" src="./go-on-rails.png">
|
6
6
|
|
7
|
-
|
8
|
-
====
|
7
|
+
# Go on Rails
|
9
8
|
|
10
9
|
[中文文档](./README_zh.md)
|
11
10
|
|
12
|
-
go-on-rails is a Rails generator aims to:
|
11
|
+
`go-on-rails` is a Rails generator aims to:
|
13
12
|
|
14
|
-
1.
|
15
|
-
2. Use your farmiliar Rails tools to
|
13
|
+
1. Help to develop and integrate some APIs written in Golang to existed Rails app for high performance
|
14
|
+
2. Use your farmiliar Rails tools to develop and manage a Golang app project
|
16
15
|
3. Convert a *not very complicated* Rails app to Golang equivalent
|
17
16
|
|
18
17
|
Here's some examples:
|
19
|
-
*
|
18
|
+
* A simple [example(tutorial)](https://github.com/goonr/example_simple) on the basic usage of go-on-rails generator
|
20
19
|
* [An advanced example](https://github.com/goonr/example_with_admin) shows how to integrate Go APIs in a Rails project
|
21
20
|
* [Another example](https://github.com/goonr/example_read_rails_session) shows how to handle a Rails session to get an user's info in a go-on-rails generated Go API
|
22
21
|
|
23
22
|
## Prerequisites
|
24
23
|
|
25
24
|
* Rails 4.2+
|
26
|
-
* Golang 1.
|
25
|
+
* Golang 1.7.x
|
27
26
|
|
28
27
|
## Installation
|
29
28
|
|
30
29
|
Add this line to your application's Gemfile:
|
31
30
|
|
32
31
|
```ruby
|
33
|
-
gem 'go-on-rails', '~> 0.
|
32
|
+
gem 'go-on-rails', '~> 0.3.0'
|
34
33
|
```
|
35
34
|
|
36
35
|
And then execute:
|
@@ -44,20 +43,23 @@ $ gem install go-on-rails
|
|
44
43
|
```
|
45
44
|
## Usage
|
46
45
|
|
47
|
-
You must have
|
46
|
+
You must have an existed Rails app or to create a new one before you try go-on-rails to generate Golang codes.
|
48
47
|
|
49
|
-
After that you can
|
48
|
+
After that you can run the command just as other Rails generators:
|
50
49
|
|
51
50
|
```bash
|
52
|
-
rails g gor [dev(elopment) | pro(duction) | test] [-m model_a model_b model_c ...]
|
51
|
+
bundle exec rails g gor [dev(elopment) | pro(duction) | test] [-m model_a model_b model_c ...]
|
52
|
+
|
53
|
+
# OR (on rails version < 5.0)
|
54
|
+
bundle exec rake g gor ...
|
53
55
|
```
|
54
56
|
|
55
|
-
Then a
|
57
|
+
Then a folder named `go_app` that includes Golang codes will be generated under your Rails app root path.
|
56
58
|
|
57
|
-
Install the dependent Golang packages:
|
59
|
+
Install the dependent Golang packages for this Go project:
|
58
60
|
|
59
61
|
```bash
|
60
|
-
rails gor:deps
|
62
|
+
bundle exec rails gor:deps
|
61
63
|
```
|
62
64
|
|
63
65
|
Then change to the `go_app` directory and run:
|
@@ -66,12 +68,12 @@ Then change to the `go_app` directory and run:
|
|
66
68
|
go run main.go
|
67
69
|
```
|
68
70
|
|
69
|
-
You can visit the page in http://localhost:
|
71
|
+
You can visit the page in http://localhost:4000 by default.
|
70
72
|
|
71
73
|
More command details about go-on-rails generator:
|
72
74
|
|
73
75
|
```bash
|
74
|
-
rails g gor --help
|
76
|
+
bundle exec rails g gor --help
|
75
77
|
```
|
76
78
|
|
77
79
|
## What will be generated?
|
@@ -87,15 +89,16 @@ rails g gor --help
|
|
87
89
|
You can view the godoc page of all functions in http://localhost:7979/doc/models.html after run:
|
88
90
|
|
89
91
|
```bash
|
90
|
-
rails gor:doc
|
92
|
+
bundle exec rails gor:doc
|
91
93
|
```
|
92
94
|
|
93
|
-
there's [a sample project](https://github.com/goonr/gor_models_sample) generated by go-on-rails, you can view its godoc
|
95
|
+
Besides, there's [a sample project](https://github.com/goonr/gor_models_sample) generated by go-on-rails, you can view its [godoc](https://godoc.org/github.com/goonr/gor_models_sample) at godoc.org just now to get a picture of what functions are generated.
|
94
96
|
|
95
|
-
And the gem is still under development, so there're a lot of known issues.
|
96
97
|
|
97
98
|
## Known issues and TODOs
|
98
99
|
|
100
|
+
The gem is still under development, so there're some known issues.
|
101
|
+
|
99
102
|
* databases specific functions between MySQL and Postgres or other databases are not covered yet
|
100
103
|
* 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 approch for manipulating the database nullable fields, see the wiki [Working with database nullable fields](https://github.com/goonr/go-on-rails/wiki/Working-with-database-nullable-fields)
|
101
104
|
|
@@ -118,6 +121,8 @@ And the gem is still under development, so there're a lot of known issues.
|
|
118
121
|
|
119
122
|
* [Built-in Pagination](https://github.com/goonr/go-on-rails/wiki/Pagination)
|
120
123
|
* [Working with database nullable fields](https://github.com/goonr/go-on-rails/wiki/Working-with-database-nullable-fields)
|
124
|
+
* [Some Make commands](https://github.com/goonr/go-on-rails/wiki/Some-Make-commands)
|
125
|
+
* [Dockerize a Go-on-Rails application](https://github.com/goonr/go-on-rails/wiki/Dockerize-a-Go-on-Rails-application)
|
121
126
|
|
122
127
|
## Golang dependencies by default
|
123
128
|
|
@@ -63,6 +63,10 @@ class GorGenerator < Rails::Generators::Base
|
|
63
63
|
template "home_controller.go.erb", "go_app/controllers/home_controller.go"
|
64
64
|
copy_file "index.tmpl", "go_app/views/index.tmpl"
|
65
65
|
copy_file "favicon.ico", "go_app/public/favicon.ico"
|
66
|
+
# generate config files for make and dockerization
|
67
|
+
template "docker-compose.yml.erb", "docker-compose.yml"
|
68
|
+
template "Dockerfile.go_app.erb", "go_app/Dockerfile"
|
69
|
+
copy_file "Makefile", "go_app/Makefile"
|
66
70
|
end
|
67
71
|
|
68
72
|
# use gofmt to prettify the generated Golang files
|
@@ -81,25 +85,26 @@ class GorGenerator < Rails::Generators::Base
|
|
81
85
|
end
|
82
86
|
|
83
87
|
def read_database_config rails_env
|
84
|
-
|
85
|
-
|
86
|
-
case
|
88
|
+
@db_config = Rails.configuration.database_configuration[rails_env].symbolize_keys
|
89
|
+
@db_config[:host] ||= "localhost"
|
90
|
+
case @db_config[:adapter]
|
87
91
|
when "sqlite3"
|
88
92
|
@db_config[:driver_name] = "sqlite3"
|
89
|
-
@db_config[:dsn] = Rails.root.join(
|
93
|
+
@db_config[:dsn] = Rails.root.join(@db_config[:database]).to_s
|
90
94
|
@db_config[:driver_package] = "_ \"github.com/goonr/go-sqlite3\""
|
91
95
|
when "mysql2"
|
92
96
|
@db_config[:driver_name] = "mysql"
|
93
|
-
|
97
|
+
@db_config[:port] ||= "3306"
|
94
98
|
# MySQL DSN format: username:password@protocol(address)/dbname?param=value
|
95
99
|
# See more: https://github.com/go-sql-driver/mysql
|
96
100
|
format = "%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local"
|
97
|
-
@db_config[:dsn] = sprintf(format,
|
101
|
+
@db_config[:dsn] = sprintf(format, *@db_config.values_at(:username, :password, :host, :port, :database, :encoding))
|
98
102
|
@db_config[:driver_package] = "_ \"github.com/go-sql-driver/mysql\""
|
99
103
|
when "postgresql"
|
100
104
|
@db_config[:driver_name] = "postgres"
|
101
|
-
|
102
|
-
|
105
|
+
@db_config[:port] ||= "5432"
|
106
|
+
format = "host=%s port=%s user=%s dbname=%s sslmode=disable password=%s"
|
107
|
+
@db_config[:dsn] = sprintf(format, *@db_config.values_at(:host, :port, :username, :database, :password))
|
103
108
|
@db_config[:driver_package] = "_ \"github.com/lib/pq\""
|
104
109
|
end
|
105
110
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Multi-stage builds require Docker 17.05 or higher on the daemon and client.
|
2
|
+
# see: https://docs.docker.com/engine/userguide/eng-image/multistage-build/
|
3
|
+
|
4
|
+
# build the go app binary
|
5
|
+
FROM golang:1.9.2 as builder
|
6
|
+
WORKDIR /root/
|
7
|
+
COPY . /root/
|
8
|
+
<%- if %w[127.0.0.1 localhost].include? @db_config[:host] and @db_config[:driver_name] != "sqlite3" -%>
|
9
|
+
RUN perl -pi -e "s/tcp\(.*?:/tcp\(database:/; s/host=\S+? /host=database /" models/db.go
|
10
|
+
<%- end -%>
|
11
|
+
RUN make deps
|
12
|
+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o myapp .
|
13
|
+
|
14
|
+
# use the binary app to build the target image
|
15
|
+
FROM alpine:latest
|
16
|
+
WORKDIR /root/
|
17
|
+
ADD views /root/views
|
18
|
+
ADD public /root/public
|
19
|
+
COPY --from=builder /root/myapp .
|
20
|
+
CMD ["./myapp"]
|
@@ -0,0 +1,32 @@
|
|
1
|
+
GO := go
|
2
|
+
MYAPP := myapp
|
3
|
+
IMAGE := $(MYAPP)
|
4
|
+
TAG := latest
|
5
|
+
|
6
|
+
$(MYAPP):
|
7
|
+
$(GO) build -o $(MYAPP)
|
8
|
+
|
9
|
+
clean:
|
10
|
+
-rm $(MYAPP)
|
11
|
+
|
12
|
+
build: $(MYAPP)
|
13
|
+
@:
|
14
|
+
|
15
|
+
deps:
|
16
|
+
$(GO) get -u github.com/jmoiron/sqlx \
|
17
|
+
github.com/gin-gonic/gin \
|
18
|
+
github.com/goonr/go-sqlite3 \
|
19
|
+
github.com/go-sql-driver/mysql \
|
20
|
+
github.com/lib/pq \
|
21
|
+
github.com/asaskevich/govalidator
|
22
|
+
|
23
|
+
test:
|
24
|
+
$(GO) test -v ./models
|
25
|
+
|
26
|
+
run: $(MYAPP)
|
27
|
+
./$(MYAPP)
|
28
|
+
|
29
|
+
image: clean
|
30
|
+
docker build -t $(USER)/$(IMAGE):$(TAG) .
|
31
|
+
|
32
|
+
.PHONY: build clean deps test run image
|
@@ -0,0 +1,37 @@
|
|
1
|
+
version: '3'
|
2
|
+
|
3
|
+
services:
|
4
|
+
<%- if %w[127.0.0.1 localhost].include? @db_config[:host] and @db_config[:driver_name] != "sqlite3" -%>
|
5
|
+
# generated according to the config/database.yml
|
6
|
+
database:
|
7
|
+
image: <%= @db_config[:driver_name] %>
|
8
|
+
ports:
|
9
|
+
- '<%= @db_config[:port] %>:<%= @db_config[:port] %>'
|
10
|
+
<%- end -%>
|
11
|
+
|
12
|
+
# rails app part. You need to modify this part by your project
|
13
|
+
# and create a Dockerfile for it before you run any docker-compose command
|
14
|
+
rails_app:
|
15
|
+
build: .
|
16
|
+
command: bundle exec rails s -p 3000 -b '0.0.0.0'
|
17
|
+
ports:
|
18
|
+
- "3000:3000"
|
19
|
+
<%- if %w[127.0.0.1 localhost].include? @db_config[:host] and @db_config[:driver_name] != "sqlite3" -%>
|
20
|
+
depends_on:
|
21
|
+
- database
|
22
|
+
<%- end -%>
|
23
|
+
|
24
|
+
# golang app part. It depends on the rails_app to initialize the database
|
25
|
+
go_app:
|
26
|
+
build: ./go_app
|
27
|
+
command: ./myapp -port 4000
|
28
|
+
environment:
|
29
|
+
# Gin webserver run mode. Or "debug" for debugging
|
30
|
+
- GIN_MODE=release
|
31
|
+
ports:
|
32
|
+
- "4000:4000"
|
33
|
+
depends_on:
|
34
|
+
<%- if %w[127.0.0.1 localhost].include? @db_config[:host] and @db_config[:driver_name] != "sqlite3" -%>
|
35
|
+
- database
|
36
|
+
<%- end -%>
|
37
|
+
- rails_app
|
@@ -8,8 +8,8 @@ import (
|
|
8
8
|
)
|
9
9
|
|
10
10
|
func main() {
|
11
|
-
// The app will run on port
|
12
|
-
servePort := flag.String("port", "
|
11
|
+
// The app will run on port 4000 by default, you can custom it with the flag -port
|
12
|
+
servePort := flag.String("port", "4000", "Http Server Port")
|
13
13
|
flag.Parse()
|
14
14
|
|
15
15
|
// Here we are instantiating the router
|
data/lib/tasks/gor.rake
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: go-on-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- B1nj0y
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Modeling, developing and testing your Golang app with your familiar Rails
|
14
14
|
tools like rails generate, db migration, console etc. It is more meant to help integrating
|
@@ -25,7 +25,10 @@ files:
|
|
25
25
|
- lib/generators/gor/converter.rb
|
26
26
|
- lib/generators/gor/validator.rb
|
27
27
|
- lib/generators/gor_generator.rb
|
28
|
+
- lib/generators/templates/Dockerfile.go_app.erb
|
29
|
+
- lib/generators/templates/Makefile
|
28
30
|
- lib/generators/templates/db.go.erb
|
31
|
+
- lib/generators/templates/docker-compose.yml.erb
|
29
32
|
- lib/generators/templates/favicon.ico
|
30
33
|
- lib/generators/templates/gor_model.go.erb
|
31
34
|
- lib/generators/templates/home_controller.go.erb
|