Beego-在线商城
商城架构
架构介绍
前端: React + redux后端: beego + MySQL
性能介绍
用户模块(登录、注册)商品模块(查看、类型筛选、点赞、评论)购物车(退出购物车、购物车结算)订单模块(查看订单、订单确认)商品秒杀缓存设计
环境筹备
设置环境变量
export GOPATH=/home/project/golangexport GOBIN=/home/project/golang/binexport PATH=$GOBIN:$PATH
装置beego&bee
go get github.com/astaxie/[email protected]go get github.com/beego/[email protected]
验证
$ bee Bee is a Fast and Flexible tool for managing your Beego Web Application.USAGE bee command [arguments]AVAILABLE COMMANDS version Prints the current Bee version migrate Runs database migrations api Creates a Beego API application bale Transforms non-Go files to Go source files fix Fixes your application by making it compatible with newer versions of Beego pro Source code generator dlv Start a debugging session using Delve dockerize Generates a Dockerfile for your Beego application generate Source code generator hprose Creates an RPC application based on Hprose and Beego frameworks new Creates a Beego application pack Compresses a Beego application into a single file rs Run customized scripts run Run the application by starting a local development server server serving static content over HTTP on port update Update BeeUse bee help [command] for more information about a command.ADDITIONAL HELP TOPICSUse bee help [topic] for more information about that topic.
疾速开始
$ bee new myproj$ cd myproj && bee run
beego框架简介
路由配置
一般路由
$ vi ./myproj/routers/router.go
package routersimport ( "myproj/controllers" "github.com/astaxie/beego")func init() { beego.Router("/", &controllers.MainController{}) beego.Router("/hello", &controllers.MainController{}) // 新增一条路由 /hello}
$ curl http://127.0.0.1:8080/hello <!DOCTYPE html><html><head> <title>Beego</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> ........
namespace 路由
// ./myproj/routers/router.gopackage routersimport ( "myproj/controllers" "github.com/astaxie/beego")func init() { beego.Router("/", &controllers.MainController{}) // 一般路由 beego.Router("/hello", &controllers.MainController{}) // namespace路由 ns := beego.NewNamespace("/api", beego.NSRouter("/hello1", &controllers.MainController{}), beego.NSRouter("/hello2", &controllers.MainController{}), ) beego.AddNamespace(ns)}
$ curl http://127.0.0.1:8080/api/hello1<!DOCTYPE html><html><head> <title>Beego</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> ........ $ curl http://127.0.0.1:8080/api/hello2<!DOCTYPE html><html><head> <title>Beego</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> ........
控制器函数
重写MainController的Get办法
// ./myproj/controllers/default.gopackage controllersimport ( "github.com/astaxie/beego")type MainController struct { beego.Controller}func (c *MainController) Get() { // c.Data["Website"] = "beego.me" // c.Data["Email"] = "[email protected]" // c.TplName = "index.tpl" ret := map[string]string{ "data": "hello golang!", } c.Data["json"] = ret c.ServeJSON()}
$ curl http://127.0.0.1:8080/hello{ "data": "hello golang!"}
ORM
根本流程
建设 Model 相干的 struct应用 orm.RegisterModel 注册定义的 Model应用 orm.RefisterDriver 函数注册数据库驱动应用 orm.RegisterDataBase 函数绑定相干的数据库应用 orm.Debug 开启 ORM 调试模式(开发过程能够关上,release 须要敞开)
建设Model
$ touch ./models/models.go
// models.go