/* * Create a web server described by a config file. */ MaHttp *maCreateWebServer(cchar *configFile) { Mpr *mpr; MaHttp *http; MaServer *server; /* * Initialize and start the portable runtime services. */ if ((mpr = mprCreate(0, NULL, NULL)) == 0) { mprError(mpr, "Can't create the web server runtime"); return 0; } if (mprStart(mpr, 0) < 0) { mprError(mpr, "Can't start the web server runtime"); return 0; } http = maCreateHttp(mpr); if ((server = maCreateServer(http, configFile, NULL, NULL, -1)) == 0) { mprError(mpr, "Can't create the web server"); return 0; } if (maParseConfig(server, configFile) < 0) { mprError(mpr, "Can't parse the config file %s", configFile); return 0; } return http; }
/* Create a simple stand-alone web server */ int main(int argc, char **argv, char **envp) { Mpr *mpr; MaAppweb *appweb; MaServer *server; int rc; rc = MPR_ERR_CANT_CREATE; if ((mpr = mprCreate(0, NULL, MPR_USER_EVENTS_THREAD)) == 0) { mprError("Cannot create the web server runtime"); return -1; } mprStart(); appweb = maCreateAppweb(mpr); mprAddRoot(appweb); server = maCreateServer(appweb, 0); if (maParseConfig(server, "appweb.conf", 0) < 0) { mprError("Cannot parse the config file %s", "appweb.conf"); return -1; } httpDefineAction("/action/myaction", myaction); if (maStartServer(server) < 0) { mprError("Cannot start the web server"); return -1; } mprServiceEvents(-1, 0); maStopServer(server); mprRemoveRoot(appweb); mprDestroy(MPR_EXIT_DEFAULT); return 0; }
static int initialize(cchar *ip, int port) { if ((app->appweb = maCreateAppweb()) == 0) { mprUserError("Can't create HTTP service for %s", mprGetAppName()); return MPR_ERR_CANT_CREATE; } MPR->appwebService = app->appweb; if ((app->server = maCreateServer(app->appweb, "default")) == 0) { mprUserError("Can't create HTTP server for %s", mprGetAppName()); return MPR_ERR_CANT_CREATE; } if (maConfigureServer(app->server, app->configFile, app->home, app->documents, ip, port) < 0) { /* mprUserError("Can't configure the server, exiting."); */ return MPR_ERR_CANT_CREATE; } if (app->workers >= 0) { mprSetMaxWorkers(app->workers); } #if BLD_WIN_LIKE writePort(app->server); #elif BLD_UNIX_LIKE app->traceToggle = mprAddSignalHandler(SIGUSR2, traceHandler, 0, 0, MPR_SIGNAL_AFTER); #endif return 0; }
static int createEndpoints(int argc, char **argv) { cchar *endpoint; char *ip; int argind, port, secure; ip = 0; port = -1; endpoint = 0; argind = 0; if ((app->appweb = maCreateAppweb()) == 0) { mprLog("error appweb", 0, "Cannot create HTTP service"); return MPR_ERR_CANT_CREATE; } if ((app->server = maCreateServer(app->appweb, "default")) == 0) { mprLog("error appweb", 0, "Cannot create HTTP server"); return MPR_ERR_CANT_CREATE; } loadStaticModules(); mprGC(MPR_GC_FORCE | MPR_GC_COMPLETE); if (argind == argc) { if (maParseConfig(app->server, app->configFile, 0) < 0) { return MPR_ERR_CANT_CREATE; } } else { app->documents = sclone(argv[argind++]); if (argind == argc) { if (maConfigureServer(app->server, NULL, app->home, app->documents, NULL, ME_HTTP_PORT, 0) < 0) { return MPR_ERR_CANT_CREATE; } } else while (argind < argc) { endpoint = argv[argind++]; mprParseSocketAddress(endpoint, &ip, &port, &secure, 80); if (maConfigureServer(app->server, NULL, app->home, app->documents, ip, port, 0) < 0) { return MPR_ERR_CANT_CREATE; } } } if (app->workers >= 0) { mprSetMaxWorkers(app->workers); } /* Call any ESP initializers from slink.c */ appwebStaticInitialize(); #if ME_WIN_LIKE writePort(app->server); #elif ME_UNIX_LIKE addSignals(); #endif mprGC(MPR_GC_FORCE | MPR_GC_COMPLETE); return 0; }
int maRunSimpleWebServer(cchar *ipAddr, int port, cchar *docRoot) { Mpr *mpr; MaHttp *http; MaServer *server; /* * Initialize and start the portable runtime services. */ if ((mpr = mprCreate(0, NULL, NULL)) == 0) { mprError(mpr, "Can't create the web server runtime"); return MPR_ERR_CANT_CREATE; } if (mprStart(mpr, 0) < 0) { mprError(mpr, "Can't start the web server runtime"); return MPR_ERR_CANT_INITIALIZE; } /* * Create the HTTP object. */ if ((http = maCreateHttp(mpr)) == 0) { mprError(mpr, "Can't create the web server http services"); return MPR_ERR_CANT_INITIALIZE; } /* * Create and start the HTTP server. Give the server a name of "default" and define "." as * the default serverRoot, ie. the directory with the server configuration files. */ server = maCreateServer(http, ipAddr, ".", ipAddr, port); if (server == 0) { mprError(mpr, "Can't create the web server"); return MPR_ERR_CANT_CREATE; } maSetDocumentRoot(server->defaultHost, docRoot); if (maStartHttp(http) < 0) { mprError(mpr, "Can't start the web server"); return MPR_ERR_CANT_CREATE; } mprServiceEvents(mprGetDispatcher(mpr), -1, MPR_SERVICE_EVENTS | MPR_SERVICE_IO); maStopHttp(http); mprFree(mpr); return 0; }
/* Run a web server not based on a config file. */ int maRunSimpleWebServer(cchar *ip, int port, cchar *home, cchar *documents) { Mpr *mpr; MaServer *server; MaAppweb *appweb; int rc; /* Initialize and start the portable runtime services. */ rc = MPR_ERR_CANT_CREATE; if ((mpr = mprCreate(0, NULL, 0)) == 0) { mprError("Can't create the web server runtime"); } else { if (mprStart(mpr) < 0) { mprError("Can't start the web server runtime"); } else { if ((appweb = maCreateAppweb(mpr)) == 0) { mprError("Can't create the web server http services"); } else { mprAddRoot(appweb); if ((server = maCreateServer(appweb, 0)) == 0) { mprError("Can't create the web server"); } else { if (maConfigureServer(server, 0, home, documents, ip, port) < 0) { mprError("Can't create the web server"); } else { if (maStartServer(server) < 0) { mprError("Can't start the web server"); } else { mprServiceEvents(-1, 0); rc = 0; } maStopServer(server); } } mprRemoveRoot(appweb); } } mprDestroy(MPR_EXIT_DEFAULT); } return rc; }
static int runServer(cchar *configFile, cchar *ip, int port, cchar *home, cchar *documents) { MaAppweb *appweb; MaServer *server; if (mprStart() < 0) { mprLog("error appweb", 0, "Cannot start the web server runtime"); return MPR_ERR_CANT_CREATE; } if ((appweb = maCreateAppweb()) == 0) { mprLog("error appweb", 0, "Cannot create appweb object"); return MPR_ERR_CANT_CREATE; } mprAddRoot(appweb); if ((server = maCreateServer(appweb, 0)) == 0) { mprLog("error appweb", 0, "Cannot create the web server"); mprRemoveRoot(appweb); return MPR_ERR_CANT_CREATE; } if (home) { if (maConfigureServer(server, 0, home, documents, ip, port, 0) < 0) { mprLog("error appweb", 0, "Cannot create the web server"); mprRemoveRoot(appweb); return MPR_ERR_BAD_STATE; } } else { if (maParseConfig(server, configFile, 0) < 0) { mprLog("error appweb", 0, "Cannot parse the config file %s", configFile); mprRemoveRoot(appweb); return MPR_ERR_CANT_READ; } } if (maStartServer(server) < 0) { mprLog("error appweb", 0, "Cannot start the web server"); mprRemoveRoot(appweb); return MPR_ERR_CANT_COMPLETE; } mprServiceEvents(-1, 0); maStopServer(server); mprRemoveRoot(appweb); return 0; }
/* Create a web server described by a config file. */ int maRunWebServer(cchar *configFile) { Mpr *mpr; MaAppweb *appweb; MaServer *server; int rc; rc = MPR_ERR_CANT_CREATE; if ((mpr = mprCreate(0, NULL, 0)) == 0) { mprError("Can't create the web server runtime"); } else { if (mprStart() < 0) { mprError("Can't start the web server runtime"); } else { if ((appweb = maCreateAppweb(mpr)) == 0) { mprError("Can't create appweb object"); } else { mprAddRoot(appweb); if ((server = maCreateServer(appweb, 0)) == 0) { mprError("Can't create the web server"); } else { if (maParseConfig(server, configFile, 0) < 0) { mprError("Can't parse the config file %s", configFile); } else { if (maStartServer(server) < 0) { mprError("Can't start the web server"); } else { mprServiceEvents(-1, 0); rc = 0; } maStopServer(server); } } mprRemoveRoot(appweb); } } } mprDestroy(MPR_EXIT_DEFAULT); return rc; }
/* Create the listening endoints */ static int createEndpoints(int argc, char **argv) { cchar *endpoint; char *ip; int argind, port, secure; ip = 0; port = -1; endpoint = 0; argind = 0; if ((app->appweb = maCreateAppweb()) == 0) { mprError("Cannot create HTTP service for %s", mprGetAppName()); return MPR_ERR_CANT_CREATE; } if ((app->server = maCreateServer(app->appweb, "default")) == 0) { mprError("Cannot create HTTP server for %s", mprGetAppName()); return MPR_ERR_CANT_CREATE; } loadStaticModules(); if (argc > argind) { app->documents = sclone(argv[argind++]); mprLog(2, "Documents %s", app->documents); } if (argind == argc) { if (maParseConfig(app->server, app->configFile, 0) < 0) { return MPR_ERR_CANT_CREATE; } } else { while (argind < argc) { endpoint = argv[argind++]; mprParseSocketAddress(endpoint, &ip, &port, &secure, 80); if (maConfigureServer(app->server, NULL, app->home, app->documents, ip, port) < 0) { return MPR_ERR_CANT_CREATE; } } } return 0; }
MAIN(appweb, int argc, char **argv) { Mpr *mpr; MaHttp *http; cchar *ipAddrPort, *documentRoot, *argp, *logSpec; char *configFile, *ipAddr, *homeDir, *timeText, *ejsPrefix, *ejsPath, *changeRoot; int workers, outputVersion, argind, port; documentRoot = 0; changeRoot = ejsPrefix = ejsPath = 0; ipAddrPort = 0; ipAddr = 0; port = -1; logSpec = 0; server = 0; outputVersion = 0; workers = -1; configFile = BLD_FEATURE_CONFIG_FILE; homeDir = BLD_FEATURE_SERVER_ROOT; mpr = mprCreate(argc, argv, memoryFailure); argc = mpr->argc; argv = mpr->argv; #if BLD_FEATURE_ROMFS extern MprRomInode romFiles[]; mprSetRomFileSystem(mpr, romFiles); #endif if (osInit(mpr) < 0) { exit(2); } if (mprStart(mpr, 0) < 0) { mprUserError(mpr, "Can't start MPR for %s", mprGetAppName(mpr)); mprFree(mpr); return MPR_ERR_CANT_INITIALIZE; } for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } if (strcmp(argp, "--config") == 0) { if (argind >= argc) { return printUsage(mpr); } configFile = argv[++argind]; #if BLD_UNIX_LIKE } else if (strcmp(argp, "--chroot") == 0) { if (argind >= argc) { return printUsage(mpr); } changeRoot = mprGetAbsPath(mpr, argv[++argind]); #endif } else if (strcmp(argp, "--debug") == 0 || strcmp(argp, "-D") == 0 || strcmp(argp, "-d") == 0 || strcmp(argp, "--debugger") == 0) { mprSetDebugMode(mpr, 1); } else if (strcmp(argp, "--ejs") == 0) { if (argind >= argc) { return printUsage(mpr); } ejsPrefix = mprStrdup(mpr, argv[++argind]); if ((ejsPath = strchr(ejsPrefix, ':')) != 0) { *ejsPath++ = '\0'; } ejsPath = mprGetAbsPath(mpr, ejsPath); } else if (strcmp(argp, "--home") == 0) { if (argind >= argc) { return printUsage(mpr); } homeDir = mprGetAbsPath(mpr, argv[++argind]); if (chdir((char*) homeDir) < 0) { mprPrintfError(mpr, "%s: Can't change directory to %s\n", mprGetAppName(mpr), homeDir); exit(2); } } else if (strcmp(argp, "--log") == 0 || strcmp(argp, "-l") == 0) { if (argind >= argc) { return printUsage(mpr); } logSpec = argv[++argind]; maStartLogging(mpr, logSpec); } else if (strcmp(argp, "--name") == 0 || strcmp(argp, "-n") == 0) { if (argind >= argc) { return printUsage(mpr); } mprSetAppName(mpr, argv[++argind], 0, 0); } else if (strcmp(argp, "--threads") == 0) { if (argind >= argc) { return printUsage(mpr); } workers = atoi(argv[++argind]); } else if (strcmp(argp, "--verbose") == 0 || strcmp(argp, "-v") == 0) { maStartLogging(mpr, "stdout:2"); } else if (strcmp(argp, "--version") == 0 || strcmp(argp, "-V") == 0) { outputVersion++; } else { mprPrintfError(mpr, "Unknown switch \"%s\"\n", argp); printUsage(mpr); exit(2); } } if (argc > argind) { if (argc > (argind + 2)) { return printUsage(mpr); } ipAddrPort = argv[argind++]; if (argc > argind) { documentRoot = argv[argind++]; } else { documentRoot = "."; } } if (outputVersion) { mprPrintf(mpr, "%s %s-%s\n", mprGetAppTitle(mpr), BLD_VERSION, BLD_NUMBER); exit(0); } if (ipAddrPort) { mprParseIp(mpr, ipAddrPort, &ipAddr, &port, MA_SERVER_DEFAULT_PORT_NUM); } else { #if BLD_FEATURE_CONFIG_PARSE if (configFile == 0) { configFile = mprStrcat(mpr, -1, mprGetAppName(mpr), ".conf", NULL); } if (!mprPathExists(mpr, configFile, R_OK)) { mprPrintfError(mpr, "Can't open config file %s\n", configFile); exit(2); } #else return printUsage(mpr); #endif #if !BLD_FEATURE_ROMFS if (homeDir == 0) { homeDir = mprGetPathParent(mpr, configFile); if (chdir((char*) homeDir) < 0) { mprPrintfError(mpr, "%s: Can't change directory to %s\n", mprGetAppName(mpr), homeDir); exit(2); } } #endif } homeDir = mprGetCurrentPath(mpr); if (checkEnvironment(mpr, argv[0], homeDir) < 0) { exit(3); } #if BLD_UNIX_LIKE if (changeRoot) { homeDir = mprGetAbsPath(mpr, changeRoot); if (chdir(homeDir) < 0) { mprError(mpr, "%s: Can't change directory to %s", homeDir); exit(4); } if (chroot(homeDir) < 0) { if (errno == EPERM) { mprError(mpr, "%s: Must be super user to use the --chroot option", mprGetAppName(mpr)); } else { mprError(mpr, "%s: Can't change change root directory to %s, errno %d", mprGetAppName(mpr), homeDir, errno); } exit(5); } } #endif /* * Create the top level HTTP service and default HTTP server. Set the initial server root to "." */ http = maCreateHttp(mpr); if (http == 0) { mprUserError(mpr, "Can't create HTTP service for %s", mprGetAppName(mpr)); return MPR_ERR_CANT_INITIALIZE; } server = maCreateServer(http, "default", ".", 0, -1); if (server == 0) { mprUserError(mpr, "Can't create HTTP server for %s", mprGetAppName(mpr)); return MPR_ERR_CANT_INITIALIZE; } if (maConfigureServer(mpr, http, server, configFile, ipAddr, port, documentRoot) < 0) { /* mprUserError(mpr, "Can't configure the server, exiting."); */ exit(6); } if (mpr->ipAddr) { mprLog(mpr, 2, "Server IP address %s", mpr->ipAddr); } timeText = mprFormatLocalTime(mpr, mprGetTime(mpr)); mprLog(mpr, 1, "Started at %s", timeText); mprFree(timeText); #if BLD_FEATURE_EJS if (ejsPrefix) { createEjsAlias(mpr, http, server, ejsPrefix, ejsPath); } #endif #if BLD_FEATURE_MULTITHREAD if (workers >= 0) { mprSetMaxWorkers(http, workers); } #endif #if BLD_WIN_LIKE if (!ejsPrefix) { writePort(server->defaultHost); } #endif if (maStartHttp(http) < 0) { mprUserError(mpr, "Can't start HTTP service, exiting."); exit(7); } #if BLD_FEATURE_MULTITHREAD mprLog(mpr, 1, "HTTP services are ready with max %d worker threads", mprGetMaxWorkers(mpr)); #else mprLog(mpr, 1, "HTTP services are ready (single-threaded)"); #endif /* * Service I/O events until instructed to exit */ while (!mprIsExiting(mpr)) { mprServiceEvents(mpr->dispatcher, -1, MPR_SERVICE_EVENTS | MPR_SERVICE_IO); } /* * Signal a graceful shutdown */ mprLog(http, 1, "Exiting ..."); maStopHttp(http); mprLog(http, 1, "Exit complete"); #if VXWORKS if (mprStop(mpr)) { mprFree(mpr); } #endif return 0; }
int main(int argc, char** argv) { MaHttp *http; /* For the http service inside our app */ MaServer *server; /* For a HTTP server */ /* * Initialize the run-time and give our app a name * "pollEventLoop" */ mprCreateMpr("pollEventLoop"); #if BLD_FEATURE_LOG /* * Do the following two statements only if you want debug trace */ mprAddLogFileListener(); mprSetLogSpec("stdout:4"); #endif /* * Start run-time services */ mprStartMpr(0); /* * Create the HTTP and server objects. Give the server a name * "default" and define "." as the default serverRoot, ie. the * directory with the server configuration files. */ http = maCreateHttp(); server = maCreateServer(http, "default", "."); /* * Activate the copy handler. Only needed when linking statically. */ mprCopyInit(0); /* * Configure the server based on the directives in * pollEventLoop.conf. */ if (maConfigureServer(server, "pollEventLoop.conf") < 0) { fprintf(stderr, "Can't configure the server. Error on line %d\n", maGetConfigErrorLine(server)); exit(2); } /* * Start serving pages. After this we are live. */ if (maStartServers(http) < 0) { fprintf(stderr, "Can't start the server\n"); exit(2); } /* * Service events. This call will block until the server is exited * Call mprTerminate() at any time to instruct the server to exit. */ eventLoop(); /* * Stop all HTTP services */ maStopServers(http); /* * Delete the server and http objects */ maDeleteServer(server); maDeleteHttp(http); /* * Stop and delete the run-time services */ mprStopMpr(); mprDeleteMpr(); return 0; }