Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Godoc api #14

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ go.work
# End of https://www.toptal.com/developers/gitignore/api/go,visualstudiocode

# Custom
waggle
waggle_dev
.secrets/
prod.env
Expand Down
2 changes: 1 addition & 1 deletion bugout.go → cmd/waggle/bugout.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion cmd.go → cmd/waggle/cmd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package waggle

import (
"encoding/csv"
Expand Down
78 changes: 78 additions & 0 deletions cmd/waggle/example_test.go
Original file line number Diff line number Diff line change
@@ -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"
// }
// ]
// }
}
2 changes: 1 addition & 1 deletion moonstream.go → cmd/waggle/moonstream.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package waggle

import (
"bytes"
Expand Down
79 changes: 72 additions & 7 deletions server.go → cmd/waggle/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package waggle

import (
"bytes"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions cmd/waggle/server_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion settings.go → cmd/waggle/settings.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package waggle

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion sign.go → cmd/waggle/sign.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package waggle

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion sign_test.go → cmd/waggle/sign_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package waggle

import (
"encoding/hex"
Expand Down
3 changes: 3 additions & 0 deletions cmd/waggle/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package waggle

var WAGGLE_VERSION = "0.1.2"
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 0 additions & 3 deletions version.go

This file was deleted.

1 change: 0 additions & 1 deletion version.txt

This file was deleted.

Loading