FlexitimeTracker/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Service1.cs
2017-01-30 20:25:18 +00:00

164 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.ServiceProcess;
using System.Threading;
using System.Web.Http;
using DalSoft.WebApi.HelpPage;
using Microsoft.Owin.FileSystems;
using Microsoft.Owin.Hosting;
using Microsoft.Owin.StaticFiles;
using Owin;
namespace WindowsDataCenter
{
public partial class Service1: ServiceBase
{
public Service1()
{
InitializeComponent();
}
private IDisposable _webApp;
private bool _stopMainWorkerThread;
private Thread _mainWorkerThread;
public void Start()
{
OnStart(new string[] {});
}
public void Stop()
{
OnStop();
}
protected override void OnStart(string[] args)
{
_mainWorkerThread = new Thread(MainWorkerThread)
{
IsBackground = false,
Name = "OWIN SELF HOST MAIN THREAD"
};
_webApp = WebApp.Start<StartOwin>("http://localhost:8800");
}
protected override void OnStop()
{
_webApp.Dispose();
_stopMainWorkerThread = true;
if (_mainWorkerThread != null && _mainWorkerThread.IsAlive)
{
_mainWorkerThread.Join(2000);
if (_mainWorkerThread.IsAlive)
{
_mainWorkerThread.Interrupt();
}
}
}
private void MainWorkerThread()
{
while (!_stopMainWorkerThread)
{
Thread.Sleep(2000);
}
}
}
public class StartOwin
{
public void Configuration(IAppBuilder appBuilder)
{
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
#if DEBUG
//Add the help-pages extension only in Debug mode.
appBuilder.UseWebApiHelpPage(config, "help-pages");
#endif
var fileSystem = new PhysicalFileSystem(@"./www");
var options = new FileServerOptions
{
EnableDefaultFiles = true,
FileSystem = fileSystem
};
options.StaticFileOptions.FileSystem = fileSystem;
options.StaticFileOptions.ServeUnknownFileTypes = true;
options.DefaultFilesOptions.DefaultFileNames = new[]
{
"index.html"
};
appBuilder.UseFileServer(options);
}
}
public class SwipeDataController : ApiController
{
[HttpPost]
//[Route("postSwipeData")]
public IHttpActionResult PostData([FromBody] CardData cData)
{
Console.WriteLine(cData.CardUId);
var respMsg = new HttpResponseMessage(HttpStatusCode.OK);
return ResponseMessage(respMsg);
}
/// <summary>
/// Get unassigned card Ids in the system.
/// </summary>
/// <returns>Json object containing list of unassigned card objects</returns>
/// <example>
/// > Get: http://{baseaddress}:port/api/swipedata/unassigned
/// > Returns:
/// {
/// "data": [
/// {
/// "id": 0,
/// "Id": 0,
/// "UserId": -1,
/// "IsSelected": false,
/// "CardUId": "39c6ec38-3879-4ee6-99ae-0a55777f6b86"
/// }
/// ]
/// }
/// </example>
[HttpGet]
[Route("api/swipedata/unassigned")]
public IHttpActionResult GetUnassignedCards()
{
throw new NotImplementedException();
}
}
public class ValuesController : ApiController
{
public IEnumerable<string> Get()
{
return new List<string> { "ASP.NET", "Docker", "Windows Containers" };
}
}
public class UsersController : ApiController
{
public IHttpActionResult GetUsers()
{
throw new NotImplementedException();
}
}
public class CardData
{
public string CardUId { get; set; }
}
}