Skip to content

Instantly share code, notes, and snippets.

View joshlong's full-sized avatar
🍃
i'm on Twitter @starbuxman

Josh Long joshlong

🍃
i'm on Twitter @starbuxman
View GitHub Profile
@joshlong
joshlong / serve.rs
Created August 22, 2026 19:50
serve.rs
use axum::{routing::get, Json, Router};
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug)]
struct Todo {
userId: u32,
id: u32,
title: String,
completed: bool,
}
@joshlong
joshlong / serve.py
Last active August 22, 2026 19:51
serve.py
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import httpx
import uvicorn
app = FastAPI()
class Todo(BaseModel):
userId: int
id: int
@joshlong
joshlong / serve.js
Last active August 22, 2026 19:49
serve.js
const express = require('express');
const app = express();
app.get('/fetch-todo', async (req, res) => {
const url = 'https://jsonplaceholder.typicode.com/todos/1';
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const todo = await response.json();
@joshlong
joshlong / serve.java
Last active August 22, 2026 20:03
a virtual threads-powered web server and build using JBang and Spring Boot
//usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25
//DEPS org.springframework.boot:spring-boot-starter-web:4.1.0
//DEPS org.springframework.boot:spring-boot-starter-restclient:4.1.0
//DEPS https://github.com/scratches/spring-script
import static org.springframework.web.servlet.function.RouterFunctions.route;
import static org.springframework.web.servlet.function.ServerResponse.*;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestClient;
import org.springframework.web.servlet.function.*;
@joshlong
joshlong / test.java
Created August 22, 2026 19:05
test.java
// save to ss.java and run with: java ss.java
sealed interface Loan permits SecuredLoan, UnsecuredLoan {}
final class SecuredLoan implements Loan {}
record UnsecuredLoan(float interest) implements Loan {}
String admonition(Loan loan) {
return switch (loan) {
case SecuredLoan sl -> "good job. nice loan.";
@joshlong
joshlong / LifecycleApplication.java
Created May 22, 2025 14:31
an example application that demonstrates creating a running background thread and that demonstrates selectively destroying bean definitions based on some criteria (in this case, the type of the bean)
package com.example.lifecycle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@joshlong
joshlong / main.kts
Created November 12, 2024 20:54
an example demonstrating how nice Kotlin can be
data class Point(val x: Int, val y: Int)
data class Rectangle(val width: Int, val height: Int)
data class Circle(val radius: Int)
fun describePoint(point: Point) = when (point) {
Point(0, 0) -> "Origin"
Point(1, 2) -> "1,2"
else -> throw IllegalStateException("Unexpected value: $point")
}
@joshlong
joshlong / Main.java
Last active April 17, 2025 04:54
a simple Java 23 program demonstrating some of the things showing how nice modern Java can be, based on the C# equivalent shown in https://youtu.be/1I3mWSiWNsI?feature=shared&t=374
// sdk install java 23.0.1-librca && sdk use java 23.0.1-librca
// java --enable-preview Main.java
record Point(int x, int y) {}
sealed interface Shape permits Rectangle, Circle {}
record Rectangle(int width, int height) implements Shape {}
record Circle(int radius) implements Shape {}
void main() {
var point = new Point(1, 2);
println(describePoint(point));
@joshlong
joshlong / DynamicDurationClientHttpRequestFactory.java
Created September 19, 2024 11:34
dynamically set the timeout for each request
package org.springframework.http.client;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.time.Duration;
@joshlong
joshlong / ConvertAuthorities.java
Created September 4, 2024 09:50
demonstrates how to change the authorities of the incoming token to reflect reconciled user state
package com.example.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;