Skip to content
Snippets Groups Projects
Verified Commit 520c4f93 authored by xavier.perret's avatar xavier.perret
Browse files

refactoring

parent 4fce1365
No related branches found
No related tags found
1 merge request!2added file to separate client function from the server
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"net" "net"
. "node/types"
"os" "os"
"strconv" "strconv"
"strings" "strings"
...@@ -20,27 +21,6 @@ import ( ...@@ -20,27 +21,6 @@ import (
* @date 28/09/2022 - 05/10/2022 * @date 28/09/2022 - 05/10/2022
*/ */
// Message - MessageBody can be of any type but will be use here for Transaction, AckTransaction
type Message struct {
MessageType string `json:"messageType"`
MessageBody interface{} `json:"messageBody"`
}
// Transaction - represents a transaction in the blockchain
type Transaction struct {
Id string `json:"id"`
Sender string `json:"sender"`
Receiver string `json:"receiver"`
Amount string `json:"amount"`
}
// AckTransaction - represents an acknowledgement of a transaction @see vote function
type AckTransaction struct {
Id string `json:"id"`
AmountOfCorrectNode int `json:"amountOfCorrectNode"`
TotalNodes int `json:"totalNodes"`
}
// Config - is the configuration of a node, it's id, address, port and neighbours // Config - is the configuration of a node, it's id, address, port and neighbours
type Config struct { type Config struct {
ID int `yaml:"id"` ID int `yaml:"id"`
...@@ -57,7 +37,7 @@ type Config struct { ...@@ -57,7 +37,7 @@ type Config struct {
var ServerReady = false var ServerReady = false
// DB in memory database // DB in memory database
var DB []Transaction var DB Database
// ConnectionType is the type of connection used by the server // ConnectionType is the type of connection used by the server
var ConnectionType = "tcp" var ConnectionType = "tcp"
......
package object_storage
import (
"bufio"
"bytes"
"context"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"log"
"math/rand"
. "node/types"
"os"
"strconv"
"time"
)
func addTransactionToBlobStorage(transaction Transaction) {
}
func removeTransactionFromBlobStorage(transaction Transaction) {
}
func fakeTransaction(transactionToFake Transaction, fakeTransaction Transaction) bool {
return false
}
func writeDatabaseToFile(filepath string, db Database) {
}
func readDatabaseFromFile(filepath string) Database {
return nil
}
func readDatabaseFromBlobStorage() Database {
return nil
}
func writeDatabaseToBlobStorage() {
}
// Azure Storage Quickstart Sample - Demonstrate how to upload, list, download, and delete blobs.
//
// Documentation References:
// - What is a Storage Account - https://docs.microsoft.com/azure/storage/common/storage-create-storage-account
// - Blob Service Concepts - https://docs.microsoft.com/rest/api/storageservices/Blob-Service-Concepts
// - Blob Service Go SDK API - https://godoc.org/github.com/Azure/azure-storage-blob-go
// - Blob Service REST API - https://docs.microsoft.com/rest/api/storageservices/Blob-Service-REST-API
// - Scalability and performance targets - https://docs.microsoft.com/azure/storage/common/storage-scalability-targets
// - Azure Storage Performance and Scalability checklist https://docs.microsoft.com/azure/storage/common/storage-performance-checklist
// - Storage Emulator - https://docs.microsoft.com/azure/storage/common/storage-use-emulator
func randomString() string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return strconv.Itoa(r.Int())
}
func createBlobStorageClient() {
}
func TestObjectStorage() {
fmt.Printf("Azure Blob storage quick start sample\n")
url := "https://hepiadistributedsystems.blob.core.windows.net/" //replace <StorageAccountName> with your Azure storage account name
ctx := context.Background()
// Create a default request pipeline using your storage account name and account key.
credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatal("Invalid credentials with error: " + err.Error())
}
serviceClient, err := azblob.NewServiceClient(url, credential, nil)
if err != nil {
log.Fatal("Invalid credentials with error: " + err.Error())
}
// Create the container
containerName := fmt.Sprintf("quickstart-%s", randomString())
fmt.Printf("Creating a container named %s\n", containerName)
containerClient := serviceClient.NewContainerClient(containerName)
_, err = containerClient.Create(ctx, nil)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Creating a dummy file to test the upload and download\n")
data := []byte("\nhello world this is a blob\n")
blobName := "quickstartblob" + "-" + randomString()
blobClient, err := azblob.NewBlockBlobClient(url+containerName+"/"+blobName, credential, nil)
if err != nil {
log.Fatal(err)
}
// Upload to data to blob storage
_, err = blobClient.UploadBufferToBlockBlob(ctx, data, azblob.HighLevelUploadToBlockBlobOption{})
if err != nil {
log.Fatalf("Failure to upload to blob: %+v", err)
}
// List the blobs in the container
fmt.Println("Listing the blobs in the container:")
pager := containerClient.ListBlobsFlat(nil)
for pager.NextPage(ctx) {
resp := pager.PageResponse()
for _, v := range resp.ContainerListBlobFlatSegmentResult.Segment.BlobItems {
fmt.Println(*v.Name)
}
}
if err = pager.Err(); err != nil {
log.Fatalf("Failure to list blobs: %+v", err)
}
// Download the blob
get, err := blobClient.Download(ctx, nil)
if err != nil {
log.Fatal(err)
}
downloadedData := &bytes.Buffer{}
reader := get.Body(azblob.RetryReaderOptions{})
_, err = downloadedData.ReadFrom(reader)
if err != nil {
log.Fatal(err)
}
err = reader.Close()
if err != nil {
log.Fatal(err)
}
fmt.Println(downloadedData.String())
fmt.Printf("Press enter key to delete the blob fils, example container, and exit the application.\n")
bufio.NewReader(os.Stdin).ReadBytes('\n')
fmt.Printf("Cleaning up.\n")
// Delete the blob
fmt.Printf("Deleting the blob " + blobName + "\n")
_, err = blobClient.Delete(ctx, nil)
if err != nil {
log.Fatalf("Failure: %+v", err)
}
// Delete the container
fmt.Printf("Deleting the blob " + containerName + "\n")
_, err = containerClient.Delete(ctx, nil)
if err != nil {
log.Fatalf("Failure: %+v", err)
}
}
package main
import (
"fmt"
. "node/object-storage"
)
func main() {
fmt.Println("Testing file for the application")
TestObjectStorage()
}
package types
// Message - MessageBody can be of any type but will be use here for Transaction, AckTransaction
type Message struct {
MessageType string `json:"messageType"`
MessageBody interface{} `json:"messageBody"`
}
// Transaction - represents a transaction in the blockchain
type Transaction struct {
Id string `json:"id"`
Sender string `json:"sender"`
Receiver string `json:"receiver"`
Amount string `json:"amount"`
}
// AckTransaction - represents an acknowledgement of a transaction @see vote function
type AckTransaction struct {
Id string `json:"id"`
AmountOfCorrectNode int `json:"amountOfCorrectNode"`
TotalNodes int `json:"totalNodes"`
}
type Database []Transaction
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment