mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-11-03 23:55:44 +00:00 
			
		
		
		
	Compare commits
	
		
			3 Commits
		
	
	
		
			v3.6.2-bet
			...
			v3.6.2-bet
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					6867667de6 | ||
| 
						 | 
					079886b54c | ||
| 
						 | 
					19eb8f3064 | 
@@ -21,6 +21,23 @@ type Server struct {
 | 
				
			|||||||
	Router   *gin.Engine
 | 
						Router   *gin.Engine
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						loggerSkipPathsPrefix = []string{
 | 
				
			||||||
 | 
							"GET /api/healthcheck",
 | 
				
			||||||
 | 
							"HEAD /api/healthcheck",
 | 
				
			||||||
 | 
							"GET /favicon.ico",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func logPath(path string) bool {
 | 
				
			||||||
 | 
						for _, prefix := range loggerSkipPathsPrefix {
 | 
				
			||||||
 | 
							if strings.HasPrefix(path, prefix) {
 | 
				
			||||||
 | 
								return false
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return true
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewServer(config types.ServerConfig, handlers *handlers.Handlers) (*Server, error) {
 | 
					func NewServer(config types.ServerConfig, handlers *handlers.Handlers) (*Server, error) {
 | 
				
			||||||
	gin.SetMode(gin.ReleaseMode)
 | 
						gin.SetMode(gin.ReleaseMode)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -68,6 +85,7 @@ func NewServer(config types.ServerConfig, handlers *handlers.Handlers) (*Server,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// App routes
 | 
						// App routes
 | 
				
			||||||
	router.GET("/api/healthcheck", handlers.HealthcheckHandler)
 | 
						router.GET("/api/healthcheck", handlers.HealthcheckHandler)
 | 
				
			||||||
 | 
						router.HEAD("/api/healthcheck", handlers.HealthcheckHandler)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return &Server{
 | 
						return &Server{
 | 
				
			||||||
		Config:   config,
 | 
							Config:   config,
 | 
				
			||||||
@@ -84,29 +102,29 @@ func (s *Server) Start() error {
 | 
				
			|||||||
// zerolog is a middleware for gin that logs requests using zerolog
 | 
					// zerolog is a middleware for gin that logs requests using zerolog
 | 
				
			||||||
func zerolog() gin.HandlerFunc {
 | 
					func zerolog() gin.HandlerFunc {
 | 
				
			||||||
	return func(c *gin.Context) {
 | 
						return func(c *gin.Context) {
 | 
				
			||||||
		// Get initial time
 | 
					 | 
				
			||||||
		tStart := time.Now()
 | 
							tStart := time.Now()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Process request
 | 
					 | 
				
			||||||
		c.Next()
 | 
							c.Next()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Get status code, address, method and path
 | 
					 | 
				
			||||||
		code := c.Writer.Status()
 | 
							code := c.Writer.Status()
 | 
				
			||||||
		address := c.Request.RemoteAddr
 | 
							address := c.Request.RemoteAddr
 | 
				
			||||||
		method := c.Request.Method
 | 
							method := c.Request.Method
 | 
				
			||||||
		path := c.Request.URL.Path
 | 
							path := c.Request.URL.Path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Get latency
 | 
					 | 
				
			||||||
		latency := time.Since(tStart).String()
 | 
							latency := time.Since(tStart).String()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Log request
 | 
							// logPath check if the path should be logged normally or with debug
 | 
				
			||||||
		switch {
 | 
							if logPath(method + " " + path) {
 | 
				
			||||||
		case code >= 200 && code < 300:
 | 
								switch {
 | 
				
			||||||
			log.Info().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
 | 
								case code >= 200 && code < 300:
 | 
				
			||||||
		case code >= 300 && code < 400:
 | 
									log.Info().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
 | 
				
			||||||
			log.Warn().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
 | 
								case code >= 300 && code < 400:
 | 
				
			||||||
		case code >= 400:
 | 
									log.Warn().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
 | 
				
			||||||
			log.Error().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
 | 
								case code >= 400:
 | 
				
			||||||
 | 
									log.Error().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								log.Debug().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -330,12 +330,21 @@ func DeriveKey(secret string, info string) (string, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func CoalesceToString(value any) string {
 | 
					func CoalesceToString(value any) string {
 | 
				
			||||||
	switch v := value.(type) {
 | 
						switch v := value.(type) {
 | 
				
			||||||
	case []string:
 | 
						case []any:
 | 
				
			||||||
		return strings.Join(v, ",")
 | 
							log.Debug().Msg("Coalescing []any to string")
 | 
				
			||||||
 | 
							strs := make([]string, 0, len(v))
 | 
				
			||||||
 | 
							for _, item := range v {
 | 
				
			||||||
 | 
								if str, ok := item.(string); ok {
 | 
				
			||||||
 | 
									strs = append(strs, str)
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								log.Warn().Interface("item", item).Msg("Item in []any is not a string, skipping")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return strings.Join(strs, ",")
 | 
				
			||||||
	case string:
 | 
						case string:
 | 
				
			||||||
		return v
 | 
							return v
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		log.Warn().Interface("value", value).Msg("Unsupported type, returning empty string")
 | 
							log.Warn().Interface("value", value).Interface("type", v).Msg("Unsupported type, returning empty string")
 | 
				
			||||||
		return ""
 | 
							return ""
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -515,7 +515,7 @@ func TestDeriveKey(t *testing.T) {
 | 
				
			|||||||
func TestCoalesceToString(t *testing.T) {
 | 
					func TestCoalesceToString(t *testing.T) {
 | 
				
			||||||
	t.Log("Testing coalesce to string with a string")
 | 
						t.Log("Testing coalesce to string with a string")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	value := "test"
 | 
						value := any("test")
 | 
				
			||||||
	expected := "test"
 | 
						expected := "test"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	result := utils.CoalesceToString(value)
 | 
						result := utils.CoalesceToString(value)
 | 
				
			||||||
@@ -526,10 +526,10 @@ func TestCoalesceToString(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	t.Log("Testing coalesce to string with a slice of strings")
 | 
						t.Log("Testing coalesce to string with a slice of strings")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	valueSlice := []string{"test1", "test2"}
 | 
						value = []any{any("test1"), any("test2"), any(123)}
 | 
				
			||||||
	expected = "test1,test2"
 | 
						expected = "test1,test2"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	result = utils.CoalesceToString(valueSlice)
 | 
						result = utils.CoalesceToString(value)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if result != expected {
 | 
						if result != expected {
 | 
				
			||||||
		t.Fatalf("Expected %v, got %v", expected, result)
 | 
							t.Fatalf("Expected %v, got %v", expected, result)
 | 
				
			||||||
@@ -537,10 +537,10 @@ func TestCoalesceToString(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	t.Log("Testing coalesce to string with an unsupported type")
 | 
						t.Log("Testing coalesce to string with an unsupported type")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	valueUnsupported := 12345
 | 
						value = 12345
 | 
				
			||||||
	expected = ""
 | 
						expected = ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	result = utils.CoalesceToString(valueUnsupported)
 | 
						result = utils.CoalesceToString(value)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if result != expected {
 | 
						if result != expected {
 | 
				
			||||||
		t.Fatalf("Expected %v, got %v", expected, result)
 | 
							t.Fatalf("Expected %v, got %v", expected, result)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user