Skip to content

go-cam/cam

Repository files navigation

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