public StatsModule(ITemplate tmpl, IIdGenerator idgen, SavegameStorage storage) { Post["/games"] = _ => { // Get the temporary location of the file on the server var file = Request.Headers["X-FILE"].FirstOrDefault(); // Get the extension of the file when it was uploaded as the // temporary file doesn't have an extension var extension = Request.Headers["X-FILE-EXTENSION"].FirstOrDefault(); if (file == null) throw new ArgumentException("File can't be null"); if (extension == null) throw new ArgumentException("File extension can't be null"); Save savegame; using (var stream = getStream(file, extension)) using (parsingTimer.NewContext()) savegame = new Save(stream); // Turn the savegame into html and return the url for it var stats = statsTimer.Time(() => Aggregate(savegame)); string contents = templateTimer.Time(() => tmpl.Render(stats)); string id = idgen.NextId(); return storage.Store(contents, id); }; }
protected override void ConfigureApplicationContainer(TinyIoCContainer container) { base.ConfigureApplicationContainer(container); // The CLR (not mono) does lazy loading of external assemblies and // will retrieve them on demand when you use their types. the // downside of this is that the razor template can't compile // because the assembly hasn't been loaded yet, so we use a type // from the assembly to force it to be loaded. #if !__MonoCS__ var a = new TradeStats.PowerStats("a", 1, 1, 1, 1, 1, 1); #endif string exe = Assembly.GetEntryAssembly().Location; string exeDir = Path.GetDirectoryName(exe); string tmplFile = Path.Combine(exeDir, "template.html"); Templater tmpl = new Templater(tmplFile); container.Register<ITemplate>(tmpl); string gamedir = Path.Combine(exeDir, "..", "games"); if (!Directory.Exists(gamedir)) Directory.CreateDirectory(gamedir); var gen = new IncrementIdGenerator(gamedir); container.Register<IIdGenerator>(gen); var module = new SavegameStorage(gamedir); container.Register<SavegameStorage>(module); }