mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Merge pull request #16 from B0pol/refactoring
add status code, mimetype to response
This commit is contained in:
commit
7de0cb1115
1 changed files with 53 additions and 13 deletions
|
@ -1,5 +1,6 @@
|
||||||
package me.kavin.piped;
|
package me.kavin.piped;
|
||||||
|
|
||||||
|
import io.netty.util.AsciiString;
|
||||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
import org.schabi.newpipe.extractor.NewPipe;
|
import org.schabi.newpipe.extractor.NewPipe;
|
||||||
import org.schabi.newpipe.extractor.localization.Localization;
|
import org.schabi.newpipe.extractor.localization.Localization;
|
||||||
|
@ -20,6 +21,11 @@ import reactor.netty.NettyOutbound;
|
||||||
import reactor.netty.http.server.HttpServer;
|
import reactor.netty.http.server.HttpServer;
|
||||||
import reactor.netty.http.server.HttpServerResponse;
|
import reactor.netty.http.server.HttpServerResponse;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import static io.netty.handler.codec.http.HttpHeaderNames.*;
|
||||||
|
import static io.netty.handler.codec.http.HttpHeaderValues.*;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
@ -34,7 +40,8 @@ public class Main {
|
||||||
QueryStringDecoder query = new QueryStringDecoder(req.uri());
|
QueryStringDecoder query = new QueryStringDecoder(req.uri());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return writeResponse(res, query.parameters().get("hub.challenge").get(0), 200, "private", start);
|
return writeResponse(res, query.parameters().get("hub.challenge").get(0), TEXT_PLAIN, 200, "private", start);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start);
|
return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start);
|
||||||
|
@ -60,7 +67,7 @@ public class Main {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return writeResponse(res, "ok", 200, "private", start);
|
return writeResponse(res, "ok", TEXT_PLAIN, 200, "private", start);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start);
|
return writeResponse(res, ExceptionUtils.getStackTrace(e), 500, "private", start);
|
||||||
|
@ -219,20 +226,53 @@ public class Main {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NettyOutbound writeResponse(HttpServerResponse res, String resp, int code, String cache, long time) {
|
public static NettyOutbound writeResponse(HttpServerResponse res, String resp, int code, String cache, long time) {
|
||||||
return res.compression(true).addHeader("Access-Control-Allow-Origin", "*").addHeader("Cache-Control", cache)
|
return writeResponse(res, resp, APPLICATION_JSON, code, cache, time);
|
||||||
.addHeader("Server-Timing", "app;dur=" + (System.nanoTime() - time) / 1000000.0)
|
|
||||||
.send(ByteBufFlux.fromString(Flux.just(resp), java.nio.charset.StandardCharsets.UTF_8,
|
|
||||||
ByteBufAllocator.DEFAULT));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static NettyOutbound writeResponse(HttpServerResponse res, String resp, AsciiString mimeType, int code, String cache, long time) {
|
||||||
|
return writeResponse(res, resp, mimeType.toString(), code, cache, time);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NettyOutbound writeResponse(HttpServerResponse res, String resp, String mimeType, int code, String cache, long time) {
|
||||||
|
return writeResponse(res, resp.getBytes(StandardCharsets.UTF_8), mimeType, code, cache, time);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, int code, String cache, long time) {
|
public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, int code, String cache, long time) {
|
||||||
return res.compression(true).addHeader("Access-Control-Allow-Origin", "*").addHeader("Cache-Control", cache)
|
return writeResponse(res, resp, APPLICATION_JSON, code, cache, time);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, AsciiString mimeType, int code, String cache, long time) {
|
||||||
|
return writeResponse(res, resp, mimeType.toString(), code, cache, time);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NettyOutbound writeResponse(HttpServerResponse res, byte[] resp, String mimeType, int code, String cache, long time) {
|
||||||
|
return res.compression(true)
|
||||||
|
.status(code)
|
||||||
|
.addHeader(CONTENT_TYPE, mimeType)
|
||||||
|
.addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
|
||||||
|
.addHeader(CACHE_CONTROL, cache)
|
||||||
.addHeader("Server-Timing", "app;dur=" + (System.nanoTime() - time) / 1000000.0)
|
.addHeader("Server-Timing", "app;dur=" + (System.nanoTime() - time) / 1000000.0)
|
||||||
.sendByteArray(Flux.just(resp));
|
.sendByteArray(Flux.just(resp));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static NettyOutbound writeResponse(HttpServerResponse res, Flux<String> resp, int code, String cache) {
|
public static NettyOutbound writeResponse(HttpServerResponse res, Flux<String> resp, int code, String cache) {
|
||||||
return res.compression(true).addHeader("Access-Control-Allow-Origin", "*").addHeader("Cache-Control", cache)
|
return writeResponse(res, resp, APPLICATION_JSON, code, cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NettyOutbound writeResponse(HttpServerResponse res, Flux<String> resp, AsciiString mimeType, int code, String cache) {
|
||||||
|
return writeResponse(res, resp, mimeType.toString(), code, cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NettyOutbound writeResponse(HttpServerResponse res, Flux<String> resp, String mimeType, int code, String cache) {
|
||||||
|
return res.compression(true)
|
||||||
|
.status(code)
|
||||||
|
.addHeader(CONTENT_TYPE, mimeType)
|
||||||
|
.addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
|
||||||
|
.addHeader(CACHE_CONTROL, cache)
|
||||||
.send(ByteBufFlux.fromString(resp, java.nio.charset.StandardCharsets.UTF_8, ByteBufAllocator.DEFAULT));
|
.send(ByteBufFlux.fromString(resp, java.nio.charset.StandardCharsets.UTF_8, ByteBufAllocator.DEFAULT));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue