test built-in rpc
This commit is contained in:
		@ -5,8 +5,11 @@ import (
 | 
				
			|||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"net/rpc"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type RequestPayload struct {
 | 
					type RequestPayload struct {
 | 
				
			||||||
@ -54,9 +57,12 @@ func (app *Config) HandleSubmission(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
	switch requestPayload.Action {
 | 
						switch requestPayload.Action {
 | 
				
			||||||
	case "auth":
 | 
						case "auth":
 | 
				
			||||||
		app.authenticate(w, requestPayload.Auth)
 | 
							app.authenticate(w, requestPayload.Auth)
 | 
				
			||||||
	case "log":
 | 
						case "logHttp": // 2729 us
 | 
				
			||||||
		// app.LogItem(w, requestPayload.Log)
 | 
							app.LogItem(w, requestPayload.Log)
 | 
				
			||||||
 | 
						case "logRabbit": // 10007 us
 | 
				
			||||||
		app.logEventViaRabbit(w, requestPayload.Log)
 | 
							app.logEventViaRabbit(w, requestPayload.Log)
 | 
				
			||||||
 | 
						case "logRpc": // 555 us
 | 
				
			||||||
 | 
							app.logItemViaRPC(w, requestPayload.Log)
 | 
				
			||||||
	case "mail":
 | 
						case "mail":
 | 
				
			||||||
		app.SendMail(w, requestPayload.Mail)
 | 
							app.SendMail(w, requestPayload.Mail)
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
@ -80,6 +86,8 @@ func (app *Config) authenticate(w http.ResponseWriter, a AuthPayload) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (app *Config) LogItem(w http.ResponseWriter, entry LogPayload) {
 | 
					func (app *Config) LogItem(w http.ResponseWriter, entry LogPayload) {
 | 
				
			||||||
	log.Println(entry)
 | 
						log.Println(entry)
 | 
				
			||||||
 | 
						now := time.Now()
 | 
				
			||||||
 | 
						entry.Data += fmt.Sprintf("HTTP sent date %d", now.UnixMicro())
 | 
				
			||||||
	loggerService := Microservice{
 | 
						loggerService := Microservice{
 | 
				
			||||||
		Input: entry,
 | 
							Input: entry,
 | 
				
			||||||
		Addr:  "http://logger-service/log",
 | 
							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) {
 | 
					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)
 | 
						err := app.pushToQueue(l.Name, l.Data)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		app.errorJSON(w, err)
 | 
							app.errorJSON(w, err)
 | 
				
			||||||
@ -212,3 +222,34 @@ func (app *Config) pushToQueue(name, msg string) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						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="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="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>
 | 
					                <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;">
 | 
					                <div id="output" class="mt-5" style="outline: 1px solid silver; padding: 2em;">
 | 
				
			||||||
@ -39,6 +41,9 @@
 | 
				
			|||||||
        let brokerBtn = document.getElementById("brokerBtn");
 | 
					        let brokerBtn = document.getElementById("brokerBtn");
 | 
				
			||||||
        let authBrokerBtn = document.getElementById("authBrokerBtn");
 | 
					        let authBrokerBtn = document.getElementById("authBrokerBtn");
 | 
				
			||||||
        let mailBtn = document.getElementById("mailBtn");
 | 
					        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 output = document.getElementById("output");
 | 
				
			||||||
        let sent = document.getElementById("payload");
 | 
					        let sent = document.getElementById("payload");
 | 
				
			||||||
        let received = document.getElementById("received");
 | 
					        let received = document.getElementById("received");
 | 
				
			||||||
@ -95,9 +100,73 @@
 | 
				
			|||||||
                });
 | 
					                });
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        logBtn.addEventListener("click", () => {
 | 
					        logHttpBtn.addEventListener("click", () => {
 | 
				
			||||||
            const payload = {
 | 
					            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: {
 | 
					                log: {
 | 
				
			||||||
                    name: "event",
 | 
					                    name: "event",
 | 
				
			||||||
                    data: "some kind of data",
 | 
					                    data: "some kind of data",
 | 
				
			||||||
 | 
				
			|||||||
@ -1,9 +1,11 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"logger/data"
 | 
						"logger/data"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type JSONPayload struct {
 | 
					type JSONPayload struct {
 | 
				
			||||||
@ -27,6 +29,9 @@ func (app *Config) WriteLog(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		Data: requestPayload.Data,
 | 
							Data: requestPayload.Data,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						now := time.Now()
 | 
				
			||||||
 | 
						event.Data += fmt.Sprintf(" received date %d", now.UnixMicro())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	log.Println("event", event)
 | 
						log.Println("event", event)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = app.Models.LogEntry.Insert(event)
 | 
						err = app.Models.LogEntry.Insert(event)
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,7 @@ package main
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"logger/data"
 | 
						"logger/data"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
@ -16,6 +17,10 @@ type RPCPayload struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (r *RPCServer) LogInfo(payload RPCPayload, resp *string) error {
 | 
					func (r *RPCServer) LogInfo(payload RPCPayload, resp *string) error {
 | 
				
			||||||
	collection := client.Database("logs").Collection("logs")
 | 
						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{
 | 
						_, err := collection.InsertOne(context.TODO(), data.LogEntry{
 | 
				
			||||||
		Name:      payload.Name,
 | 
							Name:      payload.Name,
 | 
				
			||||||
		Data:      payload.Data,
 | 
							Data:      payload.Data,
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user