Exemple #1
0
        static void Main(string[] args)
        {
            var server = new Agile.AServer.Server();

            server.AddHandler(new HttpHandler()
            {
                Method  = "GET",
                Path    = "/api/gettest",
                Handler = (req, resp) =>
                {
                    string name = req.Query.name;
                    return(resp.Write(string.IsNullOrEmpty(name) ? "ok" : name));
                }
            });

            server.AddHandler(new HttpHandler()
            {
                Method  = "POST",
                Path    = "/api/posttest",
                Handler = (req, resp) =>
                {
                    return(resp.Write(string.IsNullOrEmpty(req.BodyContent) ? "ok" : req.BodyContent));
                }
            });

            server.AddHandler(new HttpHandler()
            {
                Method  = "PUT",
                Path    = "/api/puttest",
                Handler = (req, resp) =>
                {
                    return(resp.Write(string.IsNullOrEmpty(req.BodyContent) ? "ok" : req.BodyContent));
                }
            });

            server.AddHandler(new HttpHandler()
            {
                Method  = "DELETE",
                Path    = "/api/deletetest",
                Handler = (req, resp) =>
                {
                    return(resp.Write(string.IsNullOrEmpty(req.BodyContent) ? "ok" : req.BodyContent));
                }
            });

            server.AddHandler(new HttpHandler()
            {
                Method  = "OPTIONS",
                Path    = "/api/optionstest",
                Handler = (req, resp) =>
                {
                    return(resp.Write(string.IsNullOrEmpty(req.BodyContent) ? "ok" : req.BodyContent));
                }
            });

            server.Run();

            Console.Read();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var server = new Agile.AServer.Server();

            server.AddHandler(new HttpHandler()
            {
                Method  = "GET",
                Path    = "/proxy/live",
                Handler = (req, resp) =>
                {
                    var url = "https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?";
                    // var burl = url + req.url.replace('/proxy/live', '')
                    foreach (var item in req.Query)
                    {
                        url += item.Key.ToString();
                        url += "=";
                        url += item.Value.ToString();
                        url += "&";
                    }
                    var res = GetWebRequest(url);
                    // Console.WriteLine(res);
                    return(resp.Write(res));
                }
            });
            server.Run();
            Console.ReadLine();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            Task.Run(async() =>
            {
                var server = new Agile.AServer.Server();
                server.EnableCors(new CorsOption()
                {
                    AccessControlAllowOrigins = "http://localhost:5000",
                    AccessControlAllowHeaders = "auth",
                    AccessControlAllowMethods = "GET,POST,PUT,DELETE"
                });
                server
                .AddHandler(new HttpHandler()
                {
                    Method  = "GET",
                    Path    = "/api/user",
                    Handler = (req, resp) =>
                    {
                        return(resp.Write("['kklldog','agile']"));
                    }
                })
                .AddHandler(new HttpHandler()
                {
                    Method  = "GET",
                    Path    = "/api/user/:id",
                    Handler = (req, resp) =>
                    {
                        var id = req.Params.id;

                        return(resp.Write($"userId:{id}"));
                    }
                })
                .AddHandler(new HttpHandler()
                {
                    Method  = "GET",
                    Path    = "/api/query",
                    Handler = (req, resp) =>
                    {
                        var name = req.Query.name;

                        var user = new
                        {
                            name = name,
                            id   = "0001",
                        };

                        var json = JsonConvert.SerializeObject(user);
                        return(resp.WriteJson(json));
                    }
                })
                .AddHandler(new HttpHandler()
                {
                    Method  = "POST",
                    Path    = "/api/user",
                    Handler = (req, resp) =>
                    {
                        return(resp.Write(req.BodyContent));
                    }
                })
                .AddHandler(new HttpHandler()
                {
                    Method  = "DELETE",
                    Path    = "/api/user/:id",
                    Handler = (req, resp) =>
                    {
                        var headers = new List <KeyValuePair <string, string> >();
                        headers.Add(new KeyValuePair <string, string>("Content-Type", "charset=utf-8"));
                        return(resp.Write($"user {req.Params.id} be deleted .", HttpStatusCode.OK, headers));
                    }
                })
                .AddHandler(new HttpHandler()
                {
                    Method  = "GET",
                    Path    = "/api/ex",
                    Handler = (req, resp) =>
                    {
                        throw new Exception("ex");
                    }
                })
                .AddController <ApiController>()
                ;
                await server.Run();
            });

            Console.Read();
        }