Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
Last active February 19, 2024 17:14
Show Gist options
  • Save mayankchoubey/97ee3360165b7eb07afe277c6e969a4c to your computer and use it in GitHub Desktop.
Save mayankchoubey/97ee3360165b7eb07afe277c6e969a4c to your computer and use it in GitHub Desktop.
Static file server in various languages
const basePath = "/Users/mayankc/Work/source/perfComparisons";
Bun.serve({
port: 3000,
fetch(request) {
const fp = basePath + new URL(request.url).pathname;
try {
return new Response(Bun.file(fp));
} catch (e) {
return new Response(null, { status: 404 });
}
},
});
import { Application } from "https://deno.land/x/oak/mod.ts";
const app = new Application();
app.use(async (context) => {
await context.send({
root: "/Users/mayankc/Work/source/perfComparisons",
});
});
await app.listen({ port: 3000 });
using Microsoft.Extensions.FileProviders;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider("/Users/mayankc/Work/source/perfComparisons/static"),
RequestPath = "/static",
ServeUnknownFileTypes = true
});
app.Run();
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
router := gin.New()
router.Static("/static", "/Users/mayankc/Work/source/perfComparisons/static")
router.Run(":3000")
}
const fastify = require("fastify");
const app = fastify();
app.register(require("@fastify/static"), {
root: "/Users/mayankc/Work/source/perfComparisons/static",
prefix: "/static/",
prefixAvoidTrailingSlash: true,
});
app.listen({ port: 3000 });
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/static", StaticFiles(directory="/Users/mayankc/Work/source/perfComparisons/static"), name="static")
// FileServerApplication.java
package org.acme;
import io.quarkus.runtime.StartupEvent;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.handler.FileSystemAccess;
import jakarta.enterprise.event.Observes;
public class FileServerApplication {
void installRoute(@Observes StartupEvent startupEvent, Router router) {
router.route()
.path("/static/*")
.handler(StaticHandler.create(
FileSystemAccess.ROOT,
"/Users/mayankc/Work/source/perfComparisons/static/"));
}
}
// application.properties
quarkus.http.port=3000
use actix_files as fs;
use actix_web::{App, HttpServer};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new()
.service(fs::Files::new("/static", "/Users/mayankc/Work/source/perfComparisons/static")
.use_etag(false)))
.bind(("127.0.0.1", 3000))?
.run()
.await
}
// ***** RUST CODE IS BUILT IN RELEASE MODE ****** //
// Application.java
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.reactivestreams.Publisher;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;
@SpringBootApplication
@EnableAsync
@Controller
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}
// application.properties
server.port=3000
spring.threads.virtual.enabled=true
spring.webflux.static-path-pattern=/static/**
spring.web.resources.static-locations=file:/Users/mayankc/Work/source/perfComparisons/static/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment