main.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package main
  2. import (
  3. "embed"
  4. "flag"
  5. "fmt"
  6. "io/fs"
  7. "net/http"
  8. "os"
  9. "imuslab.com/bokofs/bokofsd/mod/bokofs"
  10. "imuslab.com/bokofs/bokofsd/mod/bokofs/bokoworker"
  11. )
  12. //go:embed web/*
  13. var embeddedFiles embed.FS
  14. func main() {
  15. flag.Parse()
  16. var fileSystem http.FileSystem
  17. if *devMode {
  18. fmt.Println("Development mode enabled. Serving files from ./web directory.")
  19. fileSystem = http.Dir("./web")
  20. } else {
  21. fmt.Println("Production mode enabled. Serving files from embedded filesystem.")
  22. subFS, err := fs.Sub(embeddedFiles, "web")
  23. if err != nil {
  24. fmt.Fprintf(os.Stderr, "Error accessing embedded subdirectory: %v\n", err)
  25. os.Exit(1)
  26. }
  27. fileSystem = http.FS(subFS)
  28. }
  29. configFolderPath := "./config"
  30. if *config != "" {
  31. configFolderPath = *config
  32. }
  33. if _, err := os.Stat(configFolderPath); os.IsNotExist(err) {
  34. fmt.Printf("Config folder does not exist. Creating folder at %s\n", configFolderPath)
  35. if err := os.Mkdir(configFolderPath, os.ModePerm); err != nil {
  36. fmt.Fprintf(os.Stderr, "Error creating config folder: %v\n", err)
  37. os.Exit(1)
  38. }
  39. }
  40. //DEBUG
  41. wds, err := bokofs.NewWebdavInterfaceServer("/disk/")
  42. if err != nil {
  43. panic(err)
  44. }
  45. test, err := bokoworker.NewWorker("test", "./web")
  46. if err != nil {
  47. panic(err)
  48. }
  49. wds.AddWorker(test)
  50. test2, err := bokoworker.NewWorker("test2", "./mod")
  51. if err != nil {
  52. panic(err)
  53. }
  54. wds.AddWorker(test2)
  55. http.Handle("/", http.FileServer(fileSystem))
  56. http.Handle("/disk/", wds.Handler())
  57. http.Handle("/meta", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  58. // TODO: Implement handler logic for /meta
  59. fmt.Fprintln(w, "Meta handler not implemented yet")
  60. }))
  61. http.Handle("/cache", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  62. // TODO: Implement handler logic for /cache
  63. fmt.Fprintln(w, "Cache handler not implemented yet")
  64. }))
  65. http.Handle("/thumb", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  66. // TODO: Implement handler logic for /thumb
  67. fmt.Fprintln(w, "Thumb handler not implemented yet")
  68. }))
  69. addr := fmt.Sprintf(":%d", *httpPort)
  70. fmt.Printf("Starting static web server on %s\n", addr)
  71. if err := http.ListenAndServe(addr, nil); err != nil {
  72. fmt.Fprintf(os.Stderr, "Error starting server: %v\n", err)
  73. os.Exit(1)
  74. }
  75. }