-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: implement basic implementation for service to store value
- Loading branch information
Showing
8 changed files
with
186 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/base64" | ||
"net/http" | ||
|
||
"github.com/google/uuid" | ||
"github.com/rarimo/bio-data-svc/internal/data" | ||
"github.com/rarimo/bio-data-svc/internal/service/requests" | ||
"github.com/rarimo/bio-data-svc/resources" | ||
"gitlab.com/distributed_lab/ape" | ||
"gitlab.com/distributed_lab/ape/problems" | ||
) | ||
|
||
func AddData(w http.ResponseWriter, r *http.Request) { | ||
req, err := requests.AddData(r) | ||
if err != nil { | ||
Log(r).WithError(err).Error("invalid request") | ||
ape.RenderErr(w, problems.BadRequest(err)...) | ||
return | ||
} | ||
|
||
value, err := base64.StdEncoding.DecodeString(req.Data.Attributes.Value) | ||
if err != nil { | ||
Log(r).WithError(err).WithField("value", req.Data.Attributes.Value).Error("failed to decode base64 string") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
kv := data.KV{ | ||
Key: uuid.NewString(), | ||
Value: value, | ||
} | ||
|
||
if err = KVQ(r).Insert(kv); err != nil { | ||
Log(r).WithError(err).WithField("kv", kv).Error("failed to insert new key-value") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusCreated) | ||
ape.Render(w, newKVResponse(kv)) | ||
} | ||
|
||
func newKVResponse(kv data.KV) resources.ValueResponse { | ||
return resources.ValueResponse{ | ||
Data: resources.Value{ | ||
Key: resources.Key{ | ||
ID: kv.Key, | ||
Type: resources.VALUE, | ||
}, | ||
Attributes: resources.ValueAttributes{ | ||
Value: base64.StdEncoding.EncodeToString(kv.Value), | ||
Key: kv.Key, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/base64" | ||
"net/http" | ||
|
||
"github.com/rarimo/bio-data-svc/internal/service/requests" | ||
"gitlab.com/distributed_lab/ape" | ||
"gitlab.com/distributed_lab/ape/problems" | ||
) | ||
|
||
func GetData(w http.ResponseWriter, r *http.Request) { | ||
req, err := requests.NewGetDataRequest(r) | ||
if err != nil { | ||
Log(r).WithError(err).Error("invalid request") | ||
ape.RenderErr(w, problems.BadRequest(err)...) | ||
return | ||
} | ||
|
||
kvQuery := KVQ(r) | ||
|
||
if req.Key != nil { | ||
kvQuery = kvQuery.FilterByKey(*req.Key) | ||
} | ||
|
||
if req.Value != nil { | ||
value, err := base64.StdEncoding.DecodeString(*req.Value) | ||
if err != nil { | ||
Log(r).WithError(err).WithField("value", *req.Value).Error("failed to decode base64 string") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
kvQuery = kvQuery.FilterByValue(value) | ||
} | ||
|
||
kv, err := kvQuery.Get() | ||
if err != nil { | ||
Log(r).WithError(err).Error("failed to get key-value") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
if kv == nil { | ||
Log(r).Error("no key-value row found") | ||
ape.RenderErr(w, problems.NotFound()) | ||
return | ||
} | ||
|
||
ape.Render(w, newKVResponse(*kv)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,36 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
|
||
"github.com/rarimo/bio-data-svc/internal/config" | ||
"gitlab.com/distributed_lab/ape" | ||
"gitlab.com/distributed_lab/kit/copus/types" | ||
"gitlab.com/distributed_lab/logan/v3" | ||
"gitlab.com/distributed_lab/logan/v3/errors" | ||
) | ||
|
||
type service struct { | ||
log *logan.Entry | ||
copus types.Copus | ||
listener net.Listener | ||
cfg config.Config | ||
} | ||
|
||
func (s *service) run() error { | ||
func (s *service) run(ctx context.Context) { | ||
s.log.Info("Service started") | ||
r := s.router() | ||
|
||
if err := s.copus.RegisterChi(r); err != nil { | ||
return errors.Wrap(err, "cop failed") | ||
} | ||
|
||
return http.Serve(s.listener, r) | ||
ape.Serve(ctx, s.router(), s.cfg, ape.ServeOpts{}) | ||
} | ||
|
||
func newService(cfg config.Config) *service { | ||
return &service{ | ||
log: cfg.Log(), | ||
log: cfg.Log().WithField("service", "api"), | ||
copus: cfg.Copus(), | ||
listener: cfg.Listener(), | ||
cfg: cfg, | ||
} | ||
} | ||
|
||
func Run(cfg config.Config) { | ||
if err := newService(cfg).run(); err != nil { | ||
panic(err) | ||
} | ||
func Run(ctx context.Context, cfg config.Config) { | ||
newService(cfg).run(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package requests | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/go-ozzo/ozzo-validation/is" | ||
val "github.com/go-ozzo/ozzo-validation/v4" | ||
"github.com/rarimo/bio-data-svc/resources" | ||
) | ||
|
||
func AddData(r *http.Request) (req resources.AddValueRequest, err error) { | ||
if err = json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
return req, val.Errors{"body": err} | ||
} | ||
|
||
return req, val.Errors{ | ||
"data/type": val.Validate(req.Data.Type, val.Required, val.In(resources.VALUE)), | ||
"data/attributes/value": val.Validate(req.Data.Attributes.Value, val.Required, is.Base64), | ||
}.Filter() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package requests | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-ozzo/ozzo-validation/is" | ||
val "github.com/go-ozzo/ozzo-validation/v4" | ||
"gitlab.com/distributed_lab/urlval/v4" | ||
) | ||
|
||
type GetDataRequest struct { | ||
Key *string `filter:"key"` | ||
Value *string `filter:"value"` | ||
} | ||
|
||
func NewGetDataRequest(r *http.Request) (req GetDataRequest, err error) { | ||
if err = urlval.Decode(r.URL.Query(), &req); err != nil { | ||
return req, val.Errors{"query": err} | ||
} | ||
|
||
err = val.Errors{ | ||
"key": val.Validate(req.Key, val.When(!val.IsEmpty(req.Key), is.UUID)), | ||
"value": val.Validate(req.Value, val.When(!val.IsEmpty(req.Value), is.Base64)), | ||
}.Filter() | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters