public async Task<string> GetResult(RequestInfo RequestInfo, int EndpointId, string ResponseContent) {

            //string fileContent = "module.exports = function (cb, req) { cb(null, JSON.stringify({some:'obj'}));};";
            string fileContent = "module.exports = " + ResponseContent + ";";
            using (StringAsTempFile temp = new Microsoft.AspNet.NodeServices.StringAsTempFile(fileContent)) {

                try {
                    return await this.nodeServices.Invoke<string>(temp.FileName, RequestInfo);
                } catch (Exception ex) {
                    return ex.ToString();
                }
            }
        }
示例#2
0
        public async Task<string> Index(string pathInfo/*FRAGILE: matches route setup */) {

            // TODO: how to pass the found Endpoint from the route constraint to the controller?

            string method = this.HttpContext.Request.Method;
            string host = this.HttpContext.Request.Host.ToString();
            string url = this.HttpContext.Request.Path;

            if (this.env.IsDevelopment()) {
                host = "one.api-inator.com"; // TODO: switch/case on port?
            }
            string subdomain = this.subdomainRegex.Replace(host, "");

            Endpoint endpoint = this.endpointRepository.GetMatch(subdomain, method, url);

            this.Response.ContentType = endpoint.ContentType;
            this.Response.StatusCode = endpoint.StatusCode;
            
            string response = null;
            switch (endpoint.ResponseType) {
                case ResponseType.Static:
                    response = endpoint.ResponseContent;
                    break;
                case ResponseType.JavaScript:
                    RequestInfo requestInfo = new RequestInfo {
                        Method = method,
                        Path = url,
                        Headers = (
                            from q in this.Request.Headers.Keys
                            select new Tuple<string, string>(q, this.Request.Query[q])
                        ).ToDictionary(t => t.Item1, t => t.Item2),
                        Query = (
                            from q in this.Request.Query.Keys
                            select new Tuple<string, string>(q, this.Request.Query[q])
                        ).ToDictionary(t => t.Item1, t => t.Item2)
                    };
                    response = await this.javaScriptCompileHelper.GetResult(requestInfo, endpoint.EndpointId, endpoint.ResponseContent);
                    break;
                case ResponseType.CSharp:
                    response = this.csharpCompileHelper.GetResult(Request, endpoint.ResponseContent);
                    break;
            }
            
            return response;
        }