-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from surendratiwari3/enhancement-issue-3
Enhancement issue 3
- Loading branch information
Showing
33 changed files
with
724 additions
and
273 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 |
---|---|---|
|
@@ -25,4 +25,6 @@ go.work | |
|
||
#ignore editor folder | ||
.idea/ | ||
example/main | ||
example/main | ||
|
||
**/mock_*.go |
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,61 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/go-playground/validator/v10" | ||
"github.com/google/uuid" | ||
"github.com/labstack/echo/v4" | ||
"github.com/surendratiwari3/paota/errors" | ||
"github.com/surendratiwari3/paota/logger" | ||
"github.com/surendratiwari3/paota/schema" | ||
"github.com/surendratiwari3/paota/workerpool" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
// AddTaskHandlerV1 handles the request to add a task for API version 1 | ||
func AddTaskHandlerV1(c echo.Context) error { | ||
// Parse request body to get the Signature | ||
body, err := ioutil.ReadAll(c.Request().Body) | ||
if err != nil { | ||
return c.String(http.StatusInternalServerError, "Failed to read request body") | ||
} | ||
|
||
taskName := c.Param("task_name") | ||
|
||
var signature schema.Signature | ||
if err := json.Unmarshal(body, &signature); err != nil { | ||
return c.String(http.StatusBadRequest, "Invalid JSON payload") | ||
} | ||
signature.Name = taskName | ||
signature.UUID = uuid.NewString() | ||
validate := validator.New(validator.WithRequiredStructEnabled()) | ||
|
||
// Validate the Signature | ||
if err := validate.Struct(signature); err != nil { | ||
return c.String(http.StatusBadRequest, fmt.Sprintf("Invalid Signature: %s", err.Error())) | ||
} | ||
|
||
// Add the task based on the received Signature and task_name | ||
err = EnqueueTask(&signature) | ||
if err != nil { | ||
return c.String(http.StatusInternalServerError, fmt.Sprintf("Failed to add task: %s", err.Error())) | ||
} | ||
|
||
return c.String(http.StatusOK, "Task added successfully") | ||
} | ||
|
||
func EnqueueTask(job *schema.Signature) error { | ||
newWorkerPool, err := workerpool.NewWorkerPool(context.Background(), 10, "testWorker") | ||
if err != nil { | ||
logger.ApplicationLogger.Error("workerPool is not created", err) | ||
return err | ||
} else if newWorkerPool == nil { | ||
logger.ApplicationLogger.Info("workerPool is nil") | ||
return errors.ErrInvalidConfig | ||
} | ||
_, err = newWorkerPool.SendTaskWithContext(context.Background(), job) | ||
return err | ||
} |
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,43 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
"github.com/sirupsen/logrus" | ||
"github.com/surendratiwari3/paota/api/handlers" | ||
"github.com/surendratiwari3/paota/config" | ||
"github.com/surendratiwari3/paota/logger" | ||
) | ||
|
||
func main() { | ||
logrusLog := logrus.StandardLogger() | ||
logrusLog.SetFormatter(&logrus.JSONFormatter{}) | ||
logger.ApplicationLogger = logrusLog | ||
|
||
cnf := config.Config{ | ||
Broker: "amqp", | ||
//Store: "null", | ||
TaskQueueName: "paota_task_queue", | ||
AMQP: &config.AMQPConfig{ | ||
Url: "amqp://guest:guest@localhost:55005/", | ||
Exchange: "paota_task_exchange", | ||
ExchangeType: "direct", | ||
BindingKey: "paota_task_binding_key", | ||
PrefetchCount: 100, | ||
ConnectionPoolSize: 10, | ||
}, | ||
} | ||
err := config.GetConfigProvider().SetApplicationConfig(cnf) | ||
if err != nil { | ||
logger.ApplicationLogger.Error("config error", err) | ||
return | ||
} | ||
|
||
e := echo.New() | ||
|
||
v1 := e.Group("/v1/paota") | ||
|
||
v1.POST("/tasks/:task_name", handlers.AddTaskHandlerV1) | ||
|
||
// Start the server | ||
e.Start(":8090") | ||
} |
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
Oops, something went wrong.