feat: voting procedures
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -10,6 +12,10 @@ import (
|
||||
"github.com/iliadenisov/galaxy/internal/router/handler"
|
||||
)
|
||||
|
||||
const (
|
||||
ISO8601 = "2006-01-02 15:04:05.0 -07:00"
|
||||
)
|
||||
|
||||
func initConfig() func(*controller.Param) {
|
||||
return func(p *controller.Param) {
|
||||
// TODO: initialize base controller settings
|
||||
@@ -37,7 +43,12 @@ func NewRouterExecutor(executor handler.CommandExecutor) Router {
|
||||
func setupRouter(config controller.Config, executor handler.CommandExecutor) *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.New()
|
||||
r.Use(gin.Recovery())
|
||||
|
||||
// Logger middleware will write the logs to gin.DefaultWriter even if you set with GIN_MODE=release.
|
||||
r.Use(gin.LoggerWithFormatter(logFormatter))
|
||||
|
||||
// Recovery middleware recovers from any panics and writes a 500 if there was one.
|
||||
r.Use(gin.CustomRecovery(recoveryHandler))
|
||||
|
||||
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
||||
v.RegisterValidation("notblank", notBlankStringValidator)
|
||||
@@ -51,3 +62,20 @@ func setupRouter(config controller.Config, executor handler.CommandExecutor) *gi
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func logFormatter(param gin.LogFormatterParams) string {
|
||||
return fmt.Sprintf("[%s] \"%s %s %s %d %s\"\n",
|
||||
param.TimeStamp.Format(ISO8601),
|
||||
param.Method,
|
||||
param.Path,
|
||||
param.Request.Proto,
|
||||
param.StatusCode,
|
||||
param.Latency,
|
||||
)
|
||||
}
|
||||
func recoveryHandler(c *gin.Context, recovered any) {
|
||||
if err, ok := recovered.(string); ok {
|
||||
fmt.Fprintf(os.Stderr, "recovered: %s", err)
|
||||
}
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user