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