Skip to content

Commit 5fede02

Browse files
add more system logs
1 parent f3ddae9 commit 5fede02

File tree

7 files changed

+37
-6
lines changed

7 files changed

+37
-6
lines changed

conf/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
)
66

77
type Configuration struct {
8+
VERSION string
89
JWT_SECRET string
910
JWT_EXPIRES_IN_MINUTES int
1011
REFRESH_JWT_SECRET string

conf/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"VERSION": "0.1.2",
23
"JWT_EXPIRES_IN_MINUTES": 15,
34
"REFRESH_JWT_EXPIRES_IN_MINUTES": 300
45
}

handlers/auth.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handlers
22

33
import (
44
"http-proxy/conf"
5+
"http-proxy/logger"
56
"http-proxy/models"
67
"http-proxy/utils"
78
"strings"
@@ -17,8 +18,10 @@ var configuration = conf.GetConfig()
1718
type AuthHandler struct{}
1819

1920
func (ah AuthHandler) Authenticate(c *fiber.Ctx) error {
21+
log := logger.GetLogger(c)
2022
var body models.AuthSchema
2123
if err := c.BodyParser(&body); err != nil {
24+
log.Errorf("Authenticate: %s", err.Error())
2225
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
2326
"message": err.Error(),
2427
})
@@ -31,17 +34,21 @@ func (ah AuthHandler) Authenticate(c *fiber.Ctx) error {
3134
conn, err := memphis.Connect(configuration.MEMPHIS_HOST, body.Username, body.ConnectionToken)
3235
if err != nil {
3336
if strings.Contains(err.Error(), "Authorization Violation") {
37+
log.Warnf("Authentication error")
3438
return c.Status(401).JSON(fiber.Map{
3539
"message": "Wrong credentials",
3640
})
3741
}
42+
43+
log.Errorf("Authenticate: %s", err.Error())
3844
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
3945
"message": "Server error",
4046
})
4147
}
4248
conn.Close()
4349
token, refreshToken, tokenExpiry, refreshTokenExpiry, err := createTokens(body.TokenExpiryMins, body.RefreshTokenExpiryMins)
4450
if err != nil {
51+
log.Errorf("Authenticate: %s", err.Error())
4552
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
4653
"message": "Create tokens error",
4754
})
@@ -88,8 +95,10 @@ func createTokens(tokenExpiryMins, refreshTokenExpiryMins int) (string, string,
8895
}
8996

9097
func (ah AuthHandler) RefreshToken(c *fiber.Ctx) error {
98+
log := logger.GetLogger(c)
9199
var body models.RefreshTokenSchema
92100
if err := c.BodyParser(&body); err != nil {
101+
log.Errorf("RefreshToken: %s", err.Error())
93102
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
94103
"message": err.Error(),
95104
})
@@ -102,6 +111,7 @@ func (ah AuthHandler) RefreshToken(c *fiber.Ctx) error {
102111

103112
token, refreshToken, tokenExpiry, refreshTokenExpiry, err := createTokens(body.TokenExpiryMins, body.RefreshTokenExpiryMins)
104113
if err != nil {
114+
log.Errorf("RefreshToken: %s", err.Error())
105115
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
106116
"message": "Create tokens error",
107117
})

