[QS]: Comment \lib
This commit is contained in:
		
							parent
							
								
									7d67e04ce3
								
							
						
					
					
						commit
						591695c0b6
					
				| 
						 | 
				
			
			@ -1,5 +1,4 @@
 | 
			
		|||
// Package database provides the
 | 
			
		||||
// functionality to open, close and use a database
 | 
			
		||||
// Package that provides the functionality to open, close and use a database
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
| 
						 | 
				
			
			@ -10,32 +9,31 @@ import (
 | 
			
		|||
	"github.com/genofire/hs_master-kss-monolith/lib/log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Database connection for writing
 | 
			
		||||
// Database connection for writing purposes
 | 
			
		||||
var Write *gorm.DB
 | 
			
		||||
 | 
			
		||||
// Database connection for reading
 | 
			
		||||
// Database connection for reading purposes
 | 
			
		||||
var Read *gorm.DB
 | 
			
		||||
 | 
			
		||||
// Configuration files
 | 
			
		||||
var (
 | 
			
		||||
	config  *Config
 | 
			
		||||
	runtime []interface{}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// configuration for the database connection
 | 
			
		||||
// Configuration of the database connection
 | 
			
		||||
type Config struct {
 | 
			
		||||
	// type of database: current support sqlite and postgres
 | 
			
		||||
	// (by request other could be enabled)
 | 
			
		||||
	// type of the database, currently supports sqlite and postgres
 | 
			
		||||
	Type string
 | 
			
		||||
	// connection configuration
 | 
			
		||||
	Connection string
 | 
			
		||||
	// maybe create another connection just for reading
 | 
			
		||||
	// create another connection for reading only
 | 
			
		||||
	ReadConnection string
 | 
			
		||||
	// enable logging the generated sql string
 | 
			
		||||
	// enable logging of the generated sql string
 | 
			
		||||
	Logging bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Function to open a database and set the given configuration
 | 
			
		||||
// Input: the configuration data c
 | 
			
		||||
func Open(c Config) (err error) {
 | 
			
		||||
	writeLog := log.Log.WithField("db", "write")
 | 
			
		||||
	config = &c
 | 
			
		||||
| 
						 | 
				
			
			@ -75,7 +73,6 @@ func Close() {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// Function to add a model to the runtime
 | 
			
		||||
// Input: interface m
 | 
			
		||||
func AddModel(m interface{}) {
 | 
			
		||||
	runtime = append(runtime, m)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,4 @@
 | 
			
		|||
// Package database provides the
 | 
			
		||||
// functionality to open, close and use a database
 | 
			
		||||
// Package that provides the functionality to open, close and use a database
 | 
			
		||||
package database
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
| 
						 | 
				
			
			@ -14,7 +13,6 @@ type TestModel struct {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// Function to test the error handling for the database opening
 | 
			
		||||
// Input: pointer to testobject t
 | 
			
		||||
func TestOpenNoDB(t *testing.T) {
 | 
			
		||||
	assert := assert.New(t)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -25,7 +23,6 @@ func TestOpenNoDB(t *testing.T) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// Function to test the opening of one database
 | 
			
		||||
// Input: pointer to testobject t
 | 
			
		||||
func TestOpenOneDB(t *testing.T) {
 | 
			
		||||
	assert := assert.New(t)
 | 
			
		||||
	AddModel(&TestModel{})
 | 
			
		||||
| 
						 | 
				
			
			@ -50,7 +47,6 @@ func TestOpenOneDB(t *testing.T) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// Function to test the opening of a second database
 | 
			
		||||
// Input: pointer to testobject t
 | 
			
		||||
func TestOpenTwoDB(t *testing.T) {
 | 
			
		||||
	assert := assert.New(t)
 | 
			
		||||
	AddModel(&TestModel{})
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,4 @@
 | 
			
		|||
// Package http provides the
 | 
			
		||||
// logic of the webserver
 | 
			
		||||
// Package that provides the logic of the webserver
 | 
			
		||||
package http
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
| 
						 | 
				
			
			@ -8,8 +7,7 @@ import (
 | 
			
		|||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Function to read data from a request via json format
 | 
			
		||||
// Input: pointer to http request r, interface to
 | 
			
		||||
// Function to read data from a http request via json format (input)
 | 
			
		||||
func Read(r *http.Request, to interface{}) (err error) {
 | 
			
		||||
	if r.Header.Get("Content-Type") != "application/json" {
 | 
			
		||||
		err = errors.New("no json data recived")
 | 
			
		||||
| 
						 | 
				
			
			@ -19,8 +17,7 @@ func Read(r *http.Request, to interface{}) (err error) {
 | 
			
		|||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Function to write data as json to a http output
 | 
			
		||||
// Input: http response writer w, interface data
 | 
			
		||||
// Function to write data as json to a http response (output)
 | 
			
		||||
func Write(w http.ResponseWriter, data interface{}) {
 | 
			
		||||
	js, err := json.Marshal(data)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,4 @@
 | 
			
		|||
// Package http provides the
 | 
			
		||||
// logic of the webserver
 | 
			
		||||
// Package that provides the logic of the webserver
 | 
			
		||||
package http
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
| 
						 | 
				
			
			@ -12,8 +11,7 @@ import (
 | 
			
		|||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Function to test the writing into a http response
 | 
			
		||||
// Input: pointer to testing object
 | 
			
		||||
// Function to test write()
 | 
			
		||||
func TestWrite(t *testing.T) {
 | 
			
		||||
	assert := assert.New(t)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -37,8 +35,7 @@ func TestWrite(t *testing.T) {
 | 
			
		|||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Function to test the reading from a http response
 | 
			
		||||
// Input: pointer to testing object
 | 
			
		||||
// Function to test read()
 | 
			
		||||
func TestRead(t *testing.T) {
 | 
			
		||||
	assert := assert.New(t)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,14 +1,12 @@
 | 
			
		|||
// Package http provides the
 | 
			
		||||
// logic of the webserver
 | 
			
		||||
// Package that provides the logic of the webserver
 | 
			
		||||
package http
 | 
			
		||||
 | 
			
		||||
import "net/http"
 | 
			
		||||
 | 
			
		||||
// format of a function to bind it for the middleware handler
 | 
			
		||||
// Format of a function to bind it to the middleware handler
 | 
			
		||||
type HasPermission func(string, int) (bool, error)
 | 
			
		||||
 | 
			
		||||
// Function to evaluate the permission and implent an error handling
 | 
			
		||||
// Input: http response writer w, pointer to htto request r, bool variable HasPermission perm, int variable permission (form)
 | 
			
		||||
// Function to evaluate the permission and implement an error handling
 | 
			
		||||
func PermissionHandler(h func(w http.ResponseWriter, r *http.Request), perm HasPermission, permission int) func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		session, err := r.Cookie("session")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,4 @@
 | 
			
		|||
// Package http provides the
 | 
			
		||||
// logic of the webserver
 | 
			
		||||
// Package that provides the logic of the webserver
 | 
			
		||||
package http
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
| 
						 | 
				
			
			@ -12,7 +11,6 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
// Function to the the permission and it's error handling
 | 
			
		||||
// Input: pointer to testing object
 | 
			
		||||
func TestPermission(t *testing.T) {
 | 
			
		||||
	assert := assert.New(t)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -30,7 +28,7 @@ func TestPermission(t *testing.T) {
 | 
			
		|||
 | 
			
		||||
	r.AddCookie(&http.Cookie{Name: "session"})
 | 
			
		||||
 | 
			
		||||
	// HasPermission respond a true
 | 
			
		||||
	// HasPermission responds true
 | 
			
		||||
	reached = false
 | 
			
		||||
	PermissionHandler(func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		reached = true
 | 
			
		||||
| 
						 | 
				
			
			@ -39,7 +37,7 @@ func TestPermission(t *testing.T) {
 | 
			
		|||
	}, 1)(w, r)
 | 
			
		||||
	assert.True(reached)
 | 
			
		||||
 | 
			
		||||
	// HasPermission respond a false
 | 
			
		||||
	// HasPermission responds false
 | 
			
		||||
	reached = false
 | 
			
		||||
	PermissionHandler(func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		reached = true
 | 
			
		||||
| 
						 | 
				
			
			@ -48,7 +46,7 @@ func TestPermission(t *testing.T) {
 | 
			
		|||
	}, 1)(w, r)
 | 
			
		||||
	assert.False(reached)
 | 
			
		||||
 | 
			
		||||
	// HasPermission respond a error
 | 
			
		||||
	// HasPermission responds error
 | 
			
		||||
	reached = false
 | 
			
		||||
	PermissionHandler(func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		reached = true
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,4 @@
 | 
			
		|||
// Package log provides the
 | 
			
		||||
// functionality to start und initialize to logger
 | 
			
		||||
// Package that provides the functionality to start und initialize the logger
 | 
			
		||||
package log
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
| 
						 | 
				
			
			@ -9,7 +8,7 @@ import (
 | 
			
		|||
	logger "github.com/Sirupsen/logrus"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// current logger with configuration
 | 
			
		||||
// Crrrent logger with it's configuration
 | 
			
		||||
var Log *logger.Logger
 | 
			
		||||
 | 
			
		||||
// Function to initiate a new logger
 | 
			
		||||
| 
						 | 
				
			
			@ -19,7 +18,6 @@ func init() {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// Function to add the information of a http request to the log
 | 
			
		||||
// Input: pointer to the http request r
 | 
			
		||||
func HTTP(r *http.Request) *logger.Entry {
 | 
			
		||||
	ip := r.Header.Get("X-Forwarded-For")
 | 
			
		||||
	if len(ip) <= 1 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,4 @@
 | 
			
		|||
// Package log provides the
 | 
			
		||||
// functionality to start und initialize to logger
 | 
			
		||||
// Package that provides the functionality to start und initialize the logger
 | 
			
		||||
package log
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
| 
						 | 
				
			
			@ -10,7 +9,6 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
// Function to test the logging
 | 
			
		||||
// Input: pointer to teh testing object
 | 
			
		||||
func TestLog(t *testing.T) {
 | 
			
		||||
	assertion := assert.New(t)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,16 +1,16 @@
 | 
			
		|||
// A little lib for cronjobs to run it in background
 | 
			
		||||
// Package with a lib for cronjobs to run in background
 | 
			
		||||
package worker
 | 
			
		||||
 | 
			
		||||
import "time"
 | 
			
		||||
 | 
			
		||||
// a struct which handle the job
 | 
			
		||||
// Struct which handles the job
 | 
			
		||||
type Worker struct {
 | 
			
		||||
	every time.Duration
 | 
			
		||||
	run   func()
 | 
			
		||||
	quit  chan struct{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// create a new Worker with timestamp run every and his function
 | 
			
		||||
// Function to reate a new Worker with a timestamp, run, every and it's function
 | 
			
		||||
func NewWorker(every time.Duration, f func()) (w *Worker) {
 | 
			
		||||
	w = &Worker{
 | 
			
		||||
		every: every,
 | 
			
		||||
| 
						 | 
				
			
			@ -20,8 +20,8 @@ func NewWorker(every time.Duration, f func()) (w *Worker) {
 | 
			
		|||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// start the worker
 | 
			
		||||
// please us it as a goroutine: go w.Start()
 | 
			
		||||
// Function to start the Worker
 | 
			
		||||
// (please us it as a goroutine with go w.Start())
 | 
			
		||||
func (w *Worker) Start() {
 | 
			
		||||
	ticker := time.NewTicker(w.every)
 | 
			
		||||
	for {
 | 
			
		||||
| 
						 | 
				
			
			@ -35,7 +35,7 @@ func (w *Worker) Start() {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// stop the worker
 | 
			
		||||
// Function to stop the Worker
 | 
			
		||||
func (w *Worker) Close() {
 | 
			
		||||
	close(w.quit)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,4 @@
 | 
			
		|||
// Package with a lib for cronjobs to run in background
 | 
			
		||||
package worker
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
| 
						 | 
				
			
			@ -7,6 +8,7 @@ import (
 | 
			
		|||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Function to test the Worker
 | 
			
		||||
func TestWorker(t *testing.T) {
 | 
			
		||||
	assert := assert.New(t)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in New Issue