FlexitimeTracker/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Service1.cs
chris.watts90@outlook.com 430f91ce18 add package Microsoft.Owin.StaticFiles.
added www folder to project, with bootstrap css/js files.
added the main single page app files to the project, pulled from codepen.io test/demo pages.
set properties to copy always for the main app files, bootstrap stuff is copy if newer.
added filesystem options to the StartOwin method.
2017-01-29 15:24:23 +00:00

158 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
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.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
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);
}
}
public class ValuesController : ApiController
{
public IEnumerable<string> Get()
{
return new List<string> { "ASP.NET", "Docker", "Windows Containers" };
}
}
public class RouteController : ApiController
{
public IHttpActionResult Get()
{
List<RouteObject> routeList = new List<RouteObject>();
var routes = GlobalConfiguration.Configuration.Routes;
foreach (var route in routes)
{
var obj = new RouteObject();
obj.RouteUrl = route.RouteTemplate;
routeList.Add(obj);
}
return Json(routeList);
}
class RouteObject
{
public string RouteUrl { get; set; }
}
}
public class UsersController : ApiController
{
public IHttpActionResult GetUsers()
{
throw new NotImplementedException();
}
}
public class CardData
{
public string CardUId { get; set; }
}
}