dennybiasiolli / gorestframework

Go Rest Framework made easy!
MIT License
2 stars 1 forks source link
go golang hacktoberfest module package rest

Go Rest Framework

Go Report Card

Install module

go get -u github.com/dennybiasiolli/gorestframework

Usage example

package main

import (
    "github.com/dennybiasiolli/gorestframework"
    "github.com/gorilla/mux"
    "github.com/jinzhu/gorm"
)

// create the model definition
// more info here: https://gorm.io/docs/models.html
type Product struct {
    gorm.Model
    Code  string
    Price uint
}

// create migration function
func MigrateModels(db *gorm.DB) {
    db.AutoMigrate(
        // passing all desired models here
        &Product{},
    )
}

// create SetView function
func SetViews(router *mux.Router) {
    gorestframework.View(&gorestframework.ViewInput{
        Router:     router,
        PathPrefix: "/products",
        ModelPtr:   &Product{},
    })
}

func main() {
    // initializing database connection
    gorestframework.InitDbConn(
        "sqlite3",  // DatabaseDialect,
        "test.db",  // DatabaseConnectionString,
        MigrateModels,
    )
    defer gorestframework.CloseDbConn()

    // start HTTP listener
    gorestframework.StartHTTPListener(
        true,  // RouterActivateLog,
        true,  // RouterUseCORS,
        views.SetViews,
    )
}