Пример #1
0
 var sendFileResponse = function(req, res, fullPath, opt_prepFn, runtimeInfo) {
   debug('path: ' + fullPath);
   var mimeType = mime.lookup(fullPath);
   if (mimeType) {
     // This is scary. If there's any async between here and
     // the actual read we could get the wrong enable.
     es6Support.enable(runtimeInfo ? runtimeInfo.features.es6 : true);
     fileCache.readFile(fullPath, function(err, data){
       if (err) {
         console.error('' + err + ': ' + fullPath);
         console.error('ip: ' + getRequestIpAddress(req));
         console.error(JSON.stringify(req.headers, undefined, '  '));
         return send404(res);
       }
       if (opt_prepFn) {
         data = opt_prepFn(data.toString());
       }
       if (strings.startsWith(mimeType, 'text')) {
         res.writeHead(200, {
           'Content-Type':  mimeType + '; charset=utf-8',
           'Cache-Control': 'no-cache, no-store, must-revalidate', // HTTP 1.1.
           'Pragma':        'no-cache',                            // HTTP 1.0.
           'Expires':       '0',                                   // Proxies.
         });
         res.write(data, 'utf8');
         res.end();
       } else {
         sendStringResponse(res, data, mimeType);
       }
     });
   } else {
     send404(res);
   }
 };
Пример #2
0
    theApp.get(pathRegex, function(req, res) {
      var gameId = req.params[0];
      var runtimeInfo = g.gameDB.getGameById(gameId);
      if (!runtimeInfo) {
        var msg = [
          'url:' + req.url,
          'unknown gameId: ' + gameId,
          'have you run `hft add` for the game in ' + path.dirname(contentPath),
        ].join('\n');
        console.error(msg);
        return send404(res, msg);
      }

      if (!runtimeInfo.useTemplate[templateName]) {
        return sendGameRequestedFile(req, res);
      }

      var templatePath = runtimeInfo.versionSettings.templates[templateName];
      templatePath = path.normalize(path.join(g.cwd, templatePath));

      var contentFullPath = path.normalize(path.join(runtimeInfo.htmlPath, contentPath));

      fileCache.readFile(templatePath, function(err, templateData) {
        if (err) {
          console.error('' + err + ': ' + templatePath);
          return send404(res);
        }
        sendFileResponse(req, res, contentFullPath, function(str) {
          var result = strings.replaceParams(templateData.toString(), [
            runtimeInfo,
            {
              content: str,
              hftSettings: 'window.hftSettings = ' + JSON.stringify({
                menu: g.menu,
                apiVersion: runtimeInfo.info.happyFunTimes.apiVersion,
              }),
            },
          ]);
          return result;
        });
      });
    });
Пример #3
0
    theApp.get(pathRegex, function(req, res) {
      var gameId = req.params[0];
      var filename = req.params[1] || templateName;
      var runtimeInfo = g.gameDB.getGameById(gameId);
      if (!runtimeInfo) {
        var msg = [
          'url:' + req.url,
          'unknown gameId: ' + gameId,
          'have you run `hft add` for the game in ' + path.dirname(contentPath),
        ].join('\n');
        console.error(msg);
        return send404(res, msg);
      }

      if (!templateName) {
        contentPath = filename + ".html";
        var urlRuntimeInfo = runtimeInfo.templateFileOptions[contentPath];
        if (urlRuntimeInfo && urlRuntimeInfo.urlInfo) {
          templateName = urlRuntimeInfo.urlInfo.template;
        }
      }

      if (!runtimeInfo.useTemplate[templateName]) {
        return sendGameRequestedFile(req, res);
      }

      var templatePath = runtimeInfo.versionSettings.templates[templateName];
      templatePath = path.normalize(path.join(g.cwd, templatePath));

      var contentFullPath = path.normalize(path.join(runtimeInfo.htmlPath, contentPath));

      fileCache.readFile(templatePath, function(err, templateData) {
        if (err) {
          console.error('' + err + ': ' + templatePath);
          return send404(res);
        }
        sendFileResponse(req, res, contentFullPath, function(str) {
          debug("doing substitutions for:", contentPath);
          var scriptParams = {
            filename: filename,
          };
          var result = strings.replaceParams(templateData.toString(), [
            {
              filename: filename,
              pages: {
                game: {
                  beforeScripts: strings.replaceParams(runtimeInfo.pages.game.beforeScripts, scriptParams),
                  afterScripts: strings.replaceParams(runtimeInfo.pages.game.afterScripts, scriptParams),
                },
                controller: {
                  beforeScripts: strings.replaceParams(runtimeInfo.pages.controller.beforeScripts, scriptParams),
                  afterScripts: strings.replaceParams(runtimeInfo.pages.controller.afterScripts, scriptParams),
                },
              },
            },
            runtimeInfo,
            {
              content: str,
              hftSettings: 'window.hftSettings = ' + JSON.stringify({
                menu: g.menu,
                apiVersion: runtimeInfo.info.happyFunTimes.apiVersion,
              }),
            },
          ]);
          return result;
        });
      });
    });