commit 45a7562047822af16c738395cfdb62da60a95e8d Author: Arnaud Date: Tue Oct 14 14:16:15 2025 +0200 Init project diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8d1a63 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Ignore artifacts downloaded +libs + +# Ignore file downloaded +hello.txt + +# Ignore executable +example diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9ea1cda --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +# Destination folder for the downloaded libraries +LIBS_DIR := $(abspath ./libs) + +# Flags for CGO to find the headers and the shared library +CGO_CFLAGS := -I$(LIBS_DIR) +CGO_LDFLAGS := -L$(LIBS_DIR) -Wl,-rpath,$(LIBS_DIR) + +# Configuration for fetching the right binary +OS := "linux" +ARCH := "amd64" +VERSION := "v0.0.15" +LATEST_URL := "https://github.com/codex-storage/codex-go-bindings/releases/latest/download/codex-${OS}-${ARCH}.zip" +VERSIONED_URL := "https://github.com/codex-storage/codex-go-bindings/releases/download/$(VERSION)/codex-${OS}-${ARCH}.zip" + +# Just for the example +BIN=example + +all: run + +fetch: + @echo "Fetching libcodex from GitHub Actions" + @curl -fSL --create-dirs -o $(LIBS_DIR)/codex-${OS}-${ARCH}.zip ${VERSIONED_URL} + @unzip $(LIBS_DIR)/codex-linux-amd64.zip -d $(LIBS_DIR) + @rm -f $(LIBS_DIR)/*.zip + +build: + CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" go build -o $(BIN) main.go + +run: + ./example + +clean: + rm -f $(BIN) + rm -Rf $(LIBS_DIR)/* \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9a37549 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# Example Codex Go Bindings + +This repository demonstrates how to integrate the [Codex Go bindings](https://github.com/codex-storage/codex-go-bindings) into a Go project. + +The project starts a Codex node, uploads and downloads some data, and can be stopped with `Ctrl+C`. + +## Get the Go dependency + +```sh +go get +``` + +## Fetch the artifacts + +```sh +# Adapt for your OS +OS := "linux" +ARCH := "amd64" +make fetch +``` + +By default, the last release will be downloaded and extracted to libs folder. You can change the `Makefile` +to specify another folder or download a specific version. + +## Build + +```sh +make build +``` + +## Run + +```sh +./example +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d94fcc4 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module example + +go 1.25.1 + +require github.com/codex-storage/codex-go-bindings v0.0.15 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d54ad0f --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/codex-storage/codex-go-bindings v0.0.1 h1:u6Z/Y01Wpxckrg4ATsO/xD/9XVRa6PdHy2AVHYjx9iU= +github.com/codex-storage/codex-go-bindings v0.0.1/go.mod h1:8yC11Vr1Mu5sqZyQ33GaSCr0uUIbQnGkm0aWqZj62Kg= +github.com/codex-storage/codex-go-bindings v0.0.10 h1:szqFD1CWb5ELY+pnlj87PM+nUIiZwjnFYLzTgbiGsDQ= +github.com/codex-storage/codex-go-bindings v0.0.10/go.mod h1:8yC11Vr1Mu5sqZyQ33GaSCr0uUIbQnGkm0aWqZj62Kg= +github.com/codex-storage/codex-go-bindings v0.0.11 h1:mGzkebWuCwtfNinyplNBZ+QFlvlu0Wkg+wQ3fNegCJc= +github.com/codex-storage/codex-go-bindings v0.0.11/go.mod h1:8yC11Vr1Mu5sqZyQ33GaSCr0uUIbQnGkm0aWqZj62Kg= +github.com/codex-storage/codex-go-bindings v0.0.13 h1:svQT3hzH+J5ys5cPqNUXxrPYi57bfoNrzywFZiJZJDE= +github.com/codex-storage/codex-go-bindings v0.0.13/go.mod h1:8yC11Vr1Mu5sqZyQ33GaSCr0uUIbQnGkm0aWqZj62Kg= +github.com/codex-storage/codex-go-bindings v0.0.14 h1:Pk/DDEGW3OGTefEyoBHvw2RUgpAzG6TBtoMmtH1HXI4= +github.com/codex-storage/codex-go-bindings v0.0.14/go.mod h1:8yC11Vr1Mu5sqZyQ33GaSCr0uUIbQnGkm0aWqZj62Kg= +github.com/codex-storage/codex-go-bindings v0.0.15 h1:7usvjbqm5BjRICk7uLu1C+fU69cSLvefcH7rdCy9MTg= +github.com/codex-storage/codex-go-bindings v0.0.15/go.mod h1:8yC11Vr1Mu5sqZyQ33GaSCr0uUIbQnGkm0aWqZj62Kg= diff --git a/main.go b/main.go new file mode 100644 index 0000000..749c617 --- /dev/null +++ b/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "bytes" + "log" + "os" + "os/signal" + "syscall" + + "github.com/codex-storage/codex-go-bindings/codex" +) + +func main() { + node, err := codex.New(codex.Config{ + BlockRetries: 5, + }) + if err != nil { + log.Fatalf("Failed to create Codex node: %v", err) + } + + version, err := node.Version() + if err != nil { + log.Fatalf("Failed to get Codex version: %v", err) + } + log.Printf("Codex version: %s", version) + + if err := node.Start(); err != nil { + log.Fatalf("Failed to start Codex node: %v", err) + } + log.Println("Codex node started") + + buf := bytes.NewBuffer([]byte("Hello World!")) + len := buf.Len() + cid, err := node.UploadReader(codex.UploadOptions{Filepath: "hello.txt"}, buf) + if err != nil { + log.Fatalf("Failed to upload data: %v", err) + } + log.Printf("Uploaded data with CID: %s (size: %d bytes)", cid, len) + + f, err := os.Create("hello.txt") + if err != nil { + log.Fatal("Failed to create file:", err) + } + defer f.Close() + + opt := codex.DownloadStreamOptions{ + Writer: f, + } + + if err := node.DownloadStream(cid, opt); err != nil { + log.Fatalf("Failed to download data: %v", err) + } + + log.Println("Downloaded data to hello.txt") + + // Wait for a SIGINT or SIGTERM signal + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) + <-ch + + if err := node.Stop(); err != nil { + log.Fatalf("Failed to stop Codex node: %v", err) + } + log.Println("Codex node stopped") + + if err := node.Destroy(); err != nil { + log.Fatalf("Failed to destroy Codex node: %v", err) + } +}