예제 #1
0
        static void Main()
        {
            Console.WriteLine("Hello World!");

            var ws = new Webserver();

            RouteCollector.CollectRoutes(ws);

            ws.Get("/", (req, resp) => {
                resp.AsJson(new { status = "Routing was success!!" });
            });
            ws.Get("/hello", (req, resp) => {
                resp.Error(404, "Resource not found!");
            });

            ws.Use(new DisallowGET());
            ws.Start();
        }
예제 #2
0
        public static void CollectRoutes(Webserver ws)
        {
            var a = Assembly.GetExecutingAssembly().GetTypes();

            foreach (var t in a)
            {
                foreach (var m in t.GetMethods())
                {
                    var attr = m.GetCustomAttribute <RouteAttribute>();

                    if (attr != null)
                    {
                        if (attr.Method == "GET")
                        {
                            ws.Get(attr.Path, (req, res) => TransferMethodResult(m.Invoke(Activator.CreateInstance(t), GetInputs(req, m)), res));
                        }
                        else if (attr.Method == "POST")
                        {
                            ws.Post(attr.Path, (req, res) => TransferMethodResult(m.Invoke(Activator.CreateInstance(t), GetInputs(req, m)), res));
                        }
                    }
                }
            }
        }