From 62dd718f0f870c40ce77a75181bc848fe0f14366 Mon Sep 17 00:00:00 2001 From: Roland CW- Date: Sun, 3 Dec 2023 14:00:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=86=85=E5=AE=B9=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 6 +++--- go.mod | 29 ++++++++++++----------------- initialize/conf/conf.go | 4 ++-- initialize/mysql/mysql.go | 2 +- initialize/session/session.go | 2 +- main.go | 21 +++++++++++++++------ routers/router.go | 2 +- utils/template/template.go | 4 ++-- 8 files changed, 37 insertions(+), 33 deletions(-) diff --git a/config.yaml b/config.yaml index 93368f9..939263d 100644 --- a/config.yaml +++ b/config.yaml @@ -3,15 +3,15 @@ redis: db: 0 addr: '127.0.0.1:6379' - password: '' + password: '123456' # mysql connect configuration mysql: host: '127.0.0.1' port: 3306 - database: 'beego-admin' + database: 'beego_admin' username: 'root' - password: 'root' + password: '123456' # other configuration other: diff --git a/go.mod b/go.mod index 1c9d9ca..c495a51 100644 --- a/go.mod +++ b/go.mod @@ -2,40 +2,35 @@ module beego-admin go 1.15 -require github.com/astaxie/beego v1.12.1 - require ( github.com/360EntSecGroup-Skylar/excelize v1.4.1 github.com/adam-hanna/arrayOperations v0.2.6 - github.com/beego/bee/v2 v2.0.2 // indirect github.com/beego/beego/v2 v2.0.2-0.20210205153711-4861b1f69239 + github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f - github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3 // indirect github.com/fsnotify/fsnotify v1.4.9 - github.com/go-delve/delve v1.6.0 // indirect github.com/go-sql-driver/mysql v1.5.0 + github.com/golang/protobuf v1.5.3 // indirect github.com/google/uuid v1.1.2 - github.com/gookit/validate v1.2.4 + github.com/gookit/goutil v0.6.14 // indirect + github.com/gookit/validate v1.5.1 github.com/lib/pq v1.9.0 // indirect github.com/magiconair/properties v1.8.4 // indirect - github.com/mattn/go-colorable v0.1.8 // indirect - github.com/mattn/go-runewidth v0.0.10 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/mitchellh/mapstructure v1.4.1 // indirect - github.com/peterh/liner v1.2.1 // indirect - github.com/rivo/uniseg v0.2.0 // indirect + github.com/prometheus/client_model v0.5.0 // indirect + github.com/prometheus/procfs v0.12.0 // indirect github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect - github.com/sirupsen/logrus v1.7.0 // indirect github.com/smartystreets/goconvey v1.6.4 github.com/spf13/afero v1.5.1 // indirect github.com/spf13/cast v1.3.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.7.1 - go.starlark.net v0.0.0-20210126161401-bc864be25151 // indirect - golang.org/x/arch v0.0.0-20210127225635-455c95562d18 // indirect - golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 - golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect - golang.org/x/text v0.3.5 // indirect - gopkg.in/ini.v1 v1.62.0 + golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect + gopkg.in/ini.v1 v1.62.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/initialize/conf/conf.go b/initialize/conf/conf.go index ffbccff..9bdebb5 100644 --- a/initialize/conf/conf.go +++ b/initialize/conf/conf.go @@ -9,8 +9,8 @@ import ( "os" ) -//加载配置文件 -func init() { +// 加载配置文件 +func InitConfig() { var config string if configEnv := os.Getenv(utils.ConfigEnv); configEnv == "" { config = utils.ConfigFile diff --git a/initialize/mysql/mysql.go b/initialize/mysql/mysql.go index 0a3c981..1c3108a 100644 --- a/initialize/mysql/mysql.go +++ b/initialize/mysql/mysql.go @@ -10,7 +10,7 @@ import ( ) // init 注册mysql -func init() { +func InitDb() { err := orm.RegisterDriver("mysql", orm.DRMySQL) if err != nil { beego.Error("mysql register driver error:", err) diff --git a/initialize/session/session.go b/initialize/session/session.go index 7a0124a..cb02491 100644 --- a/initialize/session/session.go +++ b/initialize/session/session.go @@ -2,7 +2,7 @@ package session import beego "github.com/beego/beego/v2/adapter" -func init() { +func InitSession() { //session 过期时间,默认值是 3600 秒 beego.BConfig.WebConfig.Session.SessionGCMaxLifetime = 7200 diff --git a/main.go b/main.go index 72fd1bd..02798b8 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,25 @@ package main import ( - _ "beego-admin/initialize/conf" - _ "beego-admin/initialize/mysql" - _ "beego-admin/initialize/session" - _ "beego-admin/routers" - _ "beego-admin/utils/template" + "beego-admin/initialize/conf" + "beego-admin/initialize/mysql" + "beego-admin/initialize/session" + "beego-admin/routers" + "beego-admin/utils/template" beego "github.com/beego/beego/v2/adapter" ) func main() { - + //初始化配置文件 + conf.InitConfig() + //初始化数据库 + mysql.InitDb() + //初始化session + session.InitSession() + //初始化router + routers.InitRouter() + //初始化自定义模版 + template.InitTtempate() //输出文件名和行号 beego.SetLogFuncCall(true) diff --git a/routers/router.go b/routers/router.go index b3bd89c..995f14b 100644 --- a/routers/router.go +++ b/routers/router.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func init() { +func InitRouter() { //授权登录中间件 middleware.AuthMiddle() diff --git a/utils/template/template.go b/utils/template/template.go index 2c73ec1..759f47f 100644 --- a/utils/template/template.go +++ b/utils/template/template.go @@ -1,4 +1,4 @@ -//Package template 自定义模板函数 +// Package template 自定义模板函数 package template import ( @@ -8,7 +8,7 @@ import ( "time" ) -func init() { +func InitTtempate() { beego.AddFuncMap("UnixTimeForFormat", UnixTimeForFormat) beego.AddFuncMap("FormatSize", FormatSize) } From 941264044257a18d6d91fedef266b68c05ac8bc3 Mon Sep 17 00:00:00 2001 From: geekercc168 Date: Sun, 17 Dec 2023 22:58:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E7=83=AD?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6.air.tome?= =?UTF-8?q?=20=20=E5=A2=9E=E5=8A=A0=E4=BA=86=E9=95=9C=E5=83=8F=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E6=96=87=E4=BB=B6Dockerfile=20,=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E6=B5=81=E6=B0=B4=E7=BA=BF=E6=96=87=E4=BB=B6Jenkinsfi?= =?UTF-8?q?le?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .air.toml | 75 ++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 31 +++++++++++++++++++ Jenkinsfile | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 .air.toml create mode 100644 Dockerfile create mode 100644 Jenkinsfile diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..ecfed7e --- /dev/null +++ b/.air.toml @@ -0,0 +1,75 @@ +# Config file for [Air](https://github.com/cosmtrek/air) in TOML format + +# Working directory +# . or absolute path, please note that the directories following must be under root. +root = "." +tmp_dir = "tmp" + +[build] +# Array of commands to run before each build +#pre_cmd = ["echo 'hello air' > pre_cmd.txt"] +# Just plain old shell command. You could use `make` as well. +cmd = "go build -o ./tmp/main ." +# Array of commands to run after ^C +#post_cmd = ["echo 'hello air' > post_cmd.txt"] +# Binary file yields from `cmd`. +bin = "tmp/main" +# Customize binary, can setup environment variables when run your app. +full_bin = "APP_ENV=dev APP_USER=air ./tmp/main" +# Watch these filename extensions. +include_ext = ["go", "tpl", "tmpl", "html", "conf", "yaml", "tmp"] +# Ignore these filename extensions or directories. +exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules",".git"] +# Watch these directories if you specified. +include_dir = ["conf","controllers","formvalidate","global","initialize","middleware","models","routers","services","static","tests","utils","views"] +# Watch these files. +include_file = ["config.yaml","main.go","app.conf"] +# Exclude files. +#exclude_file = [] +# Exclude specific regular expressions. +#exclude_regex = ["_test\\.go"] +# Exclude unchanged files. +exclude_unchanged = true +# Follow symlink for directories +follow_symlink = true +# This log file places in your tmp_dir. +log = "air.log" +# Poll files for changes instead of using fsnotify. +poll = false +# Poll interval (defaults to the minimum interval of 500ms). +poll_interval = 500 # ms +# It's not necessary to trigger build each time file changes if it's too frequent. +delay = 0 # ms +# Stop running old binary when build errors occur. +stop_on_error = true +# Send Interrupt signal before killing process (windows does not support this feature) +send_interrupt = false +# Delay after sending Interrupt signal +kill_delay = 500 # nanosecond +# Rerun binary or not +rerun = false +# Delay after each executions +rerun_delay = 500 +# Add additional arguments when running binary (bin/full_bin). Will run './tmp/main hello world'. +#args_bin = ["hello", "world"] + +[log] +# Show log time +time = false +# Only show main log (silences watcher, build, runner) +main_only = false + +[color] +# Customize each part's color. If no color found, use the raw app log. +main = "magenta" +watcher = "cyan" +build = "yellow" +runner = "green" + +[misc] +# Delete tmp directory on exit +clean_on_exit = true + +[screen] +clear_on_rebuild = true +keep_scroll = true diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c34e23e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +FROM docker.io/centos + +MAINTAINER AUTHOR'S LUODI FROM RODDYPY.COM QQ:923401910 + +WORKDIR /usr/local/src/ + +COPY pcre-8.37.tar.gz . +COPY nginx-1.8.0.tar.gz . + +#add user www +RUN useradd -s /sbin/nologin www && yum install gcc gcc-c++ openssl openssl-devel -y + +RUN tar zxf pcre-8.37.tar.gz && cd pcre-8.37 && ./configure --prefix=/usr/local/pcre && make && make install + +RUN tar zxf nginx-1.8.0.tar.gz && cd nginx-1.8.0 && ./configure --prefix=/usr/local/nginx \ +--user=www \ +--group=www \ +--with-http_ssl_module \ +--with-http_stub_status_module \ +--with-file-aio \ +--with-http_dav_module \ +--with-pcre=/usr/local/src/pcre-8.37 && make && make install && chown -R www. /usr/local/nginx + +COPY nginx.conf /usr/local/nginx/conf/nginx.conf +ADD vhosts /usr/local/nginx/conf/vhosts +ADD run.sh /root/run.sh +RUN chmod 755 /root/run.sh + +CMD ["/root/run.sh"] + +EXPOSE 80 443 diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..f802c25 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,89 @@ +pipeline { + agent any + + tools { + go 'Go1.21.5' + } + stages { + stage('[checkout]拉取代码中....') { + steps { + // 使用 checkout 步骤拉取代码 + checkout([$class: 'GitSCM', branches: [[name: '*/main']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'b4c9eb45-0344-409f-849a-2cdd091749ee', url: 'http://113.250.161.105:8180/geeker_yuyu/beego-admin.git']],destination: 'beego-admin']) + echo '拉取代码成功' + } + } + stage('[build]构建中....') { + steps { + // 构建 Golang 项目 + script { + // 使用 Golang 工具 + def gopath = tool 'Go1.21.5' + env.GOPATH = "${WORKSPACE}/gopath" + env.PATH = "${gopath}/bin:${env.PATH}" + + // 构建命令 + sh 'go build -o beego-admin .' + + echo '构建完成' + } + } + } + stage('[test]发送软件到测试服务器....') { + steps { + script { + // 测试阶段的构建命令和其他操作 + + // 使用 SSH 将生成的可执行文件和 Dockerfile 发送到测试服务器 + sshPublisher( + failOnError: true, + publishers: [ + sshPublisherDesc( + configName: 'b4c9eb45-0344-409f-849a-2cdd091749ee', // 替换为你的 SSH 凭据的 ID + transfers: [ + sshTransfer( + sourceFiles: 'beego-admin ./Dockerfile', // 本地构建好的二进制文件和 Dockerfile 路径 + removePrefix: '', + remoteDirectory: '/opt/deployment' // 远程测试服务器的目录 + ) + ] + ) + ] + ) + + echo '发送软件到测试服务器' + } + } + } + stage('[deploy]部署中....') { + steps { + script { + def remote = [:] + remote.name = 'gitlab-jenkins-beego-admin' // 服务器名字,提前配置好的 + remote.host = '113.250.161.105' // 远程ip,待部署的服务器ip + remote.port = 22 + remote.allowAnyHosts = true + + withCredentials([usernamePassword(credentialsId: 'db4cb5f7-08fb-465a-baee-3a868f9d69e2', passwordVariable: 'Password', usernameVariable: 'Username')]) { + remote.user = "${Username}" + remote.password = "${Password}" + } + + writeFile file: 'abc.sh', text: 'docker stop demo;docker container rm demo;docker rmi demo;chmod +x /opt/deployment/beego-admin;cd /opt/deployment;docker build -t demo .;docker run --name demo -d -p 7654:7654 demo' + sshScript remote: remote, script: 'abc.sh' + } + + } + } + } + + post { + success { + // 构建成功时执行的操作 + echo 'Build and deployment successful!' + } + failure { + // 构建失败时执行的操作 + echo 'Build or deployment failed!' + } + } +}