-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathApiHandlers.cs
More file actions
51 lines (46 loc) · 2.24 KB
/
Copy pathApiHandlers.cs
File metadata and controls
51 lines (46 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using ServiceStack.Web;
using ServiceStack.Host.Handlers;
namespace ServiceStack;
/// <summary>
/// Add a new API Handler at a custom route.
///
/// RawHttpHandlers.Add(ApiHandlers.Json("/api/{Request}")) => delegates /api/* requests to JSON Request Handler, e.g:
/// - /api/Hello => {"result":"Hello"}
/// - /api/Hello?name=World => {"result":"Hello, World"}
/// </summary>
public static class ApiHandlers
{
public static Func<IHttpRequest, HttpAsyncTaskHandler> Json(string apiPath) =>
Generic(apiPath, MimeTypes.Json, RequestAttributes.Reply | RequestAttributes.Json, Feature.Json);
public static Func<IHttpRequest, HttpAsyncTaskHandler> Jsv(string apiPath) =>
Generic(apiPath, MimeTypes.Jsv, RequestAttributes.Reply | RequestAttributes.Jsv, Feature.Jsv);
public static Func<IHttpRequest, HttpAsyncTaskHandler> Csv(string apiPath) =>
Generic(apiPath, MimeTypes.Csv, RequestAttributes.Reply | RequestAttributes.Csv, Feature.Csv);
public static Func<IHttpRequest, HttpAsyncTaskHandler> Xml(string apiPath) =>
Generic(apiPath, MimeTypes.Xml, RequestAttributes.Reply | RequestAttributes.Xml, Feature.Xml);
public static Func<IHttpRequest, HttpAsyncTaskHandler> Generic(string apiPath,
string contentType, RequestAttributes requestAttributes, Feature feature)
{
if (string.IsNullOrEmpty(apiPath))
throw new ArgumentNullException(nameof(apiPath));
if (apiPath[0] != '/')
throw new ArgumentException(apiPath + " must start with '/'");
if (!apiPath.EndsWith("/{Request}"))
throw new ArgumentException(apiPath + " must end with '/{Request}'");
var useApiPath = apiPath.LastLeftPart('/') + '/';
return req => {
// Don't handle OPTIONS CORS requests
if (req.HttpMethod == HttpMethods.Options) return null;
var pathInfo = req.PathInfo;
if (pathInfo.StartsWith(useApiPath))
{
var apiName = pathInfo.LastRightPart('/');
return new GenericHandler(contentType, requestAttributes, feature) {
RequestName = apiName
};
}
return null;
};
}
}