diff --git a/cmd/query-service/main.go b/cmd/query-service/main.go index ed3a60bc0f485b5c1bb8566190a74e537a206e9f..2061f0143a8ee2835e33b7fc00cb405c1dab30e5 100755 --- a/cmd/query-service/main.go +++ b/cmd/query-service/main.go @@ -6,12 +6,14 @@ This program has been developed by students from the bachelor Computer Science a package main import ( + "encoding/json" "net/http" "os" "query-service/internal/usecases/consume" "query-service/internal/usecases/produce" "git.science.uu.nl/graphpolaris/go-common/microservice" + "git.science.uu.nl/graphpolaris/go-common/structs/v1/models" "git.science.uu.nl/graphpolaris/query-conversion" "git.science.uu.nl/graphpolaris/query-conversion/cypherv2" queryexecution "git.science.uu.nl/graphpolaris/query-execution" @@ -65,10 +67,50 @@ func main() { go consumeService.Start() + http.HandleFunc("/execute-cypher-query", handleExecuteCypherQuery(queryExecutor)) + // Expose /metrics endpoint for prometheus log.Info().Int("port", 8080).Msg("starting prometheus metrics server") http.Handle("/metrics", promhttp.Handler()) + http.ListenAndServe(":8080", nil) select {} } + +func handleExecuteCypherQuery(queryExecutor queryexecution.Executor) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + log.Info().Msg("Received request to execute Cypher query") + if r.Method == http.MethodPost { + var req struct { + Query string `json:"query"` + } + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + log.Error().Err(err).Msg("Failed to decode request payload") + http.Error(w, "Invalid request payload", http.StatusBadRequest) + return + } + + log.Info().Str("query", req.Query).Msg("Executing query") + result, err := queryExecutor.ExecuteQuery(req.Query, &models.DBConnectionModel{ + URL: "demo.neo4jlabs.com", + Protocol: "neo4j+s://", + Port: 7687, + Username: "movies", + Password: "fire$10", //not sure what the password was + InternalDatabaseName: "movies", + Type: models.Neo4j, + }) + if err != nil { + log.Error().Err(err).Msg("Query execution failed") + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + w.Write(*result) + } else { + log.Error().Msg("Invalid request method") + http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) + } + } +}