|
@@ -1,76 +1,89 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "embed"
|
|
|
"flag"
|
|
|
"fmt"
|
|
|
- "strconv"
|
|
|
+ "io/fs"
|
|
|
+ "net/http"
|
|
|
+ "os"
|
|
|
|
|
|
"imuslab.com/bokofs/bokofsd/mod/bokofs"
|
|
|
"imuslab.com/bokofs/bokofsd/mod/bokofs/bokoworker"
|
|
|
)
|
|
|
|
|
|
-func main() {
|
|
|
-
|
|
|
- httpPort := flag.Int("p", 80, "Port to serve on (Plain HTTP)")
|
|
|
- serveSecure := flag.Bool("s", false, "Serve HTTPS. Default false")
|
|
|
+//go:embed web/*
|
|
|
+var embeddedFiles embed.FS
|
|
|
|
|
|
+func main() {
|
|
|
flag.Parse()
|
|
|
|
|
|
- //Create a test worker register at /test
|
|
|
- testWorker, err := bokoworker.GetDefaultWorker("/teacat")
|
|
|
- if err != nil {
|
|
|
- panic(err)
|
|
|
+ var fileSystem http.FileSystem
|
|
|
+ if *devMode {
|
|
|
+ fmt.Println("Development mode enabled. Serving files from ./web directory.")
|
|
|
+ fileSystem = http.Dir("./web")
|
|
|
+ } else {
|
|
|
+ fmt.Println("Production mode enabled. Serving files from embedded filesystem.")
|
|
|
+ subFS, err := fs.Sub(embeddedFiles, "web")
|
|
|
+ if err != nil {
|
|
|
+ fmt.Fprintf(os.Stderr, "Error accessing embedded subdirectory: %v\n", err)
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+ fileSystem = http.FS(subFS)
|
|
|
}
|
|
|
|
|
|
- webdavHandler, err := bokofs.NewWebdavInterfaceServer(bokofs.Options{
|
|
|
- ListeningAddress: ":" + strconv.Itoa(*httpPort),
|
|
|
- SecureServe: *serveSecure,
|
|
|
- })
|
|
|
+ configFolderPath := "./config"
|
|
|
+ if *config != "" {
|
|
|
+ configFolderPath = *config
|
|
|
+ }
|
|
|
+ if _, err := os.Stat(configFolderPath); os.IsNotExist(err) {
|
|
|
+ fmt.Printf("Config folder does not exist. Creating folder at %s\n", configFolderPath)
|
|
|
+ if err := os.Mkdir(configFolderPath, os.ModePerm); err != nil {
|
|
|
+ fmt.Fprintf(os.Stderr, "Error creating config folder: %v\n", err)
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //DEBUG
|
|
|
+ wds, err := bokofs.NewWebdavInterfaceServer("/disk/")
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
|
|
|
- webdavHandler.AddWorker(testWorker)
|
|
|
-
|
|
|
- err = webdavHandler.Start()
|
|
|
+ test, err := bokoworker.NewWorker("test", "./web")
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
+ wds.AddWorker(test)
|
|
|
|
|
|
- fmt.Println("Bokofs daemon started")
|
|
|
- select {}
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
-//Get all the devices under /dev that is either sd or nvme
|
|
|
- deviceFiles, err := os.ReadDir("/dev")
|
|
|
+ test2, err := bokoworker.NewWorker("test2", "./mod")
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
-
|
|
|
- for _, deviceFile := range deviceFiles {
|
|
|
- if deviceFile.IsDir() {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- fullPath := "/dev/" + deviceFile.Name()
|
|
|
- if !smart.IsRootDisk(fullPath) {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- if !smart.IsDiskSupportedType(fullPath) {
|
|
|
- fmt.Println("Unsupported disk type")
|
|
|
- continue
|
|
|
- }
|
|
|
- fmt.Println(fullPath)
|
|
|
- //Get the SMART data printout in json
|
|
|
- smartdata, err := smart.GetSMARTData(fullPath)
|
|
|
- if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
- }
|
|
|
-
|
|
|
- js, _ := json.MarshalIndent(smartdata, "", " ")
|
|
|
- fmt.Println(string(js))
|
|
|
-
|
|
|
+ wds.AddWorker(test2)
|
|
|
+
|
|
|
+ http.Handle("/", http.FileServer(fileSystem))
|
|
|
+ http.Handle("/disk/", wds.Handler())
|
|
|
+
|
|
|
+ http.Handle("/meta", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
+ // TODO: Implement handler logic for /meta
|
|
|
+ fmt.Fprintln(w, "Meta handler not implemented yet")
|
|
|
+ }))
|
|
|
+
|
|
|
+ http.Handle("/cache", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
+ // TODO: Implement handler logic for /cache
|
|
|
+ fmt.Fprintln(w, "Cache handler not implemented yet")
|
|
|
+ }))
|
|
|
+
|
|
|
+ http.Handle("/thumb", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
+ // TODO: Implement handler logic for /thumb
|
|
|
+ fmt.Fprintln(w, "Thumb handler not implemented yet")
|
|
|
+ }))
|
|
|
+
|
|
|
+ addr := fmt.Sprintf(":%d", *httpPort)
|
|
|
+ fmt.Printf("Starting static web server on %s\n", addr)
|
|
|
+ if err := http.ListenAndServe(addr, nil); err != nil {
|
|
|
+ fmt.Fprintf(os.Stderr, "Error starting server: %v\n", err)
|
|
|
+ os.Exit(1)
|
|
|
}
|
|
|
-*/
|
|
|
+}
|