forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceRoutesExtensions.cs
More file actions
159 lines (138 loc) · 6.39 KB
/
ServiceRoutesExtensions.cs
File metadata and controls
159 lines (138 loc) · 6.39 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using ServiceStack.Common;
using ServiceStack.Common.Utils;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
using ServiceStack.Text;
namespace ServiceStack.ServiceInterface
{
public static class ServiceRoutesExtensions
{
/// <summary>
/// Scans the supplied Assemblies to infer REST paths and HTTP verbs.
/// </summary>
///<param name="routes">The <see cref="IServiceRoutes"/> instance.</param>
///<param name="assembliesWithServices">
/// The assemblies with REST services.
/// </param>
/// <returns>The same <see cref="IServiceRoutes"/> instance;
/// never <see langword="null"/>.</returns>
public static IServiceRoutes AddFromAssembly(this IServiceRoutes routes,
params Assembly[] assembliesWithServices)
{
foreach (Assembly assembly in assembliesWithServices)
{
IEnumerable<Type> services =
from t in assembly.GetExportedTypes()
where
!t.IsAbstract &&
t.IsSubclassOfRawGeneric(typeof(ServiceBase<>))
select t;
foreach (Type service in services)
{
Type baseType = service.BaseType;
//go up the hierarchy to the first generic base type
while (!baseType.IsGenericType)
{
baseType = baseType.BaseType;
}
Type requestType = baseType.GetGenericArguments()[0];
string allowedVerbs = null; //null == All Routes
if (service.IsSubclassOfRawGeneric(typeof(RestServiceBase<>)))
{
//find overriden REST methods
var allowedMethods = new List<string>();
if (service.GetMethod("OnGet").DeclaringType == service)
{
allowedMethods.Add(HttpMethods.Get);
}
if (service.GetMethod("OnPost").DeclaringType == service)
{
allowedMethods.Add(HttpMethods.Post);
}
if (service.GetMethod("OnPut").DeclaringType == service)
{
allowedMethods.Add(HttpMethods.Put);
}
if (service.GetMethod("OnDelete").DeclaringType == service)
{
allowedMethods.Add(HttpMethods.Delete);
}
if (service.GetMethod("OnPatch").DeclaringType == service)
{
allowedMethods.Add(HttpMethods.Patch);
}
if (allowedMethods.Count == 0) continue;
allowedVerbs = string.Join(" ", allowedMethods.ToArray());
}
routes.Add(requestType, requestType.Name, allowedVerbs, null);
var hasIdField = requestType.GetProperty(IdUtils.IdField) != null;
if (hasIdField)
{
var routePath = requestType.Name + "/{" + IdUtils.IdField + "}";
routes.Add(requestType, routePath, allowedVerbs, null);
}
}
}
return routes;
}
public static IServiceRoutes Add<TRequest>(this IServiceRoutes routes, string restPath, ApplyTo verbs)
{
return routes.Add<TRequest>(restPath, verbs.ToVerbsString());
}
public static IServiceRoutes Add(this IServiceRoutes routes, Type requestType, string restPath, ApplyTo verbs)
{
return routes.Add(requestType, restPath, verbs.ToVerbsString(), null);
}
private static string ToVerbsString(this ApplyTo verbs)
{
var allowedMethods = new List<string>();
if (verbs.Has(ApplyTo.Get))
allowedMethods.Add(HttpMethods.Get);
if (verbs.Has(ApplyTo.Post))
allowedMethods.Add(HttpMethods.Post);
if (verbs.Has(ApplyTo.Put))
allowedMethods.Add(HttpMethods.Put);
if (verbs.Has(ApplyTo.Delete))
allowedMethods.Add(HttpMethods.Delete);
if (verbs.Has(ApplyTo.Patch))
allowedMethods.Add(HttpMethods.Patch);
if (verbs.Has(ApplyTo.Options))
allowedMethods.Add(HttpMethods.Options);
if (verbs.Has(ApplyTo.Head))
allowedMethods.Add(HttpMethods.Head);
return string.Join(" ", allowedMethods.ToArray());
}
public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic)
{
while (toCheck != typeof(object))
{
Type cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur)
{
return true;
}
toCheck = toCheck.BaseType;
}
return false;
}
private static string FormatRoute<T>(string restPath, params Expression<Func<T, object>>[] propertyExpressions)
{
var properties = propertyExpressions.Select(x => string.Format("{{{0}}}", PropertyName(x))).ToArray();
return string.Format(restPath, properties);
}
private static string PropertyName(LambdaExpression lambdaExpression)
{
return (lambdaExpression.Body is UnaryExpression ? (MemberExpression)((UnaryExpression)lambdaExpression.Body).Operand : (MemberExpression)lambdaExpression.Body).Member.Name;
}
public static void Add<T>(this IServiceRoutes serviceRoutes, string restPath, ApplyTo verbs, params Expression<Func<T, object>>[] propertyExpressions)
{
serviceRoutes.Add<T>(FormatRoute(restPath, propertyExpressions), verbs);
}
}
}