public ExcessOwinMiddleware( OwinMiddleware next, IDistributedApp server, __Scope scope, IEnumerable <Type> functions, IEnumerable <FilterFunction> filters) : base(next) { _server = server; _scope = scope; _functions = BuildFunctions(_scope, functions, filters); }
public void Run(__Scope __scope, Action <object> success, Action <Exception> failure) { HttpServer.Start(url: "http://*****:*****@"../../client/app ", threads: 4, except: new string[] {}, nodes: 0, assemblies: new[] { typeof(Development).Assembly }, filters: new Func <Func <string, IOwinRequest, __Scope, object>, Func <string, IOwinRequest, __Scope, object> >[] { prev => (data, request, scope) => { if (request.User != null) { scope.set <IPrincipal>(request.User); } return(prev(data, request, scope)); } }); }
public static void UseExcess(this IAppBuilder app, __Scope scope, Action <ConcurrentAppSettings> initializeSettings = null, Action <IDistributedApp> initializeApp = null, IEnumerable <Type> functional = null, IEnumerable <FilterFunction> filters = null) { var settings = new ConcurrentAppSettings(); initializeSettings?.Invoke(settings); var server = new DistributedApp(new ThreadedConcurrentApp( types: null, threadCount: settings.Threads, blockUntilNextEvent: settings.BlockUntilNextEvent)); initializeApp?.Invoke(server); app.Use <ExcessOwinMiddleware>(server, scope, functional, filters); server.Start(); }
public static TestServer CreateFunctionalServer(string code) { var errors = new List <Diagnostic>(); var assembly = buildServer(code, errors: errors); if (errors.Any()) { return(null); } var functionTypes = assembly .ExportedTypes .Where(type => type.Name == "Functions"); var scope = new __Scope(null); return(TestServer.Create(appBuilder => { appBuilder.UseExcess(scope, functional: functionTypes); })); }
private DapperMock(Assembly assembly, SQLiteConnection connection) { var container = assembly .GetTypes() .Single(type => type.Name == "Functions"); var methods = container .GetMethods() .Where(method => method.IsStatic && method.IsPublic && method.GetParameters().Length == 1); foreach (var method in methods) { _functions[method.Name] = () => { var scope = new __Scope(null); scope.set <IDbConnection>(connection); return(method.Invoke(null, new object[] { scope })); }; } }
public static string compile(string code, __Scope __scope) { ICodeTranspiler _transpiler = __scope.get<ICodeTranspiler>("_transpiler"); return _transpiler.Transpile(code); }
public static void __init(__Scope scope) { scope.set <IInstantiator>(new NinjectInstantiator()); }
private IDictionary <string, FunctionAction> BuildFunctions( __Scope appScope, IEnumerable <Type> functions, IEnumerable <FilterFunction> filters) { if (functions == null) { return(null); } var result = new Dictionary <string, FunctionAction>(); foreach (var functionObject in functions) { foreach (var method in functionObject.GetTypeInfo().DeclaredMethods) { var route = default(string); if (!hasRoute(method, out route)) { continue; } if (method.IsPublic) { var parameters = method .GetParameters(); var paramCount = parameters.Length; var paramNames = parameters .Take(paramCount) .Select(param => param.Name) .ToArray(); var paramTypes = parameters .Take(paramCount) .Select(param => param.ParameterType) .ToArray(); //build the running function ExecutorFunction eval = (data, request, scope) => { var args = JObject .Parse(data); var arguments = new object[paramCount]; for (int i = 0; i < paramCount - 1; i++) { arguments[i] = args .Property(paramNames[i]) .Value .ToObject(paramTypes[i]); } arguments[paramCount - 1] = scope; return(method.Invoke(null, arguments)); }; //apply any wrappers if (filters != null) { foreach (var filter in filters) { eval = filter.Invoke(eval); } } result[route] = (data, request, response, continuation) => { //set up the scope var scope = new __Scope(appScope); try { var responseValue = eval(data, request, scope); SendResponse(response, $"{{\"__res\": {JsonConvert.SerializeObject(responseValue)}}}", continuation); } catch (Exception ex) { SendError(response, ex, continuation); } }; } } } return(result); }
static public string Transpile(string text, __Scope __scope) { ICodeTranspiler _transpiler = __scope.get <ICodeTranspiler>("_transpiler"); return(_transpiler.Transpile(text)); }
public static void Start( string url, __Scope scope, string identityUrl = null, int threads = 4, string staticFiles = null, int nodes = 0, IEnumerable <Assembly> assemblies = null, IEnumerable <string> except = null, IEnumerable <FilterFunction> filters = null) { var instantiator = scope.get <IInstantiator>() ?? new DefaultInstantiator(); using (WebApp.Start(url, (builder) => { if (!string.IsNullOrWhiteSpace(staticFiles)) { if (!Directory.Exists(staticFiles)) { throw new ArgumentException(staticFiles); } staticFiles = Path.GetFullPath(staticFiles); var physicalFileSystem = new PhysicalFileSystem(staticFiles); var options = new FileServerOptions { EnableDefaultFiles = true, FileSystem = physicalFileSystem }; options.StaticFileOptions.FileSystem = physicalFileSystem; options.StaticFileOptions.ServeUnknownFileTypes = true; options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" }; builder.UseFileServer(options); } var functionTypes = assemblies .SelectMany(assembly => assembly .ExportedTypes .Where(type => type.Name == "Functions")); builder.UseExcess( scope, initializeApp: server => { Loader.FromAssemblies(server, assemblies, instantiator, except); if (nodes > 0) { throw new NotImplementedException(); //start the identity server if we have any nodes var error = null as Exception; var waiter = new ManualResetEvent(false); NetMQFunctions.StartServer(server, identityUrl, expectedClients: nodes, connected: ex => { error = ex; waiter.Set(); }); waiter.WaitOne(); //td: timeout if (error != null) { throw error; } } }, functional: functionTypes, filters: filters); })) { Console.WriteLine($"Excess server running @{url}..."); Console.ReadLine(); } }