Skip to content

Instantly share code, notes, and snippets.

@daneb
Created April 8, 2016 22:03
Show Gist options
  • Save daneb/720544178d943e8be13d89e999b8d4c9 to your computer and use it in GitHub Desktop.
Save daneb/720544178d943e8be13d89e999b8d4c9 to your computer and use it in GitHub Desktop.
Simple Web Framework
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace RackPrototype
{
class Server
{
private Thread _serverThread;
private HttpListener _listener;
private int _port;
public int port { get; }
public void Stop()
{
_serverThread.Abort();
_listener.Stop();
}
private void Process(HttpListenerContext context)
{
// We are building nothing more than an API framework (No View)
// 1) Need To Apply Routing Rules Based on a Configuration File
// 2) This should then redirect to the matching Controller
// 3) The Controller will then manage communication to Model and return response [http_status_code, message]
}
private void Listen()
{
_listener = new HttpListener();
// Must match netsh command
// netsh http add urlacl url = http://+:80/MyUri user=DOMAIN\user
_listener.Prefixes.Add("http://+:" + _port.ToString() + "/");
_listener.Start();
while(true)
{
try
{
HttpListenerContext context = _listener.GetContext();
Process(context);
}
catch (Exception ex)
{
}
}
}
private void Initialize(int port)
{
this._port = port;
_serverThread = new Thread(this.Listen);
_serverThread.Start();
}
public void Start(KeyValuePair<string, string> options = new KeyValuePair<string, string>())
{
// options will define how the application starts (startup configuration)
this.Initialize(80);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment