예제 #1
0
        public static JsonRpcDoc GenerateJsonRpcDoc(JsonRpcInfo info)
        {
            var jsonRpcServices = AppDomain
                                  .CurrentDomain
                                  .GetAssemblies()
                                  .SelectMany(a => a.GetTypes().Where(t => !t.IsAbstract && typeof(JsonRpcWebSocketService).IsAssignableFrom(t)))
                                  .ToList();

            var jsonRpcDoc = new JsonRpcDoc(info);

            foreach (var jsonRpcService in jsonRpcServices)
            {
                jsonRpcDoc.Services.Add(GenerateJsonRpcServiceDoc(jsonRpcService, jsonRpcDoc));
            }

            return(jsonRpcDoc);
        }
예제 #2
0
        public static FileContent GetFile(string requestPath, JsonRpcInfo info)
        {
            var    basePath = info?.JsonRpcApiEndpoint ?? "/jsonrpc";
            string filePath;

            if (!basePath.StartsWith("/"))
            {
                basePath = "/" + basePath;
            }

            if (!requestPath.StartsWith(basePath))
            {
                return(new FileContent());
            }

            if (requestPath.EndsWith("/"))
            {
                requestPath = requestPath.Substring(0, requestPath.Length - 1);
            }
            if (requestPath.EndsWith("jsonRpcApi.json"))
            {
                var jsonRpcDoc = DocGenerator.GenerateJsonRpcDoc(info);

                var schemaSettings = new JsonSchemaGeneratorSettings
                {
                    SerializerSettings               = JsonSerializerSettings,
                    GenerateExamples                 = true,
                    SchemaType                       = SchemaType.JsonSchema,
                    GenerateAbstractSchemas          = false,
                    DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull
                };

                var schema = new JsonSchema();

                var resolver  = new JsonSchemaResolver(schema, schemaSettings);
                var generator = new JsonSchemaGenerator(schemaSettings);

                generator.Generate(schema, typeof(object), resolver);

                var jDoc = JObject.FromObject(jsonRpcDoc);
                // generate schema definitions
                foreach (var rpcService in jsonRpcDoc.Services)
                {
                    foreach (var rpcMethod in rpcService.Methods)
                    {
                        if (rpcMethod.Response.Type != typeof(void) && rpcMethod.Response.Type != typeof(Task))
                        {
                            generator.Generate(rpcMethod.Response.Type, resolver);
                        }

                        foreach (var rpcMethodParameter in rpcMethod.Parameters)
                        {
                            generator.Generate(rpcMethodParameter.Type, resolver);
                        }
                    }

                    foreach (var notification in rpcService.Notifications)
                    {
                        foreach (var parameter in notification.Parameters)
                        {
                            generator.Generate(parameter.Type, resolver);
                        }
                    }
                }

                var schemaJObject = JObject.Parse(schema.ToJson());
                jDoc["definitions"] = schemaJObject["definitions"];

                var buffer = Encoding.UTF8.GetBytes(jDoc.ToString(Formatting.None));

                var fileResult = new FileContent(requestPath, buffer);

                return(fileResult);
            }
            if (requestPath.Equals(basePath))
            {
                filePath = "index.html";
            }
            else
            {
                filePath = requestPath.Substring(basePath.Length + 1);
                filePath = filePath.Replace("/", ".");
            }
            var embeddedResource = $"{typeof(JsonRpcDoc).Namespace}.web.{filePath}";

            using (var stream = typeof(JsonRpcDoc).Assembly.GetManifestResourceStream(embeddedResource))
            {
                byte[] buffer = null;
                if (stream != null)
                {
                    buffer = ReadToEnd(stream);
                }
                var fileResult = new FileContent(filePath, buffer);
                return(fileResult);
            }
        }
예제 #3
0
 public JsonRpcDoc(JsonRpcInfo info)
 {
     Info = info;
 }