update according to changed interface

This commit is contained in:
Adrien Marquès 2019-05-01 18:54:36 +02:00
parent 41f197ef41
commit 0d8dc3f733
3 changed files with 23 additions and 38 deletions

2
go.sum
View File

@ -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= 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 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=

View File

@ -38,9 +38,7 @@ func (svc *Service) generateToken(req api.Request, res *api.Response) {
// 1. extract input // 1. extract input
role, err := req.Param.GetString("role") role, err := req.Param.GetString("role")
if err != nil { if err != nil {
res.Err = api.ErrorInvalidParam() res.SetError(api.ErrorInvalidParam(), "role", err.Error())
res.Err.Put("role")
res.Err.Put(err.Error())
return return
} }
@ -52,13 +50,13 @@ func (svc *Service) generateToken(req api.Request, res *api.Response) {
// 3. store token // 3. store token
if !svc.storage.Set(storage.TOKEN, token, role, 5*time.Minute) { if !svc.storage.Set(storage.TOKEN, token, role, 5*time.Minute) {
res.Err = api.ErrorFailure() res.SetError(api.ErrorFailure())
return return
} }
// 4. return data // 4. return data
res.Data["token"] = token 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 // fail if invalid header
if len(headerToken) != 128 || strings.ContainsAny(headerToken, "$-_") { if len(headerToken) != 128 || strings.ContainsAny(headerToken, "$-_") {
res.Err = api.ErrorPermission() res.SetError(api.ErrorPermission())
return return
} }
@ -96,7 +94,7 @@ func (svc *Service) CheckToken(handler api.HandlerFunc) api.HandlerFunc {
} }
// failure // failure
res.Err = api.ErrorPermission() res.SetError(api.ErrorPermission())
return return
} }
} }

View File

@ -40,23 +40,21 @@ func (svc *Service) redirect(req api.Request, res *api.Response) {
// 1. extract input // 1. extract input
tinyURL, err := req.Param.GetString("url") tinyURL, err := req.Param.GetString("url")
if err != nil { if err != nil {
res.Err = api.ErrorInvalidParam() res.SetError(api.ErrorInvalidParam(), "url", err.Error())
res.Err.Put("url")
res.Err.Put(err.Error())
return return
} }
// 2. check in db if exists // 2. check in db if exists
longURL := svc.storage.Get(storage.DATA, tinyURL) longURL := svc.storage.Get(storage.DATA, tinyURL)
if longURL == nil { if longURL == nil {
res.Err = api.ErrorNoMatchFound() res.SetError(api.ErrorNoMatchFound())
return return
} }
// 3. redirect // 3. redirect
res.Status = http.StatusPermanentRedirect res.Status = http.StatusPermanentRedirect
res.Headers.Set("Location", string(longURL)) 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 // 1. extract arguments
longURL, err := req.Param.GetString("target") longURL, err := req.Param.GetString("target")
if err != nil { if err != nil {
res.Err = api.ErrorInvalidParam() res.SetError(api.ErrorInvalidParam(), "target", err.Error())
res.Err.Put("target")
res.Err.Put(err.Error())
return return
} }
tinyURL, err := req.Param.GetString("url") tinyURL, err := req.Param.GetString("url")
if err != nil { if err != nil {
res.Err = api.ErrorInvalidParam() res.SetError(api.ErrorInvalidParam(), "url", err.Error())
res.Err.Put("url")
res.Err.Put(err.Error())
return return
} }
// 2. fail if already used // 2. fail if already used
if svc.storage.Get(storage.DATA, tinyURL) != nil { if svc.storage.Get(storage.DATA, tinyURL) != nil {
res.Err = api.ErrorAlreadyExists() res.SetError(api.ErrorAlreadyExists(), "url")
res.Err.Put("url")
return return
} }
// 3. store association // 3. store association
if !svc.storage.Set(storage.DATA, tinyURL, longURL) { if !svc.storage.Set(storage.DATA, tinyURL, longURL) {
res.Err = api.ErrorFailure() res.SetError(api.ErrorFailure())
return return
} }
res.Err = api.ErrorSuccess() res.SetError(api.ErrorSuccess())
} }
// update updates an existing tiny url to a new long one // 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 // 1. extract arguments
longURL, err := req.Param.GetString("target") longURL, err := req.Param.GetString("target")
if err != nil { if err != nil {
res.Err = api.ErrorInvalidParam() res.SetError(api.ErrorInvalidParam(), "target", err.Error())
res.Err.Put("target")
res.Err.Put(err.Error())
return return
} }
tinyURL, err := req.Param.GetString("url") tinyURL, err := req.Param.GetString("url")
if err != nil { if err != nil {
res.Err = api.ErrorInvalidParam() res.SetError(api.ErrorInvalidParam(), "url", err.Error())
res.Err.Put("url")
res.Err.Put(err.Error())
return return
} }
// 2. fail if not already existing // 2. fail if not already existing
if svc.storage.Get(storage.DATA, tinyURL) == nil { if svc.storage.Get(storage.DATA, tinyURL) == nil {
res.Err = api.ErrorNoMatchFound() res.SetError(api.ErrorNoMatchFound())
return return
} }
// 3. update association // 3. update association
if !svc.storage.Set(storage.DATA, tinyURL, longURL) { if !svc.storage.Set(storage.DATA, tinyURL, longURL) {
res.Err = api.ErrorFailure() res.SetError(api.ErrorFailure())
return return
} }
res.Err = api.ErrorSuccess() res.SetError(api.ErrorSuccess())
} }
// delete removes a new tiny url // delete removes a new tiny url
@ -135,23 +124,21 @@ func (svc *Service) delete(req api.Request, res *api.Response) {
// 1. extract arguments // 1. extract arguments
tinyURL, err := req.Param.GetString("url") tinyURL, err := req.Param.GetString("url")
if err != nil { if err != nil {
res.Err = api.ErrorInvalidParam() res.SetError(api.ErrorInvalidParam(), "url", err.Error())
res.Err.Put("url")
res.Err.Put(err.Error())
return return
} }
// 2. fail if not already existing // 2. fail if not already existing
if svc.storage.Get(storage.DATA, tinyURL) == nil { if svc.storage.Get(storage.DATA, tinyURL) == nil {
res.Err = api.ErrorNoMatchFound() res.SetError(api.ErrorNoMatchFound())
return return
} }
// 3. update association // 3. update association
if !svc.storage.Del(storage.DATA, tinyURL) { if !svc.storage.Del(storage.DATA, tinyURL) {
res.Err = api.ErrorFailure() res.SetError(api.ErrorFailure())
return return
} }
res.Err = api.ErrorSuccess() res.SetError(api.ErrorSuccess())
} }