示例#1
0
文件: Server.cs 项目: ashuai/AServer
        public Task Run()
        {
            string url  = String.Format("http://{0}:{1}", _ip, _port);
            var    urls = new List <string>();

            if (!_urls.Any())
            {
                urls.Add(url);
            }
            else
            {
                urls = _urls.ToList();
            }

            Host =
                new WebHostBuilder()
                .UseUrls(urls.ToArray())
                .UseKestrel()
                .Configure(app => {
                app.Run(http => {
                    return(Task.Run(() => {
                        var req = http.Request;
                        var resp = http.Response;
                        var method = http.Request.Method;
                        var path = req.Path;

                        var cacheKey = $"Request:{method}-{path}";
                        ConsoleUtil.DebugConsole(cacheKey);

                        //cors
                        var corsResult = CorsHandler.Handler(_corsOption, http);
                        if (corsResult != null)
                        {
                            return corsResult;
                        }

                        _handlersCache.TryGetValue(cacheKey, out HttpHandler handler);
                        if (handler == null)
                        {
                            handler = _handlers.FirstOrDefault(
                                h => h.Method == method && PathUtil.IsMatch(path, h.Path));
                            if (handler != null)
                            {
                                _handlersCache.TryAdd(cacheKey, handler);
                            }
                        }

                        if (handler != null)
                        {
                            try {
                                return handler.Handler(new Request(req, handler.Path), new Response(resp));
                            } catch (Exception e) {
                                Console.WriteLine(e.ToString());
                                resp.StatusCode = (int)HttpStatusCode.InternalServerError;
                                Console.WriteLine(
                                    $"Response:{resp.StatusCode} {HttpStatusCode.InternalServerError}");

                                return resp.WriteAsync("InternalServerError");
                            }
                        }

                        resp.StatusCode = (int)HttpStatusCode.NotFound;
                        Console.WriteLine($"Response:{resp.StatusCode} {HttpStatusCode.NotFound}");

                        return resp.WriteAsync("NotFound");
                    }));
                });
            })
                .Build();
            var task = Host.StartAsync();

            urls.ForEach(u => {
                Console.WriteLine($"AServer listen on {u} .");
            });

            return(task);
        }
示例#2
0
文件: Server.cs 项目: gamediy/AServer
        public Task Run()
        {
            Host =
                new WebHostBuilder()
                .UseKestrel(op =>
            {
                if (_ip.Equals("localhost", StringComparison.CurrentCultureIgnoreCase))
                {
                    op.ListenLocalhost(_port);
                }
                else
                {
                    op.Listen(IPAddress.Parse(_ip), _port);
                }
            })
                .Configure(app =>
            {
                app.Run(http =>
                {
                    return(Task.Run(() =>
                    {
                        var req = http.Request;
                        var resp = http.Response;
                        var method = http.Request.Method;
                        var path = req.Path;

                        var cacheKey = $"Request:{method}-{path}";
                        ConsoleUtil.WriteToConsole(cacheKey);

                        //cors
                        var corsResult = CorsHandler.Handler(_corsOption, http);
                        if (corsResult != null)
                        {
                            return corsResult;
                        }

                        _handlersCache.TryGetValue(cacheKey, out HttpHandler handler);
                        if (handler == null)
                        {
                            handler = _handlers.FirstOrDefault(
                                h => h.Method == method && PathUtil.IsMatch(path, h.Path));
                            if (handler != null)
                            {
                                _handlersCache.TryAdd(cacheKey, handler);
                            }
                        }

                        if (handler != null)
                        {
                            try
                            {
                                return handler.Handler(new Request(req, handler.Path), new Response(resp));
                            }
                            catch (Exception e)
                            {
                                ConsoleUtil.WriteToConsole(e.ToString());
                                resp.StatusCode = (int)HttpStatusCode.InternalServerError;
                                ConsoleUtil.WriteToConsole(
                                    $"Response:{resp.StatusCode} {HttpStatusCode.InternalServerError}");

                                return resp.WriteAsync("InternalServerError");
                            }
                        }

                        resp.StatusCode = (int)HttpStatusCode.NotFound;
                        ConsoleUtil.WriteToConsole($"Response:{resp.StatusCode} {HttpStatusCode.NotFound}");

                        return resp.WriteAsync("NotFound");
                    }));
                });
            })
                .Build();
            var task = Host.StartAsync();

            ConsoleUtil.WriteToConsole($"AServer listening {_ip}:{_port} now .");

            return(task);
        }