Init project

This commit is contained in:
Arnaud 2025-10-14 14:16:15 +02:00
commit 45a7562047
No known key found for this signature in database
GPG Key ID: 20E40A5D3110766F
6 changed files with 163 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Ignore artifacts downloaded
libs
# Ignore file downloaded
hello.txt
# Ignore executable
example

34
Makefile Normal file
View File

@ -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)/*

35
README.md Normal file
View File

@ -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
```

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module example
go 1.25.1
require github.com/codex-storage/codex-go-bindings v0.0.15

12
go.sum Normal file
View File

@ -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=

69
main.go Normal file
View File

@ -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)
}
}