handlers/producer.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handlers
33
import (
44
"encoding/json"
55
"errors"
6+
"http-proxy/logger"
67
"strings"
78

89
"github.com/gofiber/fiber/v2"
@@ -43,11 +44,13 @@ func createProducer(conn *memphis.Conn, producers map[string]*memphis.Producer,
4344

4445
func CreateHandleMessage(conn *memphis.Conn) func(*fiber.Ctx) error {
4546
return func(c *fiber.Ctx) error {
47+
log := logger.GetLogger(c)
4648
stationName := c.Params("stationName")
4749
var producer *memphis.Producer
4850

4951
producer, err := createProducer(conn, producers, stationName)
5052
if err != nil {
53+
log.Errorf("CreateHandleMessage - createProducer: %s", err.Error())
5154
return err
5255
}
5356

@@ -64,29 +67,33 @@ func CreateHandleMessage(conn *memphis.Conn) func(*fiber.Ctx) error {
6467
message := bodyReq
6568
hdrs, err := handleHeaders(headers)
6669
if err != nil {
70+
log.Errorf("CreateHandleMessage - handleHeaders: %s", err.Error())
6771
return err
6872
}
6973
if err := producer.Produce(message, memphis.MsgHeaders(hdrs)); err != nil {
7074
if strings.Contains(err.Error(), "memphis: no responders available for request") {
7175
delete(producers, stationName)
7276
producer, err = createProducer(conn, producers, stationName)
7377
if err != nil {
74-
c.Status(400)
78+
log.Errorf("CreateHandleMessage - createProducer retry: %s", err.Error())
79+
c.Status(500)
7580
return c.JSON(&fiber.Map{
7681
"success": false,
7782
"error": err.Error(),
7883
})
7984
}
8085
err = producer.Produce(message, memphis.MsgHeaders(hdrs))
8186
if err != nil {
82-
c.Status(400)
87+
log.Errorf("CreateHandleMessage - produce retry: %s", err.Error())
88+
c.Status(500)
8389
return c.JSON(&fiber.Map{
8490
"success": false,
8591
"error": err.Error(),
8692
})
8793
}
8894
} else {
89-
c.Status(400)
95+
log.Errorf("CreateHandleMessage - produce: %s", err.Error())
96+
c.Status(500)
9097
return c.JSON(&fiber.Map{
9198
"success": false,
9299
"error": err.Error(),
@@ -107,11 +114,13 @@ func CreateHandleMessage(conn *memphis.Conn) func(*fiber.Ctx) error {
107114

108115
func CreateHandleBatch(conn *memphis.Conn) func(*fiber.Ctx) error {
109116
return func(c *fiber.Ctx) error {
117+
log := logger.GetLogger(c)
110118
stationName := c.Params("stationName")
111119
var producer *memphis.Producer
112120

113121
producer, err := createProducer(conn, producers, stationName)
114122
if err != nil {
123+
log.Errorf("CreateHandleBatch - createProducer: %s", err.Error())
115124
return err
116125
}
117126

@@ -124,10 +133,12 @@ func CreateHandleBatch(conn *memphis.Conn) func(*fiber.Ctx) error {
124133
var batchReq []map[string]any
125134
err := json.Unmarshal(bodyReq, &batchReq)
126135
if err != nil {
136+
log.Errorf("CreateHandleBatch - body unmarshal: %s", err.Error())
127137
return errors.New("unsupported request")
128138
}
129139
hdrs, err := handleHeaders(headers)
130140
if err != nil {
141+
log.Errorf("CreateHandleBatch - handleHeaders: %s", err.Error())
131142
return err
132143
}
133144

@@ -145,6 +156,7 @@ func CreateHandleBatch(conn *memphis.Conn) func(*fiber.Ctx) error {
145156
delete(producers, stationName)
146157
producer, err = createProducer(conn, producers, stationName)
147158
if err != nil {
159+
log.Errorf("CreateHandleBatch - createProducer retry: %s", err.Error())
148160
errCount++
149161
allErr = append(allErr, err.Error())
150162
c.Status(400)
@@ -155,6 +167,7 @@ func CreateHandleBatch(conn *memphis.Conn) func(*fiber.Ctx) error {
155167
}
156168
err = producer.Produce(rawRes, memphis.MsgHeaders(hdrs))
157169
if err != nil {
170+
log.Errorf("CreateHandleBatch - produce retry: %s", err.Error())
158171
errCount++
159172
allErr = append(allErr, err.Error())
160173
c.Status(400)
@@ -164,6 +177,7 @@ func CreateHandleBatch(conn *memphis.Conn) func(*fiber.Ctx) error {
164177
})
165178
}
166179
} else {
180+
log.Errorf("CreateHandleBatch - produce: %s", err.Error())
167181
errCount++
168182
allErr = append(allErr, err.Error())
169183
c.Status(400)

logger/log.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ func (sw streamWriter) Write(p []byte) (int, error) {
4040
"ERR": syslogsErrSubject}
4141

4242
label := string(p[sw.labelStart : sw.labelStart+labelLen])
43-
fmt.Println(label)
44-
fmt.Println(string(p))
4543
subjectSuffix, ok := logLabelToSubjectMap[label]
4644
if !ok { // skip other labels
4745
return 0, nil

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ serverInit:
4444
}
4545

4646
app := router.SetupRoutes(conn, l)
47-
l.Noticef("Memphis Http Proxy is up and running")
47+
l.Noticef("Memphis REST gateway is up and running")
48+
l.Noticef("Version %s", configuration.VERSION)
4849
app.Listen(":" + configuration.HTTP_PORT)
4950
}

middlewares/auth.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"http-proxy/conf"
7+
"http-proxy/logger"
78
"strings"
89

910
"github.com/dgrijalva/jwt-go"
@@ -60,31 +61,36 @@ func verifyToken(tokenString string, secret string) error {
6061
}
6162

6263
func Authenticate(c *fiber.Ctx) error {
64+
log := logger.GetLogger(c)
6365
path := strings.ToLower(string(c.Context().URI().RequestURI()))
6466
if isAuthNeeded(path) {
6567
headers := c.GetReqHeaders()
6668
tokenString, err := extractToken(headers["Authorization"])
6769
if err != nil || tokenString == "" {
70+
log.Warnf("Authentication error - jwt token is missing")
6871
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
6972
"message": "Unauthorized",
7073
})
7174
}
7275
err = verifyToken(tokenString, configuration.JWT_SECRET)
7376
if err != nil {
77+
log.Warnf("Authentication error - jwt token validation has failed")
7478
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
7579
"message": "Unauthorized",
7680
})
7781
}
7882
} else if path == "/auth/refreshtoken" {
7983
tokenString := c.Cookies("jwt-refresh-token")
8084
if tokenString == "" {
85+
log.Warnf("Authentication error - refresh token is missing")
8186
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
8287
"message": "Unauthorized",
8388
})
8489
}
8590

8691
err := verifyToken(tokenString, configuration.REFRESH_JWT_SECRET)
8792
if err != nil {
93+
log.Warnf("Authentication error - refresh token validation has failed")
8894
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
8995
"message": "Unauthorized",
9096
})

0 commit comments

Comments
 (0)