-
-
Notifications
You must be signed in to change notification settings - Fork 565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support embedding as a library in existing Go app #1103
Comments
I'd also love to see this in Netbird. It might end up being the factor that makes me choose Tailscale over Netbird, even though I'd rather use Netbird. My constraints don't really make it all that feasible to install it as root on linux systems, so would need to embed it in my Go app that runs in user space. |
I would like to build Caddy plugin like this https://github.com/tailscale/caddy-tailscale , but this is a real blocker for me. Is it on the roadmap? |
Take a look at #3239. Example program: package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
netbird "github.com/netbirdio/netbird/client/embed"
)
func main() {
// Create client with setup key and device name
client, err := netbird.New(netbird.Options{
DeviceName: "http-server",
SetupKey: os.Getenv("NB_SETUP_KEY"),
ManagementURL: os.Getenv("NB_MANAGEMENT_URL"),
LogOutput: io.Discard,
})
if err != nil {
log.Fatal(err)
}
// Start with timeout
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := client.Start(ctx); err != nil {
log.Fatal(err)
}
// Create HTTP server
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Request from %s: %s %s\n", r.RemoteAddr, r.Method, r.URL.Path)
fmt.Fprintf(w, "Hello from netbird!")
})
// Listen on netbird network
l, err := client.ListenTCP(":8080")
if err != nil {
log.Fatal(err)
}
server := &http.Server{Handler: mux}
go func() {
if err := server.Serve(l); !errors.Is(err, http.ErrServerClosed) {
log.Printf("HTTP server error: %v", err)
}
}()
log.Printf("HTTP server listening on netbird network port 8080")
// Handle shutdown
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
<-stop
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
log.Printf("HTTP shutdown error: %v", err)
}
if err := client.Stop(shutdownCtx); err != nil {
log.Printf("Netbird shutdown error: %v", err)
}
} go.mod:
Let me know what you think of the API |
Describe the solution you'd like
Something similar to tsnet which allows us to take advantage of existing netbird functionality without having to install it separately. There are probably some challenges with this, like the need to have #1054 implemented (which is what tsnet does), but figured I'd surface this as an issue now.
The text was updated successfully, but these errors were encountered: