Skip to content

Commit a15a5f3

Browse files
committed
✨ (libp2p): add libp2p node sample
0 parents  commit a15a5f3

File tree

12 files changed

+923
-0
lines changed

12 files changed

+923
-0
lines changed

.github/workflows/go.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
jobs:
11+
build_and_test:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
- name: Setup Go
17+
uses: actions/setup-go@v5
18+
with:
19+
go-version: 'stable'
20+
check-latest: true
21+
- name: Setup dependency
22+
run: go mod tidy
23+
- name: Test
24+
run: go test -v ./internal/...

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Code coverage profiles and other test artifacts
15+
*.out
16+
coverage.*
17+
*.coverprofile
18+
profile.cov
19+
20+
# Dependency directories (remove the comment below to include it)
21+
# vendor/
22+
23+
# Go workspace file
24+
go.work
25+
go.work.sum
26+
27+
# env file
28+
.env
29+
30+
# Editor/IDE
31+
# .idea/
32+
# .vscode/
33+
bin

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.PHONY=build
2+
include .env
3+
export $(shell sed 's/=.*//' .env)
4+
build:
5+
@CGO_ENABLED=0 GOOS=linux go build -o bin/libp2p-node cmd/main.go
6+
7+
run: build
8+
@./bin/libp2p-node
9+
10+
coverage:
11+
@go test -v -cover ./internal/...
12+
13+
test:
14+
@go test -v ./internal/...

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# golang-p2p-node-sample
2+
3+
This repository is use libp2p to make peer-to-peer ping pong sample in golang
4+
5+
## Concept
6+
7+
![alt text](concept.png)
8+

