Go framework-Beego

news/2024/11/18 2:30:13/

目录

一、Beego

1、创建Beego项目

1.1、命令行方式创建项目

1.2、使用bee工具

1、bee api,创建API应用

2、bee new 命令会自动创建beego 的go mod项目。

2、Admin管理后台


一、Beego

        Beego用于在Go中快速开发企业应用程序,包括RESTful API、web应用程序和后端服务。

Beego 源码地址

Beego 官方站点

Beego 官方说明

Beego的特性

  • RESTful支持
  • MVC架构
  • 模块化
  • 自动API文档
  • 注释路由
  • 命名空间
  • 开发工具集合
  • Full stack for Web & API

Beego的模块

  • orm,对象关系映射
  • session,会话
  • logs,日志
  • config,配置
  • cache,缓存

1、创建Beego项目

Go Web--Go Module

Beego 官方说明

1.1、命令行方式创建项目


D:\GO_workspace_web>md test_beegoD:\GO_workspace_web>cd test_beegoD:\GO_workspace_web\test_beego>go mod init test_beego
go: creating new go.mod: module test_beegoD:\GO_workspace_web\test_beego>

下载并安装beego/v2

D:\GO_workspace_web\test_beego>go get github.com/beego/beego/v2@latestD:\GO_workspace_web\test_beego>

下面使用GoLand软件打开新建的项目

编写main.go

package mainimport "github.com/beego/beego/v2/server/web"func main() {web.Run()
}

启动项目

 测试:http://localhost:8080/

 看到上面页面说明beego项目启动成功

1.2、使用bee工具

安装 Bee 工具

