test built-in rpc
This commit is contained in:
parent
990385b3e7
commit
a0ce809869
@ -5,8 +5,11 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/rpc"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RequestPayload struct {
|
||||
@ -54,9 +57,12 @@ func (app *Config) HandleSubmission(w http.ResponseWriter, r *http.Request) {
|
||||
switch requestPayload.Action {
|
||||
case "auth":
|
||||
app.authenticate(w, requestPayload.Auth)
|
||||
case "log":
|
||||
// app.LogItem(w, requestPayload.Log)
|
||||
case "logHttp": // 2729 us
|
||||
app.LogItem(w, requestPayload.Log)
|
||||
case "logRabbit": // 10007 us
|
||||
app.logEventViaRabbit(w, requestPayload.Log)
|
||||
case "logRpc": // 555 us
|
||||
app.logItemViaRPC(w, requestPayload.Log)
|
||||
case "mail":
|
||||
app.SendMail(w, requestPayload.Mail)
|
||||
default:
|
||||
@ -80,6 +86,8 @@ func (app *Config) authenticate(w http.ResponseWriter, a AuthPayload) {
|
||||
|
||||
func (app *Config) LogItem(w http.ResponseWriter, entry LogPayload) {
|
||||
log.Println(entry)
|
||||
now := time.Now()
|
||||
entry.Data += fmt.Sprintf("HTTP sent date %d", now.UnixMicro())
|
||||
loggerService := Microservice{
|
||||
Input: entry,
|
||||
Addr: "http://logger-service/log",
|
||||
@ -180,6 +188,8 @@ func (app *Config) callService(w http.ResponseWriter, ms Microservice) {
|
||||
}
|
||||
|
||||
func (app *Config) logEventViaRabbit(w http.ResponseWriter, l LogPayload) {
|
||||
now := time.Now()
|
||||
l.Data += fmt.Sprintf("Rabbit sent date %d", now.UnixMicro())
|
||||
err := app.pushToQueue(l.Name, l.Data)
|
||||
if err != nil {
|
||||
app.errorJSON(w, err)
|
||||
@ -212,3 +222,34 @@ func (app *Config) pushToQueue(name, msg string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type RPCPayload struct {
|
||||
Name string
|
||||
Data string
|
||||
}
|
||||
|
||||
func (app *Config) logItemViaRPC(w http.ResponseWriter, l LogPayload) {
|
||||
client, err := rpc.Dial("tcp", "logger-service:5001")
|
||||
if err != nil {
|
||||
app.errorJSON(w, err)
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
now := time.Now()
|
||||
l.Data += fmt.Sprintf("RPC sent date %d", now.UnixMicro())
|
||||
rpcPayload := RPCPayload(l)
|
||||
|
||||
var result string
|
||||
err = client.Call("RPCServer.LogInfo", rpcPayload, &result)
|
||||
if err != nil {
|
||||
app.errorJSON(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
var payload jsonResponse
|
||||
payload.Error = false
|
||||
payload.Message = "logged via RPC"
|
||||
|
||||
app.writeJSON(w, http.StatusOK, payload)
|
||||
}
|
||||
|
@ -9,7 +9,9 @@
|
||||
|
||||
<a id="brokerBtn" class="btn btn-outline-secondary" href="javascript:void(0);">Test Broker</a>
|
||||
<a id="authBrokerBtn" class="btn btn-outline-secondary" href="javascript:void(0);">Test Auth</a>
|
||||
<a id="logBtn" class="btn btn-outline-secondary" href="javascript:void(0);">Test Log</a>
|
||||
<a id="logHttpBtn" class="btn btn-outline-secondary" href="javascript:void(0);">Test HTTP Log</a>
|
||||
<a id="logRabbitBtn" class="btn btn-outline-secondary" href="javascript:void(0);">Test Rabbit Log</a>
|
||||
<a id="logRpcBtn" class="btn btn-outline-secondary" href="javascript:void(0);">Test RPC Log</a>
|
||||
<a id="mailBtn" class="btn btn-outline-secondary" href="javascript:void(0);">Test Mail</a>
|
||||
|
||||
<div id="output" class="mt-5" style="outline: 1px solid silver; padding: 2em;">
|
||||
@ -39,6 +41,9 @@
|
||||
let brokerBtn = document.getElementById("brokerBtn");
|
||||
let authBrokerBtn = document.getElementById("authBrokerBtn");
|
||||
let mailBtn = document.getElementById("mailBtn");
|
||||
let logHttpBtn = document.getElementById("logHttpBtn");
|
||||
let logRabbitBtn = document.getElementById("logRabbitBtn");
|
||||
let logRpcBtn = document.getElementById("logRpcBtn");
|
||||
let output = document.getElementById("output");
|
||||
let sent = document.getElementById("payload");
|
||||
let received = document.getElementById("received");
|
||||
@ -95,9 +100,73 @@
|
||||
});
|
||||
});
|
||||
|
||||
logBtn.addEventListener("click", () => {
|
||||
logHttpBtn.addEventListener("click", () => {
|
||||
const payload = {
|
||||
action: "log",
|
||||
action: "logHttp",
|
||||
log: {
|
||||
name: "event",
|
||||
data: "some kind of data",
|
||||
}
|
||||
};
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
const body = {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
headers: headers,
|
||||
}
|
||||
fetch("http:\/\/localhost:8080/handle", body)
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
sent.innerHTML = JSON.stringify(payload, undefined, 4);
|
||||
received.innerHTML = JSON.stringify(data, undefined, 4);
|
||||
if (data.error) {
|
||||
console.log(data.message);
|
||||
output.innerHTML += `<br><strong>Error:</strong>: ${data.message}`;
|
||||
} else {
|
||||
output.innerHTML += `<br><strong>Response from broker service</strong>: ${data.message}`;
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
output.innerHTML += "<br><br>Error: " + error;
|
||||
});
|
||||
});
|
||||
|
||||
logRabbitBtn.addEventListener("click", () => {
|
||||
const payload = {
|
||||
action: "logRabbit",
|
||||
log: {
|
||||
name: "event",
|
||||
data: "some kind of data",
|
||||
}
|
||||
};
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
const body = {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
headers: headers,
|
||||
}
|
||||
fetch("http:\/\/localhost:8080/handle", body)
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
sent.innerHTML = JSON.stringify(payload, undefined, 4);
|
||||
received.innerHTML = JSON.stringify(data, undefined, 4);
|
||||
if (data.error) {
|
||||
console.log(data.message);
|
||||
output.innerHTML += `<br><strong>Error:</strong>: ${data.message}`;
|
||||
} else {
|
||||
output.innerHTML += `<br><strong>Response from broker service</strong>: ${data.message}`;
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
output.innerHTML += "<br><br>Error: " + error;
|
||||
});
|
||||
});
|
||||
|
||||
logRpcBtn.addEventListener("click", () => {
|
||||
const payload = {
|
||||
action: "logRpc",
|
||||
log: {
|
||||
name: "event",
|
||||
data: "some kind of data",
|
||||
|
@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"logger/data"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type JSONPayload struct {
|
||||
@ -27,6 +29,9 @@ func (app *Config) WriteLog(w http.ResponseWriter, r *http.Request) {
|
||||
Data: requestPayload.Data,
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
event.Data += fmt.Sprintf(" received date %d", now.UnixMicro())
|
||||
|
||||
log.Println("event", event)
|
||||
|
||||
err = app.Models.LogEntry.Insert(event)
|
||||
|
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"logger/data"
|
||||
"time"
|
||||
@ -16,6 +17,10 @@ type RPCPayload struct {
|
||||
|
||||
func (r *RPCServer) LogInfo(payload RPCPayload, resp *string) error {
|
||||
collection := client.Database("logs").Collection("logs")
|
||||
|
||||
now := time.Now()
|
||||
payload.Data += fmt.Sprintf(" received date %d", now.UnixMicro())
|
||||
|
||||
_, err := collection.InsertOne(context.TODO(), data.LogEntry{
|
||||
Name: payload.Name,
|
||||
Data: payload.Data,
|
||||
|
Loading…
Reference in New Issue
Block a user