removed usecase for backend port

This commit is contained in:
2022-11-22 00:02:02 +00:00
parent 0992098809
commit 200f3ea675

263
main.go
View File

@@ -3,18 +3,20 @@ package main
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"encoding/json"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"github.com/PeernetOfficial/core" "github.com/PeernetOfficial/core"
"github.com/PeernetOfficial/core/blockchain"
"github.com/PeernetOfficial/core/btcec" "github.com/PeernetOfficial/core/btcec"
"github.com/PeernetOfficial/core/merkle"
"github.com/PeernetOfficial/core/protocol"
"github.com/PeernetOfficial/core/warehouse"
"github.com/PeernetOfficial/core/webapi" "github.com/PeernetOfficial/core/webapi"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid" "github.com/google/uuid"
limiter "github.com/julianshen/gin-limiter" limiter "github.com/julianshen/gin-limiter"
"io" "io"
"io/ioutil"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"time" "time"
@@ -23,9 +25,9 @@ import (
// Variables for the flags to get the address // Variables for the flags to get the address
var ( var (
// BackEndApiAddress Refers to the Peernet address ex: <address>:<port no> // BackEndApiAddress Refers to the Peernet address ex: <address>:<port no>
BackEndApiAddress *string //BackEndApiAddress *string
// WebpageAddress Refers to the address for upload webgate server // Port Refers to the address for upload webgate server
WebpageAddress *string Port *string
// SSL To ensure SSL is required checks if SSL is required and subsequently // SSL To ensure SSL is required checks if SSL is required and subsequently
// checks if the certificate is provided // checks if the certificate is provided
SSL *bool SSL *bool
@@ -34,7 +36,7 @@ var (
// Key SSL Key file // Key SSL Key file
Key *string Key *string
// BackendAddressWithHTTP ex: http://<address>:<port no> // BackendAddressWithHTTP ex: http://<address>:<port no>
BackendAddressWithHTTP string //BackendAddressWithHTTP string
// Production mode // Production mode
Production *bool Production *bool
) )
@@ -45,8 +47,8 @@ var (
// init reading flags before any part of the code is executed // init reading flags before any part of the code is executed
func init() { func init() {
BackEndApiAddress = flag.String("BackEndApiAddress", "localhost:8088", "current environment") //BackEndApiAddress = flag.String("BackEndApiAddress", "localhost:8088", "current environment")
WebpageAddress = flag.String("WebpageAddress", "localhost:8098", "current environment") Port = flag.String("Port", "8098", "current environment")
SSL = flag.Bool("SSL", false, "Flag to check if the SSL certificate is enabled or not") SSL = flag.Bool("SSL", false, "Flag to check if the SSL certificate is enabled or not")
Certificate = flag.String("Certificate", "server.crt", "SSL Certificate file") Certificate = flag.String("Certificate", "server.crt", "SSL Certificate file")
Key = flag.String("Key", "server.key", "SSL Key file") Key = flag.String("Key", "server.key", "SSL Key file")
@@ -65,13 +67,11 @@ func InitPeernet() *core.Backend {
} }
// RunPeernet Starts the WebAPI and peernet // RunPeernet Starts the WebAPI and peernet
func RunPeernet(backend *core.Backend) { func RunPeernet(backend *core.Backend) *webapi.WebapiInstance {
webapi.Start(backend, []string{*BackEndApiAddress}, false, "", "", 10*time.Second, 10*time.Second, uuid.Nil) api := webapi.Start(backend, []string{}, false, "", "", 10*time.Second, 10*time.Second, uuid.Nil)
backend.Connect() backend.Connect()
for { return api
}
} }
// ------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------
@@ -97,9 +97,27 @@ type BlockchainRequest struct {
} }
type File struct { type File struct {
Hash []byte `json:"hash"` ID uuid.UUID `json:"id"` // Unique ID.
Type uint16 `json:"type"` Hash []byte `json:"hash"` // Blake3 hash of the file data
Name string `json:"name"` Type uint8 `json:"type"` // File Type. For example audio or document. See TypeX.
Format uint16 `json:"format"` // File Format. This is more granular, for example PDF or Word file. See FormatX.
Size uint64 `json:"size"` // Size of the file
Folder string `json:"folder"` // Folder, optional
Name string `json:"name"` // Name of the file
Description string `json:"description"` // Description. This is expected to be multiline and contain hashtags!
Date time.Time `json:"date"` // Date shared
NodeID []byte `json:"nodeid"` // Node ID, owner of the file. Read only.
Metadata []apiFileMetadata `json:"metadata"` // Additional metadata.
}
type apiFileMetadata struct {
Type uint16 `json:"type"` // See core.TagX constants.
Name string `json:"name"` // User friendly name of the metadata type. Use the Type fields to identify the metadata as this name may change.
// Depending on the exact type, one of the below fields is used for proper encoding:
Text string `json:"text"` // Text value. UTF-8 encoding.
Blob []byte `json:"blob"` // Binary data
Date time.Time `json:"date"` // Date
Number uint64 `json:"number"` // Number
} }
// BlockchainResponse blockchain backend API response struct // BlockchainResponse blockchain backend API response struct
@@ -122,31 +140,45 @@ type BlockchainResponse struct {
// ---------------------------------- Warehouse related ------------------------------------ // ---------------------------------- Warehouse related ------------------------------------
// AddFileWarehouse API call for (Storing file in the warehouse) // AddFileWarehouse API call for (Storing file in the warehouse)
func AddFileWarehouse(file io.Reader) *WarehouseResult { func AddFileWarehouse(file io.Reader, backend *core.Backend) (*WarehouseResult, error) {
url := BackendAddressWithHTTP + "/warehouse/create"
req, err := http.NewRequest("POST", url, file) var warehouse WarehouseResult
//req.Header.Set("X-Custom-Header", "myvalue") buf := new(bytes.Buffer)
//req.Header.Set("Content-Type", "application/json") buf.ReadFrom(file)
client := &http.Client{} hash, status, err := backend.UserWarehouse.CreateFile(file, uint64(buf.Len()))
resp, err := client.Do(req)
if err != nil { if err != nil {
panic(err) return nil, err
} }
defer resp.Body.Close() warehouse.Hash = hash
warehouse.Status = status
body, err := ioutil.ReadAll(resp.Body) return &warehouse, nil
if err != nil {
fmt.Println(err)
}
var result WarehouseResult
err = json.Unmarshal(body, &result)
if err != nil {
fmt.Println(err)
}
return &result //url := BackendAddressWithHTTP + "/warehouse/create"
//
//req, err := http.NewRequest("POST", url, file)
////req.Header.Set("X-Custom-Header", "myvalue")
////req.Header.Set("Content-Type", "application/json")
//
//client := &http.Client{}
//resp, err := client.Do(req)
//if err != nil {
// panic(err)
//}
//defer resp.Body.Close()
//
//body, err := ioutil.ReadAll(resp.Body)
//if err != nil {
// fmt.Println(err)
//}
//var result WarehouseResult
//err = json.Unmarshal(body, &result)
//if err != nil {
// fmt.Println(err)
//}
//
//return &result
} }
// UploadFile Simple abstracted function to add files to peernet core // UploadFile Simple abstracted function to add files to peernet core
@@ -157,12 +189,15 @@ func UploadFile(backend *core.Backend, file *multipart.File, header *multipart.F
} }
// adds file to warehouse // adds file to warehouse
warehouseResult := AddFileWarehouse(buf) warehouseResult, err := AddFileWarehouse(buf, backend)
if err != nil {
return nil, nil, err
}
// current using default port for Peernet api which is 8080 // current using default port for Peernet api which is 8080
// First add file to warehouse // First add file to warehouse
// Adds the file to a blockchain // Adds the file to a blockchain
Blockchainfo := AddFileToBlockchain(warehouseResult.Hash, header.Filename) Blockchainfo := AddFileToBlockchain(warehouseResult.Hash, header.Filename, backend)
if Blockchainfo == nil { if Blockchainfo == nil {
return nil, nil, errors.New("add file to blockchain not successful") return nil, nil, errors.New("add file to blockchain not successful")
} }
@@ -175,9 +210,60 @@ func UploadFile(backend *core.Backend, file *multipart.File, header *multipart.F
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
// ----------------------------------- Blockchain related --------------------------------- // ----------------------------------- Blockchain related ---------------------------------
// AddFileToBlockchain The follwoing function adds the filename and hash to the blockchain func blockRecordFileFromAPI(input File) (output blockchain.BlockRecordFile) {
func AddFileToBlockchain(hash []byte, filename string) *BlockchainResponse { output = blockchain.BlockRecordFile{ID: input.ID, Hash: input.Hash, Type: input.Type, Format: input.Format, Size: input.Size}
url := BackendAddressWithHTTP + "/blockchain/file/add"
if input.Name != "" {
output.Tags = append(output.Tags, blockchain.TagFromText(blockchain.TagName, input.Name))
}
if input.Folder != "" {
output.Tags = append(output.Tags, blockchain.TagFromText(blockchain.TagFolder, input.Folder))
}
if input.Description != "" {
output.Tags = append(output.Tags, blockchain.TagFromText(blockchain.TagDescription, input.Description))
}
for _, meta := range input.Metadata {
if blockchain.IsTagVirtual(meta.Type) { // Virtual tags are not mapped back. They are read-only.
continue
}
switch meta.Type {
case blockchain.TagName, blockchain.TagFolder, blockchain.TagDescription: // auto mapped tags
case blockchain.TagDateCreated:
output.Tags = append(output.Tags, blockchain.TagFromDate(meta.Type, meta.Date))
default:
output.Tags = append(output.Tags, blockchain.BlockRecordFileTag{Type: meta.Type, Data: meta.Blob})
}
}
return output
}
// setFileMerkleInfo sets the merkle fields in the BlockRecordFile
func setFileMerkleInfo(backend *core.Backend, file *blockchain.BlockRecordFile) (valid bool) {
if file.Size <= merkle.MinimumFragmentSize {
// If smaller or equal than the minimum fragment size, the merkle tree is not used.
file.MerkleRootHash = file.Hash
file.FragmentSize = merkle.MinimumFragmentSize
} else {
// Get the information from the Warehouse .merkle companion file.
tree, status, _ := backend.UserWarehouse.ReadMerkleTree(file.Hash, true)
if status != warehouse.StatusOK {
return false
}
file.MerkleRootHash = tree.RootHash
file.FragmentSize = tree.FragmentSize
}
return true
}
// AddFileToBlockchain The following function adds the filename and hash to the blockchain
func AddFileToBlockchain(hash []byte, filename string, backend *core.Backend) *BlockchainResponse {
// Get file type // Get file type
detectType, _, err := webapi.FileDetectType(filename) detectType, _, err := webapi.FileDetectType(filename)
@@ -190,30 +276,73 @@ func AddFileToBlockchain(hash []byte, filename string) *BlockchainResponse {
var files File var files File
files.Name = filename files.Name = filename
files.Hash = hash files.Hash = hash
files.Type = detectType files.Type = uint8(detectType)
blockchainRequest.Files = append(blockchainRequest.Files, files) blockchainRequest.Files = append(blockchainRequest.Files, files)
Byte, err := json.Marshal(blockchainRequest) var filesAdd []blockchain.BlockRecordFile
// convert bytes for _, file := range blockchainRequest.Files {
req, err := http.NewRequest("POST", url, bytes.NewBuffer(Byte)) if len(file.Hash) != protocol.HashSize {
//req.Header.Set("X-Custom-Header", "myvalue") //http.Error(w, "", http.StatusBadRequest)
req.Header.Set("Content-Type", "application/json") //return
}
client := &http.Client{} if file.ID == uuid.Nil { // if the ID is not provided by the caller, set it
resp, err := client.Do(req) file.ID = uuid.New()
if err != nil {
panic(err)
} }
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body) // Verify that the file exists in the warehouse. Folders are exempt from this check as they are only virtual.
//if !file.IsVirtualFolder() {
// if _, err := warehouse.ValidateHash(file.Hash); err != nil {
// //http.Error(w, "", http.StatusBadRequest)
// //return
// } else if _, fileSize, status, _ := backend.UserWarehouse.FileExists(file.Hash); status != warehouse.StatusOK {
// //EncodeJSON(api.backend, w, r, apiBlockchainBlockStatus{Status: blockchain.StatusNotInWarehouse})
// //return
// } else {
// file.Size = fileSize
// }
//} else {
// file.Hash = protocol.HashData(nil)
// file.Size = 0
//}
blockRecord := blockRecordFileFromAPI(file)
// Set the merkle tree info as appropriate.
setFileMerkleInfo(backend, &blockRecord)
filesAdd = append(filesAdd, blockRecord)
}
newHeight, newVersion, status := backend.UserBlockchain.AddFiles(filesAdd)
//Byte, err := json.Marshal(blockchainRequest)
//
//// convert bytes
//req, err := http.NewRequest("POST", url, bytes.NewBuffer(Byte))
////req.Header.Set("X-Custom-Header", "myvalue")
//req.Header.Set("Content-Type", "application/json")
//
//client := &http.Client{}
//resp, err := client.Do(req)
//if err != nil {
// panic(err)
//}
//defer resp.Body.Close()
//
//body, _ := ioutil.ReadAll(resp.Body)
//
//var result BlockchainResponse
//err = json.Unmarshal(body, &result)
//if err != nil {
// return nil
//}
var result BlockchainResponse var result BlockchainResponse
err = json.Unmarshal(body, &result) result.Status = status
if err != nil { fmt.Println(status)
return nil result.Version = int(newVersion)
} result.Height = int(newHeight)
return &result return &result
} }
@@ -231,7 +360,7 @@ func main() {
// Start peernet // Start peernet
backend := InitPeernet() backend := InitPeernet()
go RunPeernet(backend) RunPeernet(backend)
var r *gin.Engine var r *gin.Engine
if *Production { if *Production {
@@ -241,6 +370,9 @@ func main() {
r = gin.Default() r = gin.Default()
} }
// Set of trusted proxies which can be IPV4 or IPV6
r.SetTrustedProxies([]string{""})
r.LoadHTMLGlob("templates/*.html") r.LoadHTMLGlob("templates/*.html")
r.Static("/templates", "./templates") r.Static("/templates", "./templates")
@@ -282,7 +414,7 @@ func main() {
"filename": header.Filename, "filename": header.Filename,
"size": header.Size, "size": header.Size,
"link": "https://peer.ae/" + hex.EncodeToString(publicKey.SerializeCompressed()) + "/" + hex.EncodeToString(warehouseResult.Hash), "link": "https://peer.ae/" + hex.EncodeToString(publicKey.SerializeCompressed()) + "/" + hex.EncodeToString(warehouseResult.Hash),
"address": *WebpageAddress, "address": *Port,
}) })
}) })
@@ -312,13 +444,14 @@ func main() {
// ---------------------------------- Start Gin server ----------------------------------------- // ---------------------------------- Start Gin server -----------------------------------------
// check if SSL is used or not // check if SSL is used or not
if *SSL { if *SSL {
BackendAddressWithHTTP = "https://" + *BackEndApiAddress //BackendAddressWithHTTP = "https://" + *BackEndApiAddress
r.RunTLS(*WebpageAddress, *Certificate, *Key) r.RunTLS(":"+*Port, *Certificate, *Key)
*WebpageAddress = "https://" + *WebpageAddress //*Port = "https://" + *Port
} else { } else {
BackendAddressWithHTTP = "http://" + *BackEndApiAddress //BackendAddressWithHTTP = "http://" + *BackEndApiAddress
r.Run(*WebpageAddress) //r.Run(*Port)
*WebpageAddress = "http://" + *WebpageAddress r.Run(":" + *Port)
//*Port = "http://" + *Port
} }
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
} }