[QS]: comments \runtime
This commit is contained in:
parent
1259f9cfe3
commit
7cc0d98214
|
@ -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{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}
|
\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{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
|
\item \texttt{status.go}: Funktion, die den Status des Microservice abfragt
|
||||||
\end{itemize}
|
\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}
|
\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{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{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{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}
|
\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{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{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{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}
|
\end{itemize}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
\newpage
|
\newpage
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Package with supporting functionality to run the microservice
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -8,28 +9,32 @@ import (
|
||||||
"github.com/genofire/hs_master-kss-monolith/lib/log"
|
"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
|
var PermissionURL string
|
||||||
|
|
||||||
// type of permission
|
// Type of permission
|
||||||
type Permission int
|
type Permission int
|
||||||
|
|
||||||
// some permission (see Permission)
|
// Some permissions (the real permissions need to come from the permission microservice)
|
||||||
const (
|
const (
|
||||||
// has the user the permission to create need goods of a product
|
// permission to add goods to the stock
|
||||||
// e.g. if a good received and now availablity to sell
|
// e.g. if a good is received and now available to sell
|
||||||
PermissionCreateGood = 1
|
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
|
PermissionDeleteGood = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Struct that holds the information for a permission cache
|
||||||
type permissionMicroServiceCache struct {
|
type permissionMicroServiceCache struct {
|
||||||
LastCheck time.Time
|
LastCheck time.Time
|
||||||
session string
|
session string
|
||||||
permissions map[Permission]boolMicroServiceCache
|
permissions map[Permission]boolMicroServiceCache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Function to check, if a user has a permission
|
||||||
func (c *permissionMicroServiceCache) HasPermission(p Permission) (bool, error) {
|
func (c *permissionMicroServiceCache) HasPermission(p Permission) (bool, error) {
|
||||||
c.LastCheck = time.Now()
|
c.LastCheck = time.Now()
|
||||||
if cache, ok := c.permissions[p]; ok {
|
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
|
return c.permissions[p].Value, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache for permissions
|
||||||
var permissionCache map[string]*permissionMicroServiceCache
|
var permissionCache map[string]*permissionMicroServiceCache
|
||||||
|
|
||||||
|
// Function to initialize the permission cache
|
||||||
func init() {
|
func init() {
|
||||||
permissionCache = make(map[string]*permissionMicroServiceCache)
|
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) {
|
func HasPermission(session string, p int) (bool, error) {
|
||||||
_, ok := permissionCache[session]
|
_, ok := permissionCache[session]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Package with supporting functionality to run the microservice
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,6 +8,7 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Function to test the permission handling
|
||||||
func TestAuth(t *testing.T) {
|
func TestAuth(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Package with supporting functionality to run the microservice
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,13 +8,13 @@ import (
|
||||||
"github.com/genofire/hs_master-kss-monolith/models"
|
"github.com/genofire/hs_master-kss-monolith/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// config of the cache worker
|
// Configuration of the cache Worker
|
||||||
var CacheConfig models.CacheWorkerConfig
|
var CacheConfig models.CacheWorkerConfig
|
||||||
|
|
||||||
// command which is runned in the cache worker
|
// Function to run the cache Worker
|
||||||
func CleanCache() {
|
func CleanCache() {
|
||||||
before := time.Now().Add(-CacheConfig.After.Duration)
|
before := time.Now().Add(-CacheConfig.After.Duration)
|
||||||
// Cache if product exists
|
// Cache, if product exists
|
||||||
for index, cache := range productExistCache {
|
for index, cache := range productExistCache {
|
||||||
if before.After(cache.LastCheck) {
|
if before.After(cache.LastCheck) {
|
||||||
delete(productExistCache, index)
|
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) {
|
func NewCacheWorker() (w *worker.Worker) {
|
||||||
return worker.NewWorker(CacheConfig.Every.Duration, CleanCache)
|
return worker.NewWorker(CacheConfig.Every.Duration, CleanCache)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Package with supporting functionality to run the microservice
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,6 +8,7 @@ import (
|
||||||
"github.com/genofire/hs_master-kss-monolith/models"
|
"github.com/genofire/hs_master-kss-monolith/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Function to test the cache Worker
|
||||||
func TestCacheWorker(t *testing.T) {
|
func TestCacheWorker(t *testing.T) {
|
||||||
|
|
||||||
productExistCache[2] = boolMicroServiceCache{LastCheck: time.Now(), Value: true}
|
productExistCache[2] = boolMicroServiceCache{LastCheck: time.Now(), Value: true}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Package with supporting functionality to run the microservice
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -8,13 +9,14 @@ import (
|
||||||
"github.com/genofire/hs_master-kss-monolith/models"
|
"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 {
|
func NewGoodReleaseWorker(grc models.GoodReleaseConfig) *worker.Worker {
|
||||||
return worker.NewWorker(grc.Every.Duration, func() {
|
return worker.NewWorker(grc.Every.Duration, func() {
|
||||||
goodRelease(grc.After.Duration)
|
goodRelease(grc.After.Duration)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to unlock goods after a specified time
|
||||||
func goodRelease(unlockAfter time.Duration) int64 {
|
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})
|
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
|
return res.RowsAffected
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Package with supporting functionality to run the microservice
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -10,6 +11,7 @@ import (
|
||||||
"github.com/genofire/hs_master-kss-monolith/models"
|
"github.com/genofire/hs_master-kss-monolith/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Function to test the unlocking of goods
|
||||||
func TestGoodRelease(t *testing.T) {
|
func TestGoodRelease(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
database.Open(database.Config{
|
database.Open(database.Config{
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Package with supporting functionality to run the microservice
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -8,21 +9,24 @@ import (
|
||||||
"github.com/genofire/hs_master-kss-monolith/lib/log"
|
"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
|
var ProductURL string
|
||||||
|
|
||||||
|
// Struct tht holds the information on the microservice cache
|
||||||
type boolMicroServiceCache struct {
|
type boolMicroServiceCache struct {
|
||||||
LastCheck time.Time
|
LastCheck time.Time
|
||||||
Value bool
|
Value bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache for existing products
|
||||||
var productExistCache map[int64]boolMicroServiceCache
|
var productExistCache map[int64]boolMicroServiceCache
|
||||||
|
|
||||||
|
// Function to initialize the cache for existing products
|
||||||
func init() {
|
func init() {
|
||||||
productExistCache = make(map[int64]boolMicroServiceCache)
|
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) {
|
func ProductExists(id int64) (bool, error) {
|
||||||
if cache, ok := productExistCache[id]; ok {
|
if cache, ok := productExistCache[id]; ok {
|
||||||
return cache.Value, nil
|
return cache.Value, nil
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Package with supporting functionality to run the microservice
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,6 +8,7 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Function to test, if and which products exist (get information from the products catalogue)
|
||||||
func TestProductExists(t *testing.T) {
|
func TestProductExists(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,5 @@
|
||||||
// some functionality to handle it in background which is intermeshed
|
// Package with supporting functionality to run the microservice
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
|
// some functionality to handle it in background which is intermeshed
|
||||||
|
|
Reference in New Issue