mirror of https://github.com/certusone/dc4bc.git
WIP: http server for client
This commit is contained in:
parent
6cb0f59656
commit
06ef734c63
|
@ -8,8 +8,8 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/p2p-org/dc4bc/qr"
|
"github.com/depool/dc4bc/qr"
|
||||||
"github.com/p2p-org/dc4bc/storage"
|
"github.com/depool/dc4bc/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -9,13 +9,13 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/p2p-org/dc4bc/mocks/qrMocks"
|
"github.com/depool/dc4bc/mocks/qrMocks"
|
||||||
|
|
||||||
"github.com/p2p-org/dc4bc/client"
|
"github.com/depool/dc4bc/client"
|
||||||
|
|
||||||
|
"github.com/depool/dc4bc/mocks/clientMocks"
|
||||||
|
"github.com/depool/dc4bc/mocks/storageMocks"
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/p2p-org/dc4bc/mocks/clientMocks"
|
|
||||||
"github.com/p2p-org/dc4bc/mocks/storageMocks"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,109 @@
|
||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/depool/dc4bc/storage"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func errorResponse(w http.ResponseWriter, statusCode int, err string) {
|
||||||
|
log.Println(err)
|
||||||
|
w.WriteHeader(statusCode)
|
||||||
|
if _, err := w.Write([]byte(err)); err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to write response: %v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func successResponse(w http.ResponseWriter, response []byte) {
|
||||||
|
if _, err := w.Write(response); err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to write response: %v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) StartHTTPServer(listenAddr string) error {
|
||||||
|
http.HandleFunc("/sendMessage", c.sendMessageHandler)
|
||||||
|
http.HandleFunc("/getOperations", c.getOperationsHandler)
|
||||||
|
http.HandleFunc("/getOperationQRPath", c.getOperationQRPathHandler)
|
||||||
|
http.HandleFunc("/readProcessedOperationFromCamera", c.readProcessedOperationFromCameraHandler)
|
||||||
|
return http.ListenAndServe(listenAddr, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) sendMessageHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reqBytes, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
errorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to read request body: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var msg storage.Message
|
||||||
|
if err = json.Unmarshal(reqBytes, &msg); err != nil {
|
||||||
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to unmarshal message: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = c.SendMessage(msg); err != nil {
|
||||||
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to send message to the storage: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
successResponse(w, []byte("ok"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) getOperationsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
operations, err := c.GetOperations()
|
||||||
|
if err != nil {
|
||||||
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to get operations: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := json.Marshal(operations)
|
||||||
|
if err != nil {
|
||||||
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to marshal operations: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
successResponse(w, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) getOperationQRPathHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
operationID := r.URL.Query().Get("operationID")
|
||||||
|
|
||||||
|
qrPath, err := c.GetOperationQRPath(operationID)
|
||||||
|
if err != nil {
|
||||||
|
errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to get operation QR path: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
successResponse(w, []byte(qrPath))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) readProcessedOperationFromCameraHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
errorResponse(w, http.StatusBadRequest, "Wrong HTTP method")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.ReadProcessedOperation(); err != nil {
|
||||||
|
errorResponse(w, http.StatusInternalServerError,
|
||||||
|
fmt.Sprintf("failed to read processed operation from camera path: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
successResponse(w, []byte("ok"))
|
||||||
|
}
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/p2p-org/dc4bc/client"
|
"github.com/depool/dc4bc/client"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/p2p-org/dc4bc/fsm/state_machines"
|
"github.com/depool/dc4bc/fsm/state_machines"
|
||||||
"github.com/p2p-org/dc4bc/fsm/types/requests"
|
"github.com/depool/dc4bc/fsm/types/requests"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ package fsm_pool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/p2p-org/dc4bc/fsm/fsm"
|
"github.com/depool/dc4bc/fsm/fsm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IStateMachine interface {
|
type IStateMachine interface {
|
||||||
|
|
|
@ -3,11 +3,11 @@ package state_machines
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/p2p-org/dc4bc/fsm/fsm"
|
"github.com/depool/dc4bc/fsm/fsm"
|
||||||
"github.com/p2p-org/dc4bc/fsm/fsm_pool"
|
"github.com/depool/dc4bc/fsm/fsm_pool"
|
||||||
"github.com/p2p-org/dc4bc/fsm/state_machines/internal"
|
"github.com/depool/dc4bc/fsm/state_machines/internal"
|
||||||
"github.com/p2p-org/dc4bc/fsm/state_machines/signature_construct_fsm"
|
"github.com/depool/dc4bc/fsm/state_machines/signature_construct_fsm"
|
||||||
"github.com/p2p-org/dc4bc/fsm/state_machines/signature_proposal_fsm"
|
"github.com/depool/dc4bc/fsm/state_machines/signature_proposal_fsm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Is machine state scope dump will be locked?
|
// Is machine state scope dump will be locked?
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package signature_construct_fsm
|
package signature_construct_fsm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/p2p-org/dc4bc/fsm/fsm"
|
"github.com/depool/dc4bc/fsm/fsm"
|
||||||
"github.com/p2p-org/dc4bc/fsm/fsm_pool"
|
"github.com/depool/dc4bc/fsm/fsm_pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -2,9 +2,9 @@ package signature_proposal_fsm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/p2p-org/dc4bc/fsm/state_machines/internal"
|
"github.com/depool/dc4bc/fsm/state_machines/internal"
|
||||||
"github.com/p2p-org/dc4bc/fsm/types/requests"
|
"github.com/depool/dc4bc/fsm/types/requests"
|
||||||
"github.com/p2p-org/dc4bc/fsm/types/responses"
|
"github.com/depool/dc4bc/fsm/types/responses"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@ package signature_proposal_fsm
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"github.com/p2p-org/dc4bc/fsm/state_machines/internal"
|
"github.com/depool/dc4bc/fsm/state_machines/internal"
|
||||||
"github.com/p2p-org/dc4bc/fsm/types/responses"
|
"github.com/depool/dc4bc/fsm/types/responses"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package signature_proposal_fsm
|
package signature_proposal_fsm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/p2p-org/dc4bc/fsm/fsm"
|
"github.com/depool/dc4bc/fsm/fsm"
|
||||||
"github.com/p2p-org/dc4bc/fsm/fsm_pool"
|
"github.com/depool/dc4bc/fsm/fsm_pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -2,7 +2,7 @@ package requests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/p2p-org/dc4bc/fsm/config"
|
"github.com/depool/dc4bc/fsm/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Requests
|
// Requests
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,4 +1,4 @@
|
||||||
module github.com/p2p-org/dc4bc
|
module github.com/depool/dc4bc
|
||||||
|
|
||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
|
|
2
main.go
2
main.go
|
@ -10,7 +10,7 @@ import (
|
||||||
|
|
||||||
"go.dedis.ch/kyber/v3"
|
"go.dedis.ch/kyber/v3"
|
||||||
|
|
||||||
dkglib "github.com/p2p-org/dc4bc/dkg"
|
dkglib "github.com/depool/dc4bc/dkg"
|
||||||
|
|
||||||
_ "image/gif"
|
_ "image/gif"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
package clientMocks
|
package clientMocks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
client "github.com/depool/dc4bc/client"
|
||||||
gomock "github.com/golang/mock/gomock"
|
gomock "github.com/golang/mock/gomock"
|
||||||
client "github.com/p2p-org/dc4bc/client"
|
|
||||||
reflect "reflect"
|
reflect "reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
package storageMocks
|
package storageMocks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
storage "github.com/depool/dc4bc/storage"
|
||||||
gomock "github.com/golang/mock/gomock"
|
gomock "github.com/golang/mock/gomock"
|
||||||
storage "github.com/p2p-org/dc4bc/storage"
|
|
||||||
reflect "reflect"
|
reflect "reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
11
qr/qr.go
11
qr/qr.go
|
@ -2,6 +2,7 @@ package qr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -88,3 +89,13 @@ func (p *CameraProcessor) WriteQR(path string, data []byte) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadQRFromImg(img image.Image) (*gozxing.Result, error) {
|
||||||
|
bmp, err := gozxing.NewBinaryBitmapFromImage(img)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get NewBinaryBitmapFromImage: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
qrReader := qrcode.NewQRCodeReader()
|
||||||
|
return qrReader.Decode(bmp, nil)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue