tingyun_beego

package module
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2021 License: Apache-2.0 Imports: 9 Imported by: 3

README

tingyun_beego

安装

  • 运行
go get github.com/TingYunAPM/go

使用

  • 引用:

main函数文件中引入

import "github.com/TingYunAPM/go"
import "github.com/TingYunAPM/go/framework/beego"
  • 函数和方法替换:
  1. "beego.Run()" 替换为:=> "tingyun_beego.Run()" 例:
func main() {
  tingyun.AppInit("tingyun.json")
  defer tingyun.AppStop()
  //Do Other Things...
  tingyun_beego.Run()//replace beego.Run()
}
  1. "beego.Handler" 替换为:=> "tingyun_beego.Handler" 例:
func main() {
    tingyun.AppInit("tingyun.json")
    defer tingyun.AppStop()
    //beego.Handler("/api", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    tingyun_beego.Handler("/api", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
     //Do Some Things
        fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
    }))
    tingyun_beego.Run()
}
  1. "beego.Controller" 替换为:=> "tingyun_beego.Controller" 例:
type MainController struct {
    //beego.Controller
    tingyun_beego.Controller
}
func (this *MainController) Get() {
    this.Ctx.WriteString("hello world")
}
func main() {
    tingyun.AppInit("tingyun.json")
    defer tingyun.AppStop()
    beego.Router("/", &MainController{})
    tingyun_beego.Run()
}
  1. "beego.NSHandler" 替换为:=> "tingyun_beego.NSHandler" 例:
func main() {
    tingyun.AppInit("tingyun.json")
    defer tingyun.AppStop()
    beego.AddNamespace(beego.NewNamespace("/v1",
        //
        // beego.NSHandler("/handler", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        tingyun_beego.NSHandler("/handler", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
        })),
    ))
    tingyun_beego.Run()
}
  1. "beego.Namespace.Handler" 替换为:=> "tingyun_beego.NamespaceHandler" 例:
func main() {
    tingyun.AppInit("tingyun.json")
    defer tingyun.AppStop()
    nsobj := beego.NewNamespace("/v1")
    //
    //nsobj.Handler("/ttt", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    tingyun_beego.NamespaceHandler(ns, "/ttt", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "ttt Hello, %q", html.EscapeString(r.URL.Path))
    }))
    beego.AddNamespace(nsobj)
    tingyun_beego.Run()
}

获取Action,使用Component

  • 在Controller中获取tingyun.Action对象
type MainController struct {
    tingyun_beego.Controller
}
func (this *MainController) Get() {
    action := tingyun_beego.FindAction(this.Ctx)
    componentCheck := action.CreateComponent("CheckJob")
    //Do Some Check Works
    componentCheck.Finish()
    componentWrite := action.CreateComponent("Get::out")
    this.Ctx.WriteString("hello world")
    componentWrite.Finish()
}
  • 在http.Handler中获取tingyun.Action对象
tingyun_beego.Handler("/api", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    action := tingyun_beego.GetAction(w)
    componentCheck := action.CreateComponent("CheckJob")
    //
    //Do Some Check Works
    componentCheck.Finish()
    componentWrite := action.CreateComponent("Handler::out")
    fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
    componentWrite.Finish()
}))
  • 使用协程局部存储方式保存/获取tingyun.Action对象
