5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 07:43:11 +08:00
wails/v3/internal/service/template
2025-01-16 07:47:23 +11:00
..
go.mod.tmpl [v3] bugfix: update version to support application.ServiceOptions (#3836) 2024-11-16 11:07:51 +11:00
go.sum [v3] bugfix: update version to support application.ServiceOptions (#3836) 2024-11-16 11:07:51 +11:00
README.tmpl.md Breaking Change: Service method names 2025-01-16 07:47:23 +11:00
service.go.tmpl Breaking Change: Service method names 2025-01-16 07:47:23 +11:00
service.tmpl.yml [V3] Plugins implemenations (#3570) 2024-09-01 17:26:22 +10:00

Wails v3 Service Template

This README provides an overview of the Wails v3 service template and explains how to adapt it to create your own custom service.

Overview

The service template provides a basic structure for creating a Wails v3 service. A service in Wails v3 is a Go package that can be integrated into your Wails application to provide specific functionality, handle HTTP requests, and interact with the frontend.

Template Structure

The template defines a MyService struct and several methods:

MyService Struct

type MyService struct {
    ctx context.Context
    options application.ServiceOptions
}

This is the main service struct. You can rename it to better reflect your service's purpose. The struct holds a context and service options, which are set during startup.

ServiceName Method

func (p *MyService) ServiceName() string

This method returns the name of the service. It's used to identify the service within the Wails application.

ServiceStartup Method

func (p *MyService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error

This method is called when the app is starting up. Use it to initialize resources, set up connections, or perform any necessary setup tasks. It receives a context and service options, which are stored in the service struct.

ServiceShutdown Method

func (p *MyService) ServiceShutdown() error

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

ServeHTTP Method

func (p *MyService) ServeHTTP(w http.ResponseWriter, r *http.Request)

This method handles HTTP requests to the service. It's called when the frontend makes an HTTP request to the backend at the path specified in the Route field of the service options.

Service Methods

func (p *MyService) Greet(name string) string

This is an example of a service method. You can add as many methods as you need. These methods can be called from the frontend.

Adapting the Template

To create your own service:

  1. Rename the MyService struct to reflect your service's purpose (e.g., DatabaseService, AuthService).
  2. Update the ServiceName method to return your service's unique identifier.
  3. Implement the ServiceStartup method to initialize your service. This might include setting up database connections, loading configuration, etc.
  4. If needed, implement the ServiceShutdown method to properly clean up resources when the application closes.
  5. If your service needs to handle HTTP requests, implement the ServeHTTP method. Use this to create API endpoints, serve files, or handle any HTTP interactions.
  6. Add your own methods to the service. These can include database operations, business logic, or any functionality your service needs to provide.
  7. If your service requires configuration, consider adding a Config struct and a New function to create and configure your service.

Example: Database Service

Here's how you might adapt the template for a database service:

type DatabaseService struct {
    ctx context.Context
    options application.ServiceOptions
    db *sql.DB
}

func (s *DatabaseService) Name() string {
    return "github.com/myname/DatabaseService"
}

func (s *DatabaseService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
    s.ctx = ctx
    s.options = options
    // Initialize database connection
    var err error
    s.db, err = sql.Open("mysql", "user:password@/dbname")
    return err
}

func (s *DatabaseService) ServiceShutdown() error {
    return s.db.Close()
}

func (s *DatabaseService) GetUser(id int) (User, error) {
    // Implement database query
}

// Add more methods as needed

Long-running tasks

If your service needs to perform long-running tasks, consider using goroutines and channels to manage these tasks. You can use the context.Context to listen for when the application shuts down:

func (s *DatabaseService) longRunningTask() {
    for {
        select {
        case <-s.ctx.Done():
            // Cleanup and exit
            return
        // Perform long-running task
        }   
    }
}