summaryrefslogtreecommitdiff
path: root/docs/src/api/go/example.md
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-05-05 17:47:36 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-05-05 17:47:36 +0200
commit7bb7885f6eb19547b906513d2664e3730ef5b593 (patch)
treece387ad24da5e3ba3b4d1604d07a5b11d9e68a2e /docs/src/api/go/example.md
parent657776055cd07c1f9279e982fbfef88dca1ca71b (diff)
Docs: Add API and building improvements
Diffstat (limited to 'docs/src/api/go/example.md')
-rw-r--r--docs/src/api/go/example.md63
1 files changed, 63 insertions, 0 deletions
diff --git a/docs/src/api/go/example.md b/docs/src/api/go/example.md
new file mode 100644
index 0000000..e7b0d36
--- /dev/null
+++ b/docs/src/api/go/example.md
@@ -0,0 +1,63 @@
+# Example with Comments
+
+```go
+
+// Bring the library into scope with the eduvpn prefix
+import eduvpn "github.com/jwijenbergh/eduvpn-common"
+
+// Callbacks
+
+func stateCallback(state *eduvpn.VPNState, oldState string, newState string, data string) {
+
+ // OAuth is started, open the browser with the authorization URL
+ if newState == "OAuth_Started" {
+ openBrowser(data)
+ }
+
+ // Multiple profiles are found, we need to send a profile ID back using state.SetProfileID
+ if newState == "Ask_Profile" {
+ selectAndSendProfile(state, data)
+ }
+}
+
+func main() {
+ // Create the VPNState
+ state := &eduvpn.VPNState{}
+
+ // Register the state
+ // We use linux so the client ID will be org.eduvpn.app.linux
+ // We want to store the config files in configs
+ // We wrap the callback with the state argument
+ // And enable debugging
+ registerErr := state.Register("org.eduvpn.app.linux", "configs", func(old string, new string, data string) {
+ stateCallback(state, old, new, data)
+ }, true)
+
+ if registErr != nil {
+ // handle the error of not being able to register
+ }
+
+ // Cleanup the library at the end
+ defer state.Deregister()
+
+ // Connect to an example server without forcing TCP
+ config, configType, configErr := state.GetConnectConfig("eduvpn.example.com", false)
+
+ if configErr != nil {
+ // handle the error of not being able to get a config
+ }
+
+ if configType == "wireguard" {
+ // Connect using wireguard with the config
+ } else {
+ // Connect using OpenVPN with the config
+ }
+
+ // We are connected
+ setConnectErr := state.SetConnected()
+
+ if setConnectErr != nil {
+ // handle the error of not being able to call set connected
+ }
+}
+```