Skip to content

Commit

Permalink
implement http handlers for services
Browse files Browse the repository at this point in the history
remove log

trim path

prefix wails/services
  • Loading branch information
atterpac committed Jul 25, 2024
1 parent 6e43657 commit 6c167f6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
16 changes: 6 additions & 10 deletions v3/internal/assetserver/assetserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"path"
"strings"
"time"
)

const (
webViewRequestHeaderWindowId = "x-wails-window-id"
webViewRequestHeaderWindowName = "x-wails-window-name"
servicePrefix = "/services"
servicePrefix = "wails/services"
)

type RuntimeHandler interface {
Expand Down Expand Up @@ -108,26 +107,23 @@ func (a *AssetServer) serveHTTP(rw http.ResponseWriter, req *http.Request, userH
}

default:

// Check if this is a plugin asset
if !strings.HasPrefix(reqPath, servicePrefix) {
userHandler.ServeHTTP(rw, req)
return
}

// Ensure there is 4 parts to the reqPath
parts := strings.SplitN(reqPath, "/", 4)
// Ensure there is at least 3 parts to the reqPath wails/services/<service>/<path>
parts := strings.Split(reqPath, "/")
if len(parts) < 4 {
rw.WriteHeader(http.StatusNotFound)
return
}

// Get the first 3 parts of the reqPath
servicePath := "/" + path.Join(parts[1], parts[2], parts[3])

// Check if this is a registered plugin asset
if handler, ok := a.services[servicePath]; ok {
if handler, ok := a.services[parts[3]]; ok {
// Check if the file exists
// Trim the service preifx
req.URL.Path = strings.Join(parts[4:], "/")
handler.ServeHTTP(rw, req)
} else {
userHandler.ServeHTTP(rw, req)
Expand Down
3 changes: 2 additions & 1 deletion v3/internal/generator/analyse.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ func FindServices(pkgs []*packages.Package, systemPaths *config.SystemPaths, log
if fn.Name() == "NewService" && fn.Pkg().Path() == systemPaths.ApplicationPackage {
// Check signature.
signature := fn.Type().(*types.Signature)
if signature.Params().Len() != 1 || signature.Results().Len() != 1 || tp.Len() != 1 || tp.At(0).Obj() == nil {
if signature.Params().Len() > 2 || signature.Results().Len() != 1 || tp.Len() != 1 || tp.At(0).Obj() == nil {
logger.Warningf("Param Len: %d, Results Len: %d, tp.Len: %d, tp.At(0).Obj(): %v", signature.Params().Len(), signature.Results().Len(), tp.Len(), tp.At(0).Obj())
return ErrBadApplicationPackage
}

Expand Down
2 changes: 1 addition & 1 deletion v3/internal/generator/collect/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ var typeAny = types.Universe.Lookup("any").Type().Underlying()
// collectMethod collects and returns information about a service method.
// It is intended to be called only by ServiceInfo.Collect.
func (info *ServiceInfo) collectMethod(method *types.Func) *ServiceMethodInfo {
if method.Name() == "OnStartup" || method.Name() == "OnShutdown" {
if method.Name() == "OnStartup" || method.Name() == "OnShutdown" || method.Name() == "ServeHTTP" {
// Skip special methods.
return nil
}
Expand Down
9 changes: 7 additions & 2 deletions v3/pkg/application/application_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ import (
// Valid values may only be obtained by calling [NewService].
type Service struct {
instance any
prefix string
}

// NewService returns a Service value wrapping the given pointer.
// If T is not a named type, the returned value is invalid.
func NewService[T any](instance *T) Service {
return Service{instance}
// The prefix is used if Service implements a http.Handler only one allowed
func NewService[T any](instance *T, prefix ...string) Service {
if len(prefix) == 1 {
return Service{instance, prefix[0]}
}
return Service{instance, ""}
}

func (s Service) Instance() any {
Expand Down
5 changes: 5 additions & 0 deletions v3/pkg/application/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -79,12 +80,16 @@ type Bindings struct {
}

func NewBindings(instances []Service, aliases map[uint32]uint32) (*Bindings, error) {
app := Get()
b := &Bindings{
boundMethods: make(map[string]*BoundMethod),
boundByID: make(map[uint32]*BoundMethod),
methodAliases: aliases,
}
for _, binding := range instances {
if binding.instance.(http.Handler) != nil && binding.prefix != ""{
app.assets.AttachServiceHandler(binding.prefix, binding.instance.(http.Handler))
}
err := b.Add(binding.Instance())
if err != nil {
return nil, err
Expand Down

0 comments on commit 6c167f6

Please sign in to comment.