Commit 8af62295 authored by Peter Cheng's avatar Peter Cheng

追加存取Redis服務

parent 346e0d04
...@@ -47,4 +47,110 @@ go get -u -v github.com/gin-gonic/gin ...@@ -47,4 +47,110 @@ go get -u -v github.com/gin-gonic/gin
"subject": "這是一封測試信", "subject": "這是一封測試信",
"content": "這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />" "content": "這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />"
} }
```
取得Redis資料 http://[Your Host Name]:8806/Redis/Get
```
//HTTP Header需設定成Content-Type: application/json
{
"key": "score"
}
```
設定Redis資料 http://[Your Host Name]:8806/Redis/Set
```
//HTTP Header需設定成Content-Type: application/json
{
"key": "score",
"value": "0"
}
```
對Redis資料進行遞增 http://[Your Host Name]:8806/Redis/Incr
```
//HTTP Header需設定成Content-Type: application/json
{
"key": "score"
}
```
對Redis資料進行遞減 http://[Your Host Name]:8806/Redis/Decr
```
//HTTP Header需設定成Content-Type: application/json
{
"key": "score"
}
```
對Redis資料建立Hashmap http://[Your Host Name]:8806/Redis/HSet
```
//HTTP Header需設定成Content-Type: application/json
{
"key": "profile",
"hkey": "name",
"value": "Peter"
}
```
取得建立Hashmap的Redis資料 http://[Your Host Name]:8806/Redis/HGet
```
//HTTP Header需設定成Content-Type: application/json
{
"key": "profile",
"hkey": "name"
}
```
對Redis進行資料添加,並排除重複項目 http://[Your Host Name]:8806/Redis/SAdd
```
//HTTP Header需設定成Content-Type: application/json
{
"key": "box",
"value": "Joe"
}
```
取得Key中的資料集合總數 http://[Your Host Name]:8806/Redis/SCard
```
//HTTP Header需設定成Content-Type: application/json
{
"key": "box"
}
```
對Redis中List資料的後面做資料添加 http://[Your Host Name]:8806/Redis/RPush
```
//HTTP Header需設定成Content-Type: application/json
{
"key": "menu",
"value": "apple"
}
```
對Redis中List資料的前面做資料添加 http://[Your Host Name]:8806/Redis/LPush
```
//HTTP Header需設定成Content-Type: application/json
{
"key": "menu",
"value": "banana"
}
```
從Redis中List資料的第...列做資料修改 http://[Your Host Name]:8806/Redis/LSet
```
//HTTP Header需設定成Content-Type: application/json
{
"key": "menu",
"hkey": "1",
"value": "cherry"
}
```
取得Redis中List特定範圍裡面的資料 http://[Your Host Name]:8806/Redis/LRange
```
//HTTP Header需設定成Content-Type: application/json
{
"key": "menu",
"value": "0:5"
}
``` ```
\ No newline at end of file
// Code generated by protoc-gen-go.
// source: redis.proto
// DO NOT EDIT!
/*
Package beans is a generated protocol buffer package.
It is generated from these files:
redis.proto
sendMail.proto
It has these top-level messages:
Redis
SendMail
*/
package beans
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type Redis struct {
Key string `protobuf:"bytes,1,opt,name=Key" json:"key,omitempty"`
Value string `protobuf:"bytes,2,opt,name=Value" json:"value,omitempty"`
Interval int64 `protobuf:"varint,3,opt,name=Interval" json:"interval,omitempty"`
Hkey string `protobuf:"bytes,4,opt,name=Hkey" json:"hkey,omitempty"`
}
func (m *Redis) Reset() { *m = Redis{} }
func (m *Redis) String() string { return proto.CompactTextString(m) }
func (*Redis) ProtoMessage() {}
func (*Redis) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
func (m *Redis) GetKey() string {
if m != nil {
return m.Key
}
return ""
}
func (m *Redis) GetValue() string {
if m != nil {
return m.Value
}
return ""
}
func (m *Redis) GetInterval() int64 {
if m != nil {
return m.Interval
}
return 0
}
func (m *Redis) GetHkey() string {
if m != nil {
return m.Hkey
}
return ""
}
func init() {
proto.RegisterType((*Redis)(nil), "beans.Redis")
}
func init() { proto.RegisterFile("redis.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 121 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2e, 0x4a, 0x4d, 0xc9,
0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4d, 0x4a, 0x4d, 0xcc, 0x2b, 0x56, 0x8a,
0xe7, 0x62, 0x0d, 0x02, 0x89, 0x0a, 0x09, 0x70, 0x31, 0x7b, 0xa7, 0x56, 0x4a, 0x30, 0x2a, 0x30,
0x6a, 0x70, 0x06, 0x81, 0x98, 0x42, 0x22, 0x5c, 0xac, 0x61, 0x89, 0x39, 0xa5, 0xa9, 0x12, 0x4c,
0x60, 0x31, 0x08, 0x47, 0x48, 0x8a, 0x8b, 0xc3, 0x33, 0xaf, 0x24, 0xb5, 0xa8, 0x2c, 0x31, 0x47,
0x82, 0x59, 0x81, 0x51, 0x83, 0x39, 0x08, 0xce, 0x17, 0x12, 0xe2, 0x62, 0xf1, 0xc8, 0x4e, 0xad,
0x94, 0x60, 0x01, 0x6b, 0x00, 0xb3, 0x93, 0xd8, 0xc0, 0xd6, 0x19, 0x03, 0x02, 0x00, 0x00, 0xff,
0xff, 0x98, 0x98, 0x85, 0x61, 0x7d, 0x00, 0x00, 0x00,
}
syntax = "proto3";
package beans;
message Redis {
string Key = 1; //`json:"key,omitempty"`
string Value = 2; //`json:"value,omitempty"`
int64 Interval = 3; //`json:"interval,omitempty"`
string Hkey = 4; //`json:"hkey,omitempty"`
}
\ No newline at end of file
...@@ -2,15 +2,6 @@ ...@@ -2,15 +2,6 @@
// source: sendMail.proto // source: sendMail.proto
// DO NOT EDIT! // DO NOT EDIT!
/*
Package beans is a generated protocol buffer package.
It is generated from these files:
sendMail.proto
It has these top-level messages:
SendMail
*/
package beans package beans
import proto "github.com/golang/protobuf/proto" import proto "github.com/golang/protobuf/proto"
...@@ -22,22 +13,16 @@ var _ = proto.Marshal ...@@ -22,22 +13,16 @@ var _ = proto.Marshal
var _ = fmt.Errorf var _ = fmt.Errorf
var _ = math.Inf var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type SendMail struct { type SendMail struct {
To string ` protobuf:"bytes,1,opt,name=To" json:"To,omitempty" form:"to"` To string ` protobuf:"bytes,1,opt,name=To" json:"To,omitempty" form:"to"`
Subject string ` protobuf:"bytes,2,opt,name=Subject" json:"Subject,omitempty" form:"subject"` Subject string ` protobuf:"bytes,2,opt,name=Subject" json:"Subject,omitempty" form:"subject"`
Content string ` protobuf:"bytes,3,opt,name=Content" json:"Content,omitempty" form:"content"` Content string ` protobuf:"bytes,3,opt,name=Content" json:"Content,omitempty" form:"content"`
} }
func (m *SendMail) Reset() { *m = SendMail{} } func (m *SendMail) Reset() { *m = SendMail{} }
func (m *SendMail) String() string { return proto.CompactTextString(m) } func (m *SendMail) String() string { return proto.CompactTextString(m) }
func (*SendMail) ProtoMessage() {} func (*SendMail) ProtoMessage() {}
func (*SendMail) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } func (*SendMail) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{0} }
func (m *SendMail) GetTo() string { func (m *SendMail) GetTo() string {
if m != nil { if m != nil {
...@@ -64,9 +49,9 @@ func init() { ...@@ -64,9 +49,9 @@ func init() {
proto.RegisterType((*SendMail)(nil), "beans.SendMail") proto.RegisterType((*SendMail)(nil), "beans.SendMail")
} }
func init() { proto.RegisterFile("sendMail.proto", fileDescriptor0) } func init() { proto.RegisterFile("sendMail.proto", fileDescriptor1) }
var fileDescriptor0 = []byte{ var fileDescriptor1 = []byte{
// 106 bytes of a gzipped FileDescriptorProto // 106 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2b, 0x4e, 0xcd, 0x4b, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2b, 0x4e, 0xcd, 0x4b,
0xf1, 0x4d, 0xcc, 0xcc, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4d, 0x4a, 0x4d, 0xcc, 0xf1, 0x4d, 0xcc, 0xcc, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4d, 0x4a, 0x4d, 0xcc,
......
...@@ -2,6 +2,7 @@ package controllers ...@@ -2,6 +2,7 @@ package controllers
import ( import (
"log" "log"
"net/http"
"../beans" "../beans"
"../env" "../env"
...@@ -17,6 +18,7 @@ func SendMail(c *gin.Context) { ...@@ -17,6 +18,7 @@ func SendMail(c *gin.Context) {
log.Println(err) log.Println(err)
} }
doSendMail(params) doSendMail(params)
c.JSON(http.StatusOK, gin.H{"status": "true"})
} }
//doSendMail 寄發通知郵件 //doSendMail 寄發通知郵件
......
package controllers
import (
"log"
"net/http"
"strings"
"../beans"
"../env"
"github.com/garyburd/redigo/redis"
"github.com/gin-gonic/gin"
)
//GetRedis 取得Redis資料
func GetRedis(c *gin.Context) {
params := getParams(c)
client := initRedis()
value, err := redis.String(client.Do("get", params.GetKey()))
if err != nil {
log.Panic(err)
}
defer client.Close()
c.JSON(http.StatusOK, gin.H{"value": value})
}
//SetRedis 設定Redis資料
func SetRedis(c *gin.Context) {
params := getParams(c)
client := initRedis()
value, err := client.Do("set", params.GetKey(), params.GetValue())
if err != nil {
log.Panic(err)
}
defer client.Close()
c.JSON(http.StatusOK, gin.H{"success": value})
}
//IncrRedis 對Redis資料進行遞增
func IncrRedis(c *gin.Context) {
params := getParams(c)
client := initRedis()
value, err := client.Do("incr", params.GetKey())
if err != nil {
log.Panic(err)
}
defer client.Close()
c.JSON(http.StatusOK, gin.H{"value": value})
}
//DecrRedis 對Redis資料進行遞減
func DecrRedis(c *gin.Context) {
params := getParams(c)
client := initRedis()
value, err := client.Do("decr", params.GetKey())
if err != nil {
log.Panic(err)
}
defer client.Close()
c.JSON(http.StatusOK, gin.H{"value": value})
}
//HSetRedis 對Redis資料建立Hashmap
func HSetRedis(c *gin.Context) {
params := getParams(c)
client := initRedis()
value, err := client.Do("hset", params.GetKey(), params.GetHkey(), params.GetValue())
if err != nil {
log.Panic(err)
}
defer client.Close()
c.JSON(http.StatusOK, gin.H{"value": value})
}
//HGetRedis 取得建立Hashmap的Redis資料
func HGetRedis(c *gin.Context) {
params := getParams(c)
client := initRedis()
value, err := redis.String(client.Do("hget", params.GetKey(), params.GetHkey()))
if err != nil {
log.Panic(err)
}
defer client.Close()
c.JSON(http.StatusOK, gin.H{"value": value})
}
//SAddRedis 對Redis進行資料添加,並排除重複項目
func SAddRedis(c *gin.Context) {
params := getParams(c)
client := initRedis()
value, err := client.Do("sadd", params.GetKey(), params.GetValue())
if err != nil {
log.Panic(err)
}
defer client.Close()
c.JSON(http.StatusOK, gin.H{"value": value})
}
//SCardRedis 取得Key中的資料集合總數
func SCardRedis(c *gin.Context) {
params := getParams(c)
client := initRedis()
value, err := client.Do("scard", params.GetKey())
if err != nil {
log.Panic(err)
}
defer client.Close()
c.JSON(http.StatusOK, gin.H{"value": value})
}
//LPushRedis 對Redis中List資料的前面做資料添加
func LPushRedis(c *gin.Context) {
params := getParams(c)
client := initRedis()
value, err := client.Do("lpush", params.GetKey(), params.GetValue())
if err != nil {
log.Panic(err)
}
defer client.Close()
c.JSON(http.StatusOK, gin.H{"value": value})
}
//RPushRedis 對Redis中List資料的後面做資料添加
func RPushRedis(c *gin.Context) {
params := getParams(c)
client := initRedis()
value, err := client.Do("rpush", params.GetKey(), params.GetValue())
if err != nil {
log.Panic(err)
}
defer client.Close()
c.JSON(http.StatusOK, gin.H{"value": value})
}
//LSetRedis 從Redis中List資料的第...列做資料修改
func LSetRedis(c *gin.Context) {
params := getParams(c)
client := initRedis()
value, err := client.Do("lset", params.GetKey(), params.GetHkey(), params.GetValue())
if err != nil {
log.Panic(err)
}
defer client.Close()
c.JSON(http.StatusOK, gin.H{"value": value})
}
//LRangeRedis 取得Redis中List特定範圍裡面的資料
func LRangeRedis(c *gin.Context) {
params := getParams(c)
getRange := strings.Split(params.GetValue(), ":")
begin := getRange[0]
end := getRange[1]
client := initRedis()
value, err := redis.Strings(client.Do("lrange", params.GetKey(), begin, end))
if err != nil {
log.Panic(err)
}
defer client.Close()
c.JSON(http.StatusOK, gin.H{"value": value})
}
func initRedis() redis.Conn {
cfg := env.GetEnv()
client, err := redis.Dial(cfg.Redis.Protocol, cfg.Redis.Host)
if err != nil {
log.Panic(err)
}
return client
}
func getParams(c *gin.Context) *beans.Redis {
params := &beans.Redis{}
err := c.BindJSON(params)
if err != nil {
log.Println(err)
}
return params
}
...@@ -13,5 +13,17 @@ func API() *gin.Engine { ...@@ -13,5 +13,17 @@ func API() *gin.Engine {
gin.SetMode(cfg.Env) gin.SetMode(cfg.Env)
route := gin.Default() route := gin.Default()
route.POST("/Mail/SendMail", controllers.SendMail) route.POST("/Mail/SendMail", controllers.SendMail)
route.POST("/Redis/Get", controllers.GetRedis)
route.POST("/Redis/Set", controllers.SetRedis)
route.POST("/Redis/Incr", controllers.IncrRedis)
route.POST("/Redis/Decr", controllers.DecrRedis)
route.POST("/Redis/HSet", controllers.HSetRedis)
route.POST("/Redis/HGet", controllers.HGetRedis)
route.POST("/Redis/SAdd", controllers.SAddRedis)
route.POST("/Redis/SCard", controllers.SCardRedis)
route.POST("/Redis/LPush", controllers.LPushRedis)
route.POST("/Redis/RPush", controllers.RPushRedis)
route.POST("/Redis/LSet", controllers.LSetRedis)
route.POST("/Redis/LRange", controllers.LRangeRedis)
return route return route
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment