mirror of
https://github.com/openziti/zrok.git
synced 2025-08-09 08:05:04 +02:00
Python SDK Update (#523)
* added first iteration decorator for zrok and example flask server * update requirements. Add context managing for share and access. Updated pastebin example to better cleanup * setup and sample tweaks * small linting updates and example changes * A few small fixes * fix long description * Update the ignore file. Considering moving location of this. * Added flake8 linting for builds * use python 3.10 * move setup python to its own block * added back in the py name * update changelogs and add readme --------- Signed-off-by: Cam Otts <otts.cameron@gmail.com> Co-authored-by: Kenneth Bingham <kenneth.bingham@netfoundry.io>
This commit is contained in:
59
sdk/golang/examples/http-server/README.md
Normal file
59
sdk/golang/examples/http-server/README.md
Normal file
@ -0,0 +1,59 @@
|
||||
# "http-server" SDK Example
|
||||
|
||||
This `http-server` example is a minimal `zrok` application that surfaces a basic http server over a public zrok share.
|
||||
|
||||
## Implementation
|
||||
|
||||
```go
|
||||
root, err := environment.LoadRoot()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
```
|
||||
|
||||
The `root` is a structure that contains all of the user's environment detail and allows the SDK application to access the `zrok` service instance and the underlying OpenZiti network.
|
||||
|
||||
```go
|
||||
shr, err := sdk.CreateShare(root, &sdk.ShareRequest{
|
||||
BackendMode: sdk.TcpTunnelBackendMode,
|
||||
ShareMode: sdk.PublicShareMode,
|
||||
Frontends: []string{"public"},
|
||||
Target: "http-server",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := sdk.DeleteShare(root, shr); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
...
|
||||
fmt.Println("Access server at the following endpoints: ", strings.Join(shr.FrontendEndpoints, "\n"))
|
||||
|
||||
```
|
||||
|
||||
The `sdk.CreateShare` call uses the loaded `environment` root along with the details of the share request (`sdk.ShareRequest`) to create the share that will be used to access the `http-server`.
|
||||
|
||||
We are using the `sdk.TcpTunnelBackendMode` to handle tcp traffic. This time we are using `sdk.PublicShareMode` to take advantage of a public share that is running. With that we set which frontends to listen on, so we use whatever is configured, `public` here.
|
||||
|
||||
Further down we emit where to access the service.
|
||||
|
||||
Then we create a listener and use that to server our http server:
|
||||
|
||||
```go
|
||||
conn, err := sdk.NewListener(shr.Token, root)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
...
|
||||
|
||||
http.HandleFunc("/", helloZrok)
|
||||
|
||||
if err := http.Serve(conn, nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
```
|
51
sdk/golang/examples/http-server/cmd/http-server/main.go
Normal file
51
sdk/golang/examples/http-server/cmd/http-server/main.go
Normal file
@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/openziti/zrok/environment"
|
||||
"github.com/openziti/zrok/sdk/golang/sdk"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func helloZrok(w http.ResponseWriter, r *http.Request) {
|
||||
io.WriteString(w, "Hello zrok!\n")
|
||||
}
|
||||
|
||||
func main() {
|
||||
root, err := environment.LoadRoot()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
shr, err := sdk.CreateShare(root, &sdk.ShareRequest{
|
||||
BackendMode: sdk.TcpTunnelBackendMode,
|
||||
ShareMode: sdk.PublicShareMode,
|
||||
Frontends: []string{"public"},
|
||||
Target: "http-server",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := sdk.DeleteShare(root, shr); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
conn, err := sdk.NewListener(shr.Token, root)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
fmt.Println("Access server at the following endpoints: ", strings.Join(shr.FrontendEndpoints, "\n"))
|
||||
|
||||
http.HandleFunc("/", helloZrok)
|
||||
|
||||
if err := http.Serve(conn, nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user