WIP: http server for client

This commit is contained in:
programmer10110 2020-07-30 18:09:13 +03:00
parent 6cb0f59656
commit 06ef734c63
17 changed files with 149 additions and 29 deletions

View File

@ -8,8 +8,8 @@ import (
"path/filepath"
"time"
"github.com/p2p-org/dc4bc/qr"
"github.com/p2p-org/dc4bc/storage"
"github.com/depool/dc4bc/qr"
"github.com/depool/dc4bc/storage"
)
const (

View File

@ -9,13 +9,13 @@ import (
"testing"
"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/p2p-org/dc4bc/mocks/clientMocks"
"github.com/p2p-org/dc4bc/mocks/storageMocks"
"github.com/stretchr/testify/require"
)

109
client/http_server.go Normal file
View File

@ -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"))
}

View File

@ -5,7 +5,7 @@ import (
"testing"
"time"
"github.com/p2p-org/dc4bc/client"
"github.com/depool/dc4bc/client"
"github.com/stretchr/testify/require"
)

View File

@ -1,8 +1,8 @@
package main
import (
"github.com/p2p-org/dc4bc/fsm/state_machines"
"github.com/p2p-org/dc4bc/fsm/types/requests"
"github.com/depool/dc4bc/fsm/state_machines"
"github.com/depool/dc4bc/fsm/types/requests"
"log"
)

View File

@ -2,7 +2,7 @@ package fsm_pool
import (
"errors"
"github.com/p2p-org/dc4bc/fsm/fsm"
"github.com/depool/dc4bc/fsm/fsm"
)
type IStateMachine interface {

View File

@ -3,11 +3,11 @@ package state_machines
import (
"encoding/json"
"errors"
"github.com/p2p-org/dc4bc/fsm/fsm"
"github.com/p2p-org/dc4bc/fsm/fsm_pool"
"github.com/p2p-org/dc4bc/fsm/state_machines/internal"
"github.com/p2p-org/dc4bc/fsm/state_machines/signature_construct_fsm"
"github.com/p2p-org/dc4bc/fsm/state_machines/signature_proposal_fsm"
"github.com/depool/dc4bc/fsm/fsm"
"github.com/depool/dc4bc/fsm/fsm_pool"
"github.com/depool/dc4bc/fsm/state_machines/internal"
"github.com/depool/dc4bc/fsm/state_machines/signature_construct_fsm"
"github.com/depool/dc4bc/fsm/state_machines/signature_proposal_fsm"
)
// Is machine state scope dump will be locked?

View File

@ -1,8 +1,8 @@
package signature_construct_fsm
import (
"github.com/p2p-org/dc4bc/fsm/fsm"
"github.com/p2p-org/dc4bc/fsm/fsm_pool"
"github.com/depool/dc4bc/fsm/fsm"
"github.com/depool/dc4bc/fsm/fsm_pool"
)
const (

View File

@ -2,9 +2,9 @@ package signature_proposal_fsm
import (
"errors"
"github.com/p2p-org/dc4bc/fsm/state_machines/internal"
"github.com/p2p-org/dc4bc/fsm/types/requests"
"github.com/p2p-org/dc4bc/fsm/types/responses"
"github.com/depool/dc4bc/fsm/state_machines/internal"
"github.com/depool/dc4bc/fsm/types/requests"
"github.com/depool/dc4bc/fsm/types/responses"
"log"
)

View File

@ -3,8 +3,8 @@ package signature_proposal_fsm
import (
"crypto/sha256"
"encoding/base64"
"github.com/p2p-org/dc4bc/fsm/state_machines/internal"
"github.com/p2p-org/dc4bc/fsm/types/responses"
"github.com/depool/dc4bc/fsm/state_machines/internal"
"github.com/depool/dc4bc/fsm/types/responses"
"math/rand"
)

View File

@ -1,8 +1,8 @@
package signature_proposal_fsm
import (
"github.com/p2p-org/dc4bc/fsm/fsm"
"github.com/p2p-org/dc4bc/fsm/fsm_pool"
"github.com/depool/dc4bc/fsm/fsm"
"github.com/depool/dc4bc/fsm/fsm_pool"
)
const (

View File

@ -2,7 +2,7 @@ package requests
import (
"errors"
"github.com/p2p-org/dc4bc/fsm/config"
"github.com/depool/dc4bc/fsm/config"
)
// Requests

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/p2p-org/dc4bc
module github.com/depool/dc4bc
go 1.13

View File

@ -10,7 +10,7 @@ import (
"go.dedis.ch/kyber/v3"
dkglib "github.com/p2p-org/dc4bc/dkg"
dkglib "github.com/depool/dc4bc/dkg"
_ "image/gif"
_ "image/png"

View File

@ -5,8 +5,8 @@
package clientMocks
import (
client "github.com/depool/dc4bc/client"
gomock "github.com/golang/mock/gomock"
client "github.com/p2p-org/dc4bc/client"
reflect "reflect"
)

View File

@ -5,8 +5,8 @@
package storageMocks
import (
storage "github.com/depool/dc4bc/storage"
gomock "github.com/golang/mock/gomock"
storage "github.com/p2p-org/dc4bc/storage"
reflect "reflect"
)

View File

@ -2,6 +2,7 @@ package qr
import (
"fmt"
"image"
"log"
"time"
@ -88,3 +89,13 @@ func (p *CameraProcessor) WriteQR(path string, data []byte) error {
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)
}