-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code structure and configuration handling
- Loading branch information
Showing
9 changed files
with
83 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
package middleware | ||
|
||
import ( | ||
"go-gin/service/singleton" | ||
"time" | ||
|
||
"go-gin/internal/log" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func LoggingHandler() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
t := time.Now() | ||
|
||
log.ZLog.Info().Msgf("Request Info:\nMethod: %s\nHost: %s\nURL: %s", | ||
singleton.Log.Info().Msgf("Request Info:\nMethod: %s\nHost: %s\nURL: %s", | ||
c.Request.Method, c.Request.Host, c.Request.URL) | ||
log.ZLog.Debug().Msgf("Request Header:\n%v", c.Request.Header) | ||
singleton.Log.Debug().Msgf("Request Header:\n%v", c.Request.Header) | ||
|
||
c.Next() | ||
|
||
latency := time.Since(t) | ||
log.ZLog.Info().Msgf("Response Time: %s\nStatus: %d", | ||
singleton.Log.Info().Msgf("Response Time: %s\nStatus: %d", | ||
latency.String(), c.Writer.Status()) | ||
log.ZLog.Debug().Msgf("Response Header:\n%v", c.Writer.Header()) | ||
singleton.Log.Debug().Msgf("Response Header:\n%v", c.Writer.Header()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package singleton | ||
|
||
import ( | ||
"fmt" | ||
"go-gin/internal/config" | ||
"go-gin/model" | ||
|
||
config_utils "go-gin/pkg/config" | ||
logger "go-gin/pkg/logger" | ||
|
||
"github.com/rs/zerolog" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var ( | ||
Config *config.Config | ||
Log *zerolog.Logger | ||
DB *gorm.DB | ||
) | ||
|
||
func InitSingleton() { | ||
// TOO: init db | ||
} | ||
|
||
func InitConfig(name string) { | ||
_config, err := config_utils.ReadViperConfig(name, "yaml", []string{".", "./config", "../"}) | ||
if err != nil { | ||
panic(fmt.Errorf("unable to read config: %s", err)) | ||
} | ||
|
||
if err := _config.Unmarshal(&Config); err != nil { | ||
panic(fmt.Errorf("unable to unmarshal config: %s", err)) | ||
} | ||
} | ||
|
||
func InitLog(config *config.Config) { | ||
logPath := config.Log.Path | ||
if logPath == "" { | ||
logPath = model.DefaultLogPath | ||
} | ||
Log = logger.NewLogger(config.Log.Level, logPath) | ||
} |