Skip to content

Commit

Permalink
Merge pull request #57 from covalenthq/develop
Browse files Browse the repository at this point in the history
IPFS-Pinner-DTM-RC:v0.1.12
  • Loading branch information
noslav authored May 3, 2023
2 parents 5d679b3 + 4726560 commit c583fbd
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 26 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ RUN apk update && apk add --no-cache bash=5.1.16-r0
COPY --from=builder /build/ipfs-server /app
SHELL ["/bin/bash", "-c"]
RUN chmod +x ./ipfs-server

HEALTHCHECK --interval=10s --timeout=5s CMD wget --no-verbose --tries=1 --spider localhost:3000/health

ENTRYPOINT [ "/bin/bash", "-l", "-c" ]
CMD [ "./ipfs-server -port 3000 -jwt $WEB3_JWT" ]

Expand All @@ -25,4 +28,4 @@ EXPOSE 5001
# Web Gateway; can be exposed publicly with a proxy, e.g. as https://ipfs.example.org
EXPOSE 8080
# Swarm Websockets; must be exposed publicly when the node is listening using the websocket transport (/ipX/.../tcp/8081/ws).
EXPOSE 8081
EXPOSE 8081
1 change: 1 addition & 0 deletions binary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func main() {
node := pinner.NewPinnerNode(*nodeCreateReq)
ctx := context.Background()
//upload(ctx, node)
core.Version()
download(ctx, node)
}

Expand Down
29 changes: 29 additions & 0 deletions core/support.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package core

import (
"fmt"
"os"
"runtime"
)

type PinningService string

const (
Pinata PinningService = "pinata"
Web3Storage PinningService = "web3.storage"
Other PinningService = "other"
// IpfsPinnerVersionMajor is Major version component of the current release
IpfsPinnerVersionMajor = 0
// IpfsPinnerVersionMinor is Minor version component of the current release
IpfsPinnerVersionMinor = 1
// IpfsPinnerVersionPatch is Patch version component of the current release
IpfsPinnerVersionPatch = 12
clientIdentifier = "ipfs-pinner" // Client identifier to advertise over the network
)

