1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package bokoworker
- import (
- "encoding/json"
- "os"
- "path/filepath"
- "strings"
- "github.com/google/uuid"
- "imuslab.com/bokofs/bokofsd/mod/bokofs/bokofile"
- )
- /*
- Boko Worker
- A boko worker is an instance of WebDAV file server that serves a specific
- disk partition or subpath in which the user can interact with the disk
- via WebDAV interface
- */
- type Worker struct {
- /* Worker Properties */
- RootPath string //The root path (also act as ID) request by this worker, e.g. /disk1
- DiskUUID string // Disk UUID, when provided, will be used instead of disk path
- DiskPath string // Disk device path (e.g. /dev/sda1), Disk UUID will have higher priority
- Subpath string // Subpath to serve, default is root
- ReadOnly bool //Label this worker as read only
- AutoMount bool //Automatically mount the disk if not mounted
- /* Private Properties */
- Filesystem *bokofile.RouterDir
- }
- // GetDefaultWorker Generate and return a default worker serving ./ (CWD)
- func GetDefaultWorker(rootdir string) (*Worker, error) {
- if !strings.HasPrefix(rootdir, "/") {
- rootdir = "/" + rootdir
- }
- mountPath, _ := filepath.Abs("./")
- fs, err := bokofile.CreateRouterFromDir(mountPath, rootdir, false)
- if err != nil {
- return nil, err
- }
- return &Worker{
- RootPath: rootdir,
- DiskUUID: "",
- DiskPath: "",
- Subpath: mountPath,
- ReadOnly: false,
- AutoMount: false,
- Filesystem: fs,
- }, nil
- }
- func GetWorkerFromConfig(configFilePath string) (*Worker, error) {
- randomRootPath := uuid.New().String()
- thisWorker, _ := GetDefaultWorker("/" + randomRootPath)
- configFile, err := os.ReadFile(configFilePath)
- if err != nil {
- return nil, err
- }
- //parse the config file into thisWorker
- err = json.Unmarshal(configFile, &thisWorker)
- if err != nil {
- return nil, err
- }
- //TODO: Start the worker
- return thisWorker, nil
- }
|