If you have already read the Getting Started section, you may noticed that we used many command lines to generate code. In this section we are going deep in the code generator and you will find out how easy and productive is working with Wheel to build and mantain your project.
Let's check the Wheel's help.
$> wheel --help
Usage: wheel new APP_PATH [options] # Creates new app wheel generate SUBJECT NAME ATTRIBUTES # Adds new CRUD to an existing app. # SUBJECT: scaffold/model/entity/handler. # NAME: name of the model, entity or handler # ATTRIBUTES: when not a handler, is a pair of column name # and column type separated by ":" i.e. description:string # Available types are: # string/text/integer/decimal/datetime/bool/references. # When a handler "attributes" are functions inside handler. Options: -G, [--skip-git] # Skip .gitignore file More: -h, [--help] # Show this help message and quit -v, [--version] # Show Wheel version number and quit
As you can see, Wheel has basically two commands: new and generate.
The new command generates a new project. The APP_PATH is the directory path where your application will be stored inside the GOPATH. Let's imagine your GOPATH is /home/your_user/go/src and the APP_PATH is github.com/your_account/store. It will generate the project inside /home/your_user/go/src/github.com/your_account/store path. The name of the application is always the last part of APP_PATH, at this example is store.
Try it:
$> wheel new github.com/your_account/store
"Go" seems installed Checking dependences... package github.com/jinzhu/gorm was found package gopkg.in/yaml.v2 was found package github.com/gorilla/mux was found package github.com/dgrijalva/jwt-go was found package github.com/satori/go.uuid was found package github.com/lib/pq was found package golang.org/x/crypto/bcrypt was found Generating new app... created: /home/your_user/go/src/github.com/your_account/store created: /home/your_user/go/src/github.com/your_account/store/app/handlers/myself_handler.go created: /home/your_user/go/src/github.com/your_account/store/app/handlers/session_handler.go created: /home/your_user/go/src/github.com/your_account/store/app/handlers/user_handler.go created: /home/your_user/go/src/github.com/your_account/store/app/myself/myself_view.go created: /home/your_user/go/src/github.com/your_account/store/app/session/session_model.go created: /home/your_user/go/src/github.com/your_account/store/app/session/session_view.go created: /home/your_user/go/src/github.com/your_account/store/app/session/mailer/password_recovery.en.html created: /home/your_user/go/src/github.com/your_account/store/app/session/mailer/password_recovery.pt-BR.html created: /home/your_user/go/src/github.com/your_account/store/app/session/mailer/sign_up.en.html created: /home/your_user/go/src/github.com/your_account/store/app/session/mailer/sign_up.pt-BR.html created: /home/your_user/go/src/github.com/your_account/store/app/user/user_model.go created: /home/your_user/go/src/github.com/your_account/store/app/user/user_view.go created: /home/your_user/go/src/github.com/your_account/store/commons/app/handler/handler.go created: /home/your_user/go/src/github.com/your_account/store/commons/app/model/model.go created: /home/your_user/go/src/github.com/your_account/store/commons/app/model/pagination/pagination.go created: /home/your_user/go/src/github.com/your_account/store/commons/app/model/searchengine/searchengine.go created: /home/your_user/go/src/github.com/your_account/store/commons/app/view/view.go created: /home/your_user/go/src/github.com/your_account/store/commons/conversor/conversor.go created: /home/your_user/go/src/github.com/your_account/store/commons/crypto/crypto.go created: /home/your_user/go/src/github.com/your_account/store/commons/locale/locale.go created: /home/your_user/go/src/github.com/your_account/store/commons/log/log.go created: /home/your_user/go/src/github.com/your_account/store/commons/mailer/mailer.go created: /home/your_user/go/src/github.com/your_account/store/config/config.go created: /home/your_user/go/src/github.com/your_account/store/config/app.yml created: /home/your_user/go/src/github.com/your_account/store/config/database.yml created: /home/your_user/go/src/github.com/your_account/store/config/email.yml created: /home/your_user/go/src/github.com/your_account/store/config/keys/app.key.rsa created: /home/your_user/go/src/github.com/your_account/store/config/keys/app.key.rsa.pub created: /home/your_user/go/src/github.com/your_account/store/config/locales/en.yml created: /home/your_user/go/src/github.com/your_account/store/config/locales/pt-BR.yml created: /home/your_user/go/src/github.com/your_account/store/db/entities/session_entity.go created: /home/your_user/go/src/github.com/your_account/store/db/entities/user_entity.go created: /home/your_user/go/src/github.com/your_account/store/db/schema/migrate.go created: /home/your_user/go/src/github.com/your_account/store/routes/authorize.go created: /home/your_user/go/src/github.com/your_account/store/routes/middleware.go created: /home/your_user/go/src/github.com/your_account/store/routes/routes.go created: /home/your_user/go/src/github.com/your_account/store/main.go Your RESTful API was successfully created! Change to the root directory using the command line below: cd /home/your_user/go/src/github.com/your_account/store Set up your database connection modifying the file config/database.yml For more details call help: go run main.go --help
There is only one option:
The generate command adds new codes to project in an organized way. There are 3 parameters: SUBJECT, NAME and ATTRIBUTES.
SUBJECT: can be scaffold, model, entity and handler
NAME: is the name of the SUBJECT.
ATTRIBUTES: if the SUBJECT is not a handler, the attributes are pairs of "name" and "type" separated by a ":". They will be used to define the model attributes in the application and the columns in the database. For example, if we define an attribute title:string, it means that, the model in the application has a string attribute called title and the table in the database has a VARCHAR(255) column called title.
The table below contains all available types:
Attribute Type | Application | Database |
---|---|---|
string | string | VARCHAR(255) |
text | string | text |
integer | int64 | integer |
decimal | float64 | numeric |
datetime | time.Time | timestamp with time zone |
bool | bool | boolean |
reference | uint | FOREIGN KEY |
But, if it is a handler, the attributes will be functions inside the handler.
If you have questions right now, in the followings sub-sections we are going to see by examples how the generate command workds.
Scaffold generates a full set of code for model, view, handler, routes and authorization.
$> wheel generate scaffold movie title:string year:integer
"Go" seems installed Checking dependences... package github.com/jinzhu/gorm was found package gopkg.in/yaml.v2 was found package github.com/gorilla/mux was found package github.com/dgrijalva/jwt-go was found package github.com/satori/go.uuid was found package github.com/lib/pq was found package golang.org/x/crypto/bcrypt was found Generating new CRUD... created: app/movie/movie_model.go created: app/movie/movie_view.go created: db/entities/movie_entity.go created: app/handlers/movie_handler.go updated: routes/routes.go updated: db/schema/migrate.go updated: routes/authorize.go
Wheel creates the directories of models, view, entities, handlers and database schema, if they don't exist. Creates the model, view, entity and handler files. Updates the routes, migration and authorization files.
Very important!
Run the migration mode to update the database schema.
$> go run main.go -mode=migrate
Model manages the data and the rules of the application. This subject generates the model and database schema defined by the attributes.
$> wheel generate model group name:string
"Go" seems installed Checking dependences... package github.com/jinzhu/gorm was found package gopkg.in/yaml.v2 was found package github.com/gorilla/mux was found package github.com/dgrijalva/jwt-go was found package github.com/satori/go.uuid was found package github.com/lib/pq was found package golang.org/x/crypto/bcrypt was found Generating new CRUD... created: app/group/group_model.go created: db/entities/group_entity.go updated: db/schema/migrate.go
Wheel creates the directories of models, entities and database schema, if they don't exist. Creates the model and the entity files. Updates only the migration file.
Very important!
Run the migration mode to update the database schema.
$> go run main.go -mode=migrate
Entity generates the representation of the table in the database without the model.
$> wheel generate entity cast actor:string character:string movie:reference
"Go" seems installed Checking dependences... package github.com/jinzhu/gorm was found package gopkg.in/yaml.v2 was found package github.com/gorilla/mux was found package github.com/dgrijalva/jwt-go was found package github.com/satori/go.uuid was found package github.com/lib/pq was found package golang.org/x/crypto/bcrypt was found Generating new CRUD... created: db/entities/cast_entity.go updated: db/schema/migrate.go
Wheel creates the directories of entities and database schema, if they don't exist. Creates the entity file. Updates only the migration file.
Very important!
Run the migration mode to update the database schema.
$> go run main.go -mode=migrate
Handler is the controller of the MVC architecture. This subject generates just a single handler file with the functions defined by the attributes.
Let's generate a handler called animal with functions (attributes): elephant and zebra.
$> wheel generate handler animal elephant zebra
"Go" seems installed Checking dependences... package github.com/jinzhu/gorm was found package gopkg.in/yaml.v2 was found package github.com/gorilla/mux was found package github.com/dgrijalva/jwt-go was found package github.com/satori/go.uuid was found package github.com/lib/pq was found package golang.org/x/crypto/bcrypt was found Generating new CRUD... created: app/handlers/animal_handler.go updated: routes/routes.go updated: routes/authorize.go
Wheel creates the directories of handlers, if it doesn't exist. Creates only the handler file. Updates the routes and authorization files.
If you check the routes file (routes/routes.go) you will find out these two lines:
...
router.HandleFunc("/animals/elephant", handlers.AnimalDog).Methods("GET")
router.HandleFunc("/animals/zebra", handlers.AnimalCat).Methods("GET")
...
Accessing these two routes you will have the following returns:
$> curl -X GET http://localhost:8081/animals/elephant
{
"system_message": {
"type": "notice",
"content": "Handler: AnimalElephant"
}
}
$> curl -X GET http://localhost:8081/animals/zebra
{
"system_message": {
"type": "notice",
"content": "Handler: AnimalZebra"
}
}
Now you can create customized handlers and write your own code to interact with models and views.