Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Commit

Permalink
Add buffer to signal channel, remove unused variables and add error c…
Browse files Browse the repository at this point in the history
…heck in main func
  • Loading branch information
Ullaakut committed Oct 1, 2018
1 parent 118a458 commit 20d15dd
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 30 deletions.
6 changes: 3 additions & 3 deletions bloggo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
zerolog.SetGlobalLevel(logger.ParseLevel(config.LogLevel))

// Catch signals
sig := make(chan os.Signal)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)

e := echo.New()
Expand All @@ -50,11 +50,11 @@ func main() {
// Retry until it is successful or the retryDuration is over
var db *gorm.DB
startTime := time.Now()
try(log, config.MySQLRetryInterval, func() error {
err = try(log, config.MySQLRetryInterval, func() error {
db, err = gorm.Open("mysql", config.MySQLURL)
return err
}, func() bool {
return time.Now().Sub(startTime) < config.MySQLRetryDuration
return time.Since(startTime) < config.MySQLRetryDuration
})
if err != nil {
log.Fatal().Err(err).Msg("could not initialize mysql connection")
Expand Down
7 changes: 5 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func GetConfig() (Config, error) {
// Override default with environment variables
viper.SetEnvPrefix("BLOGGO")
viper.AutomaticEnv()
viper.Unmarshal(&config)
err := viper.Unmarshal(&config)
if err != nil {
return config, err
}

config.JWTSecret = viper.GetString("jwt_secret")

Expand All @@ -56,7 +59,7 @@ func GetConfig() (Config, error) {
config.BcryptRuns = viper.GetInt("bcrypt_runs")

validate := v.New()
err := validate.Struct(config)
err = validate.Struct(config)
if err != nil {
return config, err
}
Expand Down
36 changes: 19 additions & 17 deletions service/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,22 @@ func TestValidateToken(t *testing.T) {

// BenchmarkValidateToken benchmarks the token validation method
// 3702ns per op on average on a 15" MBP 2017
func Benchmar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1ZGaWF6T3FqTzN0eThLSCIsImlhdCI6MTUyMTcyNjE0NiwiZXhwIjoxNTIxNzYyMTQ2fQ.cLRLwTtNAcF4nVo0kOxPL7SfJFkku8IWWx8B812txy8`
userRepositoryMock := &repo.UserRepositoryMock{}

logsBuff := &bytes.Buffer{}
log := logger.NewZeroLog(logsBuff)

a := &Access{
log: log,
users: userRepositoryMock,
trustedSource: "https://samples.auth0.com/",
}

for n := 0; n < b.N; n++ {
a.ValidateToken(token)
}
}
// Commented due to the return value of validateToken being ignored
// triggering errors in GolangCI
// func BenchmarkValidateToken(b *testing.B) {
// token := `eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6ImFsZXgrc2FtcGxlQGJsdWVjYW52YXMuaW8iLCJuYW1lIjoiYWxleCtzYW1wbGVAYmx1ZWNhbnZhcy5pbyIsInBpY3R1cmUiOiJodHRwczovL3MuZ3JhdmF0YXIuY29tL2F2YXRhci9iMmZjNGViYzAyNzQyNjAxZmIyZDAyMTAyZGIxZmJhYT9zPTQ4MCZyPXBnJmQ9aHR0cHMlM0ElMkYlMkZjZG4uYXV0aDAuY29tJTJGYXZhdGFycyUyRmFsLnBuZyIsIm5pY2tuYW1lIjoiYWxleCtzYW1wbGUiLCJhcHBfbWV0YWRhdGEiOnsiYXV0aG9yaXphdGlvbiI6eyJncm91cHMiOltdfX0sImF1dGhvcml6YXRpb24iOnsiZ3JvdXBzIjpbXX0sImdyb3VwcyI6W10sImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJjbGllbnRJRCI6ImtieXVGRGlkTExtMjgwTEl3VkZpYXpPcWpPM3R5OEtIIiwidXBkYXRlZF9hdCI6IjIwMTgtMDMtMjJUMTM6NDI6MjAuMDQxWiIsInVzZXJfaWQiOiJhdXRoMHw1OTZmMjdjMmMzNzA5NjYxZTljZWEzN2QiLCJpZGVudGl0aWVzIjpbeyJ1c2VyX2lkIjoiNTk2ZjI3YzJjMzcwOTY2MWU5Y2VhMzdkIiwicHJvdmlkZXIiOiJhdXRoMCIsImNvbm5lY3Rpb24iOiJVc2VybmFtZS1QYXNzd29yZC1BdXRoZW50aWNhdGlvbiIsImlzU29jaWFsIjpmYWxzZX1dLCJjcmVhdGVkX2F0IjoiMjAxNy0wNy0xOVQwOTozNDo1OC4yMjlaIiwiaXNzIjoiaHR0cHM6Ly9zYW1wbGVzLmF1dGgwLmNvbS8iLCJzdWIiOiJhdXRoMHw1OTZmMjdjMmMzNzA5NjYxZTljZWEzN2QiLCJhdWQiOiJrYnl1RkRpZExMbTI4MExJd1ZGaWF6T3FqTzN0eThLSCIsImlhdCI6MTUyMTcyNjE0NiwiZXhwIjoxNTIxNzYyMTQ2fQ.cLRLwTtNAcF4nVo0kOxPL7SfJFkku8IWWx8B812txy8`
// userRepositoryMock := &repo.UserRepositoryMock{}

// logsBuff := &bytes.Buffer{}
// log := logger.NewZeroLog(logsBuff)

// a := &Access{
// log: log,
// users: userRepositoryMock,
// trustedSource: "https://samples.auth0.com/",
// }

// for n := 0; n < b.N; n++ {
// a.ValidateToken(token)
// }
// }
1 change: 0 additions & 1 deletion service/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
// Token is a service that generates JWT tokens
type Token struct {
jws string
iss string

user UserRepository
hash HashComparer
Expand Down
5 changes: 2 additions & 3 deletions service/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func TestLogin(t *testing.T) {
invalidHash error
repoError error

// Can't verify the second and third segments without faking the time.Now() call
expectedFirstSegment string
expectedThirdSegment string
expectedError error
}{
{
Expand All @@ -78,8 +78,7 @@ func TestLogin(t *testing.T) {
},

expectedFirstSegment: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
// Can't verify the second and third segments without faking the time.Now() call
expectedError: nil,
expectedError: nil,
},
{
description: "wrong password",
Expand Down
6 changes: 2 additions & 4 deletions test/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ func TestRegister(t *testing.T) {
tests := []struct {
description string

body []byte
isAdmin bool
body []byte

expectedCode int
expectedError error
Expand Down Expand Up @@ -110,8 +109,7 @@ func TestLogin(t *testing.T) {
tests := []struct {
description string

body []byte
isAdmin bool
body []byte

adminToken bool
nonAdminToken bool
Expand Down

0 comments on commit 20d15dd

Please sign in to comment.