Skip to content

Commit ce559d1

Browse files
Merge pull request #17 from Evolutionary-Algorithms-On-Click/get-run-api
Create API to retrieve run by ID and improve logging
2 parents 67bee1e + a8f0615 commit ce559d1

File tree

14 files changed

+125
-34
lines changed

14 files changed

+125
-34
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export DATABASE_URL=<cockroachdb_url>
2222
export MINIO_ENDPOINT=<minio_endpoint>
2323
export MINIO_ACCESS_KEY_ID=<minio_access_key>
2424
export MINIO_SECRET_KEY=<minio_secret_key>
25+
export RABBITMQ_URL=<rabbitmq_url>
2526
```
2627
3. Run the following command to start the server.
2728
```sh

controller/ea.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ func CreateEA(res http.ResponseWriter, req *http.Request) {
121121
return
122122
}
123123

124-
// TODO: Schedule the run.
125-
126124
if err := util.EnqueueRunRequest(req.Context(), runID, "code", "py"); err != nil {
127125
util.JSONResponse(res, http.StatusInternalServerError, err.Error(), nil)
128126
return

controller/gp.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func CreateGP(res http.ResponseWriter, req *http.Request) {
121121
return
122122
}
123123

124-
// TODO: Schedule the run.
125124
if err := util.EnqueueRunRequest(req.Context(), runID, "code", "py"); err != nil {
126125
util.JSONResponse(res, http.StatusInternalServerError, err.Error(), nil)
127126
return

controller/ml.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func CreateML(res http.ResponseWriter, req *http.Request) {
121121
return
122122
}
123123

124-
// TODO: Schedule the run.
125124
if err := util.EnqueueRunRequest(req.Context(), runID, "code", "py"); err != nil {
126125
util.JSONResponse(res, http.StatusInternalServerError, err.Error(), nil)
127126
return

controller/pso.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func CreatePSO(res http.ResponseWriter, req *http.Request) {
121121
return
122122
}
123123

124-
// TODO: Schedule the run.
125124
if err := util.EnqueueRunRequest(req.Context(), runID, "code", "py"); err != nil {
126125
util.JSONResponse(res, http.StatusInternalServerError, err.Error(), nil)
127126
return

controller/run.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,42 @@ import (
77
"net/http"
88
)
99

10+
func UserRun(res http.ResponseWriter, req *http.Request) {
11+
var logger = util.NewLogger()
12+
logger.Info("UserRuns API called.")
13+
14+
user, err := modules.Auth(req)
15+
if err != nil {
16+
util.JSONResponse(res, http.StatusUnauthorized, err.Error(), nil)
17+
return
18+
}
19+
20+
// User has id, role, userName, email & fullName.
21+
logger.Info(fmt.Sprintf("User: %s", user))
22+
23+
data, err := util.Body(req)
24+
if err != nil {
25+
util.JSONResponse(res, http.StatusBadRequest, err.Error(), nil)
26+
return
27+
}
28+
29+
run, err := modules.RunDataReqFromJSON(data)
30+
if err != nil {
31+
util.JSONResponse(res, http.StatusBadRequest, err.Error(), nil)
32+
return
33+
}
34+
35+
logger.Info(fmt.Sprintf("Run: %s", run.RunID))
36+
37+
runData, err := run.UserRun(req.Context(), user["id"], logger)
38+
if err != nil {
39+
util.JSONResponse(res, http.StatusInternalServerError, err.Error(), nil)
40+
return
41+
}
42+
43+
util.JSONResponse(res, http.StatusOK, "User run", runData)
44+
}
45+
1046
func UserRuns(res http.ResponseWriter, req *http.Request) {
1147
var logger = util.NewLogger()
1248
logger.Info("UserRuns API called.")

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22.0
55
require (
66
github.com/jackc/pgx/v5 v5.7.2
77
github.com/minio/minio-go/v7 v7.0.87
8+
github.com/rabbitmq/amqp091-go v1.10.0
89
github.com/rs/cors v1.11.1
910
google.golang.org/grpc v1.70.0
1011
google.golang.org/protobuf v1.36.5
@@ -22,7 +23,6 @@ require (
2223
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
2324
github.com/minio/crc64nvme v1.0.1 // indirect
2425
github.com/minio/md5-simd v1.1.2 // indirect
25-
github.com/rabbitmq/amqp091-go v1.10.0 // indirect
2626
github.com/rs/xid v1.6.0 // indirect
2727
golang.org/x/crypto v0.33.0 // indirect
2828
golang.org/x/net v0.35.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ go.opentelemetry.io/otel/sdk/metric v1.32.0 h1:rZvFnvmvawYb0alrYkjraqJq0Z4ZUJAiy
5959
go.opentelemetry.io/otel/sdk/metric v1.32.0/go.mod h1:PWeZlq0zt9YkYAp3gjKZ0eicRYvOh1Gd+X99x6GHpCQ=
6060
go.opentelemetry.io/otel/trace v1.32.0 h1:WIC9mYrXf8TmY/EXuULKc8hR17vE+Hjv2cssQDe03fM=
6161
go.opentelemetry.io/otel/trace v1.32.0/go.mod h1:+i4rkvCraA+tG6AzwloGaCtkx53Fa+L+V8e9a7YvhT8=
62+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
63+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
6264
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
6365
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
6466
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func main() {
2222
http.HandleFunc(routes.PSO, controller.CreatePSO)
2323
http.HandleFunc(routes.RUNS, controller.UserRuns)
2424
http.HandleFunc(routes.SHARE_RUN, controller.ShareRun)
25+
http.HandleFunc(routes.RUN, controller.UserRun)
2526

2627
logger.Info(fmt.Sprintf("Test http server on http://localhost%v/api/test", config.PORT))
2728

modules/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func Auth(req *http.Request) (map[string]string, error) {
3535
defer authConn.Close()
3636

3737
authClient := pb.NewAuthenticateClient(authConn)
38-
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
38+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
3939
defer cancel()
4040

4141
r, err := authClient.Auth(ctx, &pb.TokenValidateRequest{Token: token.Value})

0 commit comments

Comments
 (0)