Skip to content

Commit

Permalink
connect to api
Browse files Browse the repository at this point in the history
  • Loading branch information
adelowo committed Oct 6, 2024
1 parent cf856c9 commit a3eea9f
Show file tree
Hide file tree
Showing 11 changed files with 404 additions and 14 deletions.
8 changes: 7 additions & 1 deletion cmd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
awsCreds "github.com/aws/aws-sdk-go-v2/credentials"
"github.com/ayinke-llc/malak/config"
"github.com/ayinke-llc/malak/internal/datastore/postgres"
"github.com/ayinke-llc/malak/internal/pkg/cache/rediscache"
"github.com/ayinke-llc/malak/internal/pkg/jwttoken"
watermillqueue "github.com/ayinke-llc/malak/internal/pkg/queue/watermill"
"github.com/ayinke-llc/malak/internal/pkg/socialauth"
Expand Down Expand Up @@ -138,6 +139,11 @@ func addHTTPCommand(c *cobra.Command, cfg *config.Config) {
logger.Fatal("could not set up watermill queue", zap.Error(err))
}

redisCache, err := rediscache.New(redisClient)
if err != nil {
logger.Fatal("could not set up redis cache", zap.Error(err))
}

rateLimiterStore, err := getRatelimiter(*cfg)
if err != nil {
logger.Fatal("could not create rate limiter",
Expand Down Expand Up @@ -218,7 +224,7 @@ func addHTTPCommand(c *cobra.Command, cfg *config.Config) {
util.DeRef(cfg), db,
tokenManager, googleAuthProvider,
userRepo, workspaceRepo, planRepo, contactRepo, updateRepo,
mid, gulterHandler, queueHandler)
mid, gulterHandler, queueHandler, redisCache)

go func() {
if err := srv.ListenAndServe(); err != nil {
Expand Down
95 changes: 95 additions & 0 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,17 @@
],
"type": "object"
},
"server.previewUpdateRequest": {
"properties": {
"email": {
"type": "string"
}
},
"required": [
"email"
],
"type": "object"
},
"server.uploadImageResponse": {
"properties": {
"message": {
Expand Down Expand Up @@ -1442,6 +1453,90 @@
]
}
},
"/workspaces/updates/{reference}/preview": {
"post": {
"operationId": "previewUpdate",
"parameters": [
{
"description": "update unique reference.. e.g update_",
"in": "path",
"name": "reference",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/server.previewUpdateRequest"
}
}
},
"description": "request body to create a workspace",
"required": true,
"x-originalParamName": "message"
},
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/server.APIStatus"
}
}
},
"description": "OK"
},
"400": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/server.APIStatus"
}
}
},
"description": "Bad Request"
},
"401": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/server.APIStatus"
}
}
},
"description": "Unauthorized"
},
"404": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/server.APIStatus"
}
}
},
"description": "Not Found"
},
"500": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/server.APIStatus"
}
}
},
"description": "Internal Server Error"
}
},
"summary": "Send preview of an update",
"tags": [
"updates"
]
}
},
"/workspaces/{reference}": {
"post": {
"operationId": "switchworkspace",
Expand Down
16 changes: 8 additions & 8 deletions internal/pkg/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ type QueueHandler interface {
// ENUM(update_preview)
type QueueEventSubscriptionMessage string

type GenericMessage struct{}

func (g GenericMessage) Payload() ([]byte, error) {
var b = new(bytes.Buffer)
return b.Bytes(), json.NewEncoder(b).Encode(g)
}

type PreviewUpdateMessage struct {
Update *malak.Update
Schedule *malak.UpdateSchedule
GenericMessage
}

func ToPayload(m any) []byte {
var b = new(bytes.Buffer)

_ = json.NewEncoder(b).Encode(m)

return b.Bytes()
}
14 changes: 14 additions & 0 deletions mocks/update.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/adelowo/gulter"
"github.com/ayinke-llc/malak"
"github.com/ayinke-llc/malak/config"
"github.com/ayinke-llc/malak/internal/pkg/cache"
"github.com/ayinke-llc/malak/internal/pkg/jwttoken"
"github.com/ayinke-llc/malak/internal/pkg/queue"
"github.com/ayinke-llc/malak/internal/pkg/socialauth"
Expand All @@ -34,12 +35,13 @@ func New(logger *zap.Logger,
updateRepo malak.UpdateRepository,
mid *httplimit.Middleware,
gulterHandler *gulter.Gulter,
queueHandler queue.QueueHandler) (*http.Server, func()) {
queueHandler queue.QueueHandler,
redisCache cache.Cache) (*http.Server, func()) {

srv := &http.Server{
Handler: buildRoutes(logger, db, cfg, jwtTokenManager,
userRepo, workspaceRepo, planRepo, contactRepo, updateRepo,
googleAuthProvider, mid, gulterHandler, queueHandler),
googleAuthProvider, mid, gulterHandler, queueHandler, redisCache),
Addr: fmt.Sprintf(":%d", cfg.HTTP.Port),
}

Expand Down Expand Up @@ -81,6 +83,7 @@ func buildRoutes(
ratelimiterMiddleware *httplimit.Middleware,
gulterHandler *gulter.Gulter,
queueHandler queue.QueueHandler,
redisCache cache.Cache,
) http.Handler {

if cfg.HTTP.Swagger.UIEnabled {
Expand Down Expand Up @@ -126,6 +129,8 @@ func buildRoutes(
referenceGenerator: referenceGenerator,
updateRepo: updateRepo,
cfg: cfg,
queueHandler: queueHandler,
cache: redisCache,
}

router.Use(middleware.RequestID)
Expand Down
14 changes: 13 additions & 1 deletion server/update_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ayinke-llc/malak/internal/pkg/util"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/google/uuid"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
Expand Down Expand Up @@ -44,6 +45,7 @@ func (p *previewUpdateRequest) Validate() error {
// @Accept json
// @Produce json
// @Param reference path string required "update unique reference.. e.g update_"
// @Param message body previewUpdateRequest true "request body to create a workspace"
// @Success 200 {object} APIStatus
// @Failure 400 {object} APIStatus
// @Failure 401 {object} APIStatus
Expand Down Expand Up @@ -147,7 +149,17 @@ func (u *updatesHandler) previewUpdate(
wg.Add(1)
defer wg.Done()

err := u.queueHandler.Add(ctx, queue.QueueEventSubscriptionMessageUpdatePreview, nil)
m := &queue.PreviewUpdateMessage{
Update: update,
Schedule: schedule,
}

err := u.queueHandler.Add(ctx,
queue.QueueEventSubscriptionMessageUpdatePreview.String(),
&queue.Message{
ID: uuid.NewString(),
Data: queue.ToPayload(m),
})
if err != nil {
logger.Error("could not add schedule to queue to be processed",
zap.Error(err))
Expand Down
76 changes: 76 additions & 0 deletions swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,71 @@ const docTemplate = `{
}
}
},
"/workspaces/updates/{reference}/preview": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"updates"
],
"summary": "Send preview of an update",
"operationId": "previewUpdate",
"parameters": [
{
"type": "string",
"description": "update unique reference.. e.g update_",
"name": "reference",
"in": "path",
"required": true
},
{
"description": "request body to create a workspace",
"name": "message",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/server.previewUpdateRequest"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/server.APIStatus"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/server.APIStatus"
}
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/server.APIStatus"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/server.APIStatus"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/server.APIStatus"
}
}
}
}
},
"/workspaces/{reference}": {
"post": {
"consumes": [
Expand Down Expand Up @@ -1277,6 +1342,17 @@ const docTemplate = `{
}
}
},
"server.previewUpdateRequest": {
"type": "object",
"required": [
"email"
],
"properties": {
"email": {
"type": "string"
}
}
},
"server.uploadImageResponse": {
"type": "object",
"required": [
Expand Down
Loading

0 comments on commit a3eea9f

Please sign in to comment.