cam

package module
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2020 License: Apache-2.0 Imports: 20 Imported by: 0

README

Cam

Http And Socket Framework

GitHub go.mod Go version GitHub tag (latest by date) GitHub last commit GoDoc TODOs

Cam is a personal open source framework. It's goal is not to be lightweight, but to be a large framework of multi-functional integration.

Cam may not be suitable for small projects. It may be more suitable for medium and large-scale projects under multiple modules, at least this is the original intention of its development

Contents

Framework structure

Framework structure

Easy start

1. Create two file:

go.mod

module app

go 1.14

require (
	github.com/go-cam/cam v0.4.4
)

main.go

package main

import (
    "github.com/go-cam/cam"
    "github.com/go-cam/cam/plugin/camRouter"
)

func main() {
	cam.RegisterController(&HelloController{})
	cam.RunDefault()
}

type HelloController struct {
	camRouter.Controller
}

func (ctrl *HelloController) Cam() {
	ctrl.SetResponse([]byte("cam is word."))
}
2. Build and run

build

go build main.go

run

./main

Open the browser and open link: http://127.0.0.1:20200/hello/cam

Template struct:

The document is explained based on this directory
.
|-- .docker                 // docker file
|-- common                  // common module directory
  |-- config                
    |-- app.go              // common module config
  |-- templates
    |-- xorm                // xorm generate orm files's template
      |-- config
      |-- struct.go.tpl
|-- server                  // server module directory
  |-- config
    |-- app.go
    |-- bootstrap.go        // app bootstrap file
  |-- controllers           // controllers directory
    |-- HelloController.go
  |-- main.go               // entry of server module
|-- .gitignore
|-- cam.go                  // command line tools. you can build and execute it
|-- go.mod
|-- go.sum
|-- LICENSE
|-- README.md

Environment support

Components

Components are the functions provided by the framework. Such as: http, websocket, cache, database, console, etc. It is a package of individual functions.

You can also package components according to your requirements, as long as you implement camBase.ComponentInterface And corresponding configuration implementation camBase.ComponentConfigInterface that will do

Examples

.Env file

.env file must be in the same directory as the executable file.

It is recommended to create .env file in this directory: ./server/.env

./server/.env:

DB_USERNAME = root
DB_PASSWORD = 123456

use in code:

func test() {
  username := cam.App.GetEnv("DB_USERNAME") // username = "root"
  password := cam.App.GetEnv("DB_PASSWORD") // password = "123456"
  fmt.println(username + " " + password) // output: "root 123456"
}
Upload file

Example:

FileController.go:

import (
	"github.com/go-cam/cam"
	"github.com/go-cam/cam/base/camUtils"
)

type FileController struct {
	cam.HttpController
}

func (ctrl *FileController) Upload() {
	uploadFile := ctrl.GetFile("file")
	if uploadFile == nil {
		cam.App.Fatal("FileController.Upload", "no upload file")
		return
	}

	absFilename := camUtils.File.GetRunPath() + "/runtime/upload/tmp.jpg"
	err := uploadFile.Save(absFilename)
	if err != nil {
		cam.App.Fatal("FileController.Upload", err.Error())
		return
	}

	cam.App.Trace("FileController.Upload", absFilename)
}

Then post file to http://.../file/upload

Validation

Example:

package valid

import (
    "github.com/go-cam/cam"
    "github.com/go-cam/cam/base/camBase"
    "github.com/go-cam/cam/base/camStructs"
)

type User struct {
	Email   string
	MyEmail Email
}

type Email string

func (user *User) Rules() []camBase.RuleInterface {
	return []camBase.RuleInterface{
		camStructs.NewRule([]string{"Email", "MyEmail"}, cam.Rule.Email, cam.Rule.Length(0, 100)),
	}
}

func init() {
	user := new(User)
	user.Email = "123123"
	user.MyEmail = "123@123.com"
	firstErr, _ := cam.Valid(user)
	if firstErr != nil {
		panic(firstErr)
	}
}
Middleware

Support Component: HttpComponent, WebsocketComponent, SocketComponent

add in ComponentConfig

package config

import (
	"github.com/go-cam/cam"
	"github.com/go-cam/cam/base/camBase"
    "github.com/go-cam/cam/component/camHttp"
)


func httpServer() camBase.ComponentConfigInterface {
	config := camHttp.NewHttpComponentConfig(20000)
	config.Register(&controllers.TestController{}) 
	// Add middleware
	config.AddMiddleware("", &AMiddleware{}) // All route will use this Middleware
	return config
}

type AMiddleware struct {
}

func (mid *AMiddleware) Handler(ctx camBase.ContextInterface, next camBase.NextHandler) []byte {
	cam.Debug("AMiddleware", "before")
	res := next()
	cam.Debug("AMiddleware", "after")
	return res
}
Grpc
Grpc Server

Grpc Client

Contact

Type Content
QQ Group 740731115
Email 1105412681@qq.com

Documentation

Index

Constants

View Source
const (
	// Deprecated: Remove on v0.6.0 instead by camStatics.*
	LogLevelTrace = camStatics.LogLevelTrace // log level: trace
	// Deprecated: Remove on v0.6.0 instead by camStatics.*
	LogLevelDebug = camStatics.LogLevelDebug // log level: debug
	// Deprecated: Remove on v0.6.0 instead by camStatics.*
	LogLevelInfo = camStatics.LogLevelInfo // log level: info
	// Deprecated: Remove on v0.6.0 instead by camStatics.*
	LogLevelWarn = camStatics.LogLevelWarn // log level: warning
	// Deprecated: Remove on v0.6.0 instead by camStatics.*
	LogLevelError = camStatics.LogLevelError // log level: error
	// Deprecated: Remove on v0.6.0 instead by camStatics.*
	LogLevelFatal = camStatics.LogLevelFatal // log level: fatal
	// Deprecated: Remove on v0.6.0 instead by camStatics.*
	LogLevelNone = camStatics.LogLevelNone // none
	// Deprecated: Remove on v0.6.0 instead by camStatics.*
	LogLevelSuggest = camStatics.LogLevelSuggest // suggest this level to write file
	// Deprecated: Remove on v0.6.0 instead by camStatics.*
	LogLevelAll = camStatics.LogLevelAll // all
)

Log

View Source
const (
	// Deprecated: Remove on v0.6.0 instead by camStatics.*
	ValidModeInterface = camStatics.ModeInterface // Interface mode
	// Deprecated: Remove on v0.6.0 instead by camStatics.*
	ValidModeTag = camStatics.ModeTag // Tag mode
	// Deprecated: Remove on v0.6.0 instead by camStatics.*
	ValidModeBot = camStatics.ModeBoth // Interface and Tag mode
)

Validation

Variables

#################### [START] instance export ####################

Functions

func AddComponent added in v0.4.0

func AddComponent(name string, conf camStatics.ComponentConfigInterface)

must run before cam.RunDefault

func Debug added in v0.4.0

func Debug(title, content string)

debug log

func Env added in v0.4.0

func Env(key string) string

get env file values

func Error added in v0.4.0

func Error(title, content string)

error log

func Fatal added in v0.4.0

func Fatal(title, content string)

fatal log

func Info added in v0.4.0

func Info(title, content string)

info log

func NewAppConfig

func NewAppConfig() *camConfig.AppConfig

new Application config

func NewCacheConfig deprecated added in v0.3.0

func NewCacheConfig() *camCache.CacheComponentConfig

Deprecated: Remove on v0.6.0 new cacheComp config

func NewCamManager added in v0.5.0

func NewCamManager() *cmd.CamManager

func NewConfig deprecated

func NewConfig() *camConfig.Config

Deprecated: Remove on v0.6.0 new config

func NewConsoleConfig deprecated

func NewConsoleConfig() *camConsole.ConsoleComponentConfig

Deprecated: Remove on v0.6.0 new ConsoleComponent config

func NewDatabaseConfig deprecated

func NewDatabaseConfig(driverName string, host string, port string, name string, username string, password string) *camDatabase.DatabaseComponentConfig

Deprecated: Remove on v0.6.0 new DatabaseComponent config

func NewFileCache deprecated added in v0.3.0

func NewFileCache() *camCache.FileCache

Deprecated: Remove on v0.6.0 new file cacheComp engine

func NewHttpConfig deprecated added in v0.3.0

func NewHttpConfig(port uint16) *camHttp.HttpComponentConfig

Deprecated: Remove on v0.6.0 new ConsoleComponent config

func NewLogConfig deprecated added in v0.2.16

func NewLogConfig() *camLog.LogComponentConfig

Deprecated: Remove on v0.6.0 new log config

func NewMailConfig deprecated added in v0.3.0

func NewMailConfig(email string, password string, host string) *camMail.MailComponentConfig

Deprecated: Remove on v0.6.0

func NewRecover deprecated added in v0.3.0

func NewRecover(message string) *camStructs.Recover

Deprecated: Remove on v0.6.0

func NewRedisCache deprecated added in v0.3.0

func NewRedisCache() *camCache.RedisCache

Deprecated: Remove on v0.6.0 new redis engine

func NewRule deprecated added in v0.4.0

func NewRule(fields []string, handlers ...camStatics.ValidHandler) *camStructs.Rule

Deprecated: Remove on v0.6.0 new rule

func NewSocketConfig deprecated added in v0.4.0

func NewSocketConfig(port uint16) *camSocket.SocketComponentConfig

Deprecated: Remove on v0.6.0 new SocketComponentConfig

func NewValidationConfig deprecated added in v0.4.0

func NewValidationConfig() *camValidation.ValidationComponentConfig

Deprecated: Remove on v0.6.0 new ValidationComponentConfig

func NewWebsocketConfig deprecated added in v0.3.0

func NewWebsocketConfig(port uint16) *camWebsocket.WebsocketComponentConfig

Deprecated: Remove on v0.6.0 new WebsocketComponent config

func Param added in v0.4.0

func Param(key string) interface{}

get config param

func RegisterController added in v0.4.0

func RegisterController(ctrl camStatics.ControllerInterface)

