-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
186 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package config | ||
|
||
import ( | ||
"flag" | ||
log "github.com/sirupsen/logrus" | ||
"gopkg.in/yaml.v3" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
var ( | ||
root = flag.String("dir", "D:/gopath/src/edu_api", "设置配置文件的根路径") | ||
|
||
Config = parseYaml() | ||
) | ||
|
||
func parseYaml() *configuration { | ||
flag.Parse() | ||
|
||
cfg := new(configuration) | ||
cfg, err := cfg.yaml(*root + "/config/config.yaml") | ||
if err != nil { | ||
log.Info("parse yaml config error:", err.Error()) | ||
} | ||
|
||
log.Info("cfg is:", cfg.Queue.Addr) | ||
return cfg | ||
} | ||
|
||
type configuration struct { | ||
Queue queue `json:"queue",yaml:"queue"` | ||
} | ||
|
||
type queue struct { | ||
Addr string `json:"addr",yaml:"addr"` | ||
} | ||
|
||
func (cfg *configuration) yaml(f string) (*configuration, error) { | ||
file, err := os.Open(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer file.Close() | ||
|
||
data, err := ioutil.ReadAll(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = yaml.Unmarshal(data, cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
queue: | ||
addr: 127.0.0.1:9501 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
D:/gopath/src/edu_api/log/request.20191226.log | ||
D:/gopath/src/edu_api/log/request.20191227.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package tasksAndEvents | ||
|
||
import ( | ||
"bytes" | ||
"net" | ||
"time" | ||
) | ||
|
||
/** | ||
定义一个转换消息体的接口 | ||
*/ | ||
type Operate interface { | ||
ToBytes() ([]byte, error) | ||
} | ||
|
||
/** | ||
数据库操作 | ||
*/ | ||
func operateDB(operate Operate) (int, error) { | ||
conn, err := net.DialTimeout("tcp", "", 200*time.Millisecond) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
defer conn.Close() | ||
|
||
data, err := operate.ToBytes() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var buffer bytes.Buffer | ||
buffer.Write(data) | ||
buffer.WriteString("\n") //这个必须,因为php swoole默认是以\n结束消息的 | ||
|
||
return conn.Write(buffer.Bytes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package tasksAndEvents | ||
|
||
import "encoding/json" | ||
|
||
type orderExecute struct { | ||
Class string `json:"class"` | ||
Method string `json:"method"` | ||
Params *OrderExecute `json:"params"` | ||
} | ||
|
||
func (o *orderExecute) ToBytes() ([]byte, error) { | ||
o.Class = "OrderExecute" | ||
o.Method = "handle" | ||
|
||
data, err := json.Marshal(o) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
type OrderExecute struct { | ||
OrderId int `json:"order_id"` | ||
BranchType string `json:"branch_type"` | ||
} | ||
|
||
/** | ||
这个主要是更新用户课程的异步操作 | ||
*/ | ||
func (o *OrderExecute) Update() (int, error) { | ||
data := new(orderExecute) | ||
data.Params = o | ||
|
||
return operateDB(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package tasksAndEvents | ||
|
||
import "encoding/json" | ||
|
||
type paidSuccessMessage struct { | ||
Class string `json:"class"` | ||
Method string `json:"method"` | ||
Params *PaidSuccessMessage `json:"params"` | ||
} | ||
|
||
func (o *paidSuccessMessage) ToBytes() ([]byte, error) { | ||
o.Class = "OrderExecute" | ||
o.Method = "handle" | ||
|
||
data, err := json.Marshal(o) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
type PaidSuccessMessage struct { | ||
OrderId int `json:"order_id"` | ||
BranchType string `json:"branch_type"` | ||
PaySource string `json:"pay_source"` | ||
EventType string `json:"event_type"` | ||
} | ||
|
||
/** | ||
这个主要是更新用户课程的异步操作 | ||
*/ | ||
func (o *PaidSuccessMessage) Send() (int, error) { | ||
data := new(paidSuccessMessage) | ||
data.Params = o | ||
|
||
return operateDB(data) | ||
} |