int StopWebInterface() { close_dynamic_content(); AmmServer_Stop(flashy_server); return 0; }
int closeAmmarServer() { //Delete dynamic content allocations and remove stats.html and formtest.html from the server close_dynamic_content(); //Stop the server and clean state AmmServer_Stop(default_server); AmmServer_Warning("Ammar Server stopped\n"); return 1; }
int main(int argc, char *argv[]) { printf("\nAmmar Server %s starting up..\n",AmmServer_Version()); //Check binary and header spec AmmServer_CheckIfHeaderBinaryAreTheSame(AMMAR_SERVER_HTTP_HEADER_SPEC); //Register termination signal for when we receive SIGKILL etc AmmServer_RegisterTerminationSignal(&close_dynamic_content); char bindIP[MAX_IP_STRING_SIZE]; strcpy(bindIP,"0.0.0.0"); unsigned int port=DEFAULT_BINDING_PORT; //Kick start AmmarServer , bind the ports , create the threads and get things going..! default_server = AmmServer_StartWithArgs( "MySearch", argc,argv , //The internal server will use the arguments to change settings //If you don't want this look at the AmmServer_Start call bindIP, port, 0, /*This means we don't want a specific configuration file*/ webserver_root, templates_root ); if (!default_server) { AmmServer_Error("Could not start server , shutting down everything.."); exit(1); } //Create dynamic content allocations and associate context to the correct files init_dynamic_content(); //stats.html and formtest.html should be availiable from now on..! while ( (AmmServer_Running(default_server)) ) { //Main thread should just sleep and let the background threads do the hard work..! //In other applications the programmer could use the main thread to do anything he likes.. //The only caveat is that he would takeup more CPU time from the server and that he would have to poll //the AmmServer_Running() call once in a while to make sure everything is in order //usleep(60000); sleep(1); } //Delete dynamic content allocations and remove stats.html and formtest.html from the server close_dynamic_content(); //Stop the server and clean state AmmServer_Stop(default_server); AmmServer_Warning("Ammar Server stopped\n"); return 0; }
struct AmmServer_Instance * AmmServer_Start( const char * name , const char * ip, unsigned int port, const char * conf_file, const char * web_root_path, const char * templates_root_path ) { fprintf(stderr,"Binding AmmarServer v%s to %s:%u\n",FULLVERSION_STRING,ip,port); fprintf(stderr,"\n\nDISCLAIMER : \n"); fprintf(stderr,"Please note that this server version is not thoroughly\n"); fprintf(stderr," pen-tested so it is not meant for production deployment..\n"); fprintf(stderr,"Bug reports and feedback are very welcome.. \n"); fprintf(stderr,"via https://github.com/AmmarkoV/AmmarServer/issues\n\n"); //log/ could be a global directory snprintf(AccessLog,MAX_FILE_PATH,"log/%s_access.log",name); snprintf(ErrorLog,MAX_FILE_PATH,"log/%s_error.log",name); fprintf(stderr,"Access logged @ %s , Errors logged @ %s \n ",AccessLog,ErrorLog); //Allocate and Clear instance.. struct AmmServer_Instance * instance = (struct AmmServer_Instance *) malloc(sizeof(struct AmmServer_Instance)); if (!instance) { fprintf(stderr,"AmmServer_Start failed to allocate a new instance \n"); } else { memset(instance,0,sizeof(struct AmmServer_Instance)); } fprintf(stderr,"Initial AmmServer_Start instance pointing @ %p \n",instance);//Clear instance..! instance->threads_pool = (pthread_t *) malloc( sizeof(pthread_t) * MAX_CLIENT_THREADS); if (!instance->threads_pool) { fprintf(stderr,"AmmServer_Start failed to allocate %u records for a thread pool\n",MAX_CLIENT_THREADS); } else { memset(instance->threads_pool,0,sizeof(pthread_t)*MAX_CLIENT_THREADS); } strncpy(instance->instanceName , name , MAX_INSTANCE_NAME_STRING); // TODO: check for MAX_INSTANCE_NAME_STRING fprintf(stderr,"Initial AmmServer_Start ( name %s ) thread pool pointing @ %p \n",instance->instanceName,instance->threads_pool);//Clear instance..! instance->prespawned_pool = (void *) malloc( sizeof(struct PreSpawnedThread) * MAX_CLIENT_PRESPAWNED_THREADS); if (!instance->prespawned_pool) { fprintf(stderr,"AmmServer_Start failed to allocate %u records for a prespawned thread pool\n",MAX_CLIENT_PRESPAWNED_THREADS); }else { if (MAX_CLIENT_PRESPAWNED_THREADS>0) { memset(instance->prespawned_pool,0,sizeof(pthread_t)*MAX_CLIENT_PRESPAWNED_THREADS); } } //LoadConfigurationFile happens before dropping root id so we are more sure that we will manage to read the configuration file.. LoadConfigurationFile(instance,conf_file); //LoadConfigurationFile may set a binding port but if the parent call set a nonzero a port setting here it overrides configuration file.... if (port!=0) { instance->settings.BINDING_PORT = port; } //This line explains configuration conflicts in a user understandable manner :p EmmitPossibleConfigurationWarnings(instance); cache_Initialize( instance, /*These are the file cache settings , file caching is the mechanism that holds dynamic content and speeds up file serving by not accessing the whole disk drive subsystem ..*/ MAX_SEPERATE_CACHE_ITEMS , /*Seperate items*/ MAX_CACHE_SIZE_IN_MB , /*MB Limit for the WHOLE Cache*/ MAX_CACHE_SIZE_FOR_EACH_FILE_IN_MB /*MB Max Size of Individual File*/ ); if (StartHTTPServer(instance,ip,instance->settings.BINDING_PORT,web_root_path,templates_root_path)) { //All is well , we return a valid instance AccessLogAppend("127.0.0.1",0,"startup",1,0,"startup","ammarserver"); return instance; } else { AmmServer_Stop(instance); return 0; } return 0; }