func (f PinningService) GetPinningServiceBaseUrl() string {
Expand All @@ -32,3 +45,19 @@ func (f PinningService) GetFilePinBaseUrl() string {
func (f PinningService) String() string {
return string(f)
}

// IpfsPinnerVersion holds the textual version string.
var IpfsPinnerVersion = func() string {
return fmt.Sprintf("%d.%d.%d", IpfsPinnerVersionMajor, IpfsPinnerVersionMinor, IpfsPinnerVersionPatch)
}()

// Version Provides version info on bsp agent binary
func Version() {
fmt.Println(clientIdentifier)
fmt.Println("ipfs-pinner Version:", IpfsPinnerVersion)
fmt.Println("Architecture:", runtime.GOARCH)
fmt.Println("Go Version:", runtime.Version())
fmt.Println("Operating System:", runtime.GOOS)
fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))
fmt.Printf("GOROOT=%s\n", runtime.GOROOT())
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/ipfs/kubo v0.18.1
github.com/ipld/go-car v0.4.0
github.com/ipld/go-ipld-prime v0.20.0
github.com/multiformats/go-multiaddr v0.8.0
github.com/multiformats/go-multiaddr v0.9.0
github.com/multiformats/go-multibase v0.2.0
github.com/pkg/errors v0.9.1
github.com/ybbus/httpretry v1.0.2
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,9 @@ github.com/multiformats/go-multiaddr v0.4.1/go.mod h1:3afI9HfVW8csiF8UZqtpYRiDye
github.com/multiformats/go-multiaddr v0.5.0/go.mod h1:3KAxNkUqLTJ20AAwN4XVX4kZar+bR+gh4zgbfr3SNug=
github.com/multiformats/go-multiaddr v0.6.0/go.mod h1:F4IpaKZuPP360tOMn2Tpyu0At8w23aRyVqeK0DbFeGM=
github.com/multiformats/go-multiaddr v0.7.0/go.mod h1:Fs50eBDWvZu+l3/9S6xAE7ZYj6yhxlvaVZjakWN7xRs=
github.com/multiformats/go-multiaddr v0.8.0 h1:aqjksEcqK+iD/Foe1RRFsGZh8+XFiGo7FgUCZlpv3LU=
github.com/multiformats/go-multiaddr v0.8.0/go.mod h1:Fs50eBDWvZu+l3/9S6xAE7ZYj6yhxlvaVZjakWN7xRs=
github.com/multiformats/go-multiaddr v0.9.0 h1:3h4V1LHIk5w4hJHekMKWALPXErDfz/sggzwC/NcqbDQ=
github.com/multiformats/go-multiaddr v0.9.0/go.mod h1:mI67Lb1EeTOYb8GQfL/7wpIZwc46ElrvzhYnoJOmTT0=
github.com/multiformats/go-multiaddr-dns v0.0.1/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q=
github.com/multiformats/go-multiaddr-dns v0.0.2/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q=
github.com/multiformats/go-multiaddr-dns v0.2.0/go.mod h1:TJ5pr5bBO7Y1B18djPuRsVkduhQH2YqYSbxWJzYGdK0=
Expand Down
14 changes: 8 additions & 6 deletions openapi/api_filepin.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 21 additions & 17 deletions openapi/api_pins.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ import (
client "github.com/covalenthq/ipfs-pinner/pinclient"
)

const (
OK = "OK"
BAD = "BAD"
TIMEOUT = "TIMEOUT"
)

type State struct {
status string
}

func NewState() *State {
return &State{status: OK}
}

var (
emptyBytes = []byte("")
WEB3_JWT = "WEB3_JWT"
Expand All @@ -37,11 +51,13 @@ func main() {
ipfsGatewayUrls := flag.String("ipfs-gateway-urls", "https://w3s.link/ipfs/%s,https://dweb.link/ipfs/%s,https://ipfs.io/ipfs/%s", "comma separated list of ipfs gateway urls")

flag.Parse()
core.Version()
setUpAndRunServer(*portNumber, *token, *ipfsGatewayUrls)
}

func setUpAndRunServer(portNumber int, token, ipfsGatewayUrls string) {
mux := http.NewServeMux()
httpState := NewState()
if token == "" {
var present bool
token, present = os.LookupEnv(WEB3_JWT)
Expand Down Expand Up @@ -74,6 +90,10 @@ func setUpAndRunServer(portNumber int, token, ipfsGatewayUrls string) {
mux.Handle("/upload", recoveryWrapper(uploadHttpHandler(node)))
mux.Handle("/get", recoveryWrapper(downloadHttpHandler(node)))
mux.Handle("/cid", recoveryWrapper(cidHttpHandler(node)))
mux.Handle("/health", recoveryWrapper(healthHttpHandler(httpState)))
mux.Handle("/sabotage", recoveryWrapper(sabotageHttpHandler(httpState)))
mux.Handle("/recover", recoveryWrapper(recoverHttpHandler(httpState)))
mux.Handle("/timeout", recoveryWrapper(timeoutHttpHandler(httpState)))

log.Print("Listening...")
err := http.ListenAndServe(":"+strconv.Itoa(portNumber), mux)
Expand Down Expand Up @@ -279,3 +299,64 @@ func downloadHandler(cidStr string, node pinner.PinnerNode) ([]byte, error) {
}
return node.UnixfsService().Get(ctx, cid)
}

func healthHttpHandler(s *State) http.Handler {
// Check the health of the server and return a status code accordingly
fn := func(w http.ResponseWriter, r *http.Request) {
log.Println("Received /health request:", "source=", r.RemoteAddr, "status=", s.status)
switch s.status {
case OK:
_, err := io.WriteString(w, "I'm healthy")
if err != nil {
log.Println("cannot write %w string", err)
}
return
case BAD:
http.Error(w, "Internal Error", 500)
return
case TIMEOUT:
time.Sleep(30 * time.Second)
return
default:
_, err := io.WriteString(w, "UNKNOWN")
if err != nil {
log.Println("cannot write %w string", err)
}
return
}
}
return http.HandlerFunc(fn)
}

func sabotageHttpHandler(s *State) http.Handler {
fn := func(w http.ResponseWriter, _ *http.Request) {
s.status = BAD
_, err := io.WriteString(w, "Sabotage ON")
if err != nil {
log.Println("cannot write %w string", err)
}
}
return http.HandlerFunc(fn)
}

func recoverHttpHandler(s *State) http.Handler {
fn := func(w http.ResponseWriter, _ *http.Request) {
s.status = OK
_, err := io.WriteString(w, "Recovered.")
if err != nil {
log.Println("cannot write %w string", err)
}
}
return http.HandlerFunc(fn)
}

func timeoutHttpHandler(s *State) http.Handler {
fn := func(w http.ResponseWriter, _ *http.Request) {
s.status = TIMEOUT
_, err := io.WriteString(w, "Configured to timeout.")
if err != nil {
log.Println("cannot write %w string", err)
}
}
return http.HandlerFunc(fn)
}

0 comments on commit c583fbd

Please sign in to comment.