Jump to content
IPS Community Suite 简体中文
Sign in to follow this  
ipscn

Golang 二维码应用

Recommended Posts

 

安装Golang

wget https://dl.google.com/go/go1.10.2.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.10.2.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin

安装 git 客户端:

 yum install git

测试git

git --version

测试 golang

touch a.go
vim a.go

按 i 键进入Vim的输入模式,输入

package main

import "fmt"

func main(){

 fmt.Println("Golang,你好啊!")

}

按 Esc 退出输入模式,进入命令模式,输入 :wq 保存并推出,运行刚才写的go文件

go run a.go

可以从命令行看到输出说明Go语言安装完成了。

 

安装依赖

go get github.com/boombuler/barcode
go get github.com/samwierema/go-qr-generator

主文件

main_online.go

/*
* @Author: Administrator
* @Date:   2018-06-08 01:09:05
* @Last Modified by:   Administrator
* @Last Modified time: 2018-06-08 01:21:15
**/
package main

import (
	"bytes"
	"github.com/boombuler/barcode"
	"github.com/boombuler/barcode/qr"
	"image/png"
	"log"
	"net/http"
	"net/url"
	"strconv"
)

func main() {
	http.HandleFunc("/", QrGenerator)
	err := http.ListenAndServe(":6688", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

func QrGenerator(w http.ResponseWriter, r *http.Request) {
	data := r.URL.Query().Get("data")
	if data == "" {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	s, err := url.QueryUnescape(data)
	if err != nil {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	code, err := qr.Encode(s, qr.L, qr.Auto)
	if err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	size := r.URL.Query().Get("size")
	if size == "" {
		size = "250"
	}
	intsize, err := strconv.Atoi(size)
	if err != nil {
		intsize = 250
	}

	// Scale the barcode to the appropriate size
	code, err = barcode.Scale(code, intsize, intsize)
	if err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	buffer := new(bytes.Buffer)
	if err := png.Encode(buffer, code); err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "image/png")
	w.Header().Set("Content-Length", strconv.Itoa(len(buffer.Bytes())))

	if _, err := w.Write(buffer.Bytes()); err != nil {
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
}

 

构建和运行

上传到某个目录,比如 /home/wwwroot/lnmp72/qr

cd /home/wwwroot/lnmp72/domain/qr
go run main_online.go

访问

IP地址:6688?data=coolwp.com&size=300

能看到二维码输出,就说明正常了,开始构建和运行

go build -o goqr main_online.go
nohup ./goqr &

绑定域名

在域名的DNS设置处,设置下域名,比如 qr.coolwp.com

 

Nginx 指向特定端口

 

写个配置文件 qr.coolwp.com.conf,放在 /home/wwwroot/环境名称/vhost/下

server
{
	listen 80; #域名支持SSL的话,监听端口是443
	server_name qr.coolwp.com; #实际域名
	index index.html index.htm index.php; #index end


    location / {
        proxy_pass http://localhost:6688;
    }

	location ~ .*\.(gif|jpg|jpeg|png|bmp)$
	{
		expires      30d;
	}

	access_log off; #access_log end
	error_log /dev/null; #error_log end
}

 

测试

http://qr.coolwp.com/?size=300&data=coolwp.com

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×