beego项目部署
bee工具安装和升级
export GO111MODULE=on
export GOPROXY=https://goproxy.cn
go install github.com/beego/bee/v2@latest
beego 必须是2.0以上的版本
go mod tidy
安装完之后,bee可执行文件默认存放在$GOPATH/bin里面,所以您需要把 $GOPATH/bin 添加到您的环境变量中
bee操作项目命令
bee new myproject
bee generate routers
bee fix
bee update
bee run
bee run和go run main.go有区别
前端项目部署
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@ddos conf.d]# vi default.conf server { listen 8000; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html/soms/dist; try_files $uri $uri/ @router; index index.html index.htm; } location @router { rewrite ^.*$ /index.html last; } location /v1 { proxy_pass http://127.0.0.1:8889; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@ddos conf.d]# vi suyuan.conf server { listen 8001; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html/suyuan/dist; try_files $uri $uri/ @router; index index.html index.htm; } location @router { rewrite ^.*$ /index.html last; } location /v1 { proxy_pass http://127.0.0.1:8800; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
web访问服务
实现http调用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import ( "context" "encoding/json" "errors" "fmt" "github.com/beego/beego/v2/core/logs" beego "github.com/beego/beego/v2/server/web" webcontext "github.com/beego/beego/v2/server/web/context" "suyuan/models" "io/ioutil" "net/http" "reflect" "strings" "time" "crypto/md5" "encoding/hex" )
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/* 原来系统的代码 apikey := beego.AppConfig.DefaultString("WBAPI", "") url := "https://api.threatbook.cn/v3/scene/ip_reputation?lang=zh&resource=" + w.Ipaddress + "&apikey=" + apikey resp, err := http.Get(url) */ /* 修改后的接口代码 1.删除apikey url参数 2.添加flag url参数 3.添加一个http header */ uid := webcontext.Context.Input.Session("_uid") user, err:= models.UserModel.GetById(uid.(int64)) if err != nil { return err } url := "https://api.threatbook.cn/v3/scene/ip_reputation?lang=zh&resource=" + w.Ipaddress+ "&flag=" + user.UserName tokenstr := "flag="+user.UserName+"AAAA" has := md5.New() // 创建md5算法 has.Write([]byte(tokenstr)) // 写入需要加密的数据 b := has.Sum(nil) // 获取hash值字符切片;Sum函数接受一个字符切片,这个切片的内容会原样的追加到abc123加密后的hash值的前面,这里我们不需要这么做,所以传入nil token := hex.EncodeToString(b)// 通过hex包的EncodeToString函数,将数据转为16进制字符串; 打印 e99a18c428cb38d5f260853678922e03 UpperTokenStr := strings.ToUpper(token) //发送http请求并携带自己的header //resp, err := http.Get(url) req, err := http.NewRequest("GET", url, nil) if err != nil { return err } req.Header.Add("token", UpperTokenStr) resp, err := http.DefaultClient.Do(req) defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return err } tm := make(map[string]interface{}) err = json.Unmarshal(body, &tm) if err != nil { return err } realData, ok := tm["data"] if !ok { err = errors.New(reflect.ValueOf(tm["verbose_msg"]).String()) return err }
项目问题排查
本文来自博客园,作者:不懂123,转载请注明原文链接:https://www.cnblogs.com/yxh168/p/17525672.html