go get -u github.com/beego/bee/v2@latestD:\GO_workspace_web\test_beego>go get -u github.com/beego/bee/v2@latest
go: downloading github.com/beego/bee/v2 v2.1.0
go: downloading github.com/beego/bee v1.12.3
go: downloading gopkg.in/yaml.v2 v2.4.0
go: downloading github.com/fsnotify/fsnotify v1.4.9
go: downloading github.com/go-delve/delve v1.20.2
go: downloading github.com/gorilla/websocket v1.4.2
go: downloading github.com/beego/beego/v2 v2.1.0
go: downloading github.com/fsnotify/fsnotify v1.6.0
go: downloading github.com/gorilla/websocket v1.5.0
go: downloading github.com/go-delve/delve v1.21.0
go: downloading github.com/beego/beego v1.12.12
go: downloading github.com/beego/beego/v2 v2.1.1
go: downloading github.com/go-sql-driver/mysql v1.7.0
go: downloading github.com/lib/pq v1.10.5
go: downloading golang.org/x/tools v0.1.12
go: downloading github.com/flosch/pongo2 v0.0.0-20200529170236-5abacdfa4915
go: downloading github.com/smartwalle/pongo2render v1.0.1
go: downloading github.com/pelletier/go-toml v1.9.2
go: downloading github.com/spf13/viper v1.7.0
go: downloading github.com/spf13/viper v1.16.0
go: downloading golang.org/x/sys v0.6.0
go: downloading github.com/go-sql-driver/mysql v1.7.1
go: downloading github.com/pkg/errors v0.9.1
go: downloading github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18
go: downloading github.com/smartwalle/pongo2render v1.0.2
go: downloading github.com/lib/pq v1.10.9
go: downloading github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3
go: downloading github.com/hashicorp/golang-lru v0.5.4
go: downloading github.com/prometheus/client_golang v1.15.1
go: downloading golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd
go: downloading github.com/hashicorp/golang-lru v1.0.2
go: downloading github.com/shiena/ansicolor v0.0.0-20230509054315-a9deabde6e02
go: downloading github.com/prometheus/client_golang v1.16.0
go: downloading github.com/cosiner/argv v0.1.0
go: downloading github.com/derekparker/trie v0.0.0-20221213183930-4c74548207f4
go: downloading github.com/go-delve/liner v1.2.3-0.20220127212407-d32d89dd2a5d
go: downloading github.com/mattn/go-colorable v0.0.9
go: downloading github.com/derekparker/trie v0.0.0-20221221181808-1424fce0c981
go: downloading github.com/mattn/go-isatty v0.0.3
go: downloading github.com/mattn/go-colorable v0.1.13
go: downloading github.com/hashicorp/hcl v1.0.0
go: downloading github.com/magiconair/properties v1.8.1
go: downloading github.com/mitchellh/mapstructure v1.5.0
go: downloading github.com/spf13/afero v1.1.2
go: downloading github.com/spf13/cast v1.3.0
go: downloading github.com/spf13/jwalterweatherman v1.0.0
go: downloading github.com/magiconair/properties v1.8.7
go: downloading github.com/spf13/pflag v1.0.5
go: downloading github.com/spf13/afero v1.9.5
go: downloading github.com/spf13/cast v1.5.1
go: downloading github.com/subosito/gotenv v1.2.0
go: downloading github.com/spf13/jwalterweatherman v1.1.0
go: downloading gopkg.in/ini.v1 v1.51.0
go: downloading github.com/subosito/gotenv v1.6.0
go: downloading gopkg.in/ini.v1 v1.67.0
go: downloading golang.org/x/net v0.7.0
go: downloading github.com/prometheus/client_model v0.3.0
go: downloading github.com/prometheus/common v0.42.0
go: downloading github.com/prometheus/client_model v0.4.0
go: downloading github.com/prometheus/common v0.44.0
go: downloading golang.org/x/arch v0.0.0-20190927153633-4e8777c89be4
go: downloading github.com/sirupsen/logrus v1.6.0
go: downloading github.com/mattn/go-runewidth v0.0.13
go: downloading github.com/sirupsen/logrus v1.9.3
go: downloading go.starlark.net v0.0.0-20220816155156-cfacd8902214
go: downloading github.com/mattn/go-runewidth v0.0.15
go: downloading golang.org/x/text v0.7.0
go: downloading github.com/google/go-dap v0.7.0
go: downloading go.starlark.net v0.0.0-20230814145427-12f4cb8177e4
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/cespare/xxhash/v2 v2.2.0
go: downloading github.com/prometheus/procfs v0.9.0
go: downloading golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4
go: downloading github.com/cespare/xxhash v1.1.0
go: downloading github.com/golang/protobuf v1.5.3
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.4
go: downloading github.com/google/go-dap v0.10.0
go: downloading github.com/prometheus/procfs v0.11.1
go: downloading github.com/cilium/ebpf v0.7.0
go: downloading github.com/konsorten/go-windows-terminal-sequences v1.0.3
go: downloading github.com/rivo/uniseg v0.2.0
go: downloading github.com/rivo/uniseg v0.4.4
go: downloading github.com/cilium/ebpf v0.11.0
go: downloading github.com/flosch/pongo2/v6 v6.0.0
go: downloading golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2
go: downloading golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
go: added github.com/beego/bee/v2 v2.1.0
go: added github.com/beego/beego/v2 v2.1.1
go: added github.com/beorn7/perks v1.0.1
go: added github.com/cespare/xxhash/v2 v2.2.0
go: added github.com/cilium/ebpf v0.11.0
go: added github.com/cosiner/argv v0.1.0
go: added github.com/davecgh/go-spew v1.1.1
go: added github.com/derekparker/trie v0.0.0-20221221181808-1424fce0c981
go: added github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3
go: added github.com/flosch/pongo2/v6 v6.0.0
go: added github.com/fsnotify/fsnotify v1.6.0
go: added github.com/go-delve/delve v1.21.0
go: added github.com/go-delve/liner v1.2.3-0.20220127212407-d32d89dd2a5d
go: added github.com/go-sql-driver/mysql v1.7.1
go: added github.com/golang/protobuf v1.5.3
go: added github.com/google/go-dap v0.10.0
go: added github.com/gorilla/websocket v1.5.0
go: added github.com/hashicorp/golang-lru v1.0.2
go: added github.com/hashicorp/hcl v1.0.0
go: added github.com/konsorten/go-windows-terminal-sequences v1.0.3
go: added github.com/lib/pq v1.10.9
go: added github.com/magiconair/properties v1.8.7
go: added github.com/mattn/go-colorable v0.1.13
go: added github.com/mattn/go-isatty v0.0.19
go: added github.com/mattn/go-runewidth v0.0.15
go: added github.com/matttproud/golang_protobuf_extensions v1.0.4
go: added github.com/mitchellh/mapstructure v1.5.0
go: added github.com/pelletier/go-toml v1.9.5
go: added github.com/pelletier/go-toml/v2 v2.0.9
go: added github.com/pkg/errors v0.9.1
go: added github.com/prometheus/client_golang v1.16.0
go: added github.com/prometheus/client_model v0.4.0
go: added github.com/prometheus/common v0.44.0
go: added github.com/prometheus/procfs v0.11.1
go: added github.com/rivo/uniseg v0.4.4
go: added github.com/shiena/ansicolor v0.0.0-20230509054315-a9deabde6e02
go: added github.com/sirupsen/logrus v1.9.3
go: added github.com/smartwalle/pongo2render v1.0.2
go: added github.com/spf13/afero v1.9.5
go: added github.com/spf13/cast v1.5.1
go: added github.com/spf13/jwalterweatherman v1.1.0
go: added github.com/spf13/pflag v1.0.5
go: added github.com/spf13/viper v1.16.0
go: added github.com/subosito/gotenv v1.6.0
go: added go.starlark.net v0.0.0-20230814145427-12f4cb8177e4
go: added golang.org/x/arch v0.4.0
go: added golang.org/x/crypto v0.12.0
go: added golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
go: added golang.org/x/mod v0.12.0
go: added golang.org/x/net v0.14.0
go: added golang.org/x/sys v0.11.0
go: added golang.org/x/text v0.12.0
go: added golang.org/x/tools v0.12.0
go: added google.golang.org/protobuf v1.31.0
go: added gopkg.in/ini.v1 v1.67.0
go: added gopkg.in/yaml.v2 v2.4.0
go: added gopkg.in/yaml.v3 v3.0.1D:\GO_workspace_web\test_beego>

