Skip to content

Commit

Permalink
wip #12: not working but moved code and handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
donkeyx committed Sep 11, 2023
1 parent cc2b0a5 commit 1376a1b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 57 deletions.
13 changes: 7 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ go 1.21
require (
github.com/gin-gonic/gin v1.9.1
go.uber.org/zap v1.25.0
gorm.io/gorm v1.25.4
)

require (
github.com/bytedance/sonic v1.10.0 // indirect
github.com/bytedance/sonic v1.10.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
Expand All @@ -30,11 +31,11 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.0 h1:qtNZduETEIWJVIyDl01BeNxur2rW9OwTQ/yBqFRkKEk=
github.com/bytedance/sonic v1.10.0/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/bytedance/sonic v1.10.1 h1:7a1wuFXL1cMy7a3f7/VFcEtriuXQnUBhtoVfOZiaysc=
github.com/bytedance/sonic v1.10.1/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
Expand Down Expand Up @@ -90,16 +92,26 @@ go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.4.0 h1:A8WCeEWhLwPBKNbFi5Wv5UTCBx5zzubnXDlMOFAzFMc=
golang.org/x/arch v0.4.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.5.0 h1:jpGode6huXQxcskEIpOCvrU+tzo81b6+oFLUYXWtH/Y=
golang.org/x/arch v0.5.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
Expand All @@ -112,5 +124,7 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
File renamed without changes.
43 changes: 10 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

import (
"cu-api/middleware"
"fmt"
"log"
"math/rand"
"sync"

"github.com/gin-gonic/gin"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gorm.io/gorm/logger"
)

var (
Expand All @@ -19,12 +20,14 @@ var (

func main() {

securityToken = generateRandomToken(32)
r := gin.Default()

logger, err := newSugarLogger(true)
if err != nil {
log.Fatal("Error creating logger:", err)
}
useJSONOutput := true // Set this to false for non-JSON output
r.Use(middleware.SetupLoggerMiddleware(useJSONOutput))

routes.setupRouter(r)

securityToken = generateRandomToken(32)

logger.Info("Random Security Token", zap.String("token", securityToken))
logger.Info("Curl Command", zap.String("command", getCurlCommand(8080, securityToken)))
Expand All @@ -48,29 +51,3 @@ func getCurlCommand(port int, securityToken string) string {
variable := fmt.Sprintf("curl -H 'Authorization: Bearer %s' http://localhost:8080/env", securityToken)
return variable
}

func newSugarLogger(useJSON bool) (*zap.SugaredLogger, error) {
var config zap.Config

if useJSON {
config = zap.NewProductionConfig()
} else {
config = zap.NewDevelopmentConfig()
}

config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder

// Configure the logger to write logs to the terminal
config.OutputPaths = []string{"stdout"}

// Create the logger
logger, err := config.Build()
if err != nil {
return nil, err
}

// Create a sugar logger from the base logger
sugarLogger := logger.Sugar()

return sugarLogger, nil
}
30 changes: 21 additions & 9 deletions middleware.go → middleware/middleware.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
package main
package middleware

import (
"net/http"

"github.com/getsentry/sugar"
"github.com/getsentry/sugar/console"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

func loggingMiddleware(logger *zap.SugaredLogger) gin.HandlerFunc {
func SetupLoggerMiddleware(useJSON bool) gin.HandlerFunc {
return func(c *gin.Context) {
// Create a new Sugar logger
logger, _ := sugar.New()

logger.Infow("Request Information",
"Method", c.Request.Method,
"Path", c.Request.URL.Path,
"Query", c.Request.URL.RawQuery,
"UserAgent", c.Request.UserAgent(),
)
if useJSON {
// Configure the logger output to use JSON format
logger.SetFormatter(sugar.NewJSONFormatter())
} else {
// Configure the logger output to use the console writer
logger.SetWriter(console.New())
}

// You can customize other logger settings here, such as log level, etc.
// For example:
// logger.SetLevel(sugar.InfoLevel)

// Attach the logger to the context for access in handlers
c.Set("logger", logger)

// Continue with the request
c.Next()
}
}

func authMiddleware(logger *zap.SugaredLogger) gin.HandlerFunc {
func AuthMiddleware(logger *zap.SugaredLogger) gin.HandlerFunc {
return func(c *gin.Context) {
// Retrieve the logger from the Gin context
c.Set("logger", logger)
Expand Down
11 changes: 2 additions & 9 deletions router.go → routes/routes.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package main
package routes

import (
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

func setupRouter(logger *zap.SugaredLogger) *gin.Engine {
func setupRouter(r *gin.Engine) {

gin.SetMode(gin.ReleaseMode)
r := gin.Default()

r.Use(gin.LoggerWithConfig(gin.LoggerConfig{
Output: logger.Writer(),
}))

r.Use(loggingMiddleware(logger))
r.Use(authMiddleware(logger))

r.GET("/health", healthHandler)
Expand Down

0 comments on commit 1376a1b

Please sign in to comment.