Example #1
0
 public void Configure(IServerRoutingTable serverRoutingTable)
 {
     using (var context = new MusacaAppDb())
     {
         context.Database.EnsureCreated();
     }
 }
        public void Configure(IServerRoutingTable serverRoutingTable)
        {
            using (IRunesDbContext dbContext = new IRunesDbContext())
            {
                dbContext.Database.EnsureCreated();
            }

            //serverRoutingTable.Add(HttpRequestMethod.Get, "/", (_) => new RedirectResult("/Home/Index"));
            //serverRoutingTable.Add(HttpRequestMethod.Get, "/Home/Index", new HomeController().HomePage);

            //serverRoutingTable.Add(HttpRequestMethod.Get, "/Users/Login", new UsersController().Login);
            //serverRoutingTable.Add(HttpRequestMethod.Get, "/Users/Register", new UsersController().Register);
            //serverRoutingTable.Add(HttpRequestMethod.Get, "/Albums/All", new AlbumsController().All);
            //serverRoutingTable.Add(HttpRequestMethod.Get, "/Albums/Create", new AlbumsController().Create);
            //serverRoutingTable.Add(HttpRequestMethod.Get, "/Albums/Details", new AlbumsController().Info);
            //serverRoutingTable.Add(HttpRequestMethod.Get, "/Tracks/Create", new TracksController().Create);
            //serverRoutingTable.Add(HttpRequestMethod.Get, "/Tracks/Details", new TracksController().Info);


            //serverRoutingTable.Add(HttpRequestMethod.Get, "/Users/Logout", new UsersController().HandleLoggingOut);
            //serverRoutingTable.Add(HttpRequestMethod.Post, "/Users/Login", new UsersController().HandleLogingIn);
            //serverRoutingTable.Add(HttpRequestMethod.Post, "/Users/Register", new UsersController().HandleRegistration);
            //serverRoutingTable.Add(HttpRequestMethod.Post, "/Albums/Create", new AlbumsController().HandleCreatingAlbum);
            //serverRoutingTable.Add(HttpRequestMethod.Post, "/Tracks/Create", new TracksController().HandleCreatingTrack);
        }
Example #3
0
        public Server(int port, IServerRoutingTable routingTable)
        {
            this.port         = port;
            this.routingTable = routingTable;

            this.listener = new TcpListener(IPAddress.Parse(LocalhostIpAddress), port);
        }
Example #4
0
 public Server(int port, IServerRoutingTable serverRoutingTable)
 {
     CoreValidator.ThrowIfNull(serverRoutingTable, nameof(serverRoutingTable));
     this.port = port;
     this.serverRoutingTable = serverRoutingTable;
     this.tcpListener        = new TcpListener(IPAddress.Parse(LocalHostIpAddres), port);
 }
Example #5
0
        public static async Task RunAsync(IMvcApplication application, int port)
        {
            ServerRoutingTable = new ServerRoutingTable();
            IServiceCollection serviceCollection = new ServiceCollection();

            application.ConfigureServices(serviceCollection);
            application.Configure(ServerRoutingTable);

            ServerRoutingTable
            .LoadControllers(application.GetType().Assembly, serviceCollection)
            .LoadStaticFiles();

            Console.WriteLine(string.Join(Environment.NewLine, ServerRoutingTable.GetAllRouteNames()));

            TcpListener listener = new TcpListener(IPAddress.Parse(LocalhostIpAddress), port);

            listener.Start();
            IsRunning = true;

            Console.WriteLine($"Server started at http://{LocalhostIpAddress}:{port}");

            while (IsRunning)
            {
                Console.WriteLine("Waiting for client...");

                var client = await listener.AcceptSocketAsync();

                ListenAsync(client);
            }
        }
Example #6
0
        private static void AutoRegisterRoutes(IMvcApplication application, IServerRoutingTable serverRoutingTable)
        {
            var controllers =
                application.GetType().Assembly.GetTypes().Where(type => type.IsClass && !type.IsAbstract && typeof(Controller).IsAssignableFrom(type));

            foreach (var controllerType in controllers)
            {
                //TODO: Remove ToString from Info Controller
                var actions = controllerType
                              .GetMethods(BindingFlags.DeclaredOnly
                                          | BindingFlags.Public
                                          | BindingFlags.Instance)
                              .Where(x => !x.IsSpecialName && x.DeclaringType == controllerType)
                              .Where(x => x.GetCustomAttributes().All(a => a.GetType() != typeof(NonActionAttribute)));

                foreach (var action in actions)
                {
                    string path = $"/{controllerType.Name.Replace("Controller", string.Empty)}/{action.Name}";

                    var attribute = action.GetCustomAttributes()
                                    .Where(x => x.GetType().IsSubclassOf(typeof(BaseHttpAttribute))).LastOrDefault() as BaseHttpAttribute;

                    var httpMethod = HttpRequestMethod.Get;
                    if (attribute != null)
                    {
                        httpMethod = attribute.Method;
                    }

                    if (attribute?.Url != null)
                    {
                        path = attribute.Url;
                    }

                    if (attribute?.ActionName != null)
                    {
                        path = $"/{controllerType.Name.Replace("Controller", string.Empty)}/{attribute.ActionName}";
                    }

                    serverRoutingTable.Add(httpMethod, path, request =>
                    {
                        var controllerInstance = Activator.CreateInstance(controllerType);
                        ((Controller)controllerInstance).Request = request;
                        var controllerPrincipal = ((Controller)controllerInstance).User;

                        //Security Authorization TODO: refactor this
                        var authorizeAttribute = action.GetCustomAttributes().LastOrDefault(a => a.GetType() == typeof(AuthorizeAttribute)) as AuthorizeAttribute;

                        if (authorizeAttribute != null && !authorizeAttribute.IsInAuthority(controllerPrincipal))
                        {
                            //TODO: redirect to configured Url
                            return(new HttpResponse(HttpResponseStatusCode.Forbidden));
                        }

                        var response = action.Invoke(controllerInstance, new object[0]) as ActionResult;

                        return(response);
                    });
                }
            }
        }
 public void Configure(IServerRoutingTable serverRoutingTable, RouteSettings routeSettings)
 {
     using (var context = new MusacaDbContext())
     {
         context.Database.EnsureCreated();
     }
 }
Example #8
0
 public ConnectionHandler(Socket client, IServerRoutingTable serverRoutingTable)
 {
     CoreValidator.ThrowIfNull(client, nameof(client));
     CoreValidator.ThrowIfNull(serverRoutingTable, nameof(serverRoutingTable));
     this.client             = client;
     this.serverRoutingTable = serverRoutingTable;
 }
 public void Configure(IServerRoutingTable serverRoutingTable)
 {
     using (var dbConext = new MusacaDbContext())
     {
         dbConext.Database.EnsureCreated();
     }
 }
Example #10
0
        private static void AutoRegisterRoutes(
            IMvcApplication application,
            IServerRoutingTable serverRoutingTable,
            IServiceProvider serviceProvider)
        {
            var controllers = application
                              .GetType()
                              .Assembly
                              .GetTypes()
                              .Where(type =>
                                     type.IsClass &&
                                     !type.IsAbstract &&
                                     typeof(Controller).IsAssignableFrom(type));

            foreach (var controllerType in controllers)
            {
                var actions = controllerType
                              .GetMethods(
                    BindingFlags.DeclaredOnly |
                    BindingFlags.Public |
                    BindingFlags.Instance)
                              .Where(m => !m.IsSpecialName && m.DeclaringType == controllerType)
                              .Where(x => x.GetCustomAttributes().All(a => a.GetType() != typeof(NonActionAttribute)));

                foreach (var action in actions)
                {
                    var path = $"/{controllerType.Name.Replace("Controller", string.Empty)}/{action.Name}";

                    var attribute = action
                                    .GetCustomAttributes()
                                    .Where(x => x
                                           .GetType()
                                           .IsSubclassOf(typeof(BaseHttpAttribute)))
                                    .LastOrDefault() as BaseHttpAttribute;

                    var httpMethod = HttpRequestMethod.Get;

                    if (attribute != null)
                    {
                        httpMethod = attribute.Method;
                    }

                    if (attribute?.Url != null)
                    {
                        path = attribute.Url;
                    }

                    if (attribute?.ActionName != null)
                    {
                        path = $"/{controllerType.Name.Replace("Controller", string.Empty)}/{attribute.ActionName}";
                    }

                    serverRoutingTable.Add(httpMethod, path,
                                           (request) => ProcessRequest(serviceProvider, controllerType, action, request));

                    Console.WriteLine(httpMethod + " " + path);
                }
            }
        }