cmd/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log/slog"
6+
"os"
7+
"os/signal"
8+
"syscall"
9+
10+
"github.com/leetcode-golang-classroom/golang-p2p-node-sample/internal/application"
11+
"github.com/leetcode-golang-classroom/golang-p2p-node-sample/internal/config"
12+
mlog "github.com/leetcode-golang-classroom/golang-p2p-node-sample/internal/logger"
13+
)
14+
15+
func main() {
16+
// setup logger
17+
logger := slog.New(slog.NewJSONHandler(
18+
os.Stdout, &slog.HandlerOptions{
19+
AddSource: true,
20+
},
21+
))
22+
23+
rootContext := context.WithValue(context.Background(), mlog.CtxKey{}, logger)
24+
config.Init(rootContext)
25+
app := application.New(rootContext, config.AppConfig)
26+
ctx, cancel := signal.NotifyContext(rootContext, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
27+
defer cancel()
28+
err := app.Start(ctx)
29+
if err != nil {
30+
logger.Error("failed to start app", "error", err)
31+
}
32+
}

concept.png

143 KB
Loading

go.mod

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
module github.com/leetcode-golang-classroom/golang-p2p-node-sample
2+
3+
go 1.24.6
4+
5+
toolchain go1.24.9
6+
7+
require github.com/spf13/viper v1.21.0
8+
9+
require (
10+
github.com/benbjohnson/clock v1.3.5 // indirect
11+
github.com/beorn7/perks v1.0.1 // indirect
12+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
13+
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
14+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
15+
github.com/flynn/noise v1.1.0 // indirect
16+
github.com/francoispqt/gojay v1.2.13 // indirect
17+
github.com/fsnotify/fsnotify v1.9.0 // indirect
18+
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
19+
github.com/google/uuid v1.6.0 // indirect
20+
github.com/gorilla/websocket v1.5.3 // indirect
21+
github.com/huin/goupnp v1.3.0 // indirect
22+
github.com/ipfs/go-cid v0.5.0 // indirect
23+
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
24+
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
25+
github.com/klauspost/compress v1.18.0 // indirect
26+
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
27+
github.com/koron/go-ssdp v0.0.6 // indirect
28+
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
29+
github.com/libp2p/go-flow-metrics v0.2.0 // indirect
30+
github.com/libp2p/go-libp2p v0.44.0 // indirect
31+
github.com/libp2p/go-libp2p-asn-util v0.4.1 // indirect
32+
github.com/libp2p/go-msgio v0.3.0 // indirect
33+
github.com/libp2p/go-netroute v0.3.0 // indirect
34+
github.com/libp2p/go-reuseport v0.4.0 // indirect
35+
github.com/libp2p/go-yamux/v5 v5.0.1 // indirect
36+
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
37+
github.com/miekg/dns v1.1.66 // indirect
38+
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
39+
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
40+
github.com/minio/sha256-simd v1.0.1 // indirect
41+
github.com/mr-tron/base58 v1.2.0 // indirect
42+
github.com/multiformats/go-base32 v0.1.0 // indirect
43+
github.com/multiformats/go-base36 v0.2.0 // indirect
44+
github.com/multiformats/go-multiaddr v0.16.0 // indirect
45+
github.com/multiformats/go-multiaddr-dns v0.4.1 // indirect
46+
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
47+
github.com/multiformats/go-multibase v0.2.0 // indirect
48+
github.com/multiformats/go-multicodec v0.9.1 // indirect
49+
github.com/multiformats/go-multihash v0.2.3 // indirect
50+
github.com/multiformats/go-multistream v0.6.1 // indirect
51+
github.com/multiformats/go-varint v0.0.7 // indirect
52+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
53+
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
54+
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
55+
github.com/pion/datachannel v1.5.10 // indirect
56+
github.com/pion/dtls/v2 v2.2.12 // indirect
57+
github.com/pion/dtls/v3 v3.0.6 // indirect
58+
github.com/pion/ice/v4 v4.0.10 // indirect
59+
github.com/pion/interceptor v0.1.40 // indirect
60+
github.com/pion/logging v0.2.3 // indirect
61+
github.com/pion/mdns/v2 v2.0.7 // indirect
62+
github.com/pion/randutil v0.1.0 // indirect
63+
github.com/pion/rtcp v1.2.15 // indirect
64+
github.com/pion/rtp v1.8.19 // indirect
65+
github.com/pion/sctp v1.8.39 // indirect
66+
github.com/pion/sdp/v3 v3.0.13 // indirect
67+
github.com/pion/srtp/v3 v3.0.6 // indirect
68+
github.com/pion/stun v0.6.1 // indirect
69+
github.com/pion/stun/v3 v3.0.0 // indirect
70+
github.com/pion/transport/v2 v2.2.10 // indirect
71+
github.com/pion/transport/v3 v3.0.7 // indirect
72+
github.com/pion/turn/v4 v4.0.2 // indirect
73+
github.com/pion/webrtc/v4 v4.1.2 // indirect
74+
github.com/prometheus/client_golang v1.22.0 // indirect
75+
github.com/prometheus/client_model v0.6.2 // indirect
76+
github.com/prometheus/common v0.64.0 // indirect
77+
github.com/prometheus/procfs v0.16.1 // indirect
78+
github.com/quic-go/qpack v0.5.1 // indirect
79+
github.com/quic-go/quic-go v0.55.0 // indirect
80+
github.com/quic-go/webtransport-go v0.9.0 // indirect
81+
github.com/sagikazarmark/locafero v0.11.0 // indirect
82+
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
83+
github.com/spaolacci/murmur3 v1.1.0 // indirect
84+
github.com/spf13/afero v1.15.0 // indirect
85+
github.com/spf13/cast v1.10.0 // indirect
86+
github.com/spf13/pflag v1.0.10 // indirect
87+
github.com/subosito/gotenv v1.6.0 // indirect
88+
github.com/wlynxg/anet v0.0.5 // indirect
89+
go.uber.org/dig v1.19.0 // indirect
90+
go.uber.org/fx v1.24.0 // indirect
91+
go.uber.org/mock v0.5.2 // indirect
92+
go.uber.org/multierr v1.11.0 // indirect
93+
go.uber.org/zap v1.27.0 // indirect
94+
go.yaml.in/yaml/v3 v3.0.4 // indirect
95+
golang.org/x/crypto v0.41.0 // indirect
96+
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476 // indirect
97+
golang.org/x/mod v0.27.0 // indirect
98+
golang.org/x/net v0.43.0 // indirect
99+
golang.org/x/sync v0.16.0 // indirect
100+
golang.org/x/sys v0.35.0 // indirect
101+
golang.org/x/text v0.28.0 // indirect
102+
golang.org/x/time v0.12.0 // indirect
103+
golang.org/x/tools v0.36.0 // indirect
104+
google.golang.org/protobuf v1.36.6 // indirect
105+
lukechampine.com/blake3 v1.4.1 // indirect
106+
)

0 commit comments

Comments
 (0)