Skip to content

Commit fae6b8d

Browse files
committed
Remove utils.go from gateway package and update the code and the tests
Signed-off-by: Doğukan Teber <[email protected]>
1 parent 211f7c0 commit fae6b8d

File tree

7 files changed

+22
-80
lines changed

7 files changed

+22
-80
lines changed

gateway/gateway_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"net/http"
77
"net/http/httptest"
8-
"os"
98
"strings"
109
"testing"
1110
"time"
@@ -48,8 +47,6 @@ func TestNewGateway(t *testing.T) {
4847
}
4948

5049
func TestStartGateway(t *testing.T) {
51-
InitLogger(os.Stdout)
52-
5350
distributorServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
5451
frontendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
5552
alertmanagerServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))

gateway/middleware.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"net/http"
55

66
"github.com/cortexproject/auth-gateway/middleware"
7-
"github.com/go-kit/log/level"
7+
"github.com/sirupsen/logrus"
88
)
99

1010
type Authentication struct {
@@ -36,7 +36,7 @@ func (a Authentication) Wrap(next http.Handler) http.Handler {
3636
if ok {
3737
next.ServeHTTP(sr, r)
3838
} else {
39-
level.Debug(logger).Log("msg", "No valid tenant credentials are found")
39+
logrus.Infof("No valid tenant credentials are found")
4040
sr.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
4141
http.Error(sr, "Unauthorized", http.StatusUnauthorized)
4242
}

gateway/middleware_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import (
44
"encoding/base64"
55
"net/http"
66
"net/http/httptest"
7-
"os"
87
"testing"
98
)
109

1110
func TestAuthenticate(t *testing.T) {
12-
InitLogger(os.Stdout)
1311
testCases := []struct {
1412
name string
1513
config *Config

gateway/utils.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

gateway/utils_test.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/cortexproject/auth-gateway/gateway"
88
"github.com/cortexproject/auth-gateway/middleware"
99
"github.com/cortexproject/auth-gateway/server"
10+
"github.com/cortexproject/auth-gateway/utils"
1011
)
1112

1213
func main() {
@@ -15,11 +16,9 @@ func main() {
1516
os.Exit(1)
1617
}
1718

18-
gateway.InitLogger(os.Stdout)
19-
2019
filePath := os.Args[1]
2120
conf, err := gateway.Init(filePath)
22-
gateway.CheckErr("reading the configuration file", err)
21+
utils.CheckErr("reading the configuration file", err)
2322

2423
serverConf := server.Config{
2524
HTTPListenAddr: conf.Server.Address,
@@ -37,12 +36,12 @@ func main() {
3736
UnAuthorizedHTTPServerIdleTimeout: conf.Admin.IdleTimeout,
3837
}
3938
server, err := server.New(serverConf)
40-
gateway.CheckErr("initializing the server", err)
39+
utils.CheckErr("initializing the server", err)
4140

4241
defer server.Shutdown()
4342

4443
gtw, err := gateway.New(&conf, server)
45-
gateway.CheckErr("initializing the gateway", err)
44+
utils.CheckErr("initializing the gateway", err)
4645

4746
gtw.Start(&conf)
4847

utils/utils.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
package utils
22

3-
import "github.com/sirupsen/logrus"
3+
import (
4+
"os"
5+
6+
"github.com/sirupsen/logrus"
7+
)
48

59
type LogrusErrorWriter struct{}
610

711
func (w LogrusErrorWriter) Write(p []byte) (n int, err error) {
812
logrus.Errorf("%s", string(p))
913
return len(p), nil
1014
}
15+
16+
func CheckErr(msg string, err error) {
17+
CheckErrWithExit(msg, err, os.Exit)
18+
}
19+
20+
func CheckErrWithExit(msg string, err error, exitFunc func(int)) {
21+
if err != nil {
22+
logrus.Errorf("err: %v", err)
23+
exitFunc(1)
24+
}
25+
}

0 commit comments

Comments
 (0)