beego RESTFul路由

* go环境配置

https://blog.csdn.net/fareast_mzh/article/details/81463777

# native compiler darwin amd64

export GOROOT=/usr/local/Cellar/go/1.10.3/libexec
export GOBIN=${GOROOT}/bin
export GOPATH=/Volume/Application/gocode
GOARCH=amd64
GOOS=darwin
CGO_ENABLED=1

PATH=$GOBIN:$GOPATH:/bin:$PATH

LITEIDE_GDB=/usr/local/bin/gdb
LITEIDE_MAKE=make
LITEIDE_TERM=/usr/bin/open
LITEIDE_TERMARGS=-a Terminal
LITEIDE_EXEC=/usr/X11R6/bin/xterm
LITEIDE_EXECOPT=-e

* 安装beego框架

https://beego.me/

go get github.com/astaxie/beego

* 新建项目 创建main.go

// beego-route project main.go
package main

import (
	"github.com/astaxie/beego"
)

// restful controller router
type RESTfulController struct {
	beego.Controller
}

func (this *RESTfulController) Get() {
	this.Ctx.WriteString("Hello World in GET method")
}

func (this *RESTfulController) Post() {
	this.Ctx.WriteString("Hello World in POST method")
}

func main() {
	// restful controller
	beego.Router("/RESTful", &RESTfulController{})
	// start service
	beego.Run("127.0.0.1:8081")
}

* 编译运行:

beego RESTFul路由_第1张图片

beego RESTFul路由_第2张图片

访问   http://127.0.0.1:8081/RESTful

https://beego.me/docs/mvc/controller/router.md

 

* 测试post请求

// post.php

 'http://127.0.0.1:8081/RESTful',
    CURLOPT_HEADER => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1,
    CURLOPT_BINARYTRANSFER => 1,
    CURLOPT_SAFE_UPLOAD => 1,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POSTFIELDS => $postdata
]);
 
$data = curl_exec($ch);
curl_close($ch);
 
echo $data.PHP_EOL;

php post.php

HTTP/1.1 200 OK
Date: Sun, 27 Jan 2019 09:20:44 GMT
Content-Length: 26
Content-Type: text/plain; charset=utf-8

Hello World in POST method

https://www.imooc.com/video/18636

 

beego controller

// Controller defines some basic http request handler operations, such as
// http context, template and view, session and xsrf.
type Controller struct {
	// context data
	Ctx  *context.Context
	Data map[interface{}]interface{}

	// route controller info
	controllerName string
	actionName     string
	methodMapping  map[string]func() //method:routertree
	AppController  interface{}

	// template data
	TplName        string
	ViewPath       string
	Layout         string
	LayoutSections map[string]string // the key is the section name and the value is the template name
	TplPrefix      string
	TplExt         string
	EnableRender   bool

	// xsrf data
	_xsrfToken string
	XSRFExpire int
	EnableXSRF bool

	// session
	CruSession session.Store
}

 

你可能感兴趣的:(golang)