This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.Run(async (context) => | |
{ | |
await context.Response.WriteAsync("Hello, World!"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Router router = new Router("routes.json"); | |
app.Run(async (context) => | |
{ | |
var content = await router.RouteRequest(context.Request); | |
await context.Response.WriteAsync(await content.Content.ReadAsStringAsync()); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"routes": [ | |
{ | |
"endpoint": "/movies", | |
"destination": { | |
"uri": "http://localhost:8090/movies/", | |
"requiresAuthentication": "true" | |
} | |
}, | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Destination | |
{ | |
public string Uri { get; set; } | |
public bool RequiresAuthentication { get; set; } | |
static HttpClient client = new HttpClient(); | |
public Destination(string uri, bool requiresAuthentication) | |
{ | |
Uri = path; | |
RequiresAuthentication = requiresAuthentication; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private string CreateDestinationUri(HttpRequest request) | |
{ | |
string requestPath = request.Path.ToString(); | |
string queryString = request.QueryString.ToString(); | |
string endpoint = ""; | |
string[] endpointSplit = requestPath.Substring(1).Split('/'); | |
if (endpointSplit.Length > 1) | |
endpoint = endpointSplit[1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<HttpResponseMessage> SendRequest(HttpRequest request) | |
{ | |
string requestContent; | |
using (Stream receiveStream = request.Body) | |
{ | |
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) | |
{ | |
requestContent = readStream.ReadToEnd(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JsonLoader | |
{ | |
public static T LoadFromFile<T>(string filePath) | |
{ | |
using (StreamReader reader = new StreamReader(filePath)) | |
{ | |
string json = reader.ReadToEnd(); | |
T result = JsonConvert.DeserializeObject<T>(json); | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Route | |
{ | |
public string Endpoint { get; set; } | |
public Destination Destination { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Router | |
{ | |
public List<Route> Routes { get; set; } | |
public Destination AuthenticationService { get; set; } | |
public Router(string routeConfigFilePath) | |
{ | |
dynamic router = JsonLoader.LoadFromFile<dynamic>(routeConfigFilePath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<HttpResponseMessage> RouteRequest(HttpRequest request) | |
{ | |
string path = request.Path.ToString(); | |
string basePath = '/' + path.Split('/')[1]; | |
Destination destination; | |
try | |
{ | |
destination = Routes.First(r => r.Endpoint.Equals(basePath)).Destination; | |
} |
OlderNewer