5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-10 22:19:46 +08:00
wails/v3/internal/service/template/service.go.tmpl
2025-01-16 07:47:23 +11:00

61 lines
2.4 KiB
Cheetah

package {{.Name}}
import (
"context"
"github.com/wailsapp/wails/v3/pkg/application"
)
// ---------------- Service Setup ----------------
// This is the main service struct. It can be named anything you like.
// Both the ServiceStartup() and ServiceShutdown() methods are called synchronously when the app starts and stops.
// Changing the name of this struct will change the name of the services class in the frontend
// Bound methods will exist inside frontend/bindings/github.com/user/{{.Name}} under the name of the struct
type MyService struct{
ctx context.Context
options application.ServiceOptions
}
// ServiceName is the name of the service
func (p *MyService) ServiceName() string {
return "{{.Name}}"
}
// ServiceStartup is called when the app is starting up. You can use this to
// initialise any resources you need. You can also access the application
// instance via the app property.
// OPTIONAL: This method is optional.
func (p *MyService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
p.ctx = ctx
p.options = options
return nil
}
// ServiceShutdown is called when the app is shutting down via runtime.Quit() call
// You can use this to clean up any resources you have allocated
// OPTIONAL: This method is optional.
func (p *MyService) ServiceShutdown() error {
return nil
}
// ServeHTTP is called when the app is running and the frontend makes an HTTP request to the backend at the path
// specified in the `Route` field of the service Options.
// OPTIONAL: This method is optional.
func (p *MyService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// You can use the request to get the path, query parameters, headers, etc.
// You can also use the response to set the status code, headers, body etc.
// Consult the net/http documentation for more information: https://pkg.go.dev/net/http
// Log the request to the console
log.Printf("Received request: %s %s", r.Method, r.URL.Path)
}
// ---------------- Service Methods ----------------
// Service methods are just normal Go methods. You can add as many as you like.
// The only requirement is that they are exported (start with a capital letter).
// You can also return any type that is JSON serializable.
// See https://golang.org/pkg/encoding/json/#Marshal for more information.
func (p *MyService) Greet(name string) string {
return "Hello " + name
}