Skip to content

Commit

Permalink
[V3] Plugins implemenations (#3570)
Browse files Browse the repository at this point in the history
* plugin handler and lifecycle

* rebase

* remove reflect

s

* remove Config and NewPlugin from plugin template

* Remove plugin manager, generation of plugin interface

* implement http handlers for services

remove log

trim path

prefix wails/services

* update plugine example

* Misc updates

* Ported plugins to services, rewritten example

* Added fileserver

* Update OnStartup and use a context for the application

* Rename PathPrefix to Route. Create docs.

* Use service config copy. Add Name to Service Options. Improve service generation.

* Use service config copy. Add Name to Service Options. Improve service generation. Update README

* Remove rogue db

* Update changelog.md

---------

Co-authored-by: Lea O'Anthony <lea.anthony@gmail.com>
  • Loading branch information
atterpac and leaanthony authored Sep 1, 2024
1 parent 60c9d1a commit e316cd0
Show file tree
Hide file tree
Showing 78 changed files with 1,362 additions and 1,813 deletions.
1 change: 1 addition & 0 deletions mkdocs-website/docs/en/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- [windows] Window class name option by [windom](https://github.com/windom/) in [#3682](https://github.com/wailsapp/wails/pull/3682)
- Services have been expanded to provide plugin functionality. By [atterpac](https://github.com/atterpac) and [leaanthony](https://github.com/leaanthony) in [#3570](https://github.com/wailsapp/wails/pull/3570)

### Fixed
- [windows] Fixed syso icon file generation bug by [atterpac](https://github.com/atterpac) in [#3675](https://github.com/wailsapp/wails/pull/3675)
Expand Down
145 changes: 145 additions & 0 deletions mkdocs-website/docs/en/learn/services.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Services

Services in Wails v3 provide a powerful way to extend the functionality of your application. They allow you to create
modular, reusable components that can be easily integrated into your Wails application.

## Overview

Services are designed to encapsulate specific functionality and can be registered with the application at startup.
They can handle various tasks such as file serving, database operations, logging, and more.
Services can also interact with the application lifecycle and respond to HTTP requests.

## Creating a Service

To create a service, you simply define a struct. Here's a basic structure of a service:

```go
type MyService struct {
// Your service fields
}

func NewMyService() *MyService {
// Initialize and return your service
}

func (s *MyService) Greet(name string) string {
return fmt.Sprintf("Hello, %s!", name)
}
```

This service has a single method, `Greet`, which accepts a name and returns a greeting.

## Registering a Service

To register a service with the application, you need to provide an instance of the service to the `Services` field of
the `application.Options` struct (All services need to be wrapped by an `application.NewService` call. Here's an example:

```go
app := application.New(application.Options{
Services: []application.Service{
application.NewService(NewMyService()),
},
})

```

## Optional Methods

Services can implement optional methods to hook into the application lifecycle:

### Name

```go
func (s *Service) Name() string
```

This method returns the name of the service. It is used for logging purposes only.

### OnStartup

```go
func (s *Service) OnStartup(ctx context.Context, options application.ServiceOptions) error
```

This method is called when the application is starting up. You can use it to initialize resources, set up connections,
or perform any necessary setup tasks. The context is the application context, and the `options` parameter provides
additional information about the service.

### OnShutdown

```go
func (s *Service) OnShutdown() error
```

This method is called when the application is shutting down. Use it to clean up resources, close connections, or
perform any necessary cleanup tasks.

### ServeHTTP

```go
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)
```

If your service needs to handle HTTP requests, implement this method. It allows your service to act as an HTTP handler.
The route of the handler is defined in the service options:

```go
application.NewService(fileserver.New(&fileserver.Config{
RootPath: rootPath,
}), application.ServiceOptions{
Route: "/files",
}),
```

## Example: File Server Service

Let's look at a simplified version of the `fileserver` service as an example:

```go
type Service struct {
config *Config
fs http.Handler
}

func New(config *Config) *Service {
return &Service{
config: config,
fs: http.FileServer(http.Dir(config.RootPath)),
}
}

func (s *Service) Name() string {
return "github.com/wailsapp/wails/v3/services/fileserver"
}

func (s *Service) OnStartup(ctx context.Context, options application.ServiceOptions) error {
// Any initialization code here
return nil
}

func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.fs.ServeHTTP(w, r)
}
```

We can now use this service in our application:

```go
app := application.New(application.Options{
Services: []application.Service{
application.NewService(fileserver.New(&fileserver.Config{
RootPath: rootPath,
}), application.ServiceOptions{
Route: "/files",
}),
```
All requests to `/files` will be handled by the `fileserver` service.
## Application Lifecycle and Services
1. During application initialization, services are registered with the application.
2. When the application starts (`app.Run()`), the `OnStartup` method of each service is called with the application
context and service options.
3. Throughout the application's lifetime, services can perform their specific tasks.
4. If a service implements `ServeHTTP`, it can handle HTTP requests at the specified path.
5. When the application is shutting down, the `OnShutdown` method of each service is called as well as the context being cancelled.
1 change: 1 addition & 0 deletions mkdocs-website/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ nav:
- Your First Application: getting-started/your-first-app.md
- Next Steps: getting-started/next-steps.md
- Learn More:
- Services: learn/services.md
- Runtime: learn/runtime.md
- Plugins: learn/plugins.md
- Guides:
Expand Down
6 changes: 2 additions & 4 deletions v3/cmd/wails3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ func main() {
generate.NewSubCommandFunction(".desktop", "Generate .desktop file", commands.GenerateDotDesktop)
generate.NewSubCommandFunction("appimage", "Generate Linux AppImage", commands.GenerateAppImage)

plugin := app.NewSubCommand("plugin", "Plugin tools")
//plugin.NewSubCommandFunction("list", "List plugins", commands.PluginList)
plugin.NewSubCommandFunction("init", "Initialise a new plugin", commands.PluginInit)
//plugin.NewSubCommandFunction("add", "Add a plugin", commands.PluginAdd)
plugin := app.NewSubCommand("service", "Service tools")
plugin.NewSubCommandFunction("init", "Initialise a new service", commands.ServiceInit)
tool := app.NewSubCommand("tool", "Various tools")
tool.NewSubCommandFunction("checkport", "Checks if a port is open. Useful for testing if vite is running.", commands.ToolCheckPort)
tool.NewSubCommandFunction("watcher", "Watches files and runs a command when they change", commands.Watcher)
Expand Down
3 changes: 0 additions & 3 deletions v3/examples/plugins/README.md

This file was deleted.

42 changes: 0 additions & 42 deletions v3/examples/plugins/Taskfile.yml

This file was deleted.

35 changes: 0 additions & 35 deletions v3/examples/plugins/build/Info.dev.plist

This file was deleted.

27 changes: 0 additions & 27 deletions v3/examples/plugins/build/Info.plist

This file was deleted.

Binary file removed v3/examples/plugins/build/appicon.png
Binary file not shown.
Empty file.
Empty file.
15 changes: 0 additions & 15 deletions v3/examples/plugins/build/info.json

This file was deleted.

15 changes: 0 additions & 15 deletions v3/examples/plugins/build/wails.exe.manifest

This file was deleted.

65 changes: 0 additions & 65 deletions v3/examples/plugins/go.mod

This file was deleted.

Loading

0 comments on commit e316cd0

Please sign in to comment.