Compare commits
	
		
			3 Commits
		
	
	
		
			fbfa723289
			...
			e047b35f77
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| e047b35f77 | |||
| be71cc40d1 | |||
| a0eedd4250 | 
@ -1,6 +1,8 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
@ -30,6 +32,15 @@ func (app *Config) Authenticate(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		app.errorJSON(w, errors.New("invalid credentials"), http.StatusBadRequest)
 | 
							app.errorJSON(w, errors.New("invalid credentials"), http.StatusBadRequest)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// log authentication
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = app.logRequest("authentication", fmt.Sprintf("%s is logged in", user.Email))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							app.errorJSON(w, err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	payload := jsonResponse{
 | 
						payload := jsonResponse{
 | 
				
			||||||
		Error:   false,
 | 
							Error:   false,
 | 
				
			||||||
		Message: fmt.Sprintf("%s %s is authorized to log in.", user.FirstName, user.LastName),
 | 
							Message: fmt.Sprintf("%s %s is authorized to log in.", user.FirstName, user.LastName),
 | 
				
			||||||
@ -38,3 +49,30 @@ func (app *Config) Authenticate(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	app.writeJSON(w, http.StatusAccepted, payload)
 | 
						app.writeJSON(w, http.StatusAccepted, payload)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (app *Config) logRequest(name, data string) error {
 | 
				
			||||||
 | 
						var entry struct {
 | 
				
			||||||
 | 
							Name string `json:"name"`
 | 
				
			||||||
 | 
							Data string `json:"data"`
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						entry.Name = name
 | 
				
			||||||
 | 
						entry.Data = data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						jsonData, _ := json.MarshalIndent(entry, "", "\t")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						logServiceURL := "http://logger-service/log"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						request, err := http.NewRequest("POST", logServiceURL, bytes.NewBuffer(jsonData))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						request.Header.Add("Content-Type", "application/json")
 | 
				
			||||||
 | 
						client := &http.Client{}
 | 
				
			||||||
 | 
						_, err = client.Do(request)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -57,7 +57,8 @@ func (app *Config) authenticate(w http.ResponseWriter, a AuthPayload) {
 | 
				
			|||||||
		Input: a,
 | 
							Input: a,
 | 
				
			||||||
		Addr:  "http://authentication-service/authenticate",
 | 
							Addr:  "http://authentication-service/authenticate",
 | 
				
			||||||
		ErrCode: statusError{
 | 
							ErrCode: statusError{
 | 
				
			||||||
			Code: http.StatusUnauthorized,
 | 
								ExpectedCode: http.StatusAccepted,
 | 
				
			||||||
 | 
								ErrCode:      http.StatusUnauthorized,
 | 
				
			||||||
			Err:          errors.New("invalid credentials"),
 | 
								Err:          errors.New("invalid credentials"),
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		SuccessMsg: "Authenticated",
 | 
							SuccessMsg: "Authenticated",
 | 
				
			||||||
@ -66,11 +67,13 @@ 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)
 | 
				
			||||||
	loggerService := Microservice{
 | 
						loggerService := Microservice{
 | 
				
			||||||
		Input: entry,
 | 
							Input: entry,
 | 
				
			||||||
		Addr:  "http://logger-service/log",
 | 
							Addr:  "http://logger-service/log",
 | 
				
			||||||
		ErrCode: statusError{
 | 
							ErrCode: statusError{
 | 
				
			||||||
			Code: http.StatusInternalServerError,
 | 
								ExpectedCode: http.StatusAccepted,
 | 
				
			||||||
 | 
								ErrCode:      http.StatusInternalServerError,
 | 
				
			||||||
			Err:          errors.New("internal error"),
 | 
								Err:          errors.New("internal error"),
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		SuccessMsg: "Logged!",
 | 
							SuccessMsg: "Logged!",
 | 
				
			||||||
@ -79,7 +82,8 @@ func (app *Config) LogItem(w http.ResponseWriter, entry LogPayload) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type statusError struct {
 | 
					type statusError struct {
 | 
				
			||||||
	Code int
 | 
						ExpectedCode int
 | 
				
			||||||
 | 
						ErrCode      int
 | 
				
			||||||
	Err          error
 | 
						Err          error
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -97,6 +101,7 @@ func (app *Config) callService(w http.ResponseWriter, ms Microservice) {
 | 
				
			|||||||
		app.errorJSON(w, err, http.StatusBadRequest)
 | 
							app.errorJSON(w, err, http.StatusBadRequest)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						log.Println(ms.Input)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// call the service
 | 
						// call the service
 | 
				
			||||||
	req, err := http.NewRequest(
 | 
						req, err := http.NewRequest(
 | 
				
			||||||
@ -120,8 +125,8 @@ func (app *Config) callService(w http.ResponseWriter, ms Microservice) {
 | 
				
			|||||||
	log.Println(resp.Body)
 | 
						log.Println(resp.Body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// make sure we get back the correct status code
 | 
						// make sure we get back the correct status code
 | 
				
			||||||
	if resp.StatusCode != http.StatusAccepted {
 | 
						if resp.StatusCode != ms.ErrCode.ExpectedCode {
 | 
				
			||||||
		app.errorJSON(w, ms.ErrCode.Err, ms.ErrCode.Code)
 | 
							app.errorJSON(w, ms.ErrCode.Err, ms.ErrCode.ErrCode)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -9,6 +9,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                <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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                <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;">
 | 
				
			||||||
                    <span class="text-muted">Output shows here...</span>
 | 
					                    <span class="text-muted">Output shows here...</span>
 | 
				
			||||||
@ -36,6 +37,7 @@
 | 
				
			|||||||
    <script>
 | 
					    <script>
 | 
				
			||||||
        let brokerBtn = document.getElementById("brokerBtn");
 | 
					        let brokerBtn = document.getElementById("brokerBtn");
 | 
				
			||||||
        let authBrokerBtn = document.getElementById("authBrokerBtn");
 | 
					        let authBrokerBtn = document.getElementById("authBrokerBtn");
 | 
				
			||||||
 | 
					        let logBtn = document.getElementById("logBtn");
 | 
				
			||||||
        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");
 | 
				
			||||||
@ -91,5 +93,38 @@
 | 
				
			|||||||
                    output.innerHTML += "<br><br>Error: " + error;
 | 
					                    output.innerHTML += "<br><br>Error: " + error;
 | 
				
			||||||
                });
 | 
					                });
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        logBtn.addEventListener("click", () => {
 | 
				
			||||||
 | 
					            const payload = {
 | 
				
			||||||
 | 
					                action: "log",
 | 
				
			||||||
 | 
					                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;
 | 
				
			||||||
 | 
					                });
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    </script>
 | 
					    </script>
 | 
				
			||||||
{{end}}
 | 
					{{end}}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,7 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
	"logger/data"
 | 
						"logger/data"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@ -18,6 +19,7 @@ func (app *Config) WriteLog(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		app.errorJSON(w, err)
 | 
							app.errorJSON(w, err)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						log.Println("requestpayload", requestPayload)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// insert data
 | 
						// insert data
 | 
				
			||||||
	event := data.LogEntry{
 | 
						event := data.LogEntry{
 | 
				
			||||||
@ -25,6 +27,8 @@ func (app *Config) WriteLog(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		Data: requestPayload.Data,
 | 
							Data: requestPayload.Data,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						log.Println("event", event)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = app.Models.LogEntry.Insert(event)
 | 
						err = app.Models.LogEntry.Insert(event)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		app.errorJSON(w, err)
 | 
							app.errorJSON(w, err)
 | 
				
			||||||
 | 
				
			|||||||
@ -8,6 +8,7 @@ import (
 | 
				
			|||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"go.mongodb.org/mongo-driver/bson"
 | 
				
			||||||
	"go.mongodb.org/mongo-driver/mongo"
 | 
						"go.mongodb.org/mongo-driver/mongo"
 | 
				
			||||||
	"go.mongodb.org/mongo-driver/mongo/options"
 | 
						"go.mongodb.org/mongo-driver/mongo/options"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@ -42,6 +43,8 @@ func main() {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						client = mongoClient
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	app := Config{
 | 
						app := Config{
 | 
				
			||||||
		Models: data.New(client),
 | 
							Models: data.New(client),
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -75,6 +78,12 @@ func connectToMongo() (*mongo.Client, error) {
 | 
				
			|||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var result bson.M
 | 
				
			||||||
 | 
						if err = client.Database("admin").RunCommand(context.TODO(), bson.D{{Key: "ping", Value: 1}}).Decode(&result); err != nil {
 | 
				
			||||||
 | 
							log.Println("Error ping:", err)
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	log.Println("Connected to Mongo")
 | 
						log.Println("Connected to Mongo")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return client, nil
 | 
						return client, nil
 | 
				
			||||||
 | 
				
			|||||||
@ -35,13 +35,15 @@ type LogEntry struct {
 | 
				
			|||||||
func (l *LogEntry) Insert(entry LogEntry) error {
 | 
					func (l *LogEntry) Insert(entry LogEntry) error {
 | 
				
			||||||
	collection := client.Database("logs").Collection("logs")
 | 
						collection := client.Database("logs").Collection("logs")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	_, err := collection.InsertOne(context.TODO(), LogEntry{
 | 
						newLog := LogEntry{
 | 
				
			||||||
		Name:      entry.Name,
 | 
							Name:      entry.Name,
 | 
				
			||||||
		Data:      entry.Data,
 | 
							Data:      entry.Data,
 | 
				
			||||||
		CreatedAt: time.Now(),
 | 
							CreatedAt: time.Now(),
 | 
				
			||||||
		UpdatedAt: time.Now(),
 | 
							UpdatedAt: time.Now(),
 | 
				
			||||||
	})
 | 
						}
 | 
				
			||||||
 | 
						_, err := collection.InsertOne(context.TODO(), newLog)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Println("Insert error:", err)
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
 | 
				
			|||||||
@ -51,9 +51,8 @@ services:
 | 
				
			|||||||
    ports:
 | 
					    ports:
 | 
				
			||||||
      - "27017:27017"
 | 
					      - "27017:27017"
 | 
				
			||||||
    environment:
 | 
					    environment:
 | 
				
			||||||
      MONGO_INITDB_DATABASE: logs
 | 
					      MONGO_INITDB_ROOT_USERNAME: admin
 | 
				
			||||||
      MONGO_INITDB_USERNAME: admin
 | 
					      MONGO_INITDB_ROOT_PASSWORD: password
 | 
				
			||||||
      MONGO_INITDB_PASSWORD: password
 | 
					 | 
				
			||||||
    volumes:
 | 
					    volumes:
 | 
				
			||||||
      - ./db-data/mongo:/data/db
 | 
					      - ./db-data/mongo:/data/db
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -62,3 +61,14 @@ services:
 | 
				
			|||||||
    restart: always
 | 
					    restart: always
 | 
				
			||||||
    ports:
 | 
					    ports:
 | 
				
			||||||
      - 8090:8080
 | 
					      - 8090:8080
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  mongo-express:
 | 
				
			||||||
 | 
					    image: mongo-express
 | 
				
			||||||
 | 
					    restart: always
 | 
				
			||||||
 | 
					    ports:
 | 
				
			||||||
 | 
					      - 8091:8081
 | 
				
			||||||
 | 
					    environment:
 | 
				
			||||||
 | 
					      ME_CONFIG_MONGODB_ADMINUSERNAME: admin
 | 
				
			||||||
 | 
					      ME_CONFIG_MONGODB_ADMINPASSWORD: password
 | 
				
			||||||
 | 
					      ME_CONFIG_MONGODB_URL: mongodb://admin:password@mongo:27017/
 | 
				
			||||||
 | 
					      ME_CONFIG_BASICAUTH: false
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user