Compare commits
2 commits
7e871f824c
...
5c43347710
Author | SHA1 | Date | |
---|---|---|---|
5c43347710 | |||
7b40c6edea |
8 changed files with 143 additions and 11 deletions
Binary file not shown.
|
@ -46,6 +46,8 @@ public class Configuration {
|
||||||
private String jwt_key;
|
private String jwt_key;
|
||||||
|
|
||||||
private String sql_folder;
|
private String sql_folder;
|
||||||
|
|
||||||
|
private int event_chapter;
|
||||||
|
|
||||||
private File _file;
|
private File _file;
|
||||||
|
|
||||||
|
@ -228,4 +230,8 @@ public class Configuration {
|
||||||
public String getSqlFolder(){
|
public String getSqlFolder(){
|
||||||
return this.sql_folder;
|
return this.sql_folder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getEventChapter(){
|
||||||
|
return this.event_chapter;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -93,7 +93,6 @@ public class Main{
|
||||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
context.response(200);
|
context.response(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
initRoutes(router, repo, config);
|
initRoutes(router, repo, config);
|
||||||
|
@ -141,16 +140,36 @@ public class Main{
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
|
||||||
|
new Thread(new Runnable(){
|
||||||
|
public void run(){
|
||||||
|
Key key = new Key();
|
||||||
|
|
||||||
|
Locker<Throwable> locker = router.getExceptionLogger();
|
||||||
|
|
||||||
|
locker.init(key);
|
||||||
|
try {
|
||||||
|
while(true){
|
||||||
|
locker.lock(key);
|
||||||
|
locker.getValue(key).printStackTrace();
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
locker.remove(key);
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
if(config.useSsl()) router.configureSSL(config.getSslKeystore(), config.getSslKeystorePasswd());
|
if(config.useSsl()) router.configureSSL(config.getSslKeystore(), config.getSslKeystorePasswd());
|
||||||
router.listen(config.getTcpPort(), config.useSsl());
|
router.listen(config.getTcpPort(), config.useSsl());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void initRoutes(Router<PeerAtUser> router, DatabaseRepository repo, Configuration config) throws Exception{
|
private static void initRoutes(Router<PeerAtUser> router, DatabaseRepository repo, Configuration config) throws Exception{
|
||||||
router.registerPackages(new DependencyInjector()
|
router.registerPackages("dev.peerat.backend.routes",new DependencyInjector()
|
||||||
.of(repo, router, config, new RouteExtracter(router))
|
.of(repo, router, config, new RouteExtracter(router), config.getMail())
|
||||||
.of("waitting", new HashMap<>())
|
.of("waitting", new HashMap<>())
|
||||||
.of("leaderboard", new Locker<>())
|
.of("leaderboard", new Locker<>())
|
||||||
.of("groups", new Locker<>()));
|
.of("groups", new Locker<>())
|
||||||
|
.of("groupMessages", new Locker<>()));
|
||||||
|
|
||||||
// Bot bot = new Bot(config, repo, groupLock);
|
// Bot bot = new Bot(config, repo, groupLock);
|
||||||
// bot.start();
|
// bot.start();
|
||||||
|
|
75
src/dev/peerat/backend/routes/EventSSE.java
Normal file
75
src/dev/peerat/backend/routes/EventSSE.java
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
package dev.peerat.backend.routes;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.model.Group;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Injection;
|
||||||
|
import dev.peerat.framework.Locker;
|
||||||
|
import dev.peerat.framework.Locker.Key;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
|
||||||
|
public class EventSSE{
|
||||||
|
|
||||||
|
private Locker<GroupMessage> locker;
|
||||||
|
private DatabaseGroupRepository repo;
|
||||||
|
private int eventChapter;
|
||||||
|
|
||||||
|
public EventSSE(@Injection("groupMessages") Locker<GroupMessage> locker, DatabaseRepository repo, Configuration config){
|
||||||
|
this.locker = locker;
|
||||||
|
this.repo = repo.getGroupRepository();
|
||||||
|
this.eventChapter = config.getEventChapter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/group/event/$", needLogin = true)
|
||||||
|
public void connect(Context context, HttpWriter writer) throws Exception {
|
||||||
|
Group group = repo.getPlayerGroup(context.<PeerAtUser>getUser().getId(), this.eventChapter);
|
||||||
|
if(group == null){
|
||||||
|
context.response(401);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
context.response(200, "Content-Type: text/event-stream");
|
||||||
|
|
||||||
|
Key key = new Key();
|
||||||
|
|
||||||
|
locker.init(key);
|
||||||
|
try {
|
||||||
|
while(true){
|
||||||
|
GroupMessage message = locker.getValue(key);
|
||||||
|
if(message.getGroup() == null || message.getGroup().equals(group.getName())){
|
||||||
|
writer.write("data: "+message.getMessage()+"\n\n");
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
locker.lock(key);
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
locker.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class GroupMessage{
|
||||||
|
|
||||||
|
private String group;
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
public GroupMessage(String group, String message){
|
||||||
|
this.group = group;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGroup(){
|
||||||
|
return this.group;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage(){
|
||||||
|
return this.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,7 +17,7 @@ public class Swagger implements Response{
|
||||||
|
|
||||||
public Swagger(RouteExtracter extracter, Configuration config){
|
public Swagger(RouteExtracter extracter, Configuration config){
|
||||||
try{
|
try{
|
||||||
this.json = extracter.swagger(config.getTokenIssuer()).toString();
|
// this.json = extracter.swagger(config.getTokenIssuer()).toString();
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
json = "{}";
|
json = "{}";
|
||||||
|
|
31
src/dev/peerat/backend/routes/admins/GroupController.java
Normal file
31
src/dev/peerat/backend/routes/admins/GroupController.java
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package dev.peerat.backend.routes.admins;
|
||||||
|
|
||||||
|
import dev.peerat.backend.routes.EventSSE.GroupMessage;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.Injection;
|
||||||
|
import dev.peerat.framework.Locker;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class GroupController {
|
||||||
|
|
||||||
|
private Locker<GroupMessage> locker;
|
||||||
|
|
||||||
|
public GroupController(@Injection("groupMessages") Locker<GroupMessage> locker){
|
||||||
|
this.locker = locker;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/event/$", needLogin = true)
|
||||||
|
public void send(Context context, HttpReader reader) throws Exception{
|
||||||
|
JsonMap json = reader.readJson();
|
||||||
|
if(!json.has("message")){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
locker.setValue(new GroupMessage(json.get("group"), json.get("message")));
|
||||||
|
context.response(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -29,6 +29,7 @@ import javax.net.ssl.HttpsURLConnection;
|
||||||
import org.jose4j.json.internal.json_simple.JSONAware;
|
import org.jose4j.json.internal.json_simple.JSONAware;
|
||||||
import org.jose4j.json.internal.json_simple.JSONObject;
|
import org.jose4j.json.internal.json_simple.JSONObject;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
import dev.peerat.backend.model.PeerAtUser;
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
||||||
|
@ -58,15 +59,14 @@ public class MailConfirmation extends FormResponse{
|
||||||
public MailConfirmation(
|
public MailConfirmation(
|
||||||
DatabaseRepository databaseRepo,
|
DatabaseRepository databaseRepo,
|
||||||
Router<PeerAtUser> router,
|
Router<PeerAtUser> router,
|
||||||
@Injection("usersFiles") String initUsersFilesPath,
|
Configuration config,
|
||||||
@Injection("gitToken") String gitToken,
|
|
||||||
@Injection("waitting") Map<String, String> playersWaiting,
|
@Injection("waitting") Map<String, String> playersWaiting,
|
||||||
Mail mail) throws NoSuchAlgorithmException{
|
Mail mail) throws NoSuchAlgorithmException{
|
||||||
|
|
||||||
this.databaseRepo = databaseRepo.getAuthRepository();
|
this.databaseRepo = databaseRepo.getAuthRepository();
|
||||||
this.router = router;
|
this.router = router;
|
||||||
this.usersFilesPath = initUsersFilesPath;
|
this.usersFilesPath = config.getUsersFiles();
|
||||||
this.gitToken = gitToken;
|
this.gitToken = config.getGitToken();
|
||||||
this.playersWaiting = playersWaiting;
|
this.playersWaiting = playersWaiting;
|
||||||
this.mail = mail;
|
this.mail = mail;
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import java.util.Random;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
||||||
import dev.peerat.backend.repository.DatabaseRepository;
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
@ -31,11 +32,11 @@ public class Register extends FormResponse{
|
||||||
private String host;
|
private String host;
|
||||||
private List<Random> randoms;
|
private List<Random> randoms;
|
||||||
|
|
||||||
public Register(DatabaseRepository databaseRepo, @Injection("waitting") Map<String, String> playersWaiting, Mail mail, @Injection("issuer") String host){
|
public Register(DatabaseRepository databaseRepo, @Injection("waitting") Map<String, String> playersWaiting, Mail mail, Configuration config){
|
||||||
this.databaseRepo = databaseRepo.getAuthRepository();
|
this.databaseRepo = databaseRepo.getAuthRepository();
|
||||||
this.playersWaiting = playersWaiting;
|
this.playersWaiting = playersWaiting;
|
||||||
this.mail = mail;
|
this.mail = mail;
|
||||||
this.host = host;
|
this.host = config.getTokenIssuer();
|
||||||
this.randoms = new ArrayList<>();
|
this.randoms = new ArrayList<>();
|
||||||
|
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
|
|
Loading…
Add table
Reference in a new issue