Program.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System.Text;
  2. using System.Text.Json;
  3. using Sisk.Cadente;
  4. var host = new HttpHost ( 8080, session => {
  5. var request = session.Request;
  6. if (request.Path == "/plaintext") {
  7. SerializePlainTextResponse ( session.Response );
  8. }
  9. else if (request.Path == "/json") {
  10. SerializeJsonResponse ( session.Response );
  11. }
  12. else {
  13. session.Response.StatusCode = 404;
  14. }
  15. } );
  16. host.Start ();
  17. Thread.Sleep ( Timeout.Infinite );
  18. static void SerializePlainTextResponse ( HttpResponse response ) {
  19. var contentBytes = Encoding.UTF8.GetBytes ( "Hello, world!" );
  20. response.Headers.Add ( new HttpHeader ( "Content-Type", "text/plain" ) );
  21. response.ResponseStream = new MemoryStream ( contentBytes );
  22. }
  23. static void SerializeJsonResponse ( HttpResponse response ) {
  24. var contentBytes = JsonSerializer.SerializeToUtf8Bytes ( new {
  25. message = "Hello, world!"
  26. } );
  27. response.Headers.Add ( new HttpHeader ( "Content-Type", "application/json; charset=utf-8" ) );
  28. response.ResponseStream = new MemoryStream ( contentBytes );
  29. }