genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[QS]: comments \runtime

This commit is contained in:
mlabusch 2017-05-03 08:02:29 +02:00
parent 1259f9cfe3
commit 7cc0d98214
10 changed files with 50 additions and 21 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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)

View File

@ -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)
}

View File

@ -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}

View File

@ -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

View File

@ -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{

View File

@ -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

View File

@ -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)

View File

@ -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