The Project Aims to Devlop Genric Template for Creation of REST API with MYSQL DB Integration. The project includes MYSQL DB integration. The Binary File in the Project Genrates the Request ,Response ,Error & CRUD Handlers You just need to Define a struct type for your API and Run the Reform Command in order to Build Your Complete API.
For Getting Started You Need to have Go 1.10+. Install or update the following Packages
- Reform - A better ORM for Go, based on non-empty interfaces and code generation
- go-chi - easily manage HTTP request / response payloads of Go HTTP services
- packr - The simple and easy way to embed static files into Go binaries.
- Install or update
reform
package,reform
andreform-db
commandsgo get -u gopkg.in/reform.v1/...
- Create a Table in your DataBase with all the Required Fields and a Field named id which will also be the pk.
3(a). Use reform-db
command to generate models for your existing database schema.This command will Auto generate the Struct
corresponding to your Table Fields. In Case For example:
reform-db -db-driver=mysql -db-source="root:root@tcp(localhost:3306)/DBNAME" init -gofmt=false
3(b). In Case You Want to write your own struct
representing a table or view row.Rename the directory expenses
to your Struct name .For eg for employee struct your package should be employees. and store it in file struct.go
:
```
import (
"time"
)
//go:generate reform
//reform:ExpenseTable
type Expense struct {
ID int32 `reform:"id,pk"`
Description string `reform:"description"`
Type string `reform:"type"`
Amount float64 `reform:"amount"`
CreatedOn time.Time `reform:"created_on"`
UpdatedOn time.Time `reform:"updated_on"`
} ```
Magic comment
//reform:ExpenseTablelinks this model to
ExpenseTabletable in SQL database which you created in step2. The first value in field's
reformtag is a column name.
pkmarks primary key. Use value
-or omit tag completely to skip a field. Use pointers (recommended) or
sql.NullXXX` types for nullable fields.
-
Run
reform [package or directory]
orgo generate [package or file]
. This will create 'struct_reform.goin the same package with type
ExpenseTableand methods on
Expense`. -
In the
crud-op.gotpl
file you need to provide the DBNAME
d,err:=sql.Open("mysql","root:root@tcp(localhost:3306)/DBNAME?charset=utf8&parseTime=True")
. -
Now Execute the Main file with the command.
./main -S="Expense"
The S flag here is used for passing the name of struct.This Command will generatecrud.go request.go response.go
in the expenses directory. -
Now you just need to call
expenses.Init()
. You can do this by rebuilding the main file.
Explain how to run the automated tests for this system
Explain what these tests test and why
Give an example
Explain what these tests test and why
Give an example 123456yul/
Add additional notes about how to deploy this on a live system
- Reform - A better ORM for Go, based on non-empty interfaces and code generation
- go-chi - easily manage HTTP request / response payloads of Go HTTP services
- packr - The simple and easy way to embed static files into Go binaries.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
- Ayush Chauhan https://github.com/arc1999
- Shrikar Vaitala https://github.com/shrikar007
- Pawas Seth https://github.com/sethpawas
This project is licensed under the MIT License - see the LICENSE.md file for details
- reform
- go-chi
- testing