From fbb552be015c2a63ac3faf935d07c5c74985dad5 Mon Sep 17 00:00:00 2001 From: Akilan Date: Fri, 14 Oct 2022 14:40:19 +0100 Subject: [PATCH] changes --- README.md | 2 ++ go.mod | 2 +- main.go | 48 +++++++++++++++++++++++++++++++++++------------- 3 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..edc5ebe --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Web Gateway upload +The following repository consists of the Peernet Web gateway upload implementation. diff --git a/go.mod b/go.mod index 91d2ae8..7bc146f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/PeernetOfficial/WebGatewayUpload -go 1.19 +go 1.18 require ( github.com/PeernetOfficial/core v0.0.0-20220601150942-0e3e8dc9885c diff --git a/main.go b/main.go index 330bf14..4efe3ce 100644 --- a/main.go +++ b/main.go @@ -16,9 +16,23 @@ import ( "mime/multipart" "net/http" "time" + "flag" ) -// StartPeernet start peernet +// Variables for the flags to get the address +var ( + BackEndApiAddress *string + WebpageAddress *string + SSL *bool +) + +func init() { + BackEndApiAddress = flag.String("BackEndApiAddress", "0.0.0.0:8088", "current environment") + WebpageAddress = flag.String("WebpageAddress", "0.0.0.0:8098", "current environment") + SSL = flag.Bool("SSL", false, "Flag to check if the SSL certificate is enabled or not") +} + +// Initializes Peernet func InitPeernet() *core.Backend { backend, status, err := core.Init("Your application/1.0", "Config.yaml", nil, nil) if status != core.ExitSuccess { @@ -29,8 +43,9 @@ func InitPeernet() *core.Backend { return backend } +// Starts the WebAPI and peernet func RunPeernet(backend *core.Backend) { - webapi.Start(backend, []string{"0.0.0.0:8081"}, false, "", "", 10*time.Second, 10*time.Second, uuid.Nil) + webapi.Start(backend, []string{*BackEndApiAddress}, false, "", "", 10*time.Second, 10*time.Second, uuid.Nil) backend.Connect() for { @@ -54,7 +69,7 @@ type File struct { } func AddFileWarehouse(file io.Reader) *WarehouseResult { - url := "http://0.0.0.0:8081/warehouse/create" + url := *BackEndApiAddress + "/warehouse/create" req, err := http.NewRequest("POST", url, file) //req.Header.Set("X-Custom-Header", "myvalue") @@ -67,10 +82,10 @@ func AddFileWarehouse(file io.Reader) *WarehouseResult { } defer resp.Body.Close() - fmt.Println("response Status:", resp.Status) - fmt.Println("response Headers:", resp.Header) + //fmt.Println("response Status:", resp.Status) + //fmt.Println("response Headers:", resp.Header) body, _ := ioutil.ReadAll(resp.Body) - fmt.Println("response Body:", string(body)) + //fmt.Println("response Body:", string(body)) var result WarehouseResult err = json.Unmarshal(body, &result) if err != nil { @@ -86,8 +101,9 @@ type BlockchainResponse struct { Version int `json:"version"` } +// The follwoing function adds the filename and hash to the blockchain func AddFileToBlockchain(hash []byte, filename string) *BlockchainResponse { - url := "http://0.0.0.0:8081/blockchain/file/add" + url := *BackEndApiAddress + "/blockchain/file/add" // Create file object for post var blockchainRequest BlockchainRequest @@ -111,10 +127,10 @@ func AddFileToBlockchain(hash []byte, filename string) *BlockchainResponse { } defer resp.Body.Close() - fmt.Println("response Status:", resp.Status) - fmt.Println("response Headers:", resp.Header) + //fmt.Println("response Status:", resp.Status) + //fmt.Println("response Headers:", resp.Header) body, _ := ioutil.ReadAll(resp.Body) - fmt.Println("response Body:", string(body)) + //fmt.Println("response Body:", string(body)) var result BlockchainResponse err = json.Unmarshal(body, &result) if err != nil { @@ -122,8 +138,6 @@ func AddFileToBlockchain(hash []byte, filename string) *BlockchainResponse { } return &result - - //return &result } // UploadFile Simple abstracted function to add files to peernet core @@ -153,6 +167,14 @@ func UploadFile(backend *core.Backend, file *multipart.File, header *multipart.F // Add files func main() { + + // check if SSL is used or not + if *SSL { + *BackEndApiAddress = "https://" + *BackEndApiAddress + } else { + *BackEndApiAddress = "http://" + *BackEndApiAddress + } + // Start peernet backend := InitPeernet() go RunPeernet(backend) @@ -213,5 +235,5 @@ func main() { //}) }) - r.Run() + r.Run(*WebpageAddress) }