5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-11 06:29:29 +08:00
wails/v3/pkg/services/fileserver/fileserver.go
Atterpac e316cd0719
[V3] Plugins implemenations (#3570)
* 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>
2024-09-01 17:26:22 +10:00

53 lines
1.3 KiB
Go

package fileserver
import (
"context"
"net/http"
"github.com/wailsapp/wails/v3/pkg/application"
)
// ---------------- Service Setup ----------------
// This is the main Service struct. It can be named anything you like.
type Config struct {
RootPath string
}
type Service struct {
config *Config
fs http.Handler
}
func New(config *Config) *Service {
return &Service{
config: config,
fs: http.FileServer(http.Dir(config.RootPath)),
}
}
// OnShutdown is called when the app is shutting down
// You can use this to clean up any resources you have allocated
func (s *Service) OnShutdown() error {
return nil
}
// Name returns the name of the plugin.
// You should use the go module format e.g. github.com/myuser/myplugin
func (s *Service) Name() string {
return "github.com/wailsapp/wails/v3/services/fileserver"
}
// OnStartup is called when the app is starting up. You can use this to
// initialise any resources you need.
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) {
// Create a new file server rooted at the given path
// Strip the base path out of the request path
s.fs.ServeHTTP(w, r)
}