artifact

package module
v1.1.35 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 19 Imported by: 6

README

Artifact

Artifact is a web framework written in Go (Golang) based on Gin, MongoDB.

Example Repo: Golang Gin Boilerplate

Installation

go get -u github.com/shipu/artifact

Quick Start

$ cat .env
APP_NAME=app
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost
APP_PORT=8080
# assume the following codes in example.go file
$ cat example.go
package main

import (
	. "github.com/shipu/artifact"
	"github.com/gin-gonic/gin"
)

type AppConfig struct {
	Name        string `mapstructure:"APP_NAME" default:"Artifact"`
	Environment string `mapstructure:"APP_ENV" default:"local"`
	Debug       bool   `mapstructure:"APP_DEBUG" default:"true"`
	Url         string `mapstructure:"APP_URL"  default:"http://localhost"`
	Port        int    `mapstructure:"APP_PORT" default:"8098"`
	TimeZone    string `mapstructure:"APP_TIMEZONE"  default:"UTC"`
	Locale      string `mapstructure:"APP_LOCALE"  default:"en"`
	GinMode     string `mapstructure:"GIN_MODE" default:"debug"`
}


func main() {
	// Initialize the application
    New()

    Config.AddConfig("App", new(AppConfig)).Load()
    
    // artifact.Start() // Database connection will be established here
    
    Router.GET("/", func(c *gin.Context) {
        data := map[string]interface{}{
            "app": Config.GetString("App.Name"),
        }
    
        //or
        //data := gin.H{
        //	"message": "Hello World",
        //}
    
        Res.Status(200).
            Message("success").
            Data(data).Json(c)
    })
    
    Run()
}
# run example.go and visit 0.0.0.0:8080 (for windows "localhost:8080") on browser
$ go run example.go

Crud Generator

go run ./art crud package_name crud_module_name

for example:

$ cat art/main.go
package main

import "github.com/shipu/artifact/cmd"

func main() {
    cmd.Execute()
}

Run below command to generate crud.

go run ./art crud github.com/shipu/golang-gin-boilerplate notice

Below Folder structure will generate:

src/notice
├── controllers
│   └── notice_controller.go
├── models
│   └── notice.go
├── routes
│   └── api.go
└── services
└── notice_service.go

More information about crud generator can be found in Golang Gin Boilerplate

Config :

Suppose your config is config/db.go:

package config

type DatabaseConfig struct {
    Username   string `mapstructure:"DB_USER" default:""`
    Password   string `mapstructure:"DB_PASS" default:""`
    Host       string `mapstructure:"DB_HOST" default:""`
    Port       string `mapstructure:"DB_PORT" default:""`
    Database   string `mapstructure:"DB_DATABASE" default:""`
    Connection string `mapstructure:"DB_CONNECTION" default:""`
}

and your .env is:

DB_CONNECTION=mongodb
DB_HOST=mongodb.host
DB_PORT=
DB_USER=user
DB_PASS=password
DB_DATABASE=collection

For initialization DatabaseConfig config.

artifact.Config.AddConfig("NoSql", new(DatabaseConfig)).Load()
To get config:
artifact.Config.GetString("NoSql.Host")

Config Method List:

GetString("key")
GetInt("key")
Get("key")

Route

artifact.Router.GET("/", func(c *gin.Context) {
    data := map[string]interface{}{
        "app": Config.GetString("App.Name"),
    }
    
    //or
    //data := gin.H{
    //	"message": "Hello World",
    //}
    
    Res.Status(200).
        Message("success").
        Data(data).Json(c)
})
artifact.Router.GET("/someGet", getting)
artifact.Router.POST("/somePost", posting)
artifact.Router.PUT("/somePut", putting)
artifact.Router.DELETE("/someDelete", deleting)
artifact.Router.PATCH("/somePatch", patching)
artifact.Router.HEAD("/someHead", head)
artifact.Router.OPTIONS("/someOptions", options)

And all Gin router support.

Response

In Gin

Where c is the *gin.Context context.

data := map[string]interface{}{
    "app": "Golang",
}
c.JSON(200, gin.H{
    "status_code":  200,
    "message": "Success",
    "data": data,
})

In artifact

data := map[string]interface{}{
    "app": "Golang",
}

