diff --git a/.gitignore b/.gitignore index dab8513..3051806 100644 --- a/.gitignore +++ b/.gitignore @@ -46,7 +46,6 @@ go.work # End of https://www.toptal.com/developers/gitignore/api/go,visualstudiocode # Custom -waggle waggle_dev .secrets/ prod.env diff --git a/bugout.go b/cmd/waggle/bugout.go similarity index 99% rename from bugout.go rename to cmd/waggle/bugout.go index 041ae34..a8d9bfc 100644 --- a/bugout.go +++ b/cmd/waggle/bugout.go @@ -1,4 +1,4 @@ -package main +package waggle // Much of this code is copied from waggle: https://github.com/bugout-dev/waggle/blob/main/main.go diff --git a/cmd.go b/cmd/waggle/cmd.go similarity index 99% rename from cmd.go rename to cmd/waggle/cmd.go index 67f1efd..6f1ddce 100644 --- a/cmd.go +++ b/cmd/waggle/cmd.go @@ -1,4 +1,4 @@ -package main +package waggle import ( "encoding/csv" diff --git a/cmd/waggle/example_test.go b/cmd/waggle/example_test.go new file mode 100644 index 0000000..ee58400 --- /dev/null +++ b/cmd/waggle/example_test.go @@ -0,0 +1,78 @@ +package waggle + +func ExampleServer_PingRoute() { + // Example of request and response from PingRoute + + // Output: + // # Request + // GET /ping HTTP/1.1 + // + // # Response + // HTTP/1.1 200 OK + // + // { + // "status": "ok" + // } +} + +func ExampleServer_SignDropperRoute() { + // Example of request and response from SignDropperRoute + + // Output: + // # Request + // POST /sign/dropper HTTP/1.1 + // Content-Type: application/json + // + // { + // "chain_id": 80001, + // "dropper": "0x4ec36E288E1b5d6914851a141cb041152Cf95328", + // "signer": "0x629c51488a18fc75f4b8993743f3c132316951c9", + // "requests": [ + // { + // "dropId": "2", + // "requestID": "5", + // "claimant": "0x000000000000000000000000000000000000dEaD", + // "blockDeadline": "40000000", + // "amount": "3000000000000000000" + // }, + // { + // "dropId": "2", + // "requestID": "6", + // "claimant": "0x000000000000000000000000000000000000dEaD", + // "blockDeadline": "40000000", + // "amount": "3000000000000000000" + // } + // ] + // } + // + // # Response + // HTTP/1.1 200 OK + // Content-Type: application/json + // + // { + // "chain_id": 80001, + // "dropper": "0x4ec36E288E1b5d6914851a141cb041152Cf95328", + // "signer": "0x629c51488a18fc75f4b8993743f3c132316951c9", + // "sensible": false, + // "requests": [ + // { + // "dropId": "2", + // "requestID": "5", + // "claimant": "0x000000000000000000000000000000000000dEaD", + // "blockDeadline": "40000000", + // "amount": "3000000000000000000", + // "signature": "8165f3e1edba760f570c833891ef238c9e40d3e2d1c6d66ab39904d1934c4bb9642e463bd24e0464cceb16a91ea96a48965bb7603d59dc6b859f1112d077a5e61b", + // "signer": "0x629c51488a18fc75f4b8993743f3c132316951c9" + // }, + // { + // "dropId": "2", + // "requestID": "6", + // "claimant": "0x000000000000000000000000000000000000dEaD", + // "blockDeadline": "40000000", + // "amount": "3000000000000000000", + // "signature": "85177edfac02da74776e761f230dc8d1c367ec3fb400881224d8e6001b00b17326048930b0b6e4a03ce407933e12399f80b325cc0be075fad855a3c6168f3b221c", + // "signer": "0x629c51488a18fc75f4b8993743f3c132316951c9" + // } + // ] + // } +} diff --git a/moonstream.go b/cmd/waggle/moonstream.go similarity index 99% rename from moonstream.go rename to cmd/waggle/moonstream.go index a6c09a2..0e2c0cd 100644 --- a/moonstream.go +++ b/cmd/waggle/moonstream.go @@ -1,4 +1,4 @@ -package main +package waggle import ( "bytes" diff --git a/server.go b/cmd/waggle/server.go similarity index 80% rename from server.go rename to cmd/waggle/server.go index 13f91b0..c25b4f3 100644 --- a/server.go +++ b/cmd/waggle/server.go @@ -1,4 +1,4 @@ -package main +package waggle import ( "bytes" @@ -121,15 +121,80 @@ func (server *Server) panicMiddleware(next http.Handler) http.Handler { }) } -// pingRoute response with status of load balancer server itself -func (server *Server) pingRoute(w http.ResponseWriter, r *http.Request) { +// PingRoute handles a GET request to the server status endpoint. +// +// # Request +// +// GET /ping +// +// # Response +// +// HTTP/1.1 200 OK +// +// Headers: +// +// Content-Type: application/json +// +// Body: +// +// status: string +// +// This endpoint responds with a JSON object indicating the status of the waggle server. +func (server *Server) PingRoute(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") response := PingResponse{Status: "ok"} json.NewEncoder(w).Encode(response) } -// signDropperRoute response with status of load balancer server itself -func (server *Server) signDropperRoute(w http.ResponseWriter, r *http.Request) { +// SignDropperRoute handles a POST request to the signing endpoint. +// +// # Request +// +// POST /sign/dropper +// +// Headers: +// +// Content-Type: application/json +// +// Body: +// +// chain_id: int +// dropper: string +// signer: string +// requests: list[ +// dropId: string +// requestID: string +// claimant: string +// blockDeadline: string +// amount: string +// ] +// +// # Response +// +// HTTP/1.1 200 OK +// +// Headers: +// +// Content-Type: application/json +// +// Body: +// +// chain_id: int +// dropper: string +// signer: string +// sensible: bool +// requests: list[ +// dropId: string +// requestID: string +// claimant: string +// blockDeadline: string +// amount: string +// signature: string +// signer: string +// ] +// +// This endpoint responds with signed message from Dropper signer. +func (server *Server) SignDropperRoute(w http.ResponseWriter, r *http.Request) { body, readErr := io.ReadAll(r.Body) if readErr != nil { http.Error(w, "Unable to read body", http.StatusBadRequest) @@ -182,8 +247,8 @@ func (server *Server) signDropperRoute(w http.ResponseWriter, r *http.Request) { // Serve handles server run func (server *Server) Serve() error { serveMux := http.NewServeMux() - serveMux.HandleFunc("/ping", server.pingRoute) - serveMux.HandleFunc("/sign/dropper", server.signDropperRoute) + serveMux.HandleFunc("/ping", server.PingRoute) + serveMux.HandleFunc("/sign/dropper", server.SignDropperRoute) // Set list of common middleware, from bottom to top commonHandler := server.corsMiddleware(serveMux) diff --git a/cmd/waggle/server_test.go b/cmd/waggle/server_test.go new file mode 100644 index 0000000..8e1034d --- /dev/null +++ b/cmd/waggle/server_test.go @@ -0,0 +1,28 @@ +package waggle + +import ( + "encoding/json" + "fmt" + "net/http/httptest" + "testing" +) + + +func TestServerPingRoute(t *testing.T) { + // Initialize Server instance and create Request to pass handler + server := &Server{} + r := httptest.NewRequest("GET", "/ping", nil) + + // Create ResponseRecoreder which statisfies http.ResponseWriter + // to record the response. + w := httptest.NewRecorder() + + server.PingRoute(w, r) + + result := w.Result() + var resp PingResponse + if err := json.NewDecoder(result.Body).Decode(&resp); err != nil { + fmt.Printf("Error decoding response: %v", err) + } + defer result.Body.Close() +} diff --git a/settings.go b/cmd/waggle/settings.go similarity index 99% rename from settings.go rename to cmd/waggle/settings.go index 7cd5910..24f6df0 100644 --- a/settings.go +++ b/cmd/waggle/settings.go @@ -1,4 +1,4 @@ -package main +package waggle import ( "encoding/json" diff --git a/sign.go b/cmd/waggle/sign.go similarity index 99% rename from sign.go rename to cmd/waggle/sign.go index 364cfd6..60f8290 100644 --- a/sign.go +++ b/cmd/waggle/sign.go @@ -1,4 +1,4 @@ -package main +package waggle import ( "fmt" diff --git a/sign_test.go b/cmd/waggle/sign_test.go similarity index 98% rename from sign_test.go rename to cmd/waggle/sign_test.go index 643d892..41ae61d 100644 --- a/sign_test.go +++ b/cmd/waggle/sign_test.go @@ -1,4 +1,4 @@ -package main +package waggle import ( "encoding/hex" diff --git a/cmd/waggle/version.go b/cmd/waggle/version.go new file mode 100644 index 0000000..4879dae --- /dev/null +++ b/cmd/waggle/version.go @@ -0,0 +1,3 @@ +package waggle + +var WAGGLE_VERSION = "0.1.2" diff --git a/main.go b/main.go index 1e05a86..07554d0 100644 --- a/main.go +++ b/main.go @@ -3,10 +3,12 @@ package main import ( "fmt" "os" + + "github.com/moonstream-to/waggle/cmd/waggle" ) func main() { - command := CreateRootCommand() + command := waggle.CreateRootCommand() err := command.Execute() if err != nil { fmt.Println(err.Error()) diff --git a/version.go b/version.go deleted file mode 100644 index c945d23..0000000 --- a/version.go +++ /dev/null @@ -1,3 +0,0 @@ -package main - -var WAGGLE_VERSION = "0.1.0" diff --git a/version.txt b/version.txt deleted file mode 100644 index 17e51c3..0000000 --- a/version.txt +++ /dev/null @@ -1 +0,0 @@ -0.1.1