Skip to content

Commit

Permalink
export FetchResource from Go
Browse files Browse the repository at this point in the history
  • Loading branch information
jyyi1 committed Nov 8, 2024
1 parent de56679 commit 9c6a234
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
26 changes: 26 additions & 0 deletions client/go/outline/electron/cstring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

/*
#include <stdlib.h>
*/
import "C"
import "unsafe"

//export FreeString
func FreeString(str *C.char) {
C.free(unsafe.Pointer(str))
}
36 changes: 36 additions & 0 deletions client/go/outline/electron/fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

/*
#include "platerr.h"
struct FetchResourceResult {
const char *Content;
PlatformErrorHandle Error;
};
*/
import "C"
import "github.com/Jigsaw-Code/outline-apps/client/go/outline"

//export FetchResource
func FetchResource(cstr *C.char) C.struct_FetchResourceResult {
url := C.GoString(cstr)
result := outline.FetchResource(url)
return C.struct_FetchResourceResult{
Content: C.CString(result.Content),
Error: ToCPlatformErrorHandle(result.Error),
}
}
33 changes: 33 additions & 0 deletions client/go/outline/electron/handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

/*
#include <stdint.h>
*/
import "C"
import (
"runtime/cgo"
)

const NilHandle = 0

//export FreeHandle
func FreeHandle(ptr C.uintptr_t) {
if ptr != NilHandle {
h := cgo.Handle(ptr)
h.Delete()
}
}
40 changes: 40 additions & 0 deletions client/go/outline/electron/platerr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

/*
#include "platerr.h"
*/
import "C"
import (
"runtime/cgo"

"github.com/Jigsaw-Code/outline-apps/client/go/outline/platerrors"
)

func ToCPlatformErrorHandle(err *platerrors.PlatformError) C.PlatformErrorHandle {
if err == nil {
return NilHandle
}
return C.PlatformErrorHandle(cgo.NewHandle(err))
}

func FromCPlatformErrorHandle(err C.PlatformErrorHandle) *platerrors.PlatformError {
if err == NilHandle {
return nil
}
h := cgo.Handle(err)
return h.Value().(*platerrors.PlatformError)
}
17 changes: 17 additions & 0 deletions client/go/outline/electron/platerr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2024 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stdint.h>

typedef uintptr_t PlatformErrorHandle;

0 comments on commit 9c6a234

Please sign in to comment.