Res.Code(200).
    Message("Success").
    Data(data).
    Json(c)

for set custom key value in response

paginate := your paginate data

Res.Code(200).
    Message("Success").
    Data(data).
    Raw(map[string]interface{}{
        "meta": paginate,
    }).
    Json(c)

Res Api Methods:

Json
PureJSON
JsonP
AsciiJSON
IndentedJSON
Html
Xml
Yaml
ProtoBuf
AbortWithStatusJSON
Abort
AbortWithError
Redirect

Mongo Collection

var TodoCollection artifact.MongoCollection = artifact.Mongo.Collection("todos")

TodoCollection.Find(bson.M{})

Custom Command


// Define the struct
var TestCmd = &cobra.Command{
	Use:  "test",
	RunE: testCommand,
}

// Write the command function
func testCommand(cmd *cobra.Command, args []string) error {
	log.Print("test command")
	return nil
}

func main() {
	cmd.AddCommand(TestCmd) // Add the command

	cmd.Execute()
}


You can run command to generate Custom Command file

go run ./art make:command module_name command_name

And register in ./art/main.go

package main

import (
	"github.com/shipu/artifact/cmd"
	"github.com/tenminschool/enrolment-service/art/test/commands"
)

func init() {
	cmd.AddCommand(commands.TestCmd) // Register the Custom command
}

func main() {
	cmd.Execute()
}

All Go Mongo Driver Support.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Router *gin.Engine

Functions

func DatabaseConnection added in v1.1.27

func DatabaseConnection()

func LoadConfig

func LoadConfig()

func LoadRoute

func LoadRoute()

func New

func New()

func NoSqlConnection added in v1.1.27

func NoSqlConnection()

func Run

func Run()

func Start

func Start()

Types

type Configuration

type Configuration struct {
	RegisteredConfigStruct map[string]interface{}
	LoadedConfig           map[string]interface{}
	NoSqlConfig            string
	RelationDBConfig       string
}
var Config *Configuration

func NewConfig

func NewConfig() *Configuration

func (*Configuration) AddConfig

func (configuration *Configuration) AddConfig(name string, userConfig interface{}) *Configuration

func (*Configuration) AddNoSqlConfig added in v1.1.28

func (configuration *Configuration) AddNoSqlConfig(name string, userConfig interface{}) *Configuration

func (*Configuration) AddRelationDBConfig added in v1.1.28

func (configuration *Configuration) AddRelationDBConfig(name string, userConfig interface{}) *Configuration

func (Configuration) Bool added in v1.1.6

func (configuration Configuration) Bool(key string) (bool, bool)

Bool returns the byte of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not bool.

func (Configuration) Byte added in v1.1.6

func (configuration Configuration) Byte(key string) (byte, bool)

Byte returns the byte of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not byte.

func (Configuration) Bytes added in v1.1.6

func (configuration Configuration) Bytes(key string) ([]byte, bool)

Bytes returns the []byte of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not []byte.

func (Configuration) Complex128 added in v1.1.6

func (configuration Configuration) Complex128(key string) (complex128, bool)

Complex128 returns the complex128 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not complex128.

func (Configuration) Complex64 added in v1.1.6

func (configuration Configuration) Complex64(key string) (complex64, bool)

Complex64 returns the complex64 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not complex64.

func (Configuration) Float32 added in v1.1.6

func (configuration Configuration) Float32(key string) (float32, bool)

Float32 returns the float32 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not float32.

func (Configuration) Float64 added in v1.1.6

func (configuration Configuration) Float64(key string) (float64, bool)

Float64 returns the float64 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not float64.

func (Configuration) Get

func (configuration Configuration) Get(key string) (interface{}, bool)

Get returns the interface of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field.

func (Configuration) GetGetter added in v1.1.6

func (configuration Configuration) GetGetter(key string) (*structil.Getter, bool)

func (Configuration) GetString

func (configuration Configuration) GetString(key string) string

func (Configuration) GetType added in v1.1.6

func (configuration Configuration) GetType(key string) (reflect.Type, bool)

GetType returns the reflect.Type object of the original struct field named "name". 2nd return value will be false if the original struct does not have a "name" field.

func (Configuration) GetValue added in v1.1.6

func (configuration Configuration) GetValue(key string) (reflect.Value, bool)

GetValue returns the reflect.Value object of the original struct field named "name". 2nd return value will be false if the original struct does not have a "name" field.

func (Configuration) Getter added in v1.1.6

func (configuration Configuration) Getter(key string) *Getter

func (Configuration) Has added in v1.1.6

func (configuration Configuration) Has(key string) bool

Has tests whether the original struct has a field named "name".

func (Configuration) Int added in v1.1.6

func (configuration Configuration) Int(key string) (int, bool)

Int returns the int of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not int.

func (Configuration) Int16 added in v1.1.6

func (configuration Configuration) Int16(key string) (int16, bool)

Int16 returns the int16 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not int16.

func (Configuration) Int32 added in v1.1.6

func (configuration Configuration) Int32(key string) (int32, bool)

Int32 returns the int32 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not int32.

func (Configuration) Int64 added in v1.1.6

func (configuration Configuration) Int64(key string) (int64, bool)

Int64 returns the int64 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not int64.

func (Configuration) Int8 added in v1.1.6

func (configuration Configuration) Int8(key string) (int8, bool)

Int8 returns the int8 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not int8.

func (Configuration) IsArray added in v1.1.6

func (configuration Configuration) IsArray(key string) bool

IsArray reports whether type of the original struct field named name is slice.

func (Configuration) IsBool added in v1.1.6

func (configuration Configuration) IsBool(key string) bool

IsBool reports whether type of the original struct field named name is bool.

func (Configuration) IsByte added in v1.1.6

func (configuration Configuration) IsByte(key string) bool

IsByte reports whether type of the original struct field named name is byte.

func (Configuration) IsBytes added in v1.1.6

func (configuration Configuration) IsBytes(key string) bool

IsBytes reports whether type of the original struct field named name is []byte.

func (Configuration) IsChan added in v1.1.6

func (configuration Configuration) IsChan(key string) bool

IsChan reports whether type of the original struct field named name is chan.

func (Configuration) IsComplex128 added in v1.1.6

func (configuration Configuration) IsComplex128(key string) bool

IsComplex128 reports whether type of the original struct field named name is []byte.

func (Configuration) IsComplex64 added in v1.1.6

func (configuration Configuration) IsComplex64(key string) bool

IsComplex64 reports whether type of the original struct field named name is []byte.

func (Configuration) IsFloat32 added in v1.1.6

func (configuration Configuration) IsFloat32(key string) bool

IsFloat32 reports whether type of the original struct field named name is float32.

func (Configuration) IsFloat64 added in v1.1.6

func (configuration Configuration) IsFloat64(key string) bool

IsFloat64 reports whether type of the original struct field named name is float64.

func (Configuration) IsFunc added in v1.1.6

func (configuration Configuration) IsFunc(key string) bool

IsFunc reports whether type of the original struct field named name is func.

func (Configuration) IsInt added in v1.1.6

func (configuration Configuration) IsInt(key string) bool

IsInt reports whether type of the original struct field named name is int.

func (Configuration) IsInt16 added in v1.1.6

func (configuration Configuration) IsInt16(key string) bool

IsInt16 reports whether type of the original struct field named name is int16.

func (Configuration) IsInt32 added in v1.1.6

func (configuration Configuration) IsInt32(key string) bool

IsInt32 reports whether type of the original struct field named name is int32.

func (Configuration) IsInt64 added in v1.1.6

func (configuration Configuration) IsInt64(key string) bool

IsInt64 reports whether type of the original struct field named name is int64.

func (Configuration) IsInt8 added in v1.1.6

func (configuration Configuration) IsInt8(key string) bool

IsInt8 reports whether type of the original struct field named name is int8.

func (Configuration) IsMap added in v1.1.6

func (configuration Configuration) IsMap(key string) bool

IsMap reports whether type of the original struct field named name is map.

func (Configuration) IsSlice added in v1.1.6

func (configuration Configuration) IsSlice(key string) bool

IsSlice reports whether type of the original struct field named name is slice.

func (Configuration) IsString added in v1.1.6

func (configuration Configuration) IsString(key string) bool

IsString reports whether type of the original struct field named name is string.

func (Configuration) IsStruct added in v1.1.6

func (configuration Configuration) IsStruct(key string) bool

IsStruct reports whether type of the original struct field named name is struct.

