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
185 lines (159 loc) · 7.02 KB
/
Copy pathServiceRoutesExtensions.cs
File metadata and controls
185 lines (159 loc) · 7.02 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ServiceStack.Common;
using ServiceStack.Common.Utils;
using ServiceStack.Common.Web;
using ServiceStack.Text;
using ServiceStack.ServiceHost;
using ServiceStack.WebHost.Endpoints;
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)
{
AddOldApiRoutes(routes, assembly);
AddNewApiRoutes(routes, assembly);
}
return routes;
}
private static void AddNewApiRoutes(IServiceRoutes routes, Assembly assembly)
{
var services = assembly.GetExportedTypes()
.Where(t => !t.IsAbstract
&& t.HasInterface(typeof(IService)));
foreach (Type service in services)
{
var allServiceActions = service.GetActions();
foreach (var requestDtoActions in allServiceActions.GroupBy(x => x.GetParameters()[0].ParameterType))
{
var requestType = requestDtoActions.Key;
var hasWildcard = requestDtoActions.Any(x => x.Name.EqualsIgnoreCase(ActionContext.AnyAction));
string allowedVerbs = null; //null == All Routes
if (!hasWildcard)
{
var allowedMethods = new List<string>();
foreach (var action in requestDtoActions)
{
allowedMethods.Add(action.Name.ToUpper());
}
if (allowedMethods.Count == 0) continue;
allowedVerbs = string.Join(" ", allowedMethods.ToArray());
}
routes.AddRoute(requestType, allowedVerbs);
}
}
}
private static void AddOldApiRoutes(IServiceRoutes routes, Assembly assembly)
{
var services = assembly.GetExportedTypes()
.Where(t => !t.IsAbstract
&& t.IsSubclassOfRawGeneric(typeof(ServiceBase<>)));
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.AddRoute(requestType, allowedVerbs);
}
}
private static void AddRoute(this IServiceRoutes routes, Type requestType, string allowedVerbs)
{
foreach (var strategy in EndpointHost.Config.RouteNamingConventions)
{
strategy(routes, requestType, allowedVerbs);
}
}
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());
}
private static string ToVerbsString(this ApplyTo verbs)
{
var allowedMethods = new List<string>();
foreach (var entry in ApplyToUtils.ApplyToVerbs)
{
if (verbs.Has(entry.Key))
allowedMethods.Add(entry.Value);
}
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 IServiceRoutes Add<T>(this IServiceRoutes serviceRoutes, string restPath, ApplyTo verbs, params Expression<Func<T, object>>[] propertyExpressions)
{
return serviceRoutes.Add<T>(FormatRoute(restPath, propertyExpressions), verbs);
}
}
}