GithubHelp home page GithubHelp logo

alenn-m / rgen Goto Github PK

View Code? Open in Web Editor NEW
16.0 3.0 3.0 5.67 MB

RGEN - Go (GoLang) REST code generator

License: GNU General Public License v3.0

Go 100.00%
go golang generator crud rest-api rest rgen crud-actions yaml migrations-generator seeder api golang-package automatic laravel mysql

rgen's Introduction

Go Report Card codecov Maintainability

RGEN - Go (GoLang) REST code generator

Installation

go get github.com/alenn-m/rgen/v2
go install github.com/alenn-m/rgen/v2@latest

This will install the generator in $GOPATH/bin. You can execute it by calling rgen command.

Getting started

Run rgen -h to see a list of all commands:

Usage:
   [command]

Available Commands:
  build       Builds API from YAML file
  generate    Generates API CRUD with given configuration
  help        Help about any command
  migration   Manages database migrations
  new         Initializes the REST API

Usage

  1. cd into your projects directory
  2. Run rgen new and answer the given questions (it will ask you which VSC you use, VSC domain and package name). Your answers will determine the root package and name of your project.
  3. Go into your newly created project and open draft.yaml. This file contains specification of your project.
  4. Run rgen build to create REST endpoints.

draft.yaml

After initialization of a project, the draft.yaml file will look like this:

Models:
  User:
    Properties:
      ApiToken: string
      Email: string
      FirstName: string
      LastName: string
      Password: string
    Validation:
      Email:
      - required
      - email
      FirstName:
      - required
      LastName:
      - required
      Password:
      - required
      - min:5
    Actions: []
    Relationships: {}
    OnlyModel: false
    Public: false

All model definitions are under Models namespace. Each model contains:

  • Properties
  • Actions
  • Relationships
  • Validation
  • OnlyModel
  • Public

Properties

This is main portion of draft.yaml. In this section you setup all fields for your models.
Fields are listed in key:value format. key represents the name of the field, while value represents the data type.
You can use any valid Go data type like: int, int64, string, float64 ...

Actions

Actions contain the list of CRUD actions you want to have for a specific model. By default, all CRUD actions are created.
Possible values are (case-insensitive): index, show, create, update, delete

Relationships

Relationships follow key:value format where key is the name of model the current model is related with,
and value is the name of relation.
Example:

Relationships:
    User: belongsTo
    Post: hasMany

Possible values are: belongsTo, manyToMany, hasMany.
Warning: IDs are not created automatically. For example if User has many Posts, you have to add UserID
field to Post model.

Validation

Validation field contains list of validations for each model property. This field is optional and if you omit this field, all fields will be optional.

Behind the scene, rgen is using https://github.com/go-playground/validator for validations. Please check the package documentation to learn which fields you can use in your application.

OnlyModel

OnlyModel is a boolean value which indicates if you want to only create a model.
The default value is false.

Public

Public is a boolean value which indicates if given resource is public or not. The term "public" means that users don't have to login to access a particular resource. The default value is false.

File structure

├── api
│   └── auth
│       ├── controller.go
│       ├── repositories
│       │   └── mysql
│       │       └── auth.go
│       ├── repository.go
│       └── transport.go
├── config.yaml
├── database
│   └── seeds
│       ├── DatabaseSeeder.go
│       └── UserSeeder.go
├── draft.yaml
├── go.mod
├── main.go
├── middleware
│   ├── AuthMiddleware.go
│   └── ExampleMiddleware.go
├── models
│   └── Base.go
└── util
    ├── auth
    │   ├── auth.go
    │   └── interface.go
    ├── cache
    │   ├── memory
    │   │   └── memory.go
    │   └── service.go
    ├── paginate
    │   └── paginate.go
    ├── req
    │   └── req.go
    ├── resp
    │   └── response.go
    └── validators
        ├── Base.go
        ├── Equals.go
        ├── RecordExists.go
        ├── RecordsExists.go
        └── Unique.go

Next steps

  • Edit .env file with MySQL credentials and other configurations
  • Do not remove config.yaml since this file contains global variables like Package which is used when generating new services.
  • Run go mod tidy to install all dependencies, and you're good to go.

Generate command

rgen allows you to create single service as well, this is useful when you want to update existing project.

WARNING: rgen relies on various markers and file paths to add new services, if you want to use rgen generate command, then do not remove markers [services], [protected routes] and [public routes] inside main.go.

rgen generate -h
-------------------------------------------
Generates API CRUD with given configuration

Usage:
   generate [flags]

Flags:
  -a, --actions string   CRUD actions --actions='index,create,show,update,delete'
  -f, --fields string    List of fields (required) --fields='Title:string, Description:string, UserID:int64'
  -h, --help             help for generate
  -n, --name string      Resource name (required) --name='ModelName'
      --onlyModel        Create only model (default = false)
      --public           Public resource (default = false)

Example: To create a Comment resource with title, body and user_id fields run the following command:

rgen generate -n "Comment" -f "title:string#required|min:5, body:string#required, user_id:int64#required" -a "index, create, delete"

TODO

  • Add support for more databases (currently only MySQL is supported)

PRs are welcome

rgen's People

Contributors

alenn-m avatar nedimdjulic avatar ribice avatar sweep-ai[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rgen's Issues

Add support for more databases

Currently rgen is using `MySQL as the only database.

Add support for more databases.

The best new candidate is PostgreSQL.

Unable to generate a new application

rgen new
Creating new application
✔ github.com
Domain: example.com
Package: ex
stat /src/github.com/alenn-m/rgen/_app: no such file or directory

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.