Categorygithub.com/go-cam/cam
modulepackage
0.5.3
Repository: https://github.com/go-cam/cam.git
Documentation: pkg.go.dev

# 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 = "[email protected]"
	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

TypeContent
QQ Group740731115
Email[email protected]

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

must run before cam.RunDefault.
debug log.
get env file values.
error log.
fatal log.
info log.
new Application config.
new Application instance.
Deprecated: Remove on v0.6.0 new cacheComp config.
No description provided by the author
Deprecated: Remove on v0.6.0 new config.
Deprecated: Remove on v0.6.0 new ConsoleComponent config.
Deprecated: Remove on v0.6.0 new DatabaseComponent config.
Deprecated: Remove on v0.6.0 new file cacheComp engine.
Deprecated: Remove on v0.6.0 new ConsoleComponent config.
Deprecated: Remove on v0.6.0 new log config.
Deprecated: Remove on v0.6.0.
Deprecated: Remove on v0.6.0.
Deprecated: Remove on v0.6.0 new redis engine.
Deprecated: Remove on v0.6.0 new rule.
Deprecated: Remove on v0.6.0 new SocketComponentConfig.
Deprecated: Remove on v0.6.0 new ValidationComponentConfig.
Deprecated: Remove on v0.6.0 new WebsocketComponent config.
get config param.
must run before cam.RunDefault register controller.
run application.
trace log.
validComp struct.
#################### [START] other export #################### Framework version.
warn log.

# Constants

all.
log level: debug.
log level: error.
log level: fatal.
log level: info.
none.
suggest this level to write file.
log level: trace.
log level: warning.
Interface and Tag mode.
Interface mode.
Tag mode.

# Variables

No description provided by the author
#################### [START] instance export ####################.

# Structs

framework Application global instance struct define.
Deprecated: Remove on v0.6.0.
Deprecated: Remove on v0.6.0.
Deprecated: Remove on v0.6.0.
Deprecated: Remove on v0.6.0.
Deprecated: Remove on v0.6.0.

# Interfaces

Deprecated: Remove on v0.6.0.
Deprecated: Remove on v0.6.0.