Go alisms 阿里云短信服务构建

PreRequirements

proto/alisms/alisms.proto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
syntax = "proto3";

package alisms;

message SMSVerficationCodeData {
string sign_name = 1;
string phone_numbers = 2;
string template_code = 3;
string template_param = 4;
string sms_up_extend_code = 5;
string out_id = 6;
}

message SMSVerficationCodeCheckData {
string phone_numbers = 1;
string vcode = 2;
}

message SMSVerficationResponseData {
int64 return_code = 1;
string message = 2;
string data = 3;
}

message SMSVerficationQueryData {
string phone_numbers = 1;
string send_date = 2;
string page_size = 3;
string current_page = 4;
string biz_id = 5;
}

message SMSVerficationQueryResponseData {
int64 return_code = 1;
string message = 2;
string data = 3;
}

service AuthService {
rpc SMSVerficationCode(SMSVerficationCodeData) returns (SMSVerficationResponseData) {}
rpc SMSVerficationCodeCheck(SMSVerficationCodeCheckData) returns (SMSVerficationResponseData) {}
rpc SMSVerficationQuery(SMSVerficationQueryData) returns (SMSVerficationQueryResponseData) {}
}

cache/cache.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cache

import (
"fmt"
"log"
"sync"
"github.com/noahzaozao/alisms_service/coinfig"
"github.com/go-redis/redis"
)

type CacheManager struct {
config config.CacheConfig
}

var instance *CacheManager
var once sync.Once

func CacheMgr() *CacheManager {
once.Do(func () {
instance = &CacheManager{}
})
return instance
}

//
// 初始化缓存配置文件
//
func (cacheMgr *CacheManager) Init(cacheConfig config.CacheConfig) error {
cacheMgr.config = cacheConfig
if cacheMgr.config.Type == "redis" {
dbConn, err := cacheMgr.Conn()
if err != nil {
return err
}
defer dbConn.Close()
log.Println("Cache connected")
} else {
log.Println("Cache Type is incorrect")
}
return nil
}

//
// 获取缓存连接
//
func (cacheMgr *CacheManager) Conn() (*redis.Client, error) {
connStr := fmt.Sprintf(
"%s:%s",
cacheMgr.config.Host,
cacheMgr.config.Port)
client := redis.NewClient(&redis.Options{
Addr: connStr,
Password: cacheMgr.config.Password, // no password set
DB: cacheMgr.config.DB, // use default DB
})
_, err := client.Ping().Result()
if err != nil {
return nil, err
}
return client, nil
}

config/config.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package config

type SMSConfig struct {
ACCESS_KEY_ID string `yaml:"ACCESS_KEY_ID"`
ACCESS_KEY_SECRET string `yaml:"ACCESS_KEY_SECRET"`
}

type CacheConfig struct {
Type string `yaml:"type"`
Host string `yaml:"host"`
Port string `yaml:"port"`
DB int `yaml:"db"`
Password string `yaml:"password"`
}

type SettingConfig struct {
SECRET_KEY string `yaml:"SECRET_KEY"`
DEBUG string `yaml:"DEBUG"`
DEFAULT_CHARSET string `yaml:"DEFAULT_CHARSET"`
SMSConfig SMSConfig `yaml:"SMSConfig"`
CACHES []CacheConfig `yaml:"CACHES"`
}

main.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main

import (
"log"
"time"
"fmt"

"github.com/micro/go-grpc"
"github.com/micro/go-micro"
go_config "github.com/micro/go-config"

"github.com/noahzaozao/alisms_service/coinfig"
"github.com/noahzaozao/alisms_service/cache"
"context"
"github.com/noahzaozao/alisms_service/proto/alisms"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"

)

type AliSMSService struct {
Config config.SettingConfig
}