在bee命令安装:$GOPATH/bin中,保证$GOPATH/bin位于系统环境变量path中

D:\GO_workspace_web\test_beego>go env GOPATH
C:\Users\Administrator\go

For Go 1.18 and newer Go versions run:

go install github.com/beego/bee/v2@latest

And then:

bee versionD:\GO_workspace_web\test_beego>bee version
2023/08/17 00:10:34.189 [D]  init global config instance failed. If you do not use this, just ignore it.  open conf/app.conf: The system cannot find the path specified.
2023/08/17 00:10:34 INFO     ▶ 0001 Getting bee latest version...
2023/08/17 00:10:36 INFO     ▶ 0002 Your bee are up to date
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v2.1.0├── GoVersion : go1.20.6
├── GOOS      : windows
├── GOARCH    : amd64
├── NumCPU    : 8
├── GOPATH    : C:\Users\Administrator\go
├── GOROOT    : C:\Program Files\Go
├── Compiler  : gc
└── Date      : Thursday, 17 Aug 2023D:\GO_workspace_web\test_beego>

使用bee工具创建项目

1、bee api,创建API应用

D:\GO_workspace_web\test_beego>cd ..D:\GO_workspace_web>bee api test_beego1
2023/08/17 00:21:38.410 [D]  init global config instance failed. If you do not use this, just ignore it.  open conf/app.conf: The system cannot find the path specified.
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v2.1.0
2023/08/17 00:21:38 INFO     ▶ 0001 Generate api project support go modules.
2023/08/17 00:21:38 INFO     ▶ 0002 Creating API...create   D:\GO_workspace_web\test_beego1\go.modcreate   D:\GO_workspace_web\test_beego1create   D:\GO_workspace_web\test_beego1\confcreate   D:\GO_workspace_web\test_beego1\controllerscreate   D:\GO_workspace_web\test_beego1\testscreate   D:\GO_workspace_web\test_beego1\conf\app.confcreate   D:\GO_workspace_web\test_beego1\modelscreate   D:\GO_workspace_web\test_beego1\routers\create   D:\GO_workspace_web\test_beego1\controllers\object.gocreate   D:\GO_workspace_web\test_beego1\controllers\user.gocreate   D:\GO_workspace_web\test_beego1\tests\default_test.gocreate   D:\GO_workspace_web\test_beego1\routers\router.gocreate   D:\GO_workspace_web\test_beego1\models\object.gocreate   D:\GO_workspace_web\test_beego1\models\user.gocreate   D:\GO_workspace_web\test_beego1\main.go
2023/08/17 00:21:38 SUCCESS  ▶ 0003 New API successfully created!D:\GO_workspace_web>

