Compare commits

...

9 Commits

Author SHA1 Message Date
Muyao CHEN
7886788c35 feat(requestid): put the request id to the response 2024-10-03 14:06:19 +02:00
Muyao CHEN
ad1d4de7c3 feat(requestid): validate uuid 2024-10-03 14:01:15 +02:00
Muyao CHEN
e6389f0fd2 feat(requestid): handle the case for new uuid 2024-10-03 13:53:30 +02:00
Muyao CHEN
d870d2a58d feat(requestid): test for the case where no requestId is given 2024-10-03 13:50:14 +02:00
Muyao CHEN
2a52fb6b88 feat(requestid): reqID in the header 2024-10-03 13:48:36 +02:00
Muyao CHEN
7024c76032 feat: Add request id middleware test 2024-10-03 13:41:32 +02:00
Muyao CHEN
e45f4d992f fix: remove testing code, fix version print, change watch conf place 2024-10-02 23:26:42 +02:00
Muyao CHEN
792ee4ebe0 feat: set dev-mode into config 2024-10-02 23:12:35 +02:00
Muyao CHEN
d7eba0a5d6 feat: set addr from config 2024-10-02 22:58:20 +02:00
9 changed files with 119 additions and 18 deletions

View File

@ -116,3 +116,11 @@ but it is also output to files. The log files can then be fetched to
#### Version
Add versioning into the app.
### 2024/10/03
Set up the web server with some necessary/nice to have middlewares.
- Recovery, Logger (already included in Default mode)
- CORS
- RequestId

View File

@ -20,6 +20,10 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
dev-mode: true
addr: :8080
db:
# DB host
host: 127.0.0.1
@ -38,7 +42,6 @@ db:
log:
level: debug
development: true
disalbe-caller: false
disable-stacktrace: false
# console or json

4
go.mod
View File

@ -5,10 +5,12 @@ go 1.23.1
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.6.0
github.com/gosuri/uitable v0.0.4
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
)
@ -17,6 +19,7 @@ require (
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
@ -37,6 +40,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect

2
go.sum
View File

@ -36,6 +36,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY=
github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=

View File

@ -28,6 +28,7 @@ import (
"strings"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@ -68,6 +69,12 @@ func initConfig() {
return
}
// watching reloading conf
viper.OnConfigChange(func(e fsnotify.Event) {
log.InfoLog("Config file changed:", e.Name)
})
viper.WatchConfig()
log.DebugLog("Using config file", "file", viper.ConfigFileUsed())
}
@ -75,7 +82,7 @@ func initConfig() {
func logOptions() *log.Options {
return &log.Options{
Level: viper.GetString("log.level"),
Development: viper.GetBool("log.development"),
Development: viper.GetBool("dev-mode"),
DisableCaller: viper.GetBool("log.disable-caller"),
DisableStacktrace: viper.GetBool("log.disable-stacktrace"),
Format: viper.GetString("log.format"),

View File

@ -23,13 +23,11 @@
package howmuch
import (
"encoding/json"
"fmt"
"net/http"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
"git.vinchent.xyz/vinchent/howmuch/pkg/version/verflag"
"github.com/fsnotify/fsnotify"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -89,18 +87,12 @@ to share their expense of an event or a trip`,
}
func run() error {
log.DebugLog("How much do I owe you?")
settings, _ := json.MarshalIndent(viper.AllSettings(), "", " ")
// watching reloading conf
viper.OnConfigChange(func(e fsnotify.Event) {
log.InfoLog("Config file changed:", e.Name)
})
viper.WatchConfig()
log.InfoLog(string(settings))
log.InfoLog(viper.GetString("db.username"))
isDev := viper.GetBool("dev-mode")
if isDev {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
r := gin.Default()
@ -110,7 +102,7 @@ func run() error {
})
})
r.Run(":8080")
r.Run(viper.GetString("addr"))
return nil
}

View File

@ -0,0 +1,29 @@
package middleware
import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
const requestID = "X-Request-Id"
func RequestID() gin.HandlerFunc {
return func(ctx *gin.Context) {
var rid string
if rid = ctx.GetString(requestID); rid != "" {
// request id exists already
ctx.Next()
}
if rid = ctx.GetHeader(requestID); rid != "" {
ctx.Set(requestID, rid)
ctx.Next()
}
rid = uuid.NewString()
ctx.Set(requestID, rid)
ctx.Writer.Header().Add(requestID, rid)
ctx.Next()
}
}

View File

@ -0,0 +1,56 @@
package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
type header struct {
Key string
Value string
}
func performRequest(
r http.Handler,
method, path string,
headers ...header,
) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, path, nil)
for _, h := range headers {
req.Header.Add(h.Key, h.Value)
}
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
func TestRequestID(t *testing.T) {
r := gin.New()
r.Use(RequestID())
var got string
wanted := "123"
r.GET("/example", func(c *gin.Context) {
got = c.GetString(requestID)
c.Status(http.StatusOK)
})
r.POST("/example", func(c *gin.Context) {
got = c.GetString(requestID)
c.String(http.StatusAccepted, "ok")
})
// Test with Request ID
_ = performRequest(r, "GET", "/example?a=100", header{requestID, wanted})
assert.Equal(t, "123", got)
res := performRequest(r, "GET", "/example?a=100")
assert.NotEqual(t, "", got)
assert.NoError(t, uuid.Validate(got))
assert.Equal(t, res.Header()[requestID][0], got)
}

View File

@ -41,8 +41,8 @@ func AddFlags(fs *pflag.FlagSet) {
}
func PrintVersion() {
fmt.Printf("%s\n", version.Get().String())
if *doPrint {
fmt.Printf("%s\n", version.Get().String())
os.Exit(0)
}
}