func (Configuration) IsUint added in v1.1.6

func (configuration Configuration) IsUint(key string) bool

IsUint reports whether type of the original struct field named name is uint.

func (Configuration) IsUint16 added in v1.1.6

func (configuration Configuration) IsUint16(key string) bool

IsUint16 reports whether type of the original struct field named name is uint16.

func (Configuration) IsUint32 added in v1.1.6

func (configuration Configuration) IsUint32(key string) bool

IsUint32 reports whether type of the original struct field named name is uint32.

func (Configuration) IsUint64 added in v1.1.6

func (configuration Configuration) IsUint64(key string) bool

IsUint64 reports whether type of the original struct field named name is uint64.

func (Configuration) IsUint8 added in v1.1.6

func (configuration Configuration) IsUint8(key string) bool

IsUint8 reports whether type of the original struct field named name is uint8.

func (Configuration) IsUintptr added in v1.1.6

func (configuration Configuration) IsUintptr(key string) bool

IsUintptr reports whether type of the original struct field named name is uintptr.

func (Configuration) IsUnsafePointer added in v1.1.6

func (configuration Configuration) IsUnsafePointer(key string) bool

IsUnsafePointer reports whether type of the original struct field named name is []byte.

func (*Configuration) Load

func (configuration *Configuration) Load() map[string]interface{}

func (Configuration) Names added in v1.1.6

func (configuration Configuration) Names(key string) []string

Names returns names of struct field.

func (Configuration) NumField added in v1.1.6

func (configuration Configuration) NumField(key string) int

NumField returns num of struct field.

func (Configuration) Slice added in v1.1.6

func (configuration Configuration) Slice(key string) ([]interface{}, bool)

Slice returns the slice of interface of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not slice of interface.

func (*Configuration) String added in v1.1.6

func (configuration *Configuration) String(key string) (string, bool)

String returns the string of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not string.

func (Configuration) ToMap added in v1.1.6

func (configuration Configuration) ToMap(key string) map[string]interface{}

ToMap returns a map converted from this Getter.

func (Configuration) Uint added in v1.1.6

func (configuration Configuration) Uint(key string) (uint, bool)

Uint returns the uint of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not uint.

func (Configuration) Uint16 added in v1.1.6

func (configuration Configuration) Uint16(key string) (uint16, bool)

Uint16 returns the uint16 of the original struct field named name.Getter 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not uint16.

func (Configuration) Uint32 added in v1.1.6

func (configuration Configuration) Uint32(key string) (uint32, bool)

Uint32 returns the uint32 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not uint32.

func (Configuration) Uint64 added in v1.1.6

func (configuration Configuration) Uint64(key string) (uint64, bool)

Uint64 returns the uint64 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not uint64.

func (Configuration) Uint8 added in v1.1.6

func (configuration Configuration) Uint8(key string) (uint8, bool)

Uint8 returns the uint8 of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not uint8.

func (Configuration) Uintptr added in v1.1.6

func (configuration Configuration) Uintptr(key string) (uintptr, bool)

Uintptr returns the uintptr of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not uintptr.

func (Configuration) UnsafePointer added in v1.1.6

func (configuration Configuration) UnsafePointer(key string) (unsafe.Pointer, bool)

UnsafePointer returns the unsafe.Pointer of the original struct field named name. 2nd return value will be false if the original struct does not have a "name" field. 2nd return value will be false if type of the original struct "name" field is not unsafe.Pointer.

type Database added in v1.1.27

type Database struct {
	*gorm.DB
}
var DB *Database

func NewDatabase added in v1.1.27

func NewDatabase() *Database

type Getter added in v1.1.6

type Getter struct {
	*structil.Getter
}

type LoggerBuilder

type LoggerBuilder struct {
	*zap.SugaredLogger
}

LoggerBuilder structure

func GetLogger

func GetLogger() LoggerBuilder

GetLogger gets the global instance of the logger

func InitializeLogger added in v1.1.27

func InitializeLogger() LoggerBuilder

func NewLogger

func NewLogger() LoggerBuilder

NewLogger sets up logger

type MongoCollection

type MongoCollection struct {
	*mongo.Collection
	Ctx        context.Context
	CancelFunc context.CancelFunc
}

func (MongoCollection) Find

