5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-17 01:19:29 +08:00

Ignore internal service methods when binding (#3720)

* Ignore internal service methods when binding

* Updated changelog.md
This commit is contained in:
Lea Anthony 2024-09-09 08:36:19 +10:00 committed by GitHub
parent cc70b98eed
commit ef8c886b10
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 53 additions and 15 deletions

View File

@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [linux] Fixed linux compile error introduced by IgnoreMouseEvents addition by [atterpac](https://github.com/atterpac) in [#3721](https://github.com/wailsapp/wails/pull/3721) - [linux] Fixed linux compile error introduced by IgnoreMouseEvents addition by [atterpac](https://github.com/atterpac) in [#3721](https://github.com/wailsapp/wails/pull/3721)
- [windows] Fixed syso icon file generation bug by [atterpac](https://github.com/atterpac) in [#3675](https://github.com/wailsapp/wails/pull/3675) - [windows] Fixed syso icon file generation bug by [atterpac](https://github.com/atterpac) in [#3675](https://github.com/wailsapp/wails/pull/3675)
- [linux] Fix to run natively in wayland incorporated from [#1811](https://github.com/wailsapp/wails/pull/1811) in [#3614](https://github.com/wailsapp/wails/pull/3614) by [@stendler](https://github.com/stendler) - [linux] Fix to run natively in wayland incorporated from [#1811](https://github.com/wailsapp/wails/pull/1811) in [#3614](https://github.com/wailsapp/wails/pull/3614) by [@stendler](https://github.com/stendler)
- Do not bind internal service methods in [#3720](https://github.com/wailsapp/wails/pull/3720)
by [leaanthony](https://github.com/leaanthony)
- [windows] Fixed system tray startup panic in [#3693](https://github.com/wailsapp/wails/issues/3693) by [@DeltaLaboratory](https://github.com/DeltaLaboratory) - [windows] Fixed system tray startup panic in [#3693](https://github.com/wailsapp/wails/issues/3693) by [@DeltaLaboratory](https://github.com/DeltaLaboratory)
## v3.0.0-alpha.6 - 2024-07-30 ## v3.0.0-alpha.6 - 2024-07-30

View File

@ -1,10 +1,12 @@
package hashes package hashes
import ( import (
"context"
"crypto/md5" "crypto/md5"
"crypto/sha1" "crypto/sha1"
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"github.com/wailsapp/wails/v3/pkg/application"
) )
type Hashes struct { type Hashes struct {
@ -35,6 +37,6 @@ func (h *Hashes) Name() string {
return "Hashes Service" return "Hashes Service"
} }
func (h *Hashes) OnStartup() error { func (h *Hashes) OnStartup(_ context.Context, _ application.ServiceOptions) error {
return nil return nil
} }

View File

@ -11,6 +11,7 @@ import (
"log/slog" "log/slog"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
) )
//go:embed assets/* //go:embed assets/*
@ -18,7 +19,13 @@ var assets embed.FS
func main() { func main() {
rootPath, _ := filepath.Abs("./files") // Get the local directory of this source file
// This isn't needed when running the example with `go run .`
// but is needed when running the example from an IDE
_, thisFile, _, _ := runtime.Caller(0)
localDir := filepath.Dir(thisFile)
rootPath := filepath.Join(localDir, "files")
app := application.New(application.Options{ app := application.New(application.Options{
Name: "Services Demo", Name: "Services Demo",
Description: "A demo of the services API", Description: "A demo of the services API",

View File

@ -112,8 +112,6 @@ func (a *AssetServer) serveHTTP(rw http.ResponseWriter, req *http.Request, userH
for route, handler := range a.services { for route, handler := range a.services {
if strings.HasPrefix(reqPath, route) { if strings.HasPrefix(reqPath, route) {
req.URL.Path = strings.TrimPrefix(reqPath, route) req.URL.Path = strings.TrimPrefix(reqPath, route)
// Strip leading slash
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/")
handler.ServeHTTP(rw, req) handler.ServeHTTP(rw, req)
return return
} }

View File

@ -101,7 +101,7 @@ func NewBindings(instances []Service, aliases map[uint32]uint32) (*Bindings, err
// Add the given named type pointer methods to the Bindings // Add the given named type pointer methods to the Bindings
func (b *Bindings) Add(namedPtr interface{}) error { func (b *Bindings) Add(namedPtr interface{}) error {
methods, err := b.getMethods(namedPtr, false) methods, err := b.getMethods(namedPtr)
if err != nil { if err != nil {
return fmt.Errorf("cannot bind value to app: %s", err.Error()) return fmt.Errorf("cannot bind value to app: %s", err.Error())
} }
@ -153,7 +153,7 @@ func (b *BoundMethod) String() string {
return fmt.Sprintf("%s.%s.%s", b.PackagePath, b.TypeName, b.Name) return fmt.Sprintf("%s.%s.%s", b.PackagePath, b.TypeName, b.Name)
} }
func (b *Bindings) getMethods(value interface{}, isPlugin bool) ([]*BoundMethod, error) { func (b *Bindings) getMethods(value interface{}) ([]*BoundMethod, error) {
// Create result placeholder // Create result placeholder
var result []*BoundMethod var result []*BoundMethod
@ -188,6 +188,10 @@ func (b *Bindings) getMethods(value interface{}, isPlugin bool) ([]*BoundMethod,
methodName := methodDef.Name methodName := methodDef.Name
method := namedValue.MethodByName(methodName) method := namedValue.MethodByName(methodName)
if b.internalMethod(methodDef) {
continue
}
// Create new method // Create new method
boundMethod := &BoundMethod{ boundMethod := &BoundMethod{
Name: methodName, Name: methodName,
@ -204,7 +208,6 @@ func (b *Bindings) getMethods(value interface{}, isPlugin bool) ([]*BoundMethod,
return nil, err return nil, err
} }
if !isPlugin {
args := []any{"name", boundMethod, "id", boundMethod.ID} args := []any{"name", boundMethod, "id", boundMethod.ID}
if b.methodAliases != nil { if b.methodAliases != nil {
alias, found := lo.FindKey(b.methodAliases, boundMethod.ID) alias, found := lo.FindKey(b.methodAliases, boundMethod.ID)
@ -213,7 +216,7 @@ func (b *Bindings) getMethods(value interface{}, isPlugin bool) ([]*BoundMethod,
} }
} }
globalApplication.debug("Adding method:", args...) globalApplication.debug("Adding method:", args...)
}
// Iterate inputs // Iterate inputs
methodType := method.Type() methodType := method.Type()
inputParamCount := methodType.NumIn() inputParamCount := methodType.NumIn()
@ -245,6 +248,33 @@ func (b *Bindings) getMethods(value interface{}, isPlugin bool) ([]*BoundMethod,
return result, nil return result, nil
} }
func (b *Bindings) internalMethod(def reflect.Method) bool {
// Get the receiver type
receiverType := def.Type.In(0)
// Create a new instance of the receiver type
instance := reflect.New(receiverType.Elem()).Interface()
// Check if the instance implements any of our service interfaces
// and if the method matches the interface method
switch def.Name {
case "Name":
if _, ok := instance.(ServiceName); ok {
return true
}
case "OnStartup":
if _, ok := instance.(ServiceStartup); ok {
return true
}
case "OnShutdown":
if _, ok := instance.(ServiceShutdown); ok {
return true
}
}
return false
}
var errorType = reflect.TypeFor[error]() var errorType = reflect.TypeFor[error]()
// Call will attempt to call this bound method with the given args // Call will attempt to call this bound method with the given args

View File

@ -47,6 +47,5 @@ func (s *Service) OnStartup(ctx context.Context, options application.ServiceOpti
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Create a new file server rooted at the given path // Create a new file server rooted at the given path
// Strip the base path out of the request path
s.fs.ServeHTTP(w, r) s.fs.ServeHTTP(w, r)
} }