golang 使用golang构建Web应用程序的字段验证实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 使用golang构建Web应用程序的字段验证实例相关的知识,希望对你有一定的参考价值。

package verify

import (
	"regexp"
)

// 验证年龄是为数字
func Is_age(s string) bool {
	if m, _ := regexp.MatchString("^[0-9]+$", s); !m {
		return false
	}
	return true
}

// 验证中文
func Is_ch(s string) bool {
	if m, _ := regexp.MatchString("^[\\x{4e00}-\\x{9fa5}]+$", s); !m {
		return false
	}
	return true
}

// 验证英文
func Is_en(s string) bool {
	if m, _ := regexp.MatchString("^[a-zA-Z]+$", s); !m {
		return false
	}
	return true
}

// 验证email
func Is_email(s string) bool {
	if m, _ := regexp.MatchString(`^([\w\.\_]{2,10})@(\w{1,}).([a-z]{2,4})$`, s); !m {
		return false
	} else {
		return true
	}
}

// 验证手机号码
func Is_phone(s string) bool {
	if m, _ := regexp.MatchString(`^(1[3|4|5|8][0-9]\d{4,8})$`, s); !m {
		return false
	}
	return true
}

// 验证身份证号码
func Is_usercard(s string) bool {
	//验证15位身份证,15位的是全部数字
	if m, _ := regexp.MatchString(`^(\d{15})$`, s); !m {
		return false
	}

	//验证18位身份证,18位前17位为数字,最后一位是校验位,可能为数字或字符X。
	if m, _ := regexp.MatchString(`^(\d{17})([0-9]|X)$`, s); !m {
		return false
	}

	return true
}

62-CICD持续集成工具-Jenkins构建Golang的web项目

实现 Golang 应用源码编译并部署

  • 安装 Golang 环境

#编译安装

[root@jenkins ~]#cat install_go.sh
#!/bin/bash

GO_VERSION=1.18.4
URL=https://studygolang.com/dl/golang/go$GO_VERSION.linux-amd64.tar.gz
#URL=https://golang.google.cn/dl/go$GO_VERSION.linux-amd64.tar.gz

INSTALL_DIR=/usr/local
GOPATH_DIR=/opt/go
. /etc/os-release

color ()
RES_COL=60
MOVE_TO_COL="echo -en \\\\033[$RES_COLG"
SETCOLOR_SUCCESS="echo -en \\\\033[1;32m"
SETCOLOR_FAILURE="echo -en \\\\033[1;31m"
SETCOLOR_WARNING="echo -en \\\\033[1;33m"
SETCOLOR_NORMAL="echo -en \\E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
$SETCOLOR_SUCCESS
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ] ;then
$SETCOLOR_FAILURE
echo -n $"FAILED"
else
$SETCOLOR_WARNING
echo -n $"WARNING"
fi
$SETCOLOR_NORMAL
echo -n "]"
echo



install_go ()
if ! [[ $ID_LIKE =~ debian|rhel ]];then
color "不支持此操作系统,退出!" 1
exit
fi
if [ ! -f go$GO_VERSION.linux-amd64.tar.gz ] ;then
wget $URL
fi

