Last active
July 20, 2021 11:21
-
-
Save Azahe/046b5eb2e69ea432e1bef97047deee19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
class Scratch { | |
public static void main(String[] args) { | |
// in Intelij scratch couldn't get static imports to work - probably needs to be in a package | |
ServerInstance server = Javalin.create( | |
Routing.path("/api", | |
Routing.get("/users", UserController::getAll), | |
Routing.post("/users", UserController::create), | |
Routing.path("/users", | |
Routing.get("/:user-id", UserController::getOne), | |
Routing.patch("/:user-id", UserController::update), | |
Routing.delete("/:user-id", UserController::delete) | |
) | |
) | |
); | |
} | |
} | |
class Javalin { | |
public static ServerInstance create(ServerInstanceMutator routing) { // potentially vararg | |
ServerInstance server = new ServerInstance(); | |
routing.applyTo(server); // could also be server.resolve(routing); | |
return server; | |
} | |
} | |
class Routing implements ServerInstanceMutator { | |
public static ServerInstanceMutator path(String path, ServerInstanceMutator... subRoutings) { | |
return new CompositeRouting(path, List.of(subRoutings)); | |
} | |
public static ServerInstanceMutator get(String path, Runnable mapping) { | |
return new Routing(path, mapping); | |
} | |
public static ServerInstanceMutator post(String path, Runnable mapping) { | |
return new Routing(path, mapping); | |
} | |
public static ServerInstanceMutator patch(String path, Runnable mapping) { | |
return new Routing(path, mapping); | |
} | |
public static ServerInstanceMutator delete(String path, Runnable mapping) { | |
return new Routing(path, mapping); | |
} | |
private final String path; | |
private final Runnable mapping; | |
public Routing(String path, Runnable mapping) { | |
this.path = path; | |
this.mapping = mapping; | |
} | |
// should probably be somehow encapsulated - like maybe not *public* | |
public void applyTo(ServerInstance context) { | |
context.resolve(path, mapping); | |
} | |
} | |
class CompositeRouting implements ServerInstanceMutator { | |
private final String path; | |
private final List<ServerInstanceMutator> subRoutings; | |
public CompositeRouting(String path, List<ServerInstanceMutator> subRoutings) { | |
this.path = path; | |
this.subRoutings = subRoutings; | |
} | |
public void applyTo(ServerInstance context) { | |
for (ServerInstanceMutator route : subRoutings) { | |
route.applyTo(context.subContext(path)); | |
} | |
} | |
} | |
interface ServerInstanceMutator { | |
void applyTo(ServerInstance context); | |
} | |
class ServerInstance { | |
public ServerInstance subContext(String path) { | |
throw new RuntimeException("TODO"); | |
} | |
public void resolve(String path, Runnable mapping) { | |
throw new RuntimeException("TODO"); | |
} | |
} | |
class UserController { | |
public static void getAll() {} | |
public static void create() {} | |
public static void getOne() {} | |
public static void update() {} | |
public static void delete() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment