From 7cc0d982143cd088e34565a5e938c6f8b4e46fc7 Mon Sep 17 00:00:00 2001 From: mlabusch Date: Wed, 3 May 2017 08:02:29 +0200 Subject: [PATCH] [QS]: comments \runtime --- .../microservice_stock/chapter/Struktur.tex | 14 +++++++---- runtime/auth.go | 23 ++++++++++++------- runtime/auth_test.go | 2 ++ runtime/cache_worker.go | 9 ++++---- runtime/cache_worker_test.go | 2 ++ runtime/good_release.go | 4 +++- runtime/good_release_test.go | 2 ++ runtime/productcache.go | 8 +++++-- runtime/productcache_test.go | 2 ++ runtime/{main.go => runtime.go} | 5 +++- 10 files changed, 50 insertions(+), 21 deletions(-) rename runtime/{main.go => runtime.go} (56%) diff --git a/documentation/microservice_stock/chapter/Struktur.tex b/documentation/microservice_stock/chapter/Struktur.tex index 0261e07..8396e86 100644 --- a/documentation/microservice_stock/chapter/Struktur.tex +++ b/documentation/microservice_stock/chapter/Struktur.tex @@ -27,20 +27,22 @@ Der Presentation Layer umfasst alle Packages, die sich mit der eigentlichen Dar \paragraph{cmd:} Go-File main.go, welches alle Angaben zu den Config-Files der Applikation enthält -\paragraph{http:} Go-Files, die die Anwendungslogik (Funktionen) beinhalten. +\paragraph{http:} Go-Files, die die Anwendungslogik (Funktionen) und die API-Rounten beinhalten. \begin{itemize} - \item \texttt{good.go}: Funktionen für die Auflistung und Zählung der vorhandenen Waren sowie die Feststellung ihrer Verfügbarkeit zusammen - \item \texttt{good\_temp.go}: Hilfsfunktionen, die für die Darstellung des Warenbestandes als Ampel im Kunden-Frontend benötigt werden \item \texttt{bindapi.go}: Funktionen, die für das Binden der URL-Pfade notwendig sind + \item \texttt{good.go}: Funktionen fpr das Hinzufügen von Waren zum Warenbestand + \item \texttt{good_show.go}: Funktionen für die Auflistung und Zählung der vorhandenen Waren sowie die Feststellung ihrer Verfügbarkeit zusammen + \item \texttt{good\_temp.go}: Hilfsfunktionen, die für die Darstellung des Warenbestandes als Ampel im Kunden-Frontend benötigt werden \item \texttt{status.go}: Funktion, die den Status des Microservice abfragt \end{itemize} -\paragraph{models:} Go-Files, die Structs und zugehörige Hilfsfunktionen beinhalten +\paragraph{models:} Go-Files, die Structs und zugehörige Hilfsfunktionen (hauptsächlich statischen Inhalt des Microservice) beinhalten \begin{itemize} \item \texttt{config.go}: Structs mit den Informationen zur Konfiguration des Webservers, der Datenbank und dem Cache-Management sowie Hilfsfunktionen zum Lesen von Config-Files \item \texttt{duration.go}: Structs und Hilfsfunktionen zur Definition eines Typs für Zeitangaben \item \texttt{good.go}: Structs und Hilfsfunktionen zur Darstellung von Waren, hier werden auch die beschriebenen Funktionalitäten wie das Blockieren von Waren beschrieben + \item \texttt{structstorage}: \end{itemize} @@ -49,8 +51,10 @@ Der Presentation Layer umfasst alle Packages, die sich mit der eigentlichen Dar \item \texttt{auth.go}: Hilfsfunktionen zur Prüfung, ob eine Berechtigung für den Zugriff vorliegt \item \texttt{cache\_worker.go}: Hilfsfunktionen für das Löschen und Anlegen von Cache-Workers \item \texttt{good\_release.go}: Hilfsfunktionen zum Entsperren von blockierten Waren - \item \texttt{product.go}: Hilfsfunktionen zum Anlegen eines Caches für Produkte und zur Prüfung + \item \texttt{productcache.go}: Hilfsfunktionen zum Anlegen eines Caches für Produkte und zur Prüfung + \item \texttt{runtime.go}: Übergreifende Hintergrundfunktionalitäten \end{itemize} + \newpage diff --git a/runtime/auth.go b/runtime/auth.go index 518d6d7..262b8f7 100644 --- a/runtime/auth.go +++ b/runtime/auth.go @@ -1,3 +1,4 @@ +// Package with supporting functionality to run the microservice package runtime import ( @@ -8,28 +9,32 @@ import ( "github.com/genofire/hs_master-kss-monolith/lib/log" ) -// url to the microservice which manage the permissions +// URL to the microservice which manages permissions var PermissionURL string -// type of permission +// Type of permission type Permission int -// some permission (see Permission) +// Some permissions (the real permissions need to come from the permission microservice) const ( - // has the user the permission to create need goods of a product - // e.g. if a good received and now availablity to sell + // permission to add goods to the stock + // e.g. if a good is received and now available to sell PermissionCreateGood = 1 - // has the user the permission to delete need goods of a product - // e.g. if a good become rancid and has to remove from stock + + // permission to delete goods from the stock + // e.g. if a good become rancid and has to be removed PermissionDeleteGood = 2 ) +// Struct that holds the information for a permission cache type permissionMicroServiceCache struct { LastCheck time.Time session string permissions map[Permission]boolMicroServiceCache } + +// Function to check, if a user has a permission func (c *permissionMicroServiceCache) HasPermission(p Permission) (bool, error) { c.LastCheck = time.Now() if cache, ok := c.permissions[p]; ok { @@ -56,13 +61,15 @@ func (c *permissionMicroServiceCache) HasPermission(p Permission) (bool, error) return c.permissions[p].Value, err } +// Cache for permissions var permissionCache map[string]*permissionMicroServiceCache +// Function to initialize the permission cache func init() { permissionCache = make(map[string]*permissionMicroServiceCache) } -// check if the client with the session string has a permissions (see Permission) +// Function to check, if the current session has any permissions func HasPermission(session string, p int) (bool, error) { _, ok := permissionCache[session] if !ok { diff --git a/runtime/auth_test.go b/runtime/auth_test.go index acad310..2de1c9b 100644 --- a/runtime/auth_test.go +++ b/runtime/auth_test.go @@ -1,3 +1,4 @@ +// Package with supporting functionality to run the microservice package runtime import ( @@ -7,6 +8,7 @@ import ( "github.com/stretchr/testify/assert" ) +// Function to test the permission handling func TestAuth(t *testing.T) { assert := assert.New(t) diff --git a/runtime/cache_worker.go b/runtime/cache_worker.go index 70067e3..d95df59 100644 --- a/runtime/cache_worker.go +++ b/runtime/cache_worker.go @@ -1,3 +1,4 @@ +// Package with supporting functionality to run the microservice package runtime import ( @@ -7,13 +8,13 @@ import ( "github.com/genofire/hs_master-kss-monolith/models" ) -// config of the cache worker +// Configuration of the cache Worker var CacheConfig models.CacheWorkerConfig -// command which is runned in the cache worker +// Function to run the cache Worker func CleanCache() { before := time.Now().Add(-CacheConfig.After.Duration) - // Cache if product exists + // Cache, if product exists for index, cache := range productExistCache { if before.After(cache.LastCheck) { delete(productExistCache, index) @@ -27,7 +28,7 @@ func CleanCache() { } } -// create a worker to clean the caches which stored from other microservice +// Function to create a Worker and to clean the caches from other microservice func NewCacheWorker() (w *worker.Worker) { return worker.NewWorker(CacheConfig.Every.Duration, CleanCache) } diff --git a/runtime/cache_worker_test.go b/runtime/cache_worker_test.go index 629d133..7476f51 100644 --- a/runtime/cache_worker_test.go +++ b/runtime/cache_worker_test.go @@ -1,3 +1,4 @@ +// Package with supporting functionality to run the microservice package runtime import ( @@ -7,6 +8,7 @@ import ( "github.com/genofire/hs_master-kss-monolith/models" ) +// Function to test the cache Worker func TestCacheWorker(t *testing.T) { productExistCache[2] = boolMicroServiceCache{LastCheck: time.Now(), Value: true} diff --git a/runtime/good_release.go b/runtime/good_release.go index fd0742f..5a6a5ce 100644 --- a/runtime/good_release.go +++ b/runtime/good_release.go @@ -1,3 +1,4 @@ +// Package with supporting functionality to run the microservice package runtime import ( @@ -8,13 +9,14 @@ import ( "github.com/genofire/hs_master-kss-monolith/models" ) -// create a worker to unlock goods which are locked by clients +// Function to create a Worker and to unlock goods func NewGoodReleaseWorker(grc models.GoodReleaseConfig) *worker.Worker { return worker.NewWorker(grc.Every.Duration, func() { goodRelease(grc.After.Duration) }) } +// Function to unlock goods after a specified time func goodRelease(unlockAfter time.Duration) int64 { res := database.Write.Model(&models.Good{}).Where("locked_secret is not NULL and locked_at < ?", time.Now().Add(-unlockAfter)).Updates(map[string]interface{}{"locked_secret": "", "locked_at": nil}) return res.RowsAffected diff --git a/runtime/good_release_test.go b/runtime/good_release_test.go index bd557a7..048cfb3 100644 --- a/runtime/good_release_test.go +++ b/runtime/good_release_test.go @@ -1,3 +1,4 @@ +// Package with supporting functionality to run the microservice package runtime import ( @@ -10,6 +11,7 @@ import ( "github.com/genofire/hs_master-kss-monolith/models" ) +// Function to test the unlocking of goods func TestGoodRelease(t *testing.T) { assert := assert.New(t) database.Open(database.Config{ diff --git a/runtime/productcache.go b/runtime/productcache.go index 79e6da1..52effa9 100644 --- a/runtime/productcache.go +++ b/runtime/productcache.go @@ -1,3 +1,4 @@ +// Package with supporting functionality to run the microservice package runtime import ( @@ -8,21 +9,24 @@ import ( "github.com/genofire/hs_master-kss-monolith/lib/log" ) -// url to the microservice which manage the products +// URL to the microservice which manages the products (product catalogue) var ProductURL string +// Struct tht holds the information on the microservice cache type boolMicroServiceCache struct { LastCheck time.Time Value bool } +// Cache for existing products var productExistCache map[int64]boolMicroServiceCache +// Function to initialize the cache for existing products func init() { productExistCache = make(map[int64]boolMicroServiceCache) } -// check on the other microservice if the product exists +// Function to check on the other microservice (product catalogue) if the product exists func ProductExists(id int64) (bool, error) { if cache, ok := productExistCache[id]; ok { return cache.Value, nil diff --git a/runtime/productcache_test.go b/runtime/productcache_test.go index 979185b..71de270 100644 --- a/runtime/productcache_test.go +++ b/runtime/productcache_test.go @@ -1,3 +1,4 @@ +// Package with supporting functionality to run the microservice package runtime import ( @@ -7,6 +8,7 @@ import ( "github.com/stretchr/testify/assert" ) +// Function to test, if and which products exist (get information from the products catalogue) func TestProductExists(t *testing.T) { assert := assert.New(t) diff --git a/runtime/main.go b/runtime/runtime.go similarity index 56% rename from runtime/main.go rename to runtime/runtime.go index 4826849..5f19351 100644 --- a/runtime/main.go +++ b/runtime/runtime.go @@ -1,2 +1,5 @@ -// some functionality to handle it in background which is intermeshed +// Package with supporting functionality to run the microservice package runtime + +// some functionality to handle it in background which is intermeshed +