Skip to content

Commit

Permalink
more modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
tacheraSasi committed Dec 10, 2024
1 parent 9a38ab9 commit 46b2973
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 46 deletions.
28 changes: 8 additions & 20 deletions module/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func init() {
HttpFunctions["fileServer"] = fileServer
}

// fileServer serves files from a specified directory with enhanced features.
// fileServer serves files from a specified directory with directory listing enabled.
func fileServer(args []object.Object, defs map[string]object.Object) object.Object {
if len(args) < 2 || len(args) > 4 {
return &object.Error{Message: "Usage: http.fileServer(port, directory, [message], [enableListing])"}
if len(args) < 2 || len(args) > 3 {
return &object.Error{Message: "Usage: http.fileServer(port, directory, [message])"}
}

// Validate port argument
Expand All @@ -47,18 +47,7 @@ func fileServer(args []object.Object, defs map[string]object.Object) object.Obje
return &object.Error{Message: "Message must be a string"}
}
} else {
message = fmt.Sprintf("Server started on port %s serving files from %s", port.Value, directory.Value)
}

// Validates enableListing flag (optional)
enableListing := false
if len(args) == 4 {
listingFlag, ok := args[3].(*object.Boolean)
if ok {
enableListing = listingFlag.Value
} else {
return &object.Error{Message: "enableListing must be a boolean"}
}
message = fmt.Sprintf("Server started on port %s serving files from %s with directory listing enabled", port.Value, directory.Value)
}

// Ensure directory exists
Expand All @@ -67,11 +56,9 @@ func fileServer(args []object.Object, defs map[string]object.Object) object.Obje
return &object.Error{Message: "Invalid or non-existent directory"}
}

// Create a file server with optional middleware
// Create a file server with directory listing enabled
fileHandler := http.FileServer(http.Dir(absDirectory))
if enableListing {
fileHandler = enableDirectoryListing(fileHandler)
}
fileHandler = enableDirectoryListing(fileHandler)

// Wrap the file server in middleware for logging requests
http.Handle("/", logMiddleware(fileHandler))
Expand All @@ -98,9 +85,10 @@ func logMiddleware(next http.Handler) http.Handler {
})
}

// enableDirectoryListing modifies the handler to allow directory listings.
// enableDirectoryListing modifies the handler to always show directory listings.
func enableDirectoryListing(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Setting the content type to HTML so directory contents are rendered as a webpage.
w.Header().Set("Content-Type", "text/html")
next.ServeHTTP(w, r)
})
Expand Down
39 changes: 13 additions & 26 deletions vintLang/http.vint
Original file line number Diff line number Diff line change
@@ -1,44 +1,31 @@
import http

let port = "3000"
let dir = "/var/www/html"
let port = "3000";
let dir = "/var/www/html";

// Scenario 1: Using the default message and no directory listing
http.fileServer(port, dir)
http.fileServer(port, dir);

// Scenario 2: Starting a file server with a custom message
http.fileServer(
port,
dir,
"My custom Vint server is running on port 3000!"
)
);

// Scenario 3: Starting a file server with directory listing enabled (default message)
http.fileServer(
port,
dir,
null, // No custom message, use the default
true // Enable directory listing
)
dir
);

// Scenario 4: Starting a file server with a custom message and directory listing enabled
http.fileServer(
port,
dir,
"Directory listing is now enabled! Check it out!",
true
)

// Scenario 5: Starting a file server with a custom message and directory listing disabled
http.fileServer(
port,
dir,
"No directory listing. Just serving static files.",
false
)
"Directory listing is now enabled! Check it out!"
);

// Scenario 6: Invalid usage (e.g., wrong arguments) - this should return an error
http.fileServer(3000, dir) // Port must be a string
http.fileServer(port, 123) // Directory must be a string
http.fileServer(port, dir, 404) // Message must be a string
http.fileServer(port, dir, "Message", "true") // enableListing must be a boolean
// Scenario 5: Invalid usage (e.g., wrong arguments) - this should return an error
http.fileServer(3000, dir); // Port must be a string
http.fileServer(port, 123); // Directory must be a string
http.fileServer(port, dir, 404); // Message must be a string
http.fileServer(port, dir, "Message", "true"); // enableListing must be a boolean

0 comments on commit 46b2973

Please sign in to comment.