From 9688892825b30175a66806a7fc4a4a6ffa364377 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 27 Sep 2023 21:23:06 -0300 Subject: [PATCH 1/3] chore: update tangle-client-go to version 1.2.0 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 563de7f..39e5550 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/allancapistrano/tangle-hornet-api go 1.20 require ( - github.com/allancapistrano/tangle-client-go v1.1.1 + github.com/allancapistrano/tangle-client-go v1.2.0 github.com/gorilla/mux v1.8.0 ) diff --git a/go.sum b/go.sum index 005befe..aa93d02 100644 --- a/go.sum +++ b/go.sum @@ -17,8 +17,8 @@ github.com/Joker/jade v1.0.1-0.20190614124447-d475f43051e7/go.mod h1:6E6s8o2AE4K github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0= github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= -github.com/allancapistrano/tangle-client-go v1.1.1 h1:zgj4qoiJvrJ7lGsXzmkA0xrZ9A3iImICwdcGM0KrgV0= -github.com/allancapistrano/tangle-client-go v1.1.1/go.mod h1:mDBHurufVjrhBL100yRzvpJEPNscjFlip6efNI3YQKM= +github.com/allancapistrano/tangle-client-go v1.2.0 h1:oiOZONDUF0wzhd4z4VMl+Q3tkttU5mRofmTtzYY8Eh8= +github.com/allancapistrano/tangle-client-go v1.2.0/go.mod h1:AwPSVwcfCVDDXdLQIM//9s9AHL7EOOn/LOxbk+0Sq80= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= From 3e55475ccac83f093c853ccc6636cfe9b4a7e332 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 27 Sep 2023 21:24:02 -0300 Subject: [PATCH 2/3] feat: create GetMessageByMessageId function --- endpoints/message.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/endpoints/message.go b/endpoints/message.go index 1270081..1259d9d 100644 --- a/endpoints/message.go +++ b/endpoints/message.go @@ -46,6 +46,34 @@ func GetAllMessagesByIndex(writer http.ResponseWriter, request *http.Request) { fmt.Fprint(writer, jsonInString) } +// Get a message by given message ID. +func GetMessageByMessageId(writer http.ResponseWriter, request *http.Request) { + var jsonInString string + nodeURL := config.GetNodeUrl(CONFIG_FILE_NAME, true) + nodePort := config.GetNodePort(CONFIG_FILE_NAME, true) + nodeAddress := fmt.Sprintf("http://%s:%s", nodeURL, nodePort) + + vars := mux.Vars(request) + messageId := vars["messageID"] + + message, err := messages.GetMessageFormattedByMessageID(nodeAddress, messageId) + if err != nil { + jsonInString = fmt.Sprintf("{\"error\": \"%s\"}", err.Error()) + http.Error(writer, jsonInString, http.StatusInternalServerError) + } else { + jsonInBytes, err := json.Marshal(message) + + if err != nil { + jsonInString = "{\"error\": \"Unable to convert the messages struct into JSON format.\"}" + http.Error(writer, jsonInString, http.StatusInternalServerError) + } else { + jsonInString = string(jsonInBytes) // TODO: Corrigir espaços em branco + } + } + + fmt.Fprint(writer, jsonInString) +} + // Create and submit a new message. func CreateNewMessage(writer http.ResponseWriter, request *http.Request) { var message Message From 0729433aeb42c90dfe22e7d1640b8151e1a6699b Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Wed, 27 Sep 2023 21:24:28 -0300 Subject: [PATCH 3/3] feat: create message/messageId route --- router/router.go | 1 + 1 file changed, 1 insertion(+) diff --git a/router/router.go b/router/router.go index 12a271a..7793440 100644 --- a/router/router.go +++ b/router/router.go @@ -15,6 +15,7 @@ func Routes() (router *mux.Router) { // Messages router.HandleFunc("/message", endpoints.CreateNewMessage).Methods("POST") router.HandleFunc("/message/{index}", endpoints.GetAllMessagesByIndex) + router.HandleFunc("/message/messageId/{messageID}", endpoints.GetMessageByMessageId) return router }