Example #11
0
 public void Configure(IServerRoutingTable serverRoutingTable)
 {
     // Once on start
     using (var db = new PandaDbContext())
     {
         db.Database.EnsureCreated();
     }
 }
Example #12
0
 public void Configure(IServerRoutingTable serverRoutingTable)
 {
     using (var context = new ExamDbContext())
     {
         //context.Database.EnsureDeleted();
         context.Database.EnsureCreated();
     }
 }
        public Server(int port, IServerRoutingTable serverRoutingTable, IHttpSessionStorage httpSessionStorage)
        {
            this.port   = port;
            tcpListener = new TcpListener(IPAddress.Parse(LocalHostIpAddress), this.port);

            this.serverRoutingTable = serverRoutingTable;
            this.httpSessionStorage = httpSessionStorage;
        }
Example #14
0
        private static void AddAction(IServerRoutingTable serverRoutingTable, string controllerName, MethodInfo actionInfo, Controller controllerInstanse)
        {
            var(requestMethod, url) = GetActionRequestMethod(actionInfo);

            string path = url ?? $"/{controllerName}/{actionInfo.Name}";

            serverRoutingTable.Add(requestMethod, path, (request) => ProcessAction(request, actionInfo, controllerInstanse));
        }
        public static void Main()
        {
            IServiceProvider    services = ConfigureServices();
            IServerRoutingTable routes   = RegisterRoutes(services);
            Server server = new Server(8000, services);

            server.Run();
        }
        public ConnectionHandler(Socket client, IServerRoutingTable serverRoutingTable, IHttpSessionStorage sessionStorage)
        {
            client.ThrowIfNull(nameof(client));
            serverRoutingTable.ThrowIfNull(nameof(serverRoutingTable));

            this.client             = client;
            this.serverRoutingTable = serverRoutingTable;
            this.sessionStorage     = sessionStorage;
        }
Example #17
0
        public void Configure(IServerRoutingTable serverRoutingTable)
        {
            using (var context = new RunesDbContext())
            {
                context.Database.EnsureCreated();
            }

            // serverRoutingTable.Add(HttpRequestMethod.Get, "/Info/About", request => new InfoController().About(request));
        }
Example #18
0
        public void Configure(IServerRoutingTable routeTable)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en");

            using (var context = new ApplicationDbContext())
            {
                context.Database.EnsureCreated();
            }
        }
        public Server(int port, IServerRoutingTable serverRoutingTable, IHttpSessionStorage httpSessionStorage)
        {
            CoreValidator.ThrowIfNull(serverRoutingTable, nameof(serverRoutingTable));
            CoreValidator.ThrowIfNull(httpSessionStorage, nameof(httpSessionStorage));

            this.port = port;
            this.serverRoutingTable = serverRoutingTable;
            listener = new TcpListener(IPAddress.Parse(LocalhostIpAdderss), port);
            this.httpSessionStorage = httpSessionStorage;
        }
Example #20
0
        public ConnectionHandler(Socket client, IServerRoutingTable serverRoutingTable, IHttpSessionStorage httpSessionStorage)
        {
            CoreValidator.ThrowIfNull(client, nameof(client));
            CoreValidator.ThrowIfNull(serverRoutingTable, nameof(serverRoutingTable));
            CoreValidator.ThrowIfNull(httpSessionStorage, nameof(httpSessionStorage));

            this.client             = client;
            this.serverRoutingTable = serverRoutingTable;
            this.httpSessionStorage = httpSessionStorage;
        }
        public void Configure(IServerRoutingTable serverRoutingTable, RouteSettings routeSettings)
        {
            using (var context = new RunesDbContext())
            {
                context.Database.EnsureCreated();
            }

            serverRoutingTable.Add(HttpRequestMethod.Get, "/", request => new RedirectResult("/Home/Index"));
            routeSettings.UnauthorizedRedirectRoute = "/Users/Login";
        }
Example #22
0
        private static void AutoRegisterRoutes(
            IMvcApplication application, IServerRoutingTable serverRoutingTable)
        {
            var controllers = application.GetType().Assembly.GetTypes()
                              .Where(type => type.IsClass && !type.IsAbstract &&
                                     typeof(Controller).IsAssignableFrom(type));

            // TODO: RemoveToString from InfoController
            foreach (var controller in controllers)
            {
                var actions = controller
                              .GetMethods(BindingFlags.DeclaredOnly
                                          | BindingFlags.Public
                                          | BindingFlags.Instance)
                              .Where(x => !x.IsSpecialName && x.DeclaringType == controller);
                foreach (var action in actions)
                {
                    var path      = $"/{controller.Name.Replace("Controller", string.Empty)}/{action.Name}";
                    var attribute = action.GetCustomAttributes().Where(
                        x => x.GetType().IsSubclassOf(typeof(BaseHttpAttribute))).LastOrDefault() as BaseHttpAttribute;
                    var httpMethod = HttpRequestMethod.Get;
                    if (attribute != null)
                    {
                        httpMethod = attribute.Method;
                    }

                    if (attribute?.Url != null)
                    {
                        path = attribute.Url;
                    }

                    if (attribute?.ActionName != null)
                    {
                        path = $"/{controller.Name.Replace("Controller", string.Empty)}/{attribute.ActionName}";
                    }

                    serverRoutingTable.Add(httpMethod, path, request =>
                    {
                        // request => new UsersController().Login(request)
                        var controllerInstance = Activator.CreateInstance(controller);
                        var response           = action.Invoke(controllerInstance, new[] { request }) as IHttpResponse;
                        return(response);
                    });

                    Console.WriteLine(httpMethod + " " + path);
                }
            }
            // Reflection
            // Assembly
            // typeof(Server).GetMethods()
            // sb.GetType().GetMethods();
            // Activator.CreateInstance(typeof(Server))
            var sb = DateTime.UtcNow;
        }
 public void Configure(IServerRoutingTable serverRoutingTable, RouteSettings routeSettings)
 {
     using (var context = new TorshiaDbContext())
     {
         bool isCreated = context.Database.EnsureCreated();
         if (isCreated)
         {
             Seeder.SeedRoles();
             Seeder.SeedAdmin();
         }
     }
 }
Example #24
0
        private static void AutoRegisterRoutes(IMvcApplication application, IServerRoutingTable serverRoutingTable)
        {
            var controllers = application.GetType().Assembly.GetTypes()
                              .Where(type => type.IsClass && !type.IsAbstract &&
                                     typeof(Controller).IsAssignableFrom(type));

            foreach (var controllerType in controllers)
            {
                var actions = controllerType
                              .GetMethods(BindingFlags.DeclaredOnly
                                          | BindingFlags.Public
                                          | BindingFlags.Instance)
                              .Where(x => !x.IsSpecialName && x.DeclaringType == controllerType)
                              .Where(x => x.GetCustomAttributes().All(a => a.GetType() != typeof(NonActionAttribute)));

                foreach (var action in actions)
                {
                    var path      = $"/{controllerType.Name.Replace("Controller", string.Empty)}/{action.Name}";
                    var attribute = action.GetCustomAttributes().Where(
                        x => x.GetType().IsSubclassOf(typeof(BaseHttpAttribute))).LastOrDefault() as BaseHttpAttribute;
                    var httpMethod = HttpRequestMethod.Get;
                    if (attribute != null)
                    {
                        httpMethod = attribute.Method;
                    }

                    if (attribute?.Url != null)
                    {
                        path = attribute.Url;
                    }

                    if (attribute?.ActionName != null)
                    {
                        path = $"/{controllerType.Name.Replace("Controller", string.Empty)}/{attribute.ActionName}";
                    }

                    serverRoutingTable.Add(httpMethod, path,
                                           request =>
                    {
                        var controllerInstance = Activator.CreateInstance(controllerType);
                        var response           = action.Invoke(controllerInstance, new [] { request }) as IHttpResponse;
                        return(response);
                    });


                    Console.WriteLine(httpMethod + " " + path);
                }
            }

            Console.WriteLine(string.Join(Environment.NewLine, serverRoutingTable));
        }
Example #25
0
        public static IServerRoutingTable LoadControllers(this IServerRoutingTable serverRoutingTable, Assembly caller, IServiceCollection serviceCollection)
        {
            var controllersTypes = caller
                                   .GetExportedTypes()
                                   .Where(x => x.BaseType == BaseControllerType)
                                   .ToList();

            foreach (var controllerType in controllersTypes)
            {
                ProcessController(serverRoutingTable, controllerType, serviceCollection);
            }

            return(serverRoutingTable);
        }
Example #26
0
        private static void AutoRegisterRoutes(IMvcApplication application,
                                               IServerRoutingTable serverRoutingTable,
                                               IServiceProvider serviceProvider,
                                               RouteSettings routeSettings)
        {
            IEnumerable <System.Type> controllers = application.GetType().Assembly
                                                    .GetTypes()
                                                    .Where(type => type.IsClass && !type.IsAbstract && typeof(Controller).IsAssignableFrom(type));

            foreach (var controllerType in controllers)
            {
                IEnumerable <MethodInfo> actions = controllerType.GetMethods(BindingFlags.DeclaredOnly |
                                                                             BindingFlags.Instance | BindingFlags.Public)
                                                   .Where(m => !m.IsSpecialName && !m.IsVirtual && m.GetCustomAttribute <NonActionAttribute>() == null);

                string controllerName = controllerType.Name.Replace("Controller", "");

                AuthorizeAttribute controllerAuthorizeAttribute = controllerType.GetCustomAttribute <AuthorizeAttribute>() as AuthorizeAttribute;

                foreach (var action in actions)
                {
                    string path = $"/{controllerName}/{action.Name}";

                    BaseHttpAttribute attribute = action.GetCustomAttributes()
                                                  .LastOrDefault(a => a.GetType().IsSubclassOf(typeof(BaseHttpAttribute))) as BaseHttpAttribute;

                    HttpRequestMethod requestMethod = HttpRequestMethod.Get;

                    if (attribute != null)
                    {
                        requestMethod = attribute.HttpRequestMethod;
                    }
                    if (attribute?.ActionName != null)
                    {
                        path = $"/{controllerName}/{attribute.ActionName}";
                    }
                    if (attribute?.Url != null)
                    {
                        path = attribute.Url;
                    }

                    serverRoutingTable.Add(requestMethod, path, request =>
                    {
                        return(ProcessRequest(serviceProvider, request, controllerType, controllerAuthorizeAttribute, action, routeSettings));
                    });
                }
            }
        }
Example #27
0
        private static void ProcessController(IServerRoutingTable serverRoutingTable, Type controller, IServiceCollection serviceCollection)
        {
            string controllerName = controller.Name.Replace(BaseControllerName, string.Empty);

            var controllerActions = controller
                                    .GetMethods(MethodCriteria)
                                    .Where(x => x.ReturnType == BaseResponceType)
                                    .ToList();

            foreach (var actionInfo in controllerActions)
            {
                var controllerInstanse = serviceCollection.CreateInstance(controller) as Controller;

                AddAction(serverRoutingTable, controllerName, actionInfo, controllerInstanse);
            }
        }