goahead.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. goahead.c -- Main program for GoAhead
  3. Usage: goahead [options] [documents] [IP][:port] ...
  4. Options:
  5. --auth authFile # User and role configuration
  6. --background # Run as a Linux daemon
  7. --home directory # Change to directory to run
  8. --log logFile:level # Log to file file at verbosity level
  9. --route routeFile # Route configuration file
  10. --verbose # Same as --log stdout:2
  11. --version # Output version information
  12. Copyright (c) All Rights Reserved. See details at the end of the file.
  13. */
  14. /********************************* Includes ***********************************/
  15. #include "goahead.h"
  16. /********************************* Defines ************************************/
  17. static int finished = 0;
  18. /********************************* Forwards ***********************************/
  19. static void initPlatform(void);
  20. static void logHeader(void);
  21. static void usage(void);
  22. #if WINDOWS
  23. static void windowsClose();
  24. static int windowsInit();
  25. static LRESULT CALLBACK websWindProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp);
  26. #endif
  27. #if ME_UNIX_LIKE
  28. static void sigHandler(int signo);
  29. #endif
  30. static void buy(Webs *wp);
  31. static void actionTest(Webs *wp);
  32. // static void personInfoAction(Webs *wp);
  33. static void buy(Webs *wp)
  34. {
  35. char *name, *age;
  36. name = websGetVar(wp, "name", NULL);
  37. age = websGetVar(wp, "age", NULL);
  38. logmsg(2, "---------------------------------------------");
  39. logmsg(2, "name value is : %s", name );
  40. logmsg(2, "age value is : %s", age );
  41. websSetStatus(wp, 200);
  42. websWriteHeaders(wp, 0, 0);
  43. websWriteEndHeaders(wp);
  44. printf("aa");
  45. websWrite(wp, "Name %s", name);
  46. websWrite(wp, "Age %s", age);
  47. websFlush(wp, 0);
  48. websDone(wp);
  49. }
  50. /*
  51. Implement /action/actionTest. Parse the form variables: name, address and echo back.
  52. */
  53. static void actionTest(Webs *wp)
  54. {
  55. cchar *name, *address;
  56. name = websGetVar(wp, "name", NULL);
  57. address = websGetVar(wp, "address", NULL);
  58. websSetStatus(wp, 200);
  59. websWriteHeaders(wp, -1, 0);
  60. websWriteEndHeaders(wp);
  61. websWrite(wp, "<html><body><h2>name: %s, address: %s</h2></body></html>\n", name, address);
  62. websFlush(wp, 0);
  63. websDone(wp);
  64. }
  65. /*********************************** Code *************************************/
  66. MAIN(goahead, int argc, char **argv, char **envp)
  67. {
  68. char *argp, *home, *documents, *endpoints, *endpoint, *route, *auth, *tok, *lspec;
  69. int argind;
  70. logmsg(2, "---> log1\n");
  71. error("---> log1\n");
  72. #if WINDOWS
  73. if (windowsInit() < 0) {
  74. return 0;
  75. }
  76. #endif
  77. route = "route.txt";
  78. auth = "auth.txt";
  79. error("---> main enter, argc = %d\n", argc);
  80. for (argind = 1; argind < argc; argind++) {
  81. argp = argv[argind];
  82. error("---> argp = %s\n", argp);
  83. if (*argp != '-') {
  84. break;
  85. } else if (smatch(argp, "--auth") || smatch(argp, "-a")) {
  86. if (argind >= argc) usage();
  87. auth = argv[++argind];
  88. #if ME_UNIX_LIKE && !MACOSX
  89. } else if (smatch(argp, "--background") || smatch(argp, "-b")) {
  90. websSetBackground(1);
  91. #endif
  92. } else if (smatch(argp, "--debugger") || smatch(argp, "-d") || smatch(argp, "-D")) {
  93. websSetDebug(1);
  94. } else if (smatch(argp, "--home")) {
  95. if (argind >= argc) usage();
  96. home = argv[++argind];
  97. if (chdir(home) < 0) {
  98. error("Cannot change directory to %s", home);
  99. exit(-1);
  100. }
  101. error("---> get home\n");
  102. } else if (smatch(argp, "--log") || smatch(argp, "-l")) {
  103. if (argind >= argc) usage();
  104. logSetPath(argv[++argind]);
  105. } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) {
  106. logSetPath("stdout:2");
  107. error( "---> get -v\n");
  108. } else if (smatch(argp, "--route") || smatch(argp, "-r")) {
  109. route = argv[++argind];
  110. } else if (smatch(argp, "--version") || smatch(argp, "-V")) {
  111. printf("%s\n", ME_VERSION);
  112. exit(0);
  113. } else if (*argp == '-' && isdigit((uchar) argp[1])) {
  114. lspec = sfmt("stdout:%s", &argp[1]);
  115. logSetPath(lspec);
  116. wfree(lspec);
  117. } else {
  118. usage();
  119. }
  120. }
  121. documents = ME_GOAHEAD_DOCUMENTS;
  122. if (argc > argind) {
  123. documents = argv[argind++];
  124. }
  125. error("---> initPlatform\n");
  126. initPlatform();
  127. if (websOpen(documents, route) < 0) {
  128. error("Cannot initialize server. Exiting.");
  129. return -1;
  130. }
  131. #if ME_GOAHEAD_AUTH
  132. error("---> ME_GOAHEAD_AUTH: %d\n", ME_GOAHEAD_AUTH);
  133. if (websLoad(auth) < 0) {
  134. error("Cannot load %s", auth);
  135. return -1;
  136. }
  137. #endif
  138. logHeader();
  139. error("---> argind = %d, argc = %d\n", argind, argc);
  140. if (argind < argc) {
  141. while (argind < argc) {
  142. endpoint = argv[argind++];
  143. if (websListen(endpoint) < 0) {
  144. return -1;
  145. }
  146. }
  147. } else {
  148. endpoints = sclone(ME_GOAHEAD_LISTEN);
  149. for (endpoint = stok(endpoints, ", \t", &tok); endpoint; endpoint = stok(NULL, ", \t,", &tok)) {
  150. #if !ME_COM_SSL
  151. error("ME_COM_SSL %d\n", ME_COM_SSL);
  152. if (strstr(endpoint, "https")) continue;
  153. #endif
  154. if (websListen(endpoint) < 0) {
  155. wfree(endpoints);
  156. return -1;
  157. }
  158. }
  159. wfree(endpoints);
  160. }
  161. #if ME_ROM && KEEP
  162. /*
  163. If not using a route/auth config files, then manually create the routes like this:
  164. If custom matching is required, use websSetRouteMatch. If authentication is required, use websSetRouteAuth.
  165. */
  166. websAddRoute("/", "file", 0);
  167. #endif
  168. //add by lusa start
  169. websDefineAction("buy", buy);
  170. websDefineAction("test", actionTest);
  171. websDefineAction("person", personInfoAction);
  172. // add by lusa end
  173. #ifdef GOAHEAD_INIT
  174. /*
  175. Define your init function in main.me goahead.init, or
  176. configure with DFLAGS=GOAHEAD_INIT=myInitFunction
  177. */
  178. {
  179. extern int GOAHEAD_INIT();
  180. if (GOAHEAD_INIT() < 0) {
  181. exit(1);
  182. }
  183. }
  184. #endif
  185. #if ME_UNIX_LIKE && !MACOSX
  186. /*
  187. Service events till terminated
  188. */
  189. if (websGetBackground()) {
  190. if (daemon(0, 0) < 0) {
  191. error("Cannot run as daemon");
  192. return -1;
  193. }
  194. }
  195. #endif
  196. error( "---> websServiceEvents\n");
  197. websServiceEvents(&finished);
  198. logmsg(1, "Instructed to exit");
  199. websClose();
  200. #if WINDOWS
  201. windowsClose();
  202. #endif
  203. return 0;
  204. }
  205. static void logHeader(void)
  206. {
  207. char home[ME_GOAHEAD_LIMIT_STRING];
  208. getcwd(home, sizeof(home));
  209. logmsg(2, "Configuration for %s", ME_TITLE);
  210. logmsg(2, "---------------------------------------------");
  211. logmsg(2, "Version: %s", ME_VERSION);
  212. logmsg(2, "BuildType: %s", ME_DEBUG ? "Debug" : "Release");
  213. logmsg(2, "CPU: %s", ME_CPU);
  214. logmsg(2, "OS: %s", ME_OS);
  215. logmsg(2, "Host: %s", websGetServer());
  216. logmsg(2, "Directory: %s", home);
  217. logmsg(2, "Documents: %s", websGetDocuments());
  218. logmsg(2, "Configure: %s", ME_CONFIG_CMD);
  219. logmsg(2, "---------------------------------------------");
  220. }
  221. static void usage(void) {
  222. fprintf(stderr, "\n%s Usage:\n\n"
  223. " %s [options] [documents] [[IPaddress][:port] ...]\n\n"
  224. " Options:\n"
  225. #if ME_GOAHEAD_AUTH
  226. " --auth authFile # User and role configuration\n"
  227. #endif
  228. #if ME_UNIX_LIKE && !MACOSX
  229. " --background # Run as a Unix daemon\n"
  230. #endif
  231. " --debugger # Run in debug mode\n"
  232. " --home directory # Change to directory to run\n"
  233. " --log logFile:level # Log to file file at verbosity level\n"
  234. " --route routeFile # Route configuration file\n"
  235. " --verbose # Same as --log stdout:2\n"
  236. " --version # Output version information\n\n",
  237. ME_TITLE, ME_NAME);
  238. exit(-1);
  239. }
  240. static void initPlatform(void)
  241. {
  242. #if ME_UNIX_LIKE
  243. error("---> initPlatform, ME_OS: %s\n", ME_OS);
  244. signal(SIGTERM, sigHandler);
  245. #ifdef SIGPIPE
  246. error("---> define SIGPIPE\n");
  247. signal(SIGPIPE, SIG_IGN);
  248. #endif
  249. #elif ME_WIN_LIKE
  250. _fmode=_O_BINARY;
  251. printf("%s\n", "fff");
  252. #endif
  253. }
  254. #if ME_UNIX_LIKE
  255. static void sigHandler(int signo)
  256. {
  257. finished = 1;
  258. }
  259. #endif
  260. //#if WINDOWS
  261. ///*
  262. // Create a taskbar entry. Register the window class and create a window
  263. // */
  264. //static int windowsInit()
  265. //{
  266. // HINSTANCE inst;
  267. // WNDCLASS wc; /* Window class */
  268. // HMENU hSysMenu;
  269. // HWND hwnd;
  270. //
  271. // inst = websGetInst();
  272. // wc.style = CS_HREDRAW | CS_VREDRAW;
  273. // wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  274. // wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  275. // wc.cbClsExtra = 0;
  276. // wc.cbWndExtra = 0;
  277. // wc.hInstance = inst;
  278. // wc.hIcon = NULL;
  279. // wc.lpfnWndProc = (WNDPROC) websWindProc;
  280. // wc.lpszMenuName = wc.lpszClassName = ME_NAME;
  281. // if (! RegisterClass(&wc)) {
  282. // return -1;
  283. // }
  284. // /*
  285. // Create a window just so we can have a taskbar to close this web server
  286. // */
  287. // hwnd = CreateWindow(ME_NAME, ME_TITLE, WS_MINIMIZE | WS_POPUPWINDOW, CW_USEDEFAULT,
  288. // 0, 0, 0, NULL, NULL, inst, NULL);
  289. // if (hwnd == NULL) {
  290. // return -1;
  291. // }
  292. //
  293. // /*
  294. // Add the about box menu item to the system menu
  295. // */
  296. // hSysMenu = GetSystemMenu(hwnd, FALSE);
  297. // if (hSysMenu != NULL) {
  298. // AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
  299. // }
  300. // ShowWindow(hwnd, SW_SHOWNORMAL);
  301. // UpdateWindow(hwnd);
  302. // return 0;
  303. //}
  304. //
  305. //
  306. //static void windowsClose()
  307. //{
  308. // HINSTANCE inst;
  309. //
  310. // inst = websGetInst();
  311. // UnregisterClass(ME_NAME, inst);
  312. //}
  313. //
  314. //
  315. ///*
  316. // Main menu window message handler.
  317. // */
  318. //static LRESULT CALLBACK websWindProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp)
  319. //{
  320. // switch (msg) {
  321. // case WM_DESTROY:
  322. // PostQuitMessage(0);
  323. // finished++;
  324. // return 0;
  325. //
  326. // case WM_SYSCOMMAND:
  327. // break;
  328. // }
  329. // return DefWindowProc(hwnd, msg, wp, lp);
  330. //}
  331. //
  332. //
  333. ///*
  334. // Check for Windows Messages
  335. // */
  336. //WPARAM checkWindowsMsgLoop()
  337. //{
  338. // MSG msg;
  339. //
  340. // if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
  341. // if (!GetMessage(&msg, NULL, 0, 0) || msg.message == WM_QUIT) {
  342. // return msg.wParam;
  343. // }
  344. // TranslateMessage(&msg);
  345. // DispatchMessage(&msg);
  346. // }
  347. // return 0;
  348. //}
  349. //
  350. //
  351. ///*
  352. // Windows message handler
  353. // */
  354. //static LRESULT CALLBACK websAboutProc(HWND hwndDlg, uint msg, uint wp, long lp)
  355. //{
  356. // LRESULT lResult;
  357. //
  358. // lResult = DefWindowProc(hwndDlg, msg, wp, lp);
  359. //
  360. // switch (msg) {
  361. // case WM_CREATE:
  362. // break;
  363. // case WM_DESTROY:
  364. // break;
  365. // case WM_COMMAND:
  366. // break;
  367. // }
  368. // return lResult;
  369. //}
  370. //
  371. //#endif
  372. /*
  373. Copyright (c) Embedthis Software. All Rights Reserved.
  374. This software is distributed under commercial and open source licenses.
  375. You may use the Embedthis GoAhead open source license or you may acquire
  376. a commercial license from Embedthis Software. You agree to be fully bound
  377. by the terms of either license. Consult the LICENSE.md distributed with
  378. this software for full details and other copyrights.
  379. */