Chess Server Implementation Tips
CS – Advanced Programming Concepts
Server Class Structure
What do Web API handlers do?
Example Web API handler logic
…
…
LoginRequest request = (LoginRequest)gson.fromJson(reqData, LoginRequest.class);
LoginService service = new LoginService();
LoginResult result = service.login(request);
return gson.toJson(result);
…
…
Best to create a separate class with fromJson and toJson methods instead of calling Gson directly from handlers
How are Handlers Created and Invoked?
post("/user", context ->
(new RegisterHandler()).handleRequest(context));�
Note: There are multiple ways to avoid creating a new handler instance for every request:
Tip – Avoid code duplication
JSON Tips
class Response {
String message;
}
class LoginResponse extends Response {
String authtoken;
String username;
}
Generating an Auth Token
UUID.randomUUID().toString()
Server Implementation Approach