[ ! -d $INSTALL_DIR ] && mkdir -pv $INSTALL_DIR
tar xf go$GO_VERSION.linux-amd64.tar.gz -C $INSTALL_DIR || color "Go 解压缩失败!" 1;exit;
cat > /etc/profile.d/go.sh <<EOF
export GOROOT=$INSTALL_DIR/go
export PATH=$PATH:\\$GOROOT/bin
EOF
. /etc/profile.d/go.sh
ln -s $INSTALL_DIR/go/bin/* /usr/local/bin/
go version
if [ $? -eq 0 ] ;then
color "Golang 安装成功!" 0
else
color "Golang 安装失败!" 1
exit 1
fi


config_go()
cat >> /etc/profile.d/go.sh <<EOF
export GOPATH=$GOPATH_DIR
EOF
[ ! -d $GOPATH_DIR ] && mkdir -pv $GOPATH_DIR/src
. /etc/profile.d/go.sh
go env -w GOPROXY=https://goproxy.cn,direct


hello_go ()
mkdir -pv $GOPATH_DIR/src/hello
cd $GOPATH_DIR/src/hello
cat > hello.go <<EOF
package main

import "fmt"

func main()
fmt.Println("Hello, world!")

EOF
go mod init
go build
./hello


install_go
config_go
hello_go
exec bash

[root@jenkins ~]#go version
go version go1.18.4 linux/amd64

[root@web01 hello]#go version
go version go1.18.4 linux/amd64

[root@web02 hello]#go version
go version go1.18.4 linux/amd64

  • 准备 Golang 源代码

62-CICD持续集成工具-Jenkins构建Golang的web项目_Golang

  • 生成ssh密钥并上传到GitLab

[root@jenkins ~]#ssh-keygen
[root@jenkins ~]#cat .ssh/id_rsa.pub
ssh-rsa
...

复制到这里

62-CICD持续集成工具-Jenkins构建Golang的web项目_CICD_02

  • 验证jenkins是否可以基于SSH密钥拉取代码

[root@jenkins data]#git clone git@gitlab.mooreyxia.org:myprj/http_demo_go.git
Cloning into http_demo_go...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 5 (delta 0), pack-reused 0
Receiving objects: 100% (5/5), done.

[root@jenkins data]#tree http_demo_go/
http_demo_go/
├── go.mod
├── hello.html
└── main.go

0 directories, 3 files

  • 编写自动化部署Shell脚本

#部署对象服务器提前建立好数据文件夹
[root@web01 ~]#mkdir -p /data
[root@web02 ~]# mkdir -p /data

#Jenkins服务器上编写自动化脚本
[root@jenkins script]#vim http_demo_go.sh
[root@jenkins script]#cat http_demo_go.sh
#!/bin/bash

APP=http_server_demo
APP_PATH=/data
DATE=`date +%F_%H-%M-%S`
HOST_LIST="
192.168.11.202
192.168.11.203
"

#最新版Go编译依赖环境变量GOCACHE,如果缺失就手动加入
export GOCACHE="/root/.cache/go-build"

build ()
CGO_ENABLED=0 go build -o $APP


deloy ()
for host in $HOST_LIST;do
ssh $host "mkdir -p $APP_PATH/$APP-$DATE"
scp -r hello.html $APP $host:$APP_PATH/$APP-$DATE/
ssh $host "killall -0 $APP &>/dev/null && killall -9 $APP;rm -f $APP_PATH/$APP && \\
ln -s $APP_PATH/$APP-$DATE $APP_PATH/$APP; \\
cd $APP_PATH/$APP/ && ./$APP" &
done


build

deloy

  • 创建 Jenkins 任务

62-CICD持续集成工具-Jenkins构建Golang的web项目_Golang_03

62-CICD持续集成工具-Jenkins构建Golang的web项目_CICD_04

[root@jenkins data]#tree script/
script/
├── http_demo_go.sh
└── spring-boot-helloworld.sh

0 directories, 2 files

62-CICD持续集成工具-Jenkins构建Golang的web项目_Jenkins_05

  • 构建并确认

62-CICD持续集成工具-Jenkins构建Golang的web项目_Jenkins_06

62-CICD持续集成工具-Jenkins构建Golang的web项目_Golang_07

  • 验证

62-CICD持续集成工具-Jenkins构建Golang的web项目_Jenkins_08

62-CICD持续集成工具-Jenkins构建Golang的web项目_Jenkins_09

  • 升级测试

[root@gitlab data]#git clone git@gitlab.mooreyxia.org:myprj/http_demo_go.git
Cloning into http_demo_go...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 5 (delta 0), pack-reused 0
Receiving objects: 100% (5/5), done.
[root@gitlab data]#cd http_demo_go/
[root@gitlab http_demo_go]#ls
go.mod hello.html main.go
[root@gitlab http_demo_go]#vim main.go
[root@gitlab http_demo_go]#git branch
* master
[root@gitlab http_demo_go]#git commit -am v1.0
[master 07c8b7a] v1.0
Committer: root <root@gitlab.mooreyxia.org>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

git config --global --edit

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab http_demo_go]#git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 280 bytes | 280.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
To gitlab.mooreyxia.org:myprj/http_demo_go.git
99131f2..07c8b7a master -> master
[root@gitlab http_demo_go]#git log
commit 07c8b7a596899a5114a49a9eb9ee432334b4c7de (HEAD -> master, origin/master, origin/HEAD)
Author: root <root@gitlab.mooreyxia.org>
Date: Sat Feb 18 03:06:07 2023 +0000

v1.0

commit 99131f252ad74aee49aaffbe4fae7e5beea5fee8
Author: 南山 <lbtooth@qq.com>
Date: Sun Jul 24 11:52:46 2022 +0800

v1.0

再次构建

62-CICD持续集成工具-Jenkins构建Golang的web项目_Golang_10

确认升级成功

62-CICD持续集成工具-Jenkins构建Golang的web项目_CICD_11


我是moore,大家一起加油!

以上是关于golang 使用golang构建Web应用程序的字段验证实例的主要内容,如果未能解决你的问题,请参考以下文章

Golang编写 Web 应用程序

golang实践:构建gin web项目,让web开发更轻便,碗里的spring boot并不香了

golang构建web服务入门教程-环境部署

golang反射框架Fx

golang 都有哪些比较稳定的 web 开发框架

golang web有必要容器化吗