Skip to content
This repository has been archived by the owner on Jul 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #68 from half-ogre/half-ogre/startup-cleanup
Browse files Browse the repository at this point in the history
Startup cleanup
  • Loading branch information
half-ogre committed Dec 8, 2013
2 parents ca77749 + eea91b0 commit 358d53f
Showing 1 changed file with 91 additions and 43 deletions.
134 changes: 91 additions & 43 deletions qed/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Owin.Builder;
Expand All @@ -16,30 +18,32 @@ class Program
{
static void EnsureAdmiministrator()
{
if (!fn.GetAdministrators().Any())
if (fn.GetAdministrators().Any())
return;

Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("There are no administrator users.");
Console.ResetColor();
Console.WriteLine("You must create an administrator user to start QED.");
Console.Write("Enter a username: ");
var username = Console.ReadLine();
if (String.IsNullOrEmpty(username) || !Constants.UsernameRegex.IsMatch(username))
{
Console.WriteLine("There are no administrator users.");
Console.WriteLine("You must create an administrator user to start QED.");
Console.WriteLine("");
Console.WriteLine("Enter a username for your new administrator user:");
var username = Console.ReadLine();
if (String.IsNullOrEmpty(username) || !Constants.UsernameRegex.IsMatch(username))
{
Console.WriteLine("Invalid username (must have just letters and numbers). Exiting.");
Environment.Exit(1);
}
Console.WriteLine("Enter a password for your new administrator user:");
var password = Console.ReadLine();
if (String.IsNullOrEmpty(password) || password.Length < 8)
{
Console.WriteLine("Invalid password (must be at least 8 characters). Exiting.");
Environment.Exit(1);
}
Console.WriteLine("Invalid username (must have just letters and numbers). Exiting.");
Environment.Exit(1);
}
Console.Write("Enter a password: ");
var password = ReadPassword();
if (String.IsNullOrEmpty(password) || password.Length < 8)
{
Console.WriteLine("Invalid password (must be at least 8 characters). Exiting.");
Environment.Exit(1);
}

fn.CreateUser(username, password, null);
fn.CreateUser(username, password, null);

Console.WriteLine("Created admnistrator.");
}
Console.WriteLine("Created administrator.");
}

static void ShowHelp(OptionSet options)
Expand All @@ -51,6 +55,17 @@ static void ShowHelp(OptionSet options)
}

static void Main(string[] args)
{
ReadOptions(args);

EnsureAdmiministrator();

StartWebServer();

RunBuilds();
}

static void ReadOptions(string[] args)
{
string hostArg = null;
var showHelp = false;
Expand All @@ -70,41 +85,48 @@ static void Main(string[] args)
Console.Write("qed: ");
Console.WriteLine(optionEx.Message);
Console.WriteLine("Try `qed.exe --help' for more information.");
return;
Environment.Exit(1);
}

if (showHelp)
{
ShowHelp(options);
return;
Environment.Exit(1);
}

fn.SetConfiguration(Constants.Configuration.HostKey, hostArg);
}

EnsureAdmiministrator();

var appBuilder = new AppBuilder();

OwinServerFactory.Initialize(appBuilder.Properties);
appBuilder.Properties.Add("host.AppName", "QED" );
static string ReadPassword()
{
var password = new StringBuilder();
ConsoleKeyInfo key;

fn.ConfigureBuilder(appBuilder);

var serverBuilder = ServerBuilder
.New()
.SetPort(1754)
.SetOwinApp(appBuilder.Build())
.SetOwinCapabilities((IDictionary<string, object>)appBuilder.Properties[OwinKeys.ServerCapabilitiesKey]);
do
{
key = Console.ReadKey(true);

var server = serverBuilder.Start();
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
password.Append(key.KeyChar);
Console.Write("*");
}
else
{
if (key.Key == ConsoleKey.Backspace && password.Length > 0)
{
password.Length--;
Console.Write("\b \b");
}
}
}
while (key.Key != ConsoleKey.Enter);

Console.CancelKeyPress += (sender, eventArgs) => server.Dispose();

Console.WriteLine("Started qed.");
Console.WriteLine("Listening on port 1754.");
Console.WriteLine("Press CTRL+C to exit.");
Console.WriteLine();
return password.ToString();
}

static void RunBuilds()
{
// TODO: Decide on a better loggin approach
var fail = new Action<Exception>(ex =>
{
Expand Down Expand Up @@ -136,5 +158,31 @@ static void Main(string[] args)
}
}
}

static void StartWebServer()
{
var appBuilder = new AppBuilder();

OwinServerFactory.Initialize(appBuilder.Properties);

appBuilder.Properties.Add("host.AppName", "QED");

fn.ConfigureBuilder(appBuilder);

var serverBuilder = ServerBuilder
.New()
.SetPort(1754)
.SetOwinApp(appBuilder.Build())
.SetOwinCapabilities((IDictionary<string, object>)appBuilder.Properties[OwinKeys.ServerCapabilitiesKey]);

var server = serverBuilder.Start();

Console.CancelKeyPress += (sender, eventArgs) => server.Dispose();

Console.WriteLine("Started qed.");
Console.WriteLine("Listening on port 1754.");
Console.WriteLine("Press CTRL+C to exit.");
Console.WriteLine();
}
}
}

0 comments on commit 358d53f

Please sign in to comment.