Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化结构 #2

Merged
merged 1 commit into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
优化结构
  • Loading branch information
bydmm committed Jul 22, 2019
commit 3c0ac1ff9f2747ce9f83230615e3a1e305a92756
46 changes: 2 additions & 44 deletions middleware/jiode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,15 @@ package middleware

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"time"

"github.com/bydmm/jiode/model"
"github.com/gin-gonic/gin"
)

// Message 消息
type Message struct {
Client string `json:"c"`
Msg string `json:"m"`
}

// JSONDumpLog JSON格式的log
type JSONDumpLog struct {
Time string `json:"time"`
Expand Down Expand Up @@ -89,43 +79,11 @@ func JSONDump() gin.HandlerFunc {

json, _ := json.Marshal(log)
go func(json string) {
Send(json)
model.Send(json)
}(string(json))
}
}

// Send 发送给jiode server
func Send(payload string) {
addr := os.Getenv("JIODE_ADDR")
room := os.Getenv("JIODE_ROOM")
token := os.Getenv("JIODE_SECRET_TOKEN")
name := os.Getenv("JIODE_SERVICE_NAME")

u := url.URL{
Scheme: "http",
Host: addr,
Path: fmt.Sprintf("api/%s/%s", token, room),
}

message := Message{
Client: name,
Msg: payload,
}
body, _ := json.Marshal(message)
req, err := http.NewRequest("POST", u.String(), bytes.NewReader(body))

req.Header.Add("Content-Type", "application/json")

ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
req = req.WithContext(ctx)

_, err = http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
}
}

type bodyLogWriter struct {
gin.ResponseWriter
body *bytes.Buffer
Expand Down
43 changes: 43 additions & 0 deletions model/message.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
package model

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"time"
)

// Message 消息
type Message struct {
Client string `json:"c"`
Msg string `json:"m"`
}

// Send 发送给jiode server
func Send(payload string) {
addr := os.Getenv("JIODE_ADDR")
room := os.Getenv("JIODE_ROOM")
token := os.Getenv("JIODE_SECRET_TOKEN")
name := os.Getenv("JIODE_SERVICE_NAME")

u := url.URL{
Scheme: "http",
Host: addr,
Path: fmt.Sprintf("api/%s/%s", token, room),
}

message := Message{
Client: name,
Msg: payload,
}
body, _ := json.Marshal(message)
req, err := http.NewRequest("POST", u.String(), bytes.NewReader(body))

req.Header.Add("Content-Type", "application/json")

ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
req = req.WithContext(ctx)

_, err = http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
}
}