Beego-在线商城

Beego-在线商城

商城架构

  • 架构介绍

    前端: React + redux
    后端: beego + MySQL
  • 功能介绍

    用户模块(登录、注册)
    商品模块(查看、类型筛选、点赞、评论)
    购物车(加入购物车、购物车结算)
    订单模块(查看订单、订单确认)
    商品秒杀
    缓存设计

环境准备

  • 设置环境变量

    export GOPATH=/home/project/golang
    export GOBIN=/home/project/golang/bin
    export 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 Bee
    
    Use bee help [command] for more information about a command.
    
    ADDITIONAL HELP TOPICS
    
    
    Use bee help [topic] for more information about that topic.
  • 快速开始

    $ bee new myproj
    $ cd myproj && bee run

beego框架简介

路由配置

  • 普通路由

    $ vi ./myproj/routers/router.go
    package routers
    
    import (
        "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       
    
    
    
    
      Beego
      
      ........
  • namespace 路由

    // ./myproj/routers/router.go
    package routers
    
    import (
        "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
    
    
    
    
      Beego
      
      ........
      
    $ curl http://127.0.0.1:8080/api/hello2
    
    
    
    
      Beego
      
      ........

控制器函数

  • 重写MainController的Get方法

    // ./myproj/controllers/default.go
    package controllers
    
    import (
        "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
    

你可能感兴趣的:(gobeego)