From 0d8dc3f7330127df1a36077a159662dd0d872226 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 1 May 2019 18:54:36 +0200 Subject: [PATCH] update according to changed interface --- go.sum | 2 +- service/auth/auth.go | 12 ++++----- service/shortener/shortener.go | 47 ++++++++++++---------------------- 3 files changed, 23 insertions(+), 38 deletions(-) diff --git a/go.sum b/go.sum index d47f4a6..947c255 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,4 @@ -git.xdrm.io/go/aicra v0.2.0 h1:Q/vaQlKhq7PdTBl8Ps9qNdb2SWnEPaIwwB74ee3RAaw= +git.xdrm.io/go/aicra v0.2.0 h1:qDGeHilx46sdQAWVFAn8buVtpLxSlMCBPGMY+yCkpNo= git.xdrm.io/go/aicra v0.2.0/go.mod h1:ulAzCdKqUN5X4eWQSER70QXSYteSXtybAqupcUuYPdw= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= diff --git a/service/auth/auth.go b/service/auth/auth.go index f5cc856..7e1a22a 100644 --- a/service/auth/auth.go +++ b/service/auth/auth.go @@ -38,9 +38,7 @@ func (svc *Service) generateToken(req api.Request, res *api.Response) { // 1. extract input role, err := req.Param.GetString("role") if err != nil { - res.Err = api.ErrorInvalidParam() - res.Err.Put("role") - res.Err.Put(err.Error()) + res.SetError(api.ErrorInvalidParam(), "role", err.Error()) return } @@ -52,13 +50,13 @@ func (svc *Service) generateToken(req api.Request, res *api.Response) { // 3. store token if !svc.storage.Set(storage.TOKEN, token, role, 5*time.Minute) { - res.Err = api.ErrorFailure() + res.SetError(api.ErrorFailure()) return } // 4. return data res.Data["token"] = token - res.Err = api.ErrorSuccess() + res.SetError(api.ErrorSuccess()) } @@ -76,7 +74,7 @@ func (svc *Service) CheckToken(handler api.HandlerFunc) api.HandlerFunc { // fail if invalid header if len(headerToken) != 128 || strings.ContainsAny(headerToken, "$-_") { - res.Err = api.ErrorPermission() + res.SetError(api.ErrorPermission()) return } @@ -96,7 +94,7 @@ func (svc *Service) CheckToken(handler api.HandlerFunc) api.HandlerFunc { } // failure - res.Err = api.ErrorPermission() + res.SetError(api.ErrorPermission()) return } } diff --git a/service/shortener/shortener.go b/service/shortener/shortener.go index 6791bec..8b598f0 100644 --- a/service/shortener/shortener.go +++ b/service/shortener/shortener.go @@ -40,23 +40,21 @@ func (svc *Service) redirect(req api.Request, res *api.Response) { // 1. extract input tinyURL, err := req.Param.GetString("url") if err != nil { - res.Err = api.ErrorInvalidParam() - res.Err.Put("url") - res.Err.Put(err.Error()) + res.SetError(api.ErrorInvalidParam(), "url", err.Error()) return } // 2. check in db if exists longURL := svc.storage.Get(storage.DATA, tinyURL) if longURL == nil { - res.Err = api.ErrorNoMatchFound() + res.SetError(api.ErrorNoMatchFound()) return } // 3. redirect res.Status = http.StatusPermanentRedirect res.Headers.Set("Location", string(longURL)) - res.Err = api.ErrorSuccess() + res.SetError(api.ErrorSuccess()) } @@ -66,33 +64,28 @@ func (svc *Service) register(req api.Request, res *api.Response) { // 1. extract arguments longURL, err := req.Param.GetString("target") if err != nil { - res.Err = api.ErrorInvalidParam() - res.Err.Put("target") - res.Err.Put(err.Error()) + res.SetError(api.ErrorInvalidParam(), "target", err.Error()) return } tinyURL, err := req.Param.GetString("url") if err != nil { - res.Err = api.ErrorInvalidParam() - res.Err.Put("url") - res.Err.Put(err.Error()) + res.SetError(api.ErrorInvalidParam(), "url", err.Error()) return } // 2. fail if already used if svc.storage.Get(storage.DATA, tinyURL) != nil { - res.Err = api.ErrorAlreadyExists() - res.Err.Put("url") + res.SetError(api.ErrorAlreadyExists(), "url") return } // 3. store association if !svc.storage.Set(storage.DATA, tinyURL, longURL) { - res.Err = api.ErrorFailure() + res.SetError(api.ErrorFailure()) return } - res.Err = api.ErrorSuccess() + res.SetError(api.ErrorSuccess()) } // update updates an existing tiny url to a new long one @@ -101,32 +94,28 @@ func (svc *Service) update(req api.Request, res *api.Response) { // 1. extract arguments longURL, err := req.Param.GetString("target") if err != nil { - res.Err = api.ErrorInvalidParam() - res.Err.Put("target") - res.Err.Put(err.Error()) + res.SetError(api.ErrorInvalidParam(), "target", err.Error()) return } tinyURL, err := req.Param.GetString("url") if err != nil { - res.Err = api.ErrorInvalidParam() - res.Err.Put("url") - res.Err.Put(err.Error()) + res.SetError(api.ErrorInvalidParam(), "url", err.Error()) return } // 2. fail if not already existing if svc.storage.Get(storage.DATA, tinyURL) == nil { - res.Err = api.ErrorNoMatchFound() + res.SetError(api.ErrorNoMatchFound()) return } // 3. update association if !svc.storage.Set(storage.DATA, tinyURL, longURL) { - res.Err = api.ErrorFailure() + res.SetError(api.ErrorFailure()) return } - res.Err = api.ErrorSuccess() + res.SetError(api.ErrorSuccess()) } // delete removes a new tiny url @@ -135,23 +124,21 @@ func (svc *Service) delete(req api.Request, res *api.Response) { // 1. extract arguments tinyURL, err := req.Param.GetString("url") if err != nil { - res.Err = api.ErrorInvalidParam() - res.Err.Put("url") - res.Err.Put(err.Error()) + res.SetError(api.ErrorInvalidParam(), "url", err.Error()) return } // 2. fail if not already existing if svc.storage.Get(storage.DATA, tinyURL) == nil { - res.Err = api.ErrorNoMatchFound() + res.SetError(api.ErrorNoMatchFound()) return } // 3. update association if !svc.storage.Del(storage.DATA, tinyURL) { - res.Err = api.ErrorFailure() + res.SetError(api.ErrorFailure()) return } - res.Err = api.ErrorSuccess() + res.SetError(api.ErrorSuccess()) }