Add registered users badge.

This commit is contained in:
FireMaskterK 2021-11-11 11:35:50 +00:00
parent 1c8e8b190e
commit e1e5382959
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 19 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress;
import static io.activej.http.HttpHeaders.AUTHORIZATION;
import static io.activej.http.HttpHeaders.CACHE_CONTROL;
import static io.activej.http.HttpHeaders.CONTENT_TYPE;
import static io.activej.http.HttpHeaders.LOCATION;
import static io.activej.http.HttpMethod.GET;
import static io.activej.http.HttpMethod.POST;
@ -272,6 +273,13 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(GET, "/registered/badge", AsyncServlet.ofBlocking(executor, request -> {
try {
return HttpResponse.ofCode(302).withHeader(LOCATION, ResponseHelper.registeredBadgeRedirect())
.withHeader(CACHE_CONTROL, "public, max-age=30");
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
}));
return new CustomServletDecorator(router);

View file

@ -967,6 +967,17 @@ public class ResponseHelper {
}
public static final String registeredBadgeRedirect() {
Session s = DatabaseSessionFactory.createSession();
long registered = ((Long) s.createQuery("select count(*) from User").uniqueResult()).longValue();
s.close();
return String.format("https://img.shields.io/badge/Registered%%20Users-%s-blue", String.valueOf(registered));
}
private static final String getLBRYId(String videoId) throws IOException, InterruptedException {
return new JSONObject(Constants.h2client.send(HttpRequest
.newBuilder(URI.create("https://api.lbry.com/yt/resolve?video_ids=" + URLUtils.silentEncode(videoId)))