与 web 相比,不需要 html、css、js这些前端资源的响应,仅仅是业务逻辑,和数据的响应。是我们需要的典型的API功能!

2、bee new 命令会自动创建beego 的go mod项目

同时会初始化基础的目录结构!

D:\GO_workspace_web>bee new test_beego2
2023/08/17 00:24:51.524 [D]  init global config instance failed. If you do not use this, just ignore it.  open conf/app.conf: The system cannot find the path specified.
2023/08/17 00:24:51 INFO     ▶ 0001 Generate new project support go modules.
2023/08/17 00:24:51 INFO     ▶ 0002 Creating application...create   D:\GO_workspace_web\test_beego2\go.modcreate   D:\GO_workspace_web\test_beego2\create   D:\GO_workspace_web\test_beego2\conf\create   D:\GO_workspace_web\test_beego2\controllers\create   D:\GO_workspace_web\test_beego2\models\create   D:\GO_workspace_web\test_beego2\routers\create   D:\GO_workspace_web\test_beego2\tests\create   D:\GO_workspace_web\test_beego2\static\create   D:\GO_workspace_web\test_beego2\static\js\create   D:\GO_workspace_web\test_beego2\static\css\create   D:\GO_workspace_web\test_beego2\static\img\create   D:\GO_workspace_web\test_beego2\views\create   D:\GO_workspace_web\test_beego2\conf\app.confcreate   D:\GO_workspace_web\test_beego2\controllers\default.gocreate   D:\GO_workspace_web\test_beego2\views\index.tplcreate   D:\GO_workspace_web\test_beego2\routers\router.gocreate   D:\GO_workspace_web\test_beego2\tests\default_test.gocreate   D:\GO_workspace_web\test_beego2\main.go
2023/08/17 00:24:52 SUCCESS  ▶ 0003 New application successfully created!D:\GO_workspace_web>

web项目,比 api项目增加 views(html模板)和static(css,image,js)静态资源相关给目录。

运行应用前,保证全部的包被安装:

D:\GO_workspace_web\test_beego2>go mod tidy
go: downloading github.com/smartystreets/goconvey v1.6.4
go: downloading github.com/elazarl/go-bindata-assetfs v1.0.1
go: downloading github.com/stretchr/testify v1.8.1
go: downloading github.com/jtolds/gls v4.20.0+incompatible
go: downloading github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d
go: downloading github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1
go: downloading github.com/google/go-cmp v0.5.9
go: downloading gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
go: downloading github.com/kr/pretty v0.3.1
go: downloading github.com/rogpeppe/go-internal v1.10.0
go: finding module for package github.com/kr/text
go: downloading github.com/kr/text v0.2.0
go: found github.com/kr/text in github.com/kr/text v0.2.0

bee run 运行应用

D:\GO_workspace_web\test_beego2>bee run
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v2.1.0
2023/08/17 23:53:12 WARN     ▶ 0001 Running application outside of GOPATH
2023/08/17 23:53:12 INFO     ▶ 0002 Using 'test_beego2' as 'appname'
2023/08/17 23:53:12 INFO     ▶ 0003 Initializing watcher...
test_beego2/controllers
test_beego2/routers
test_beego2
2023/08/17 23:53:18 SUCCESS  ▶ 0004 Built Successfully!
2023/08/17 23:53:18 INFO     ▶ 0005 Restarting 'test_beego2.exe'...
2023/08/17 23:53:18 SUCCESS  ▶ 0006 './test_beego2.exe' is running...
2023/08/17 23:53:19.472 [I] [server.go:281]  http server Running on http://:8080

