关于go:Beego在线商城

40次阅读

共计 2877 个字符,预计需要花费 8 分钟才能阅读完成。

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       
    <!DOCTYPE html>
    
    <html>
    <head>
      <title>Beego</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      ........
  • 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
    <!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.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
    

正文完
 0