Http模块

Http server


对于Http模块的简单学习记录。

基础

import (
	"github.com/astaxie/beego"
	)
	func main() {	
	// now you start the beego as http server.	
	// it will listen to port 8080
	beego.Run()	
	
	// it will listen to 8080	
	// beego.Run("localhost")	
	
	// it will listen to 8089	
	// beego.Run(":8089")	
	
	// it will listen to 8089	
	// beego.Run("127.0.0.1:8089")}

Route

import (
	"github.com/astaxie/beego"
	)
	func main() {	
	
	ctrl := &MainController{}	
	
	// we register the path / to &MainController	
	// if we don't pass methodName as third param	
	// beego will use the default mappingMethods	
	// GET http://localhost:8080  -> Get()	
	// POST http://localhost:8080 -> Post()	
	// ...	
	beego.Router("/", ctrl)	
	
	// GET http://localhost:8080/health => ctrl.Health()	
	beego.Router("/health", ctrl, "get:Health")	
	
	// POST http://localhost:8080/update => ctrl.Update()	
	beego.Router("/update", ctrl, "post:Update")	
	
	// support multiple http methods.	
	// POST or GET http://localhost:8080/update => ctrl.GetOrPost()	
	beego.Router("/getOrPost", ctrl, "get,post:GetOrPost")	
	
	// support any http method	
	// POST, GET, PUT, DELETE... http://localhost:8080/update => ctrl.Any()	
	beego.Router("/any", ctrl, "*:Any")	
	
	beego.Run()
	}
	
	// MainController:
	// The controller must implement ControllerInterface
	// Usually we extends beego.Controllertype 
	
	MainController struct {
		beego.Controller
	}
	
	// address: http://localhost:8080 GET
	func (ctrl *MainController) Get()  {
	
		// beego-example/views/hello_world.html	
		ctrl.TplName = "hello_world.html"	
		ctrl.Data["name"] = "Get()"	
		
		// don't forget this	
		_ = ctrl.Render()
	}
	// GET http://localhost:8080/healthfunc (ctrl *MainController) Health()  {	
	// beego-example/views/hello_world.html	
	ctrl.TplName = "hello_world.html"	
	ctrl.Data["name"] = "Health()"	
	// don't forget this	
	_ = ctrl.Render()}
	// POST http://localhost:8080/update
	func (ctrl *MainController) Update()  {	
	// beego-example/views/hello_world.html	
	ctrl.TplName = "hello_world.html"	
	ctrl.Data["name"] = "Update()"	
	// don't forget this	
	_ = ctrl.Render()}
	// GET or POST http://localhost:8080/update
	func (ctrl *MainController) GetOrPost()  {	
	// beego-example/views/hello_world.html	
	ctrl.TplName = "hello_world.html"	
	ctrl.Data["name"] = "GetOrPost()"	
	// don't forget this	_ = ctrl.Render()}
	// any http method http://localhost:8080/any
	func (ctrl *MainController) Any()  {	
	// beego-example/views/hello_world.html	
	ctrl.TplName = "hello_world.html"	
	ctrl.Data["name"] = "Any()"	
	// don't forget this	
	_ = ctrl.Render()}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值