L
This commit is contained in:
parent
0b18622add
commit
901c6f1512
3 changed files with 36 additions and 8 deletions
|
@ -5,6 +5,8 @@ import static be.jeffcheasey88.peeratcode.framework.RequestType.OPTIONS;
|
|||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import javax.net.ssl.SSLServerSocket;
|
||||
|
@ -64,14 +66,14 @@ public class Main{
|
|||
}
|
||||
});
|
||||
|
||||
initRoutes(router, config);
|
||||
// RouteExtracter extracter = new RouteExtracter(router);
|
||||
// extracter.extract();
|
||||
|
||||
startWebServer(config, router);
|
||||
startWebServer(config, router, initRoutes(router, config));
|
||||
}
|
||||
|
||||
private static void initRoutes(Router router, Configuration config){
|
||||
private static List<Locker<?>> initRoutes(Router router, Configuration config){
|
||||
List<Locker<?>> list = new ArrayList<>();
|
||||
router.register(new ChapterElement(router.getDataBase()));
|
||||
router.register(new ChapterList(router.getDataBase()));
|
||||
router.register(new PuzzleElement(router.getDataBase()));
|
||||
|
@ -83,12 +85,14 @@ public class Main{
|
|||
router.register(new BadgeDetails(router.getDataBase()));
|
||||
|
||||
Locker<Group> groupLock = new Locker<>();
|
||||
list.add(groupLock);
|
||||
router.register(new GroupCreate(router.getDataBase(), groupLock, config.getGroupJoinMinutes()));
|
||||
|
||||
DynamicLeaderboard dlb = new DynamicLeaderboard(router.getDataBase());
|
||||
router.register(dlb);
|
||||
|
||||
Locker<Completion> leaderboard = dlb.getLocker();
|
||||
list.add(leaderboard);
|
||||
|
||||
router.register(new PuzzleResponse(router.getDataBase(), config.getUsersFiles(), leaderboard));
|
||||
router.register(new GroupList(router.getDataBase()));
|
||||
|
@ -97,9 +101,11 @@ public class Main{
|
|||
|
||||
// Bot bot = new Bot(config, router.getDataBase(), groupLock);
|
||||
// bot.start();
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private static void startWebServer(Configuration config, Router router) throws IOException {
|
||||
private static void startWebServer(Configuration config, Router router, List<Locker<?>> lockers) throws IOException {
|
||||
if (config.useSsl()) { // Not needed with the use of a proxy
|
||||
SSLServerSocket server = null;
|
||||
try {
|
||||
|
@ -111,7 +117,7 @@ public class Main{
|
|||
|
||||
while (!server.isClosed()) {
|
||||
Socket socket = server.accept();
|
||||
Client client = new Client(socket, router);
|
||||
Client client = new Client(socket, router, lockers);
|
||||
client.start();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
@ -125,7 +131,7 @@ public class Main{
|
|||
try (ServerSocket server = new ServerSocket(config.getTcpPort())) {
|
||||
while (!server.isClosed()) {
|
||||
Socket socket = server.accept();
|
||||
Client client = new Client(socket, router);
|
||||
Client client = new Client(socket, router, lockers);
|
||||
client.start();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.framework;
|
|||
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.jose4j.jwa.AlgorithmConstraints.ConstraintType;
|
||||
import org.jose4j.jws.AlgorithmIdentifiers;
|
||||
|
@ -14,26 +15,36 @@ public class Client extends Thread{
|
|||
private HttpReader reader;
|
||||
private HttpWriter writer;
|
||||
private Router router;
|
||||
private List<Locker<?>> lockers;
|
||||
|
||||
public Client(Socket socket, Router router) throws Exception{
|
||||
public Client(Socket socket, Router router, List<Locker<?>> lockers) throws Exception{
|
||||
this.reader = new HttpReader(socket);
|
||||
this.writer = new HttpWriter(socket);
|
||||
this.router = router;
|
||||
this.lockers = lockers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
User user = null;
|
||||
try {
|
||||
String[] headers = reader.readLine().split("\\s");
|
||||
System.out.println(Arrays.toString(headers));
|
||||
reader.readHeaders();
|
||||
|
||||
router.exec(RequestType.valueOf(headers[0]), headers[1], isLogin(reader), reader, writer);
|
||||
router.exec(RequestType.valueOf(headers[0]), headers[1], user = isLogin(reader), reader, writer);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(user != null){
|
||||
if(user.getKey() != null){
|
||||
for(Locker<?> locker : this.lockers){
|
||||
locker.remove(user.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private User isLogin(HttpReader reader) throws Exception{
|
||||
|
|
|
@ -2,13 +2,24 @@ package be.jeffcheasey88.peeratcode.framework;
|
|||
|
||||
import org.jose4j.jwt.JwtClaims;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.framework.Locker.Key;
|
||||
|
||||
public class User{
|
||||
|
||||
private int id;
|
||||
private Key key;
|
||||
|
||||
public User(JwtClaims jwtClaims){
|
||||
this.id = ((Long) jwtClaims.getClaimValue("id")).intValue();
|
||||
}
|
||||
|
||||
public void setKey(Key key){
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public Key getKey(){
|
||||
return this.key;
|
||||
}
|
||||
|
||||
public int getId(){
|
||||
return this.id;
|
||||
|
|
Loading…
Add table
Reference in a new issue