func (collection MongoCollection) Find(filter interface{},
	opts ...*options.FindOptions) (*mongo.Cursor, error, context.Context)

func (MongoCollection) FindOne

func (collection MongoCollection) FindOne(filter interface{},
	opts ...*options.FindOneOptions) *mongo.SingleResult

func (MongoCollection) FindOneAndDelete

func (collection MongoCollection) FindOneAndDelete(filter interface{},
	opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult

func (MongoCollection) FindOneAndUpdate

func (collection MongoCollection) FindOneAndUpdate(filter interface{},
	update interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult

func (MongoCollection) InsertOne

func (collection MongoCollection) InsertOne(document interface{},
	opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)

func (MongoCollection) WithContext

func (mongoCollection MongoCollection) WithContext() MongoCollection

type MongoDB

type MongoDB struct {
	Client   *mongo.Client
	Database *mongo.Database
	Ctx      context.Context
}
var Mongo *MongoDB

func NewNoSqlDB added in v1.1.27

func NewNoSqlDB() *MongoDB

func (*MongoDB) Collection

func (mongodb *MongoDB) Collection(name string) MongoCollection

type Response

type Response struct {
	Code    int                    `json:"code"`
	Message string                 `json:"message"`
	Data    interface{}            `json:"data"`
	Raw     map[string]interface{} `json:"raw"`
}

type ResponseBuilder

type ResponseBuilder struct {
	CodeKeyName    string
	MessageKeyName string
	DataKeyName    string
	Response
}
var Res ResponseBuilder

func (ResponseBuilder) Abort added in v1.1.7

func (response ResponseBuilder) Abort(c *gin.Context)

func (ResponseBuilder) AbortWithError added in v1.1.7

func (response ResponseBuilder) AbortWithError(c *gin.Context, err error)

func (ResponseBuilder) AbortWithStatusJSON

func (response ResponseBuilder) AbortWithStatusJSON(c *gin.Context)

func (ResponseBuilder) AsciiJSON added in v1.1.7

func (response ResponseBuilder) AsciiJSON(c *gin.Context)

func (ResponseBuilder) Build

func (response ResponseBuilder) Build() interface{}

func (ResponseBuilder) Code added in v1.1.7

func (response ResponseBuilder) Code(status int) ResponseBuilder

func (ResponseBuilder) Data

func (response ResponseBuilder) Data(data interface{}) ResponseBuilder

func (ResponseBuilder) Html added in v1.1.7

func (response ResponseBuilder) Html(c *gin.Context, name string)

func (ResponseBuilder) IndentedJSON added in v1.1.7

func (response ResponseBuilder) IndentedJSON(c *gin.Context)

func (ResponseBuilder) Json

func (response ResponseBuilder) Json(c *gin.Context)

func (ResponseBuilder) JsonP added in v1.1.7

func (response ResponseBuilder) JsonP(c *gin.Context)

func (ResponseBuilder) Message

func (response ResponseBuilder) Message(message string) ResponseBuilder

func (ResponseBuilder) ProtoBuf added in v1.1.7

func (response ResponseBuilder) ProtoBuf(c *gin.Context)

func (ResponseBuilder) PureJSON added in v1.1.7

func (response ResponseBuilder) PureJSON(c *gin.Context)

func (ResponseBuilder) Raw added in v1.1.10

func (response ResponseBuilder) Raw(raw map[string]interface{}) ResponseBuilder

func (ResponseBuilder) Redirect added in v1.1.7

func (response ResponseBuilder) Redirect(c *gin.Context, location string)

func (*ResponseBuilder) SetCodeKeyName added in v1.1.31

func (response *ResponseBuilder) SetCodeKeyName(name string) *ResponseBuilder

func (*ResponseBuilder) SetDataKeyName added in v1.1.31

func (response *ResponseBuilder) SetDataKeyName(name string) *ResponseBuilder

func (*ResponseBuilder) SetMessageKeyName added in v1.1.31

func (response *ResponseBuilder) SetMessageKeyName(name string) *ResponseBuilder

func (ResponseBuilder) Xml added in v1.1.7

func (response ResponseBuilder) Xml(c *gin.Context)

func (ResponseBuilder) Yaml added in v1.1.7

func (response ResponseBuilder) Yaml(c *gin.Context)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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