//go get github.com/TingYunAPM/routinelocal 安装协程局部存储包
//在main函数文件 import "github.com/TingYunAPM/routinelocal"
//或者 import "github.com/TingYunAPM/routinelocal/native" //参考 github.com/TingYunAPM/routinelocal/native/Readme.md
//在main函数开始使用routinelocal.Storage初始化 tingyun_beego,实际用例参考 github.com/TingYunAPM/go/framework/beego/example/server_d
func main() {
	err := tingyun.AppInit("tingyun.json")
	//注意: 如果要使用 tingyun_beego.RoutineLocalGetAction(),下边这行必须添加
	tingyun_beego.RoutineLocalInit(routinelocal.Get()
	...
//在某个函数中要获取tingyun.Action, 使用: tingyun_beego.RoutineLocalGetAction()

其他

请参考 https://github.com/TingYunAPM/go/blob/master/README.md

Documentation

Overview

beego's Wrapper

Example
err := tingyun.AppInit("tingyun.json")
if err != nil {
	fmt.Println(err)
}
defer tingyun.AppStop()
//"beego.Run" 替换为:=> "tingyun_beego.Run"
tingyun_beego.Run()
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindAction

func FindAction(ctx *context.Context) *tingyun.Action

API: 在应用过程中 对于没有使用beego.Handler, beego.NSHandler 函数和 beego.Namespace.Handler方法 的方式 使用FindAction获取对应的tingyun.Action

func GetAction

func GetAction(rw http.ResponseWriter) *tingyun.Action

API: 对于使用beego.Handler, beego.NSHandler函数 或 beego.Namespace.Handler方法的方式 使用GetAction获取对应的tingyun.Action 在没有hook的情况下,返回nil

func Handler

func Handler(rootpath string, h http.Handler, options ...interface{}) *beego.App

替换beego.Handler

Example
err := tingyun.AppInit("tingyun.json")
if err != nil {
	fmt.Println(err)
}
defer tingyun.AppStop()
//"beego.Handler" 替换为:=> "tingyun_beego.Handler"
tingyun_beego.Handler("/api", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}))
//"beego.Run" 替换为:=> "tingyun_beego.Run"
tingyun_beego.Run()
Output:

func NSHandler

func NSHandler(rootpath string, h http.Handler) beego.LinkNamespace

替换beego.NSHandler

Example
err := tingyun.AppInit("tingyun.json")
if err != nil {
	fmt.Println(err)
}
defer tingyun.AppStop()
ns := beego.NewNamespace("/v1",
	beego.NSCond(func(ctx *context.Context) bool {
		if ctx.Input.Domain() == "127.0.0.1" {
			return true
		}
		return false
	}),
	//"beego.NSHandler" 替换为:=> "tingyun_beego.NSHandler"
	tingyun_beego.NSHandler("/handler", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
	})),
)
beego.AddNamespace(ns)
//"beego.Run" 替换为:=> "tingyun_beego.Run"
tingyun_beego.Run()
Output:

func NamespaceHandler

func NamespaceHandler(n *beego.Namespace, rootpath string, h http.Handler) *beego.Namespace

替换beego.Namesapce.Handler

Example
err := tingyun.AppInit("tingyun.json")
if err != nil {
	fmt.Println(err)
}
defer tingyun.AppStop()
ns := beego.NewNamespace("/v1",
	beego.NSCond(func(ctx *context.Context) bool {
		if ctx.Input.Domain() == "127.0.0.1" {
			return true
		}
		return false
	}),
)
//"beego.Namespace.Handler" 替换为:=> "tingyun_beego.NamespaceHandler"
//ns.Handler("/ttt", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tingyun_beego.NamespaceHandler(ns, "/ttt", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "ttt Hello, %q", html.EscapeString(r.URL.Path))
}))

beego.AddNamespace(ns)
//"beego.Run" 替换为:=> "tingyun_beego.Run"
tingyun_beego.Run()
Output:

func RoutineLocalGetAction

func RoutineLocalGetAction() *tingyun.Action

func RoutineLocalInit

func RoutineLocalInit(rls Storage)

func Run

func Run(params ...string)

替换beego.Run

Types

type Controller

type Controller struct {
	beego.Controller
}

要使用tingyun_beego.Controller 替换 beego.Controller

Example
err := tingyun.AppInit("tingyun.json")
if err != nil {
	fmt.Println(err)
}
defer tingyun.AppStop()
//"beego.Controller" 替换为:=> "tingyun_beego.Controller"
//type MainController struct {
//	tingyun_beego.Controller
//}
//func (this *MainController) Get() {
//	this.Ctx.WriteString("hello world")
//}
beego.Router("/", &MainController{})
//"beego.Run" 替换为:=> "tingyun_beego.Run"
tingyun_beego.Run()
Output:

func (*Controller) Finish

func (c *Controller) Finish()

func (*Controller) Init

func (c *Controller) Init(ctx *context.Context, controllerName, actionName string, app interface{})

type Storage

type Storage interface {
	Get() unsafe.Pointer
	Set(p unsafe.Pointer)
}

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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