我已经开始学习beego。我创建了一个我从有角度的前端调用的API。 为了克服前端中的CORS问题,我在main.go文件
中使用了以下代码package main
import (
_ "gateway/routers"
"github.com/astaxie/beego"
"github.com/astaxie/beego/plugins/cors"
)
func main() {
// Set CORS option for front-end applications
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowOrigins: []string{"*"},
AllowMethods: []string{"OPTIONS","PUT", "PATCH", "POST", "GET", },
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))
if beego.BConfig.RunMode == "dev" {
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
beego.Run()
}
现在我有以下路由器:
package routers
import (
"gateway/controllers"
"github.com/astaxie/beego"
)
func init() {
ns := beego.NewNamespace("/v1",
beego.NSNamespace("/info/host",
beego.NSInclude(&controllers.HostController{})),
beego.NSNamespace("/info/ifaces",
beego.NSInclude(&controllers.NetworkController{})),
beego.NSNamespace("/info/cpu",
beego.NSInclude(&controllers.CpuController{})),
beego.NSNamespace("/info/disk",
beego.NSInclude(&controllers.DiskController{})),
beego.NSNamespace("/info/memory",
beego.NSInclude(&controllers.MemoryController{})),
beego.NSNamespace("/info/users",
beego.NSInclude(&controllers.UserController{})),
)
beego.AddNamespace(ns)
}
如果我尝试在“ / v1 / info / ifaces”处执行get请求,我会从相应的控制器获得响应。但是,如果我发布帖子或遇到相同的错误,则会得到以下提示:
::1| 200 | 41.587µs| nomatch| OPTIONS /v1/info/ifaces
有任何想法为什么会这样?
这也是我的NetworkController
package controllers
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego"
"github.com/shirou/gopsutil/net"
)
// NetworkController operations for Network
type NetworkController struct {
beego.Controller
}
// URLMapping ...
func (c *NetworkController) URLMapping() {
c.Mapping("Post", c.Post)
c.Mapping("GetOne", c.GetOne)
c.Mapping("GetAll", c.GetAll)
c.Mapping("Put", c.Put)
c.Mapping("Delete", c.Delete)
}
// Post ...
// @Title Create
// @Description create Network
// @Param body body models.Network true "body for Network content"
// @Success 201 {object} models.Network
// @Failure 403 body is empty
// @router / [post]
func (c *NetworkController) Post() {
fmt.Println("Posting")
c.ServeJSON()
}
// GetOne ...
// @Title GetOne
// @Description get Network by id
// @Param id path string true "The key for staticblock"
// @Success 200 {object} models.Network
// @Failure 403 :id is empty
// @router /:id [get]
func (c *NetworkController) GetOne() {
}
// GetAll ...
// @Title GetAll
// @Description Retrieve network interfaces of gateway
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
// @Param fields query string false "Fields returned. e.g. col1,col2 ..."
// @Param sortby query string false "Sorted-by fields. e.g. col1,col2 ..."
// @Param order query string false "Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param limit query string false "Limit the size of result set. Must be an integer"
// @Param offset query string false "Start position of result set. Must be an integer"
// @Success 200 {object} models.Network
// @Failure 403
// @router / [get]
func (c *NetworkController) GetAll() {
info, _ := net.Interfaces()
c.Data["json"] = &info
c.ServeJSON()
}
// Put ...
// @Title Put
// @Description update the Network
// @Param id path string true "The id you want to update"
// @Param body body models.Network true "body for Network content"
// @Success 200 {object} models.Network
// @Failure 403 :id is not int
// @router /:id [put,options]
func (c *NetworkController) Put() {
var iface net.InterfaceStat
_ = json.Unmarshal(c.Ctx.Input.RequestBody,&iface)
fmt.Println(iface.Name)
}
// Delete ...
// @Title Delete
// @Description delete the Network
// @Param id path string true "The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *NetworkController) Delete() {
}