webserv.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package webserv
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "sync"
  10. "imuslab.com/zoraxy/mod/utils"
  11. )
  12. /*
  13. Static Web Server package
  14. This module host a static web server
  15. */
  16. type WebServerOptions struct {
  17. Port string //Port for listening
  18. EnableDirectoryListing bool //Enable listing of directory
  19. WebRoot string //Folder for stroing the static web folders
  20. }
  21. type WebServer struct {
  22. mux *http.ServeMux
  23. server *http.Server
  24. option *WebServerOptions
  25. isRunning bool
  26. mu sync.Mutex
  27. }
  28. // NewWebServer creates a new WebServer instance.
  29. func NewWebServer(options *WebServerOptions) *WebServer {
  30. if !utils.FileExists(options.WebRoot) {
  31. //Web root folder not exists. Create one
  32. os.MkdirAll(filepath.Join(options.WebRoot, "html"), 0775)
  33. os.MkdirAll(filepath.Join(options.WebRoot, "templates"), 0775)
  34. }
  35. return &WebServer{
  36. mux: http.NewServeMux(),
  37. option: options,
  38. isRunning: false,
  39. mu: sync.Mutex{},
  40. }
  41. }
  42. // ChangePort changes the server's port.
  43. func (ws *WebServer) ChangePort(port string) error {
  44. ws.mu.Lock()
  45. defer ws.mu.Unlock()
  46. if ws.isRunning {
  47. if err := ws.Stop(); err != nil {
  48. return err
  49. }
  50. }
  51. ws.option.Port = port
  52. ws.server.Addr = ":" + port
  53. return nil
  54. }
  55. // Start starts the web server.
  56. func (ws *WebServer) Start() error {
  57. ws.mu.Lock()
  58. defer ws.mu.Unlock()
  59. //Check if server already running
  60. if ws.isRunning {
  61. return fmt.Errorf("web server is already running")
  62. }
  63. //Check if the port is usable
  64. if IsPortInUse(ws.option.Port) {
  65. return errors.New("Port already in use or access denied by host OS")
  66. }
  67. //Dispose the old mux and create a new one
  68. ws.mux = http.NewServeMux()
  69. //Create a static web server
  70. fs := http.FileServer(http.Dir(filepath.Join(ws.option.WebRoot, "html")))
  71. ws.mux.Handle("/", ws.fsMiddleware(fs))
  72. ws.server = &http.Server{
  73. Addr: ":" + ws.option.Port,
  74. Handler: ws.mux,
  75. }
  76. go func() {
  77. if err := ws.server.ListenAndServe(); err != nil {
  78. if err != http.ErrServerClosed {
  79. fmt.Printf("Web server error: %v\n", err)
  80. }
  81. }
  82. }()
  83. log.Println("Static Web Server started. Listeing on :" + ws.option.Port)
  84. ws.isRunning = true
  85. return nil
  86. }
  87. // Stop stops the web server.
  88. func (ws *WebServer) Stop() error {
  89. ws.mu.Lock()
  90. defer ws.mu.Unlock()
  91. if !ws.isRunning {
  92. return fmt.Errorf("web server is not running")
  93. }
  94. if err := ws.server.Close(); err != nil {
  95. return err
  96. }
  97. ws.isRunning = false
  98. return nil
  99. }
  100. // UpdateDirectoryListing enables or disables directory listing.
  101. func (ws *WebServer) UpdateDirectoryListing(enable bool) {
  102. ws.mu.Lock()
  103. defer ws.mu.Unlock()
  104. ws.option.EnableDirectoryListing = enable
  105. }
  106. // Close stops the web server without returning an error.
  107. func (ws *WebServer) Close() {
  108. ws.Stop()
  109. }