Compare commits
9 Commits
608f435fb1
...
7886788c35
Author | SHA1 | Date | |
---|---|---|---|
|
7886788c35 | ||
|
ad1d4de7c3 | ||
|
e6389f0fd2 | ||
|
d870d2a58d | ||
|
2a52fb6b88 | ||
|
7024c76032 | ||
|
e45f4d992f | ||
|
792ee4ebe0 | ||
|
d7eba0a5d6 |
@ -116,3 +116,11 @@ but it is also output to files. The log files can then be fetched to
|
|||||||
#### Version
|
#### Version
|
||||||
|
|
||||||
Add versioning into the app.
|
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
|
||||||
|
@ -20,6 +20,10 @@
|
|||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
# SOFTWARE.
|
# SOFTWARE.
|
||||||
|
|
||||||
|
dev-mode: true
|
||||||
|
|
||||||
|
addr: :8080
|
||||||
|
|
||||||
db:
|
db:
|
||||||
# DB host
|
# DB host
|
||||||
host: 127.0.0.1
|
host: 127.0.0.1
|
||||||
@ -38,7 +42,6 @@ db:
|
|||||||
|
|
||||||
log:
|
log:
|
||||||
level: debug
|
level: debug
|
||||||
development: true
|
|
||||||
disalbe-caller: false
|
disalbe-caller: false
|
||||||
disable-stacktrace: false
|
disable-stacktrace: false
|
||||||
# console or json
|
# console or json
|
||||||
|
4
go.mod
4
go.mod
@ -5,10 +5,12 @@ go 1.23.1
|
|||||||
require (
|
require (
|
||||||
github.com/fsnotify/fsnotify v1.7.0
|
github.com/fsnotify/fsnotify v1.7.0
|
||||||
github.com/gin-gonic/gin v1.10.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/gosuri/uitable v0.0.4
|
||||||
github.com/spf13/cobra v1.8.1
|
github.com/spf13/cobra v1.8.1
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
github.com/spf13/viper v1.19.0
|
github.com/spf13/viper v1.19.0
|
||||||
|
github.com/stretchr/testify v1.9.0
|
||||||
go.uber.org/zap v1.27.0
|
go.uber.org/zap v1.27.0
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,6 +19,7 @@ require (
|
|||||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||||
github.com/cloudwego/iasm v0.2.0 // 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/fatih/color v1.14.1 // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // 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/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.2.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/rivo/uniseg v0.2.0 // indirect
|
||||||
github.com/sagikazarmark/locafero v0.4.0 // indirect
|
github.com/sagikazarmark/locafero v0.4.0 // indirect
|
||||||
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
|
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -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 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
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/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 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY=
|
||||||
github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo=
|
github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo=
|
||||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||||
|
@ -28,6 +28,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
|
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
|
||||||
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@ -68,6 +69,12 @@ func initConfig() {
|
|||||||
return
|
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())
|
log.DebugLog("Using config file", "file", viper.ConfigFileUsed())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +82,7 @@ func initConfig() {
|
|||||||
func logOptions() *log.Options {
|
func logOptions() *log.Options {
|
||||||
return &log.Options{
|
return &log.Options{
|
||||||
Level: viper.GetString("log.level"),
|
Level: viper.GetString("log.level"),
|
||||||
Development: viper.GetBool("log.development"),
|
Development: viper.GetBool("dev-mode"),
|
||||||
DisableCaller: viper.GetBool("log.disable-caller"),
|
DisableCaller: viper.GetBool("log.disable-caller"),
|
||||||
DisableStacktrace: viper.GetBool("log.disable-stacktrace"),
|
DisableStacktrace: viper.GetBool("log.disable-stacktrace"),
|
||||||
Format: viper.GetString("log.format"),
|
Format: viper.GetString("log.format"),
|
||||||
|
@ -23,13 +23,11 @@
|
|||||||
package howmuch
|
package howmuch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
|
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
|
||||||
"git.vinchent.xyz/vinchent/howmuch/pkg/version/verflag"
|
"git.vinchent.xyz/vinchent/howmuch/pkg/version/verflag"
|
||||||
"github.com/fsnotify/fsnotify"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
@ -89,18 +87,12 @@ to share their expense of an event or a trip`,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run() error {
|
func run() error {
|
||||||
log.DebugLog("How much do I owe you?")
|
isDev := viper.GetBool("dev-mode")
|
||||||
|
if isDev {
|
||||||
settings, _ := json.MarshalIndent(viper.AllSettings(), "", " ")
|
gin.SetMode(gin.DebugMode)
|
||||||
|
} else {
|
||||||
// watching reloading conf
|
gin.SetMode(gin.ReleaseMode)
|
||||||
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"))
|
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
@ -110,7 +102,7 @@ func run() error {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Run(":8080")
|
r.Run(viper.GetString("addr"))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
29
internal/pkg/middleware/requestid.go
Normal file
29
internal/pkg/middleware/requestid.go
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
56
internal/pkg/middleware/requestid_test.go
Normal file
56
internal/pkg/middleware/requestid_test.go
Normal 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)
|
||||||
|
}
|
@ -41,8 +41,8 @@ func AddFlags(fs *pflag.FlagSet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func PrintVersion() {
|
func PrintVersion() {
|
||||||
fmt.Printf("%s\n", version.Get().String())
|
|
||||||
if *doPrint {
|
if *doPrint {
|
||||||
|
fmt.Printf("%s\n", version.Get().String())
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user