From 5dec8072f5a68a5ca0c2809fe78923a4f3723e5b Mon Sep 17 00:00:00 2001 From: Marston Connell <34043723+TheMarstonConnell@users.noreply.github.com> Date: Fri, 18 Aug 2023 09:26:10 +0100 Subject: [PATCH] fixed path derivation --- README.md | 2 +- go.mod | 3 +- go.sum | 5 ++- jhttp/main.go | 98 +++++++++++++++++++++++++++++++++------------------ 4 files changed, 68 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index d186c91..782807c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ You will need to supply the API with a seed phrase that corresponds to a Jackal ```shell git clone https://github.com/JackalLabs/jackalapi.git cd jackalapi -git checkout v0.0.2 +git checkout v0.0.5 go install ./jhttp ``` diff --git a/go.mod b/go.mod index ca89bc3..6f6481a 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/JackalLabs/jackalapi go 1.20 require ( - github.com/JackalLabs/jackalgo v0.0.2 + github.com/JackalLabs/jackalgo v0.0.3-0.20230818081538-af58178037c3 github.com/ipfs/boxo v0.10.0 github.com/ipfs/go-cid v0.4.1 github.com/ipfs/go-datastore v0.6.0 @@ -35,7 +35,6 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect - github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/cespare/xxhash v1.1.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect diff --git a/go.sum b/go.sum index e1577bb..dd2387c 100644 --- a/go.sum +++ b/go.sum @@ -88,8 +88,8 @@ github.com/JackalLabs/canine-provider v1.0.10-beta.1.0.20230626192716-041b1c701a github.com/JackalLabs/canine-provider v1.0.10-beta.1.0.20230626192716-041b1c701abd/go.mod h1:73IyiWJUVcVlDla0nSOUNma2+qnvuZvMArEpnLcSVco= github.com/JackalLabs/cosmos-sdk-new v0.45.17-0.20230704034202-b88b1fbc9b2f h1:yiMZXfLKf8j60mcc78EGd4JthE23WAwxOC0UgQt6Mww= github.com/JackalLabs/cosmos-sdk-new v0.45.17-0.20230704034202-b88b1fbc9b2f/go.mod h1:bScuNwWAP0TZJpUf+SHXRU3xGoUPp+X9nAzfeIXts40= -github.com/JackalLabs/jackalgo v0.0.2 h1:ZeVsqA4ncEaU8eOM6WYsEu8JF6njjjOIQeNmnkfXsnQ= -github.com/JackalLabs/jackalgo v0.0.2/go.mod h1:mbFEsh2V5TgoNs0DDCm32hSxSZG+sNaMFh+GVAgQBPs= +github.com/JackalLabs/jackalgo v0.0.3-0.20230818081538-af58178037c3 h1:ZlZfaCR7reijmgAOlAgb9FJCIbjzAxtr71AnIeB0sXs= +github.com/JackalLabs/jackalgo v0.0.3-0.20230818081538-af58178037c3/go.mod h1:mbFEsh2V5TgoNs0DDCm32hSxSZG+sNaMFh+GVAgQBPs= github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY= github.com/Joker/jade v1.0.1-0.20190614124447-d475f43051e7/go.mod h1:6E6s8o2AE4KhCrqr6GRJjdC/gNfTdxkIXvuGZZda2VM= github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= @@ -157,7 +157,6 @@ github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY github.com/btcsuite/btcd/btcutil v1.1.2 h1:XLMbX8JQEiwMcYft2EGi8zPUkoa0abKIU6/BJSRsjzQ= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 h1:KdUfX2zKommPRa+PD0sWZUyXe9w277ABlgELO7H04IM= -github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= diff --git a/jhttp/main.go b/jhttp/main.go index 8811c66..a0f644d 100644 --- a/jhttp/main.go +++ b/jhttp/main.go @@ -3,16 +3,19 @@ package main import ( "bytes" "encoding/json" + "errors" "fmt" "io" + "os" + "strconv" "sync" - "github.com/JackalLabs/jackalapi/jhttp/http" - "github.com/JackalLabs/jackalgo/handlers/file_io_handler" + jhttp "github.com/JackalLabs/jackalapi/jhttp/http" "github.com/JackalLabs/jackalgo/handlers/file_upload_handler" - "github.com/julienschmidt/httprouter" + "github.com/rs/cors" + "github.com/uptrace/bunrouter" - http2 "net/http" + "net/http" ) const MaxFileSize = 32 << 30 @@ -20,56 +23,69 @@ const ParentFolder = "s/jhttp" func main() { - Gets := make(http.Handlers, 0) - Posts := make(http.Handlers, 0) + router := bunrouter.New() - getVersion := func(w http2.ResponseWriter, r *http2.Request, ps httprouter.Params, q *http.Queue, fileIo *file_io_handler.FileIoHandler) { + port := os.Getenv("JHTTP_PORT") + if len(port) == 0 { + port = "3535" + } + + portNum, err := strconv.ParseInt(port, 10, 64) + if err != nil { + panic(err) + } + + q, fileIo := jhttp.InitServer([]string{"jhttp"}) + + router.GET("/version", func(w http.ResponseWriter, r bunrouter.Request) error { _, err := w.Write([]byte("v0.0.0")) if err != nil { - panic(err) + return err } - } - Gets["/version"] = &getVersion - getDownload := func(w http2.ResponseWriter, r *http2.Request, ps httprouter.Params, q *http.Queue, fileIo *file_io_handler.FileIoHandler) { + return nil + }) + + router.GET("/download/:name", func(w http.ResponseWriter, r bunrouter.Request) error { - name := ps.ByName("name") + name := r.Param("name") var allBytes []byte handler, err := fileIo.DownloadFile(fmt.Sprintf("%s/%s", ParentFolder, name)) if err != nil { fmt.Println("download file failed", err.Error()) - w.WriteHeader(http2.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(err.Error())) - return + return err } allBytes = handler.GetFile().Buffer().Bytes() _, err = w.Write(allBytes) if err != nil { - panic(err) + return err } - } - Gets["/download/:name"] = &getDownload - postUpload := func(w http2.ResponseWriter, r *http2.Request, ps httprouter.Params, q *http.Queue, fileIo *file_io_handler.FileIoHandler) { + return nil + }) + + router.POST("/upload", func(w http.ResponseWriter, r bunrouter.Request) error { // ParseMultipartForm parses a request body as multipart/form-data err := r.ParseMultipartForm(MaxFileSize) // MAX file size lives here if err != nil { fmt.Println(err) - w.WriteHeader(http2.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(err.Error())) - return + return err } file, h, err := r.FormFile("file") // Retrieve the file from form data if err != nil { fmt.Println(err) - w.WriteHeader(http2.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(err.Error())) - return + return err } var b bytes.Buffer @@ -77,25 +93,25 @@ func main() { _, err = io.Copy(&b, file) if err != nil { fmt.Println(err) - w.WriteHeader(http2.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(err.Error())) - return + return err } fileUpload, err := file_upload_handler.TrackVirtualFile(b.Bytes(), h.Filename, ParentFolder) if err != nil { fmt.Println("fileupload failed", err) - w.WriteHeader(http2.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(err.Error())) - return + return err } folder, err := fileIo.DownloadFolder(ParentFolder) if err != nil { fmt.Println("download folder failed", err) - w.WriteHeader(http2.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(err.Error())) - return + return err } var wg sync.WaitGroup @@ -107,22 +123,36 @@ func main() { if m.Error() != nil { fmt.Println("upload file failed", m.Error()) - w.WriteHeader(http2.StatusInternalServerError) + w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(m.Error().Error())) - return + return err } - nv := http.UploadResponse{ + nv := jhttp.UploadResponse{ FID: m.Fid(), } err = json.NewEncoder(w).Encode(nv) if err != nil { panic(err) } - } - Posts["/upload"] = &postUpload + return nil + }) - http.StartServer(Gets, Posts, []string{"jhttp"}) + handler := cors.Default().Handler(router) + + fmt.Printf("🌍 Started jHTTP: http://0.0.0.0:%d\n", portNum) + err = http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", portNum), handler) + if err != nil { + panic(err) + } + + if errors.Is(err, http.ErrServerClosed) { + fmt.Printf("Server Closed\n") + return + } else if err != nil { + fmt.Printf("error starting server: %s\n", err) + os.Exit(1) + } }