bee run 命令会利用 fsnotify 监控文件系统,若文件改动时,可以实时重新编译运行。

2、Admin管理后台

Beego 内置了管理后台,用于监视和统计应用信息,默认关闭。通过配置控制:

修改main.go文件

package mainimport (beego "github.com/beego/beego/v2/server/web"_ "test_beego2/routers"
)func main() {// 是否开启Adminbeego.BConfig.Listen.EnableAdmin = true// Admin监听的地址和端口beego.BConfig.Listen.AdminAddr = "localhost"beego.BConfig.Listen.AdminPort = 8088beego.Run()
}

访问:http://localhost:8088/

支持:
Requests statistics, 请求统计信息
性能分析

  • lookup goroutine,goroutine 信息
  • lookup heap,堆栈信息
  • lookup threadcreate,线程创建信息
  • lookup block,阻塞信息
  • get cpuprof,CPU的pprof
  • get memprof,Memory的pprof
  • gc summary,GC 概要

健康检查
任务
配置状态

  • configs, 配置
  • routers, 路由
  • filters, 过滤器

Web framework-Gin

人必须要成长!

如今一道为尊,万道相和,几乎很难破。最可怕的是苍穹!


http://www.ppmy.cn/news/1041535.html

相关文章

Leetcode32 最长有效括号

给你一个只包含 ( 和 ) 的字符串&#xff0c;找出最长有效&#xff08;格式正确且连续&#xff09;括号子串的长度。 代码如下&#xff1a; class Solution {public int longestValidParentheses(String str) {Stack<Integer> s new Stack<>();int res 0;int st…

3D数据转换工具HOOPS Exchange概览

HOOPS Exchange SDK是一组C软件库&#xff0c;使开发团队能够快速为其应用程序添加可靠的2D和3D CAD导入和导出功能。这允许访问广泛的数据&#xff0c;包括边界表示&#xff08;BREP&#xff09;、产品制造信息&#xff08;PMI&#xff09;、模型树、视图、持久ID、样式、构造…

微服务观测性提升专项梳理

文章目录 项目背景&#xff1a;项目目标&#xff1a;专项人员关键问题及风险APM 进展 项目背景&#xff1a; 随着微服务架构的普及&#xff0c;构建和管理大规模的分布式系统变得越来越复杂。为了确保这些系统的可靠性和性能&#xff0c;以及快速排除故障&#xff0c;对微服务…

【脚踢数据结构】图(纯享版)

(꒪ꇴ꒪ )&#xff0c;Hello我是祐言QAQ我的博客主页&#xff1a;C/C语言&#xff0c;Linux基础&#xff0c;ARM开发板&#xff0c;软件配置等领域博主&#x1f30d;快上&#x1f698;&#xff0c;一起学习&#xff0c;让我们成为一个强大的攻城狮&#xff01;送给自己和读者的…

数据结构--拓扑排序

数据结构–拓扑排序 AOV⽹ A O V ⽹ \color{red}AOV⽹ AOV⽹(Activity On Vertex NetWork&#xff0c;⽤顶点表示活动的⽹)&#xff1a; ⽤ D A G 图 \color{red}DAG图 DAG图&#xff08;有向⽆环图&#xff09;表示⼀个⼯程。顶点表示活动&#xff0c;有向边 < V i , V j …

k8s容器加入host解析字段

一、通过edit或path来修改 kubectl edit deploy /xxxxx. x-n cattle-system xxxxx为你的资源对象名称 二、添加字段 三、code hostAliases:- hostnames:- www.rancher.localip: 10.10.2.180

css3阴影效果

首先效果如下&#xff1a; 阴影效果完整代码如下 上面的动态图是没有加transition的&#xff0c;为了美观加上了一个 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content&…

面试题-React(二):React中的虚拟DOM是什么?

一、什么是虚拟DOM&#xff1f; 虚拟DOM是React的核心概念之一&#xff0c;它是一个轻量级的JavaScript对象树&#xff0c;用于表示真实DOM的状态。在React中&#xff0c;当数据发生变化时&#xff0c;首先会在虚拟DOM上执行DOM更新&#xff0c;而不是直接操作真实DOM。然后&a…