Exemplo n.º 1
0
        static void Main(string[] args)
        {
            bool show_help = false;
            bool rebuild = false;
            string staticFilesDir = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "public"));
            string imagesDir = Path.GetFullPath(Path.Combine(Path.Combine(staticFilesDir, "Content"), "Images"));
            string database = "database.xml";
            int port = 9200;
            bool multithreaded = false;

            var p = new OptionSet() {
                { "p|port=", "port to run the service on.", v => port = int.Parse(v) },
                { "d|database=", "database file.", v => database = v },
                { "m|multithreaded", "run multithreaded server.", v => multithreaded = v != null },
                { "i|images=", "images directory.", v => imagesDir = v },
                { "s|staticfiles=", "static files directory.", v => staticFilesDir = v },
                { "r|rebuild", "rebuild the database.", v => rebuild = v != null },
                { "h|help",  "show this message and exit",  v => show_help = v != null },
            };

            List<string> extra;
            try
            {
                extra = p.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Write("coloursearch: ");
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `coloursearch --help' for more information.");
                return;
            }

            if (show_help)
            {
                ShowHelp(p);
                return;
            }

            Console.WriteLine("Starting search engine, database: " + database);
            var searchEngine = new SearchEngine();

            if (rebuild || !File.Exists(database))
            {
                if (!Directory.Exists(imagesDir))
                    Directory.CreateDirectory(imagesDir);

                searchEngine.RebuildDatabase(imagesDir);
                searchEngine.SaveDatabase(database);
            }
            else
            {
                searchEngine.LoadDatabase(database);
            }

            Console.WriteLine("Images loaded, {0} images", searchEngine.IndexSize);

            var service = new VerySimpleWebServer(port, searchEngine, staticFilesDir, multithreaded);
            service.Run();
        }
Exemplo n.º 2
0
        public VerySimpleWebServer(int port, SearchEngine searchEngine, string staticFilesDir, bool multithreaded)
        {
            _port = port;
            _searchEngine = searchEngine;
            _multithreaded = multithreaded;

            _routes.Add(Tuple.Create("/search", (IHandler)new SearchHandler(_searchEngine)));
            _routes.Add(Tuple.Create("/", (IHandler)new StaticFileHandler(staticFilesDir)));
        }
Exemplo n.º 3
0
 public SearchHandler(SearchEngine searchEngine)
 {
     _searchEngine = searchEngine;
 }