Skip to content

Commit 3a7b3ff

Browse files
authored
Merge pull request #4 from DrOctavius/main
http server fixes
2 parents 390ff6c + 8acb55f commit 3a7b3ff

File tree

19 files changed

+150
-101
lines changed

19 files changed

+150
-101
lines changed

Diff for: core/clients/db/client.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
)
1313

1414
// here we store the default clients for specific drivers... (for default instances only)
15+
16+
var DefaultInstanceClient *gorm.DB
1517
var CRDBDefaultInstanceClient *gorm.DB
1618
var MySQLDefaultInstanceClient *gorm.DB
1719

@@ -64,9 +66,17 @@ func New(
6466
// GetDefaultClient -> this is a common function which helps getting the default Database client
6567
// based on the current configuration!
6668
func GetDefaultClient() (*gorm.DB, error) {
69+
// TODO: do we need a lock?!
70+
if DefaultInstanceClient != nil {
71+
return DefaultInstanceClient, nil
72+
}
6773
cl := getDefaultDBClient()
6874
// TODO: add function and variable which can override the get default client!...
69-
return cl.GetDefaultClient()
75+
c, _err := cl.GetDefaultClient()
76+
if _err == nil {
77+
DefaultInstanceClient = c
78+
}
79+
return c, _err
7080
}
7181

7282
// DB -> will get the default client without any errors

Diff for: core/clients/db/filter/filter_conditions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func (f *Input) processGroupCondition(input *GroupConditionInput) *gorm.DB {
251251
for groupIndex, group := range gc.Groups {
252252
// The first item OR/AND operator is ignored
253253

254-
// Process the group
254+
// Check the group
255255
groupDb := f.processGroupCondition(&GroupConditionInput{
256256
// here we should give a clean DB!
257257
DB: input.DB,

Diff for: core/console/command/command.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (c *AddCmd) StopProcess() (bool, error) {
142142
_pid, _err := strconv.Atoi(pid)
143143
p, _err := os.FindProcess(_pid)
144144
if _err != nil {
145-
//log.Println("Process not found!")
145+
//log.Println("Check not found!")
146146
return false, define.Err(0, "process not found -> ", _err.Error())
147147
}
148148

@@ -151,7 +151,7 @@ func (c *AddCmd) StopProcess() (bool, error) {
151151
//log.Println("Failed to Interrupt process!!")
152152
return false, define.Err(0, "failed to interrupt process -> ", _err.Error())
153153
}
154-
//log.Println("Process stopped!")
154+
//log.Println("Check stopped!")
155155
return true, nil
156156
}
157157

Diff for: core/console/menu/menu.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (m *Menu) AddCommand(c *command.AddCmd) *Menu {
233233
return
234234
}
235235

236-
//log.Println("PID", command.Process.Pid)
236+
//log.Println("PID", command.Check.Pid)
237237
if logger.GetAppLogger() != nil {
238238
appLog.Info().Int("pid", _command.Process.Pid).Msg("getting pid")
239239
}

Diff for: core/helpers/filesystem/lock/flock.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func FLock(lockName string, wait bool) bool {
8282
func FRelease(lockName string) {
8383
// Get lock name hashed form
8484
lockNameHash := getLockName(lockName)
85-
// Lock inside the Main Process! (Globally)
85+
// Lock inside the Main Check! (Globally)
8686
locksLocker.Lock()
8787
// Check if there is a key with this lock name!
8888
if fileLock, ok := locks[lockNameHash]; ok {

Diff for: core/helpers/filesystem/realpath/realpath.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Realpath(fPath string) (string, error) {
2626
return "", err
2727
}
2828
} else {
29-
// Process directory
29+
// Check directory
3030
pwd, err = os.Executable()
3131
if err != nil {
3232
return "", err

Diff for: core/listeners/graphql/helper/resolver_helper.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/99designs/gqlgen/graphql"
77
"github.com/gin-gonic/gin"
88
"github.com/kyaxcorp/go-core/core/helpers/_context"
9+
"github.com/kyaxcorp/go-core/core/helpers/err/define"
910
"github.com/kyaxcorp/go-core/core/listeners/http/middlewares/authentication"
1011
"github.com/kyaxcorp/go-core/core/listeners/http/middlewares/connection"
1112
)
@@ -30,7 +31,11 @@ func NewResolverHelper(rh *ResolverHelper) *ResolverHelper {
3031
}
3132

3233
// call it once...
33-
rh.GetGinContext()
34+
_, _err := rh.GetGinContext()
35+
if _err != nil {
36+
panic(define.Err(0, "failed to get gin context -> ", _err.Error()))
37+
//return nil, define.Err(0, "failed to get gin context -> ", _err.Error())
38+
}
3439
rh.GetAuthDetails()
3540
rh.GetConnectionDetails()
3641
if !rh.DisableGetRequestedFields {

Diff for: core/listeners/graphql/scalar/iban.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package scalar

Diff for: core/listeners/graphql/scalar/scalars.graphqls

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ scalar TimeRFC3339Nano
77

88

99
scalar UUID
10-
scalar NonNilUUID
10+
scalar NonNilUUID
11+
12+
scalar IBAN

Diff for: core/listeners/http/middlewares/authentication/auth_functions.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package authentication
22

3-
import "github.com/gin-gonic/gin"
3+
import (
4+
"github.com/gin-gonic/gin"
5+
)
46

57
// ByHeaderKeys > Search in header specific keys
68
func (a *Auth) ByHeaderKeys(authHeaderKeys []string) *Auth {
@@ -51,6 +53,8 @@ func (a *Auth) GetAuthTypeKeyName() string {
5153
func (a *Auth) SetAuthDetails(details *AuthDetails) {
5254
// Saving the authentication details into Http Connection Context
5355
a.C.Set(HttpContextAuthDetailsKey, details)
56+
//ctx := context.WithValue(a.C.Request.Context(), HttpContextAuthDetailsKey, details)
57+
//a.C.Request = a.C.Request.WithContext(ctx)
5458
}
5559

5660
func (a *Auth) Abort(code int, httpCode int, msg string) {

Diff for: core/listeners/http/middlewares/authentication/authentication.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const ByHeader = 1
1818
const ByGetParam = 2
1919
const ByCookie = 3
2020

21-
// CheckIsAuthenticated -> This is the Constructor or first function to call!
22-
func CheckIsAuthenticated() *Auth {
21+
// New -> This is the Constructor or first function to call!
22+
func New() *Auth {
2323
auth := &Auth{}
2424
return auth
2525
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package authentication
2+
3+
import "github.com/gin-gonic/gin"
4+
5+
func (a *Auth) SetGinContext(ctx *gin.Context) *Auth {
6+
a.C = ctx
7+
return a
8+
}
+70-77
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,96 @@
11
package authentication
22

3-
import "github.com/gin-gonic/gin"
3+
func (a *Auth) Check() {
4+
// Set the context to the struct!
5+
receivedAuthToken := ""
6+
authByType := 0
7+
authTypeKeyName := ""
8+
// Start first by checking in headers
49

5-
func (a *Auth) GetHandlerFunc() gin.HandlerFunc {
6-
return func(c *gin.Context) {
7-
// Set the context to the struct!
8-
a.C = c
9-
10-
receivedAuthToken := ""
11-
authByType := 0
12-
authTypeKeyName := ""
13-
// Start first by checking in headers
14-
15-
if len(a.authHeaderKeys) == 0 {
16-
// If there is nothing defined, we set the default one!
17-
a.authHeaderKeys = []string{DefaultHeaderAuthKey}
18-
}
10+
if len(a.authHeaderKeys) == 0 {
11+
// If there is nothing defined, we set the default one!
12+
a.authHeaderKeys = []string{DefaultHeaderAuthKey}
13+
}
1914

20-
if len(a.authHeaderKeys) > 0 {
21-
// If there are defined multiple auth header keys!
22-
for _, authKey := range a.authHeaderKeys {
15+
if len(a.authHeaderKeys) > 0 {
16+
// If there are defined multiple auth header keys!
17+
for _, authKey := range a.authHeaderKeys {
2318

24-
if tmpKey, ok := c.Request.Header[authKey]; ok {
25-
if checkHeaderKey(tmpKey) {
26-
receivedAuthToken = tmpKey[0]
27-
authTypeKeyName = authKey
28-
authByType = ByHeader
29-
break
30-
}
19+
if tmpKey, ok := a.C.Request.Header[authKey]; ok {
20+
if checkHeaderKey(tmpKey) {
21+
receivedAuthToken = tmpKey[0]
22+
authTypeKeyName = authKey
23+
authByType = ByHeader
24+
break
3125
}
3226
}
3327
}
28+
}
3429

35-
// If nothing found check in GET params, but check if it's a GET Method
30+
// If nothing found check in GET params, but check if it's a GET Method
3631

37-
reqMethod := c.Request.Method
32+
reqMethod := a.C.Request.Method
3833

39-
if (reqMethod == "" || reqMethod == "GET") && receivedAuthToken == "" {
40-
// Try searching here
41-
getParams := c.Request.URL.Query()
42-
if getParams != nil {
43-
if len(a.authGetKeys) == 0 {
44-
// If there is nothing defined, we set the default one!
45-
a.authGetKeys = DefaultGETAuthKeys
46-
}
34+
if (reqMethod == "" || reqMethod == "GET") && receivedAuthToken == "" {
35+
// Try searching here
36+
getParams := a.C.Request.URL.Query()
37+
if getParams != nil {
38+
if len(a.authGetKeys) == 0 {
39+
// If there is nothing defined, we set the default one!
40+
a.authGetKeys = DefaultGETAuthKeys
41+
}
4742

48-
if len(a.authGetKeys) > 0 {
49-
// If there are defined multiple auth header keys!
50-
for _, authKey := range a.authGetKeys {
51-
if tmpKey, ok := getParams[authKey]; ok {
52-
if checkGETKey(tmpKey) {
53-
receivedAuthToken = tmpKey[0]
54-
authTypeKeyName = authKey
55-
authByType = ByGetParam
56-
break
57-
}
43+
if len(a.authGetKeys) > 0 {
44+
// If there are defined multiple auth header keys!
45+
for _, authKey := range a.authGetKeys {
46+
if tmpKey, ok := getParams[authKey]; ok {
47+
if checkGETKey(tmpKey) {
48+
receivedAuthToken = tmpKey[0]
49+
authTypeKeyName = authKey
50+
authByType = ByGetParam
51+
break
5852
}
5953
}
6054
}
6155
}
6256
}
57+
}
6358

64-
// By Cookies
65-
if receivedAuthToken == "" {
66-
// Check by cookie
67-
if len(a.authCookieKeys) == 0 {
68-
// If there is nothing defined, we set the default one!
69-
a.authCookieKeys = DefaultCookieAuthKeys
70-
}
59+
// By Cookies
60+
if receivedAuthToken == "" {
61+
// Check by cookie
62+
if len(a.authCookieKeys) == 0 {
63+
// If there is nothing defined, we set the default one!
64+
a.authCookieKeys = DefaultCookieAuthKeys
65+
}
7166

72-
if len(a.authCookieKeys) > 0 {
73-
// If there are defined multiple auth header keys!
74-
for _, authKey := range a.authCookieKeys {
75-
cookie, err := c.Request.Cookie(authKey)
76-
if err != nil {
77-
continue
78-
}
67+
if len(a.authCookieKeys) > 0 {
68+
// If there are defined multiple auth header keys!
69+
for _, authKey := range a.authCookieKeys {
70+
cookie, err := a.C.Request.Cookie(authKey)
71+
if err != nil {
72+
continue
73+
}
7974

80-
if cookie.Value == "" {
81-
continue
82-
}
83-
authByType = ByCookie
84-
authTypeKeyName = authKey
85-
receivedAuthToken = cookie.Value
86-
break
75+
if cookie.Value == "" {
76+
continue
8777
}
78+
authByType = ByCookie
79+
authTypeKeyName = authKey
80+
receivedAuthToken = cookie.Value
81+
break
8882
}
8983
}
84+
}
9085

91-
if receivedAuthToken == "" {
92-
a.onTokenInvalid(a)
93-
return
94-
}
95-
// Token it's ok!
96-
a.authToken = receivedAuthToken
97-
a.authTypeKeyName = authTypeKeyName
98-
a.authType = uint8(authByType) // By what type it has authenticated
99-
a.onTokenValid(a)
100-
101-
// log.Println(receivedAuthToken)
86+
if receivedAuthToken == "" {
87+
a.onTokenInvalid(a)
88+
return
10289
}
90+
// Token it's ok!
91+
a.authToken = receivedAuthToken
92+
a.authTypeKeyName = authTypeKeyName
93+
a.authType = uint8(authByType) // By what type it has authenticated
94+
a.onTokenValid(a)
95+
// log.Println(receivedAuthToken)
10396
}

Diff for: core/listeners/http/middlewares/connection/functions.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package connection
22

33
import (
4-
"github.com/kyaxcorp/go-core/core/helpers/conv"
54
"github.com/kyaxcorp/go-core/core/helpers/slice"
65
)
76

@@ -25,10 +24,10 @@ func (c *ConnDetails) generateDetails() {
2524
// Saving the authentication details into Http Connection Context
2625
//a.C.Request.TLS
2726

28-
remoteIP := ""
29-
if remIP, gotIP := c.C.RemoteIP(); gotIP {
30-
remoteIP = conv.BytesToStr(remIP)
31-
}
27+
remoteIP := c.C.RemoteIP()
28+
//if remIP, gotIP := c.C.RemoteIP(); gotIP {
29+
// remoteIP = conv.BytesToStr(remIP)
30+
//}
3231

3332
c.DomainName = "" // TODO:
3433
c.Host = c.C.Request.Host

Diff for: core/listeners/http/server/server_constructor.go

+3
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ func New(
210210
//gin.SetMode(gin.DebugMode)
211211

212212
infoServer().Msg("creating new gin server")
213+
214+
// TODO: use HTTP/2?
215+
213216
// Create the HTTP SERVER
214217
s.HttpServer = gin.New()
215218

Diff for: core/listeners/http/server/server_start.go

-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ func (s *Server) Start() error {
256256
// TODO: make a callback for fail listening!
257257
//err := s.WSServer.Run(*addr)
258258
_err := instance.ListenAndServe()
259-
260259
if _err != nil {
261260
_error().Err(_err).Msg(color.Style{color.LightRed}.Render("failed to listen http server"))
262261
}

Diff for: core/services/fcm/function/sender/model.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type messageQueue struct {
2525

2626
//
2727
LastRetry time.Time `gorm:"index"` // This is the time when the process started to send the messages
28-
RetryAfterSeconds uint64 `gorm:"index"` // This is the time after when the Process should retry the sending?!
28+
RetryAfterSeconds uint64 `gorm:"index"` // This is the time after when the Check should retry the sending?!
2929
LockedBy uint64 `gorm:"index"` // By whom it's being locked (by what process/goroutine?)
3030
LockedWhen time.Time `gorm:"index"` // When it's being locked
3131
LockedTTL uint64 `gorm:"index"` // For how long it's being locked?!

0 commit comments

Comments
 (0)