示例#1
0
        private static void Main(string[] args)
        {
            #region Setup buttons

            Setup.WiringPiSetupGpio();
            CoreFunctions.SetPinMode(26, Constants.PinMode.Input);

            Task.Run(() =>
            {
                ApiController.ButtonCheck.Add(26, 0);
                var prev = true;

                while (true)
                {
                    var x = CoreFunctions.ReadBit(26);

                    if (x != prev)
                    {
                        ApiController.ButtonCheck[26] += prev ? 1 : 0;
                        prev = x;
                    }

                    System.Threading.Thread.Sleep(5);
                }
            });

            #endregion

            var url = "http://*:9696/";
            if (args.Length > 0)
                url = args[0];

            // Our web server is disposable. Note that if you don't want to use logging,
            // there are alternate constructors that allow you to skip specifying an ILog object.
            using (var server = new WebServer(url, new SimpleConsoleLog()))
            {
                server.WithWebApiController<ApiController>();
                server.RegisterModule(new CorsModule());

                // Here we setup serving of static files
                server.RegisterModule(new StaticFilesModule(Directory.GetCurrentDirectory()));

                // The static files module will cache small files in ram until it detects they have been modified.
                server.Module<StaticFilesModule>().UseRamCache = true;
                server.Module<StaticFilesModule>().DefaultExtension = ".html";
                // We don't need to add the line below. The default document is always index.html.
                //server.Module<Modules.StaticFilesWebModule>().DefaultDocument = "index.html";

                // Once we've registered our modules and configured them, we call the RunAsync() method.
                // This is a non-blocking method (it return immediately) so in this case we avoid
                // disposing of the object until a key is pressed.
                //server.Run();
                server.RunAsync();

                // Wait for any key to be pressed before disposing of our web server.
                // In a service we'd manage the lifecycle of of our web server using
                // something like a BackgroundWorker or a ManualResetEvent.
                Console.ReadKey(true);
            }
        }
示例#2
0
        public static void RunServer()
        {
            try
            {
                using (_server = new WebServer("http://*:80/", new SimpleConsoleLog()))
                {
                    _server.WithWebApiController<ApiController>();
                    _server.RegisterModule(new CorsModule());
                    _server.RegisterModule(new StaticFilesModule(TmpFolder));

                    _server.Module<StaticFilesModule>().UseRamCache = true;
                    _server.Module<StaticFilesModule>().DefaultExtension = ".html";

                    File.WriteAllText(Path.Combine(TmpFolder, "index.html"), Resources.index);
                    File.WriteAllText(Path.Combine(TmpFolder, "app.js"), Resources.app);
                    File.WriteAllText(Path.Combine(TmpFolder, "app.jsx"), Resources.appjsx);
                    File.WriteAllText(Path.Combine(TmpFolder, "jquery.js"), Resources.jquery_2_1_4_min);
                    File.WriteAllText(Path.Combine(TmpFolder, "JSXTransformer.js"), Resources.JSXTransformer);
                    File.WriteAllText(Path.Combine(TmpFolder, "react.js"),
                        Resources.react_with_addons_min);

                    _server.RunAsync();

                    while (true)
                    {
                        Console.WriteLine("Type an Url to add or press Enter to stop...");
                        var result = Console.ReadLine();

                        if (string.IsNullOrWhiteSpace(result)) break;

                        AddEntry(result);
                    }

                    var currentHost = File.ReadAllText(HostFile);

                    // Restore
                    if (OriginalHostFile != currentHost) File.WriteAllText(HostFile, OriginalHostFile);
                }
            }
            catch (Exception ex)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
            }
        }