gorf

package module
v0.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2023 License: MIT Imports: 5 Imported by: 6

README

Golang Rest Framework (Gorf)

Build Status

Django inspired Rest Framework for Golang.

Vision

Introducing Gorf, the Golang REST API framework that empowers developers to easily create and reuse modular apps. Inspired by the simplicity and flexibility of Python Django, Gorf utilizes the full MVC architecture, providing dedicated model, URL, and view files for effortless understanding. Gorf's plug and play app concept enables infinite scalability and customization, making Gorf the go-to solution for anyone looking to kickstart a new project in Golang. Join our community and contribute your ideas and innovations to build a framework that streamlines the development process for all.

Features

  • Generic
  • Simplicity
  • Based on known architecture
  • Reusability
  • Support for apps
  • Custom database backends
  • More feature work in progress

Installation

go get github.com/go-gorf/gorf

Install gorf auth app

go get github.com/go-gorf/auth

Quickstart new project using gorf-cli

Install gorf-cli for creating new projects and apps. Make sure you have configured Go install directory to your system's shell path (export PATH="$PATH:$(go env GOPATH)/bin")

go install github.com/go-gorf/gorf-cli@latest

Once cli is installed new gorf project can be initialized as follows

gorf-cli init hello

Now go tho the project directory and run the project as follows

cd hello
go run .

Now open the browser and go to http://localhost:8080/health.

new gorf apps can be created using

gorf-cli app newApp

Tutorial without cli

Firstly, Create a new main package with following code

package main

import (
	"log"
)

func main() {
	r := BootstrapRouter()
	err := r.Run()
	if err != nil {
		log.Fatal("Unable to create the gin server")
	} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

settings.go

Next, Create a settings.go file in the main package with the following code snippet

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/go-gorf/auth"
	"github.com/go-gorf/gorf"
)

// add all the apps
var apps = []gorf.GorfApp{
	&auth.AuthApp,
}

func LoadSettings() {
	// jwt secret key
	gorf.Settings.SecretKey = "GOo8Rs8ht7qdxv6uUAjkQuopRGnql2zWJu08YleBx6pEv0cQ09a"
	gorf.Settings.DbConf = &gorf.SqliteBackend{
		Name: "db.sqlite",
	}
}

// bootstrap server
func BootstrapRouter() *gin.Engine {
	gorf.Apps = append(apps)
	LoadSettings()
	gorf.InitializeDatabase()
	gorf.SetupApps()
	r := gin.Default()
	gorf.RegisterApps(r)
	return r
}

Write your own apps

Create a new package named "hello"

add app.go file

package hello

import (
	"github.com/go-gorf/gorf"
)

func setup() error {
	// Add setup here
	return nil
}

var HelloApp = gorf.GorfBaseApp{
	Name:         "Hello",
	RouteHandler: Urls,
	SetUpHandler: setup,
}

add urls.go file

package hello

import "github.com/gin-gonic/gin"

func Urls(r *gin.Engine) {
	r.POST("/hello", Hello)
}

add views.go file

package hello

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func Hello(ctx *gin.Context) {
	ctx.JSON(http.StatusOK, gin.H{
		"message": "Hello world",
	})
}

Check the Gorf project template

https://github.com/go-gorf/template

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Apps []App

Apps array of all registered apps

View Source
var Settings = &GlobalSettings{
	UserObjKey: "user",
	UserObjId:  "id",
	DbBackends: nil,
}

Functions

func BadRequest added in v0.0.13

func BadRequest(ctx *gin.Context, msg string, err error)

func ErrorResponse added in v0.0.14

func ErrorResponse(ctx *gin.Context, msg string, status int)

func Health added in v0.0.7

func Health(ctx *gin.Context)

func InitializeDatabase

func InitializeDatabase() error

func RegisterApps

func RegisterApps(r *gin.Engine)

RegisterApps Register apps

func Response added in v0.0.13

func Response(ctx *gin.Context, resp gin.H)

func SetupApps

func SetupApps()

SetupApps Setup all apps

Types

type App added in v0.0.13

type App interface {
	Name() string
	Description() string
	Setup() error
	Register(r *gin.Engine)
}

App Gorp App interface

type BaseApp added in v0.0.13

type BaseApp struct {
	Title        string
	Info         string
	RouteHandler func(r *gin.Engine)
	SetUpHandler func() error
}

func (*BaseApp) Description added in v0.0.13

func (app *BaseApp) Description() string

func (*BaseApp) Name added in v0.0.13

func (app *BaseApp) Name() string

func (*BaseApp) Register added in v0.0.13

func (app *BaseApp) Register(r *gin.Engine)

func (*BaseApp) Setup added in v0.0.13

func (app *BaseApp) Setup() error

type BaseUser added in v0.0.14

type BaseUser struct {
	Id        string `json:"id"`
	Email     string `json:"email"`
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
}

func NewUser added in v0.0.14

func NewUser(id, firstName, lastName, email string) *BaseUser

func (*BaseUser) GetEmail added in v0.0.14

func (u *BaseUser) GetEmail() string

func (*BaseUser) GetFirstName added in v0.0.14

func (u *BaseUser) GetFirstName() string

func (*BaseUser) GetLastName added in v0.0.14

func (u *BaseUser) GetLastName() string

func (*BaseUser) ID added in v0.0.14

func (u *BaseUser) ID() string

type Db added in v0.0.13

type Db interface {
	Get(dest interface{}, key string) error
	Filter(dest interface{}, conds ...interface{}) error
	AutoMigrate(dst ...interface{}) error
	First(dest interface{}, conds ...interface{}) error
	Create(value interface{}) error
	GetUser(dest interface{}, id string) error
}
var DB Db

type DbBackend added in v0.0.13

type DbBackend interface {
	Connect() (Db, error)
	Disconnect() error
}

type DbOperationType added in v0.0.13

type DbOperationType int
const (
	CREATE DbOperationType = iota
	GET
	UPDATE
	DELETE
)

type Err added in v0.0.13

type Err struct {
	Msg string
	Er  error
	// contains filtered or unexported fields
}

func NewErr added in v0.0.14

func NewErr(msg string, status int, err error) *Err

func (*Err) Error added in v0.0.13

func (e *Err) Error() string

func (*Err) Response added in v0.0.13

func (e *Err) Response() gin.H

type GlobalSettings

type GlobalSettings struct {
	SecretKey          string
	UserObjKey         string
	UserObjId          string
	DbBackends         DbBackend
	AllowAnonymousUser bool
}

GlobalSettings Global Project settings

type Query added in v0.0.13

type Query interface {
	Describe() (string, error)
	Operation() DbOperationType
}

type QuerySet added in v0.0.13

type QuerySet interface {
	Count() (int, error)
	First() (any, error)
	Last() (any, error)
}

type User added in v0.0.14

type User interface {
	GetFirstName() string
	GetLastName() string
	GetEmail() string
	ID() string
}

Directories

Path Synopsis
backends
dynamodbi Module
gormi Module
common module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL