Skip to content

Commit

Permalink
checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jmnote committed Dec 29, 2023
1 parent 04e88c6 commit 14efa00
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 19 deletions.
15 changes: 15 additions & 0 deletions hack/go-licenses.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
cd $(dirname $0)/..

which go-licenses || go install github.com/google/[email protected]

echo go-licenses report
go-licenses report . | tee docs/go-licenses.csv

echo go-licenses check
go-licenses check .
if [[ $? != 0 ]]; then
echo "❌ FAIL"
exit 1
fi
echo "✔️ OK"
20 changes: 20 additions & 0 deletions hack/test-cover.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
MIN_COVER=50.0

cd $(dirname $0)/..
export PS4='[$(basename $0):$LINENO] '
set -x

go test ./... -v -failfast -race -coverprofile /tmp/coverprofile
if [[ $? != 0 ]]; then
echo "❌ FAIL - test failed"
exit 1
fi

COVER=$(go tool cover -func /tmp/coverprofile | tail -1 | grep -oP [0-9.]+)
rm -f /tmp/coverprofile
if [[ $COVER < $MIN_COVER ]]; then
echo "⚠️ WARN - total COVER: ${COVER}% (<${MIN_COVER}%)"
exit
fi
echo "✔️ OK - total COVER: ${COVER}% (>=${MIN_COVER}%)"
16 changes: 11 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ func loadConfig() (Config, error) {
return cfg, nil
}

func setupRouter(cfg Config) *gin.Engine {
func setupRouter(cfg Config) (*gin.Engine, error) {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.SetTrustedProxies(cfg.Proxies)
err := router.SetTrustedProxies(cfg.Proxies)
if err != nil {
return nil, fmt.Errorf("setTrustedProxies err: %w", err)
}
router.ForwardedByClientIP = true
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, c.ClientIP()+"\n")
})
return router
return router, nil
}

func run() error {
Expand All @@ -44,8 +47,11 @@ func run() error {
return fmt.Errorf("loadConfig err: %w", err)
}
log.Println("IP App started...")
r := setupRouter(cfg)
return r.Run(cfg.Addr)
router, err := setupRouter(cfg)
if err != nil {
return fmt.Errorf("setupRouter err: %w", err)
}
return router.Run(cfg.Addr)
}

func main() {
Expand Down
47 changes: 33 additions & 14 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,43 @@ func TestLoadConfig(t *testing.T) {
}

func TestSetupRouter(t *testing.T) {
router := setupRouter(Config{})
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
router.ServeHTTP(w, req)
require.Equal(t, 200, w.Code)
require.Equal(t, "\n", w.Body.String())
t.Run("error", func(t *testing.T) {
router, err := setupRouter(Config{Proxies: []string{"hello", "world"}})
require.EqualError(t, err, "setTrustedProxies err: invalid IP address: hello")
require.Nil(t, router)
})
t.Run("ok", func(t *testing.T) {
router, err := setupRouter(Config{})
require.NoError(t, err)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
router.ServeHTTP(w, req)
require.Equal(t, 200, w.Code)
require.Equal(t, "\n", w.Body.String())
})
}

func TestRun(t *testing.T) {
os.Clearenv()
os.Setenv("APP_DEBUG", "hello")
err := run()
require.EqualError(t, err, `loadConfig err: process err: envconfig.Process: assigning APP_DEBUG to Debug: converting 'hello' to type bool. details: strconv.ParseBool: parsing "hello": invalid syntax`)
t.Run("error APP_DEBUG", func(t *testing.T) {
os.Clearenv()
os.Setenv("APP_DEBUG", "hello")
err := run()
require.EqualError(t, err, `loadConfig err: process err: envconfig.Process: assigning APP_DEBUG to Debug: converting 'hello' to type bool. details: strconv.ParseBool: parsing "hello": invalid syntax`)
})

os.Clearenv()
os.Setenv("APP_ADDR", "hello")
err = run()
require.EqualError(t, err, "listen tcp: address hello: missing port in address")
t.Run("error APP_ADDR", func(t *testing.T) {
os.Clearenv()
os.Setenv("APP_ADDR", "hello")
err := run()
require.EqualError(t, err, "listen tcp: address hello: missing port in address")
})

t.Run("error APP_PROXIES", func(t *testing.T) {
os.Clearenv()
os.Setenv("APP_PROXIES", "hello")
err := run()
require.EqualError(t, err, "setupRouter err: setTrustedProxies err: invalid IP address: hello")
})
}

func TestMain(t *testing.T) {
Expand Down

0 comments on commit 14efa00

Please sign in to comment.