Skip to content

dennybiasiolli/gorestframework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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,
	)
}