must run before cam.RunDefault register controller

func RunDefault added in v0.4.0

func RunDefault()

run application

func Trace added in v0.4.0

func Trace(title, content string)

trace log

func Valid added in v0.4.0

func Valid(v interface{}) (firstErr error, errDict map[string][]error)

validComp struct

func Version added in v0.5.0

func Version() string

#################### [START] other export #################### Framework version

func Warn added in v0.4.0

func Warn(title, content string)

warn log

Types

type Application added in v0.3.0

type Application struct {
	// contains filtered or unexported fields
}

framework Application global instance struct define

func NewApplication added in v0.3.0

func NewApplication() *Application

new Application instance

func (*Application) AddComponentAfterRun added in v0.4.0

add component after app ran

func (*Application) AddConfig added in v0.3.0

func (app *Application) AddConfig(configI camStatics.AppConfigInterface)

Add config. Must be called before calling cam.App.Run (). Merge as much as possible, otherwise overwrite.

config: new config

func (*Application) AddMigration added in v0.3.0

func (app *Application) AddMigration(m camStatics.MigrationInterface)

add migration struct

func (*Application) AfterInit added in v0.5.0

func (app *Application) AfterInit(handler func())

func (*Application) AfterStart added in v0.5.0

func (app *Application) AfterStart(handler func())

func (*Application) AfterStop added in v0.5.0

func (app *Application) AfterStop(handler func())

func (*Application) BeforeInit added in v0.5.0

func (app *Application) BeforeInit(handler func())

func (*Application) BeforeStart added in v0.5.0

func (app *Application) BeforeStart(handler func())

func (*Application) BeforeStop added in v0.5.0

func (app *Application) BeforeStop(handler func())

func (*Application) Debug added in v0.3.0

func (app *Application) Debug(title string, content string)

log debug

func (*Application) Error added in v0.3.0

func (app *Application) Error(title string, content string)

log error

func (*Application) Fatal added in v0.3.0

func (app *Application) Fatal(title string, content string)

log fatal

func (*Application) GetCache added in v0.3.0

get cacheComp component

func (*Application) GetComponent added in v0.3.0

Overwrite: Try to get instance using struct type

func (*Application) GetComponentByName added in v0.3.0

func (app *Application) GetComponentByName(name string) camStatics.ComponentInterface

Overwrite: Try to get component instance by name. The name is define in config

func (*Application) GetDB added in v0.3.0

get default db component

func (*Application) GetEvn added in v0.3.0

func (app *Application) GetEvn(key string) string

get one .evn file values

func (*Application) GetGrpcClientConn added in v0.5.0

func (app *Application) GetGrpcClientConn(name string) *grpc.ClientConn

func (*Application) GetMail added in v0.3.0

get mail component

func (*Application) GetMigrateDict added in v0.3.0

func (app *Application) GetMigrateDict() map[string]camStatics.MigrationInterface

func (*Application) GetParam added in v0.3.0

func (app *Application) GetParam(key string) interface{}

get value form app.config.Params.

func (*Application) Info added in v0.3.0

func (app *Application) Info(title string, content string)

log info

func (*Application) Run added in v0.3.0

func (app *Application) Run()

run Application

func (*Application) Stop added in v0.3.0

func (app *Application) Stop()

stop Application

func (*Application) Trace added in v0.3.0

func (app *Application) Trace(title string, content string)

log trace

func (*Application) Valid added in v0.4.0

func (app *Application) Valid(v interface{}) (firstErr error, errDict map[string][]error)

validComp struct

func (*Application) Warn added in v0.3.0

func (app *Application) Warn(title string, content string)

log warning

type ConstantController deprecated added in v0.3.0

type ConstantController struct {
	camConsole.ConsoleController
}

Deprecated: Remove on v0.6.0

type Context deprecated added in v0.3.0

type Context struct {
	camContext.Context
}

Deprecated: Remove on v0.6.0

type Controller deprecated added in v0.3.0

type Controller struct {
	camRouter.Controller
}

Deprecated: Remove on v0.6.0

type ControllerAction deprecated added in v0.3.0

type ControllerAction struct {
	camRouter.ControllerAction
}

Deprecated: Remove on v0.6.0

type HttpController deprecated added in v0.3.0

type HttpController struct {
	camHttp.HttpController
}

Deprecated: Remove on v0.6.0

type MiddlewareInterface deprecated added in v0.4.1

type MiddlewareInterface interface {
	camStatics.MiddlewareInterface
}

Deprecated: Remove on v0.6.0

type ValidInterface deprecated added in v0.4.0

type ValidInterface interface {
	camStatics.ValidInterface
}

Deprecated: Remove on v0.6.0

Directories

Path Synopsis
base
camMail
From: https://github.com/jordan-wright/email Datetime: 2020-03-09 17:28:00 From: https://github.com/jordan-wright/email Datetime: 2020-03-09 17:28:00
From: https://github.com/jordan-wright/email Datetime: 2020-03-09 17:28:00 From: https://github.com/jordan-wright/email Datetime: 2020-03-09 17:28:00

Jump to

Keyboard shortcuts

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