func (aliSmsService *AliSMSService) SMSVerficationCode(
ctx context.Context, in *alisms.SMSVerficationCodeData, out *alisms.SMSVerficationResponseData) error {

client, err := sdk.NewClientWithAccessKey(
"default",
aliSmsService.Config.SMSConfig.ACCESS_KEY_ID,
aliSmsService.Config.SMSConfig.ACCESS_KEY_SECRET)
if err != nil {
panic(err)
}

request := requests.NewCommonRequest()
request.Method = "POST"
request.Scheme = "https"
request.Domain = "dysmsapi.aliyuncs.com"
request.Version = "2017-05-25"
request.ApiName = "SendSms"

request.QueryParams["SignName"] = in.SignName
request.QueryParams["PhoneNumbers"] = in.PhoneNumbers
request.QueryParams["TemplateCode"] = in.TemplateCode
request.QueryParams["TemplateParam"] = in.TemplateParam
request.QueryParams["SmsUpExtendCode"] = in.SmsUpExtendCode
request.QueryParams["OutId"] = in.OutId

response, err := client.ProcessCommonRequest(request)
if err != nil {
panic(err)
}
fmt.Print(response.GetHttpContentString())

return nil
}

func (aliSmsService *AliSMSService) SMSVerficationCodeCheck(
ctx context.Context, in *alisms.SMSVerficationCodeCheckData, out *alisms.SMSVerficationResponseData) error {

return nil
}

func (aliSmsService *AliSMSService) SMSVerficationQuery(
ctx context.Context, in *alisms.SMSVerficationQueryData, out *alisms.SMSVerficationQueryResponseData) error {

client, err := sdk.NewClientWithAccessKey(
"default",
aliSmsService.Config.SMSConfig.ACCESS_KEY_ID,
aliSmsService.Config.SMSConfig.ACCESS_KEY_SECRET)
if err != nil {
panic(err)
}

request := requests.NewCommonRequest()
request.Method = "POST"
request.Scheme = "https"
request.Domain = "dysmsapi.aliyuncs.com"
request.Version = "2017-05-25"
request.ApiName = "QuerySendDetails"
request.QueryParams["PhoneNumber"] = in.PhoneNumbers
request.QueryParams["SendDate"] = in.SendDate
request.QueryParams["PageSize"] = in.PageSize
request.QueryParams["CurrentPage"] = in.CurrentPage
request.QueryParams["BizId"] = in.BizId

response, err := client.ProcessCommonRequest(request)
if err != nil {
panic(err)
}
fmt.Print(response.GetHttpContentString())

return nil
}

func main() {

// Load json config file
if err := go_config.LoadFile("./config.yaml"); err != nil {
log.Println(err.Error())
return
}

var settingsConfig config.SettingConfig

if err := go_config.Get("config").Scan(&settingsConfig); err != nil {
log.Println(err.Error())
return
}

log.Println("DEBUG: " + settingsConfig.DEBUG)
log.Println("CHARSET: " + settingsConfig.DEFAULT_CHARSET)

if len(settingsConfig.CACHES) < 1 {
log.Println("CACHES config not exist")
return
}

if err := cache.CacheMgr().Init(settingsConfig.CACHES[0]); err != nil {
log.Println(err.Error())
return
}
log.Println("Init CACHE...")

service := grpc.NewService(
micro.Name("alisms.srv"),
micro.RegisterTTL(time.Second*30),
micro.RegisterInterval(time.Second*10),
)
service.Init()

alismsService := &AliSMSService{
Config: settingsConfig,
}

if err := alisms.RegisterAuthServiceHandler(service.Server(), alismsService); err != nil {
log.Println(err.Error())
return
}

if err := service.Run(); err != nil {
log.Fatal(err)
}
}

config.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
config:
SECRET_KEY: ""
DEBUG: "true"
DEFAULT_CHARSET: "utf-8"
SMSConfig:
ACCESS_KEY_ID: ""
ACCESS_KEY_SECRET: ""
CACHES:
- type: "redis"
host: "127.0.0.1"
port: "6379"
db: 0
password: "password"

build.sh

1
2
3
4
5
6
7
8
echo 'protoc'
protoc -I/usr/local/include -I. \
--proto_path=$GOPATH/src:. \
--micro_out=./proto/alisms/ \
--go_out=./proto/alisms/ \
-I./proto/alisms/ alisms.proto &&
go build
echo 'success'