Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fast RESTful API application builder, built on top of "gin" + "gorm".

License

LeoTurnell-Ritson/gorest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

😴 GoREST 😴

tests codecov version license

GoREST is a lightweight REST API builder for Go, built on Gin and Gorm. It generates RESTful endpoints directly from your struct definitions, letting you spin up APIs quickly with minimal boilerplate.

The library is designed to be additive rather than intrusive. It drops cleanly into an existing Gin application without interfering with your routes or middleware. Likewise, it doesn’t wrap or replace your Gorm connection—your database stays in your hands. By avoiding unnecessary abstraction layers, GoREST keeps your application transparent, flexible, and easy to maintain.

Feature Roadmap

This is the current list of features planned and implimented for a feature complete 1.0.0 release.

  • Automatic endpoint generation
  • Automatic filtering of struct fields by query parameters
  • Add support for RESTful hypermedia
  • Add for complex relationships between structs
  • Automatic pagination
  • Automatic sorting
  • More filtering customisation
  • Security scanning continuous integration

Usage

package main

import (
    "github.com/LeoTurnell-Ritson/gorest"
    "github.com/gin-gonic/gin"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
)

type User struct {
    ID    uint    `json:"id" gorm:"primaryKey"`
    Name  string  `json:"name"`
    Email string  `json:"email"`
}

type Order struct {
    ID      uint  `json:"id" gorm:"primaryKey"`
    Item  string  `json:"item"`
    Quantity int  `json:"quantity"`
}

func main() {
    // Initialize Gorm with a SQLite database.
    r := gin.Default()
    db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
    if err != nil {
        panic("failed to connect database")
    }
    db.AutoMigrate(&User{})

    // Add the REQUIRED Gorm middleware. 
    r.Use(gorest.GormMiddleware(db))

    // Alternatively, you can use add the middleware via the gorest.Config 
    // struct when registering endpoints, avoiding polluting the global 
    // route handlers. For example:
    // gorest.CRUD[User](r, "/users", &gorest.Config{
    //      Handlers: []gin.HandlerFunc{GormHandlerFunc(db)},
    // })

    // Register the all endpoints, GET, POST, PUT and DELETE, for the User struct
    // in one call:
    // GET /users  - List all users
    // GET /users/:id - Get a user by ID
    // POST /users - Create a new user
    // PUT /users/:id - Update a user by ID
    // DELETE /users/:id - Delete a user by ID
    gorest.CRUD[User](r, "/users", &gorest.Config{})

    // Register only the GET endpoints for the Order struct, as well as enabling
    // automatic filtering by query parameters:
    // GET /orders?item=foo&quantity=10 - List all orders with item "foo" and quantity 10
    gorest.Read[Order](r, "/orders", &gorest.Config{
        AddQueryParams: true,
    })

    // Start the server
    r.Run(":8080")
}

About

Fast RESTful API application builder, built on top of "gin" + "gorm".

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages