forked from maekitalo/tntnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtntnet-wizzard
executable file
·726 lines (579 loc) · 15.8 KB
/
tntnet-wizzard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#!/bin/sh
EMAIL=your-email@somewhere
usage()
{
cat <<EOF
Usage: $0 [OPTION]
--simple=app create a simple project directory
--mvc=app create a mvc project directory
--page=app add a page to the existing project
EOF
exit $1
}
checkProc()
{
echo checking for $1
if ! which $1 >/dev/null 2>&1
then
echo ERROR: $1 not installed
exit 1
fi
}
checkSystem()
{
if [ -e "$1" ]
then
echo ERROR: $1 does already exits
exit 1
fi
checkProc autoreconf
checkProc automake
}
project()
{
project=$1
echo create directories $project $project/manager $project/model $project/view $project/controller $project/resources
mkdir $project $project/manager $project/model $project/view $project/controller $project/resources
echo create $project/configure.ac
cat >$project/configure.ac <<EOF
AC_INIT([${project}], [0.1], [$EMAIL])
AM_INIT_AUTOMAKE([foreign])
AC_PROG_CXX
AC_LANG(C++)
AC_CONFIG_HEADERS([config.h])
AC_CHECK_HEADER([cxxtools/mutex.h], , AC_MSG_ERROR([cxxtools headers not found]))
AC_CHECK_HEADER([tnt/tntnet.h], , AC_MSG_ERROR([tntnet headers not found]))
# AC_CHECK_HEADER([tntdb/connection.h], , AC_MSG_ERROR([tntdb headers not found]))
AC_CHECK_PROGS(ECPPC, [ecppc])
AC_PROG_SED
AC_OUTPUT([
Makefile
])
EOF
echo create $project/Makefile.am
cat >$project/Makefile.am <<EOF
AUTOMAKE_OPTIONS = subdir-objects
bin_PROGRAMS = ${project}
noinst_HEADERS = \\
configuration.h
ecppSources = \\
index.ecpp \\
webmain.ecpp
staticSources = \\
resources/${project}.css
${project}_SOURCES = \\
main.cpp \\
configuration.cpp \\
\$(ecppSources)
nodist_${project}_SOURCES = \\
resources.cpp
${project}_LDFLAGS = -lcxxtools -ltntnet
CLEANFILES = \$(ecppSources:.ecpp=.cpp) \$(ecppSources:.ecpp=.deps) resources.cpp
EXTRA_DIST = \$(staticSources)
#
# Rules for tntnet applications
#
# resources
resources.cpp: \$(staticSources) Makefile.am
\$(ECPPC) -bb -z -n resources -p -o resources.cpp -I\$(srcdir) \$(staticSources)
SUFFIXES=.ecpp .ico .cpp
.ecpp.cpp:
\$(ECPPC) -I \$(top_srcdir)/src -n \$(basename \$<) -o \$@ \$<
.ecpp.deps:
\$(ECPPC) -M -n \$* -I \$(srcdir) -I \$(srcdir)/include \$< | \$(SED) '1s/\(.*\).cpp:/\1.cpp \1.deps:/' > \$@
.ico.cpp:
\$(ECPPC) -b -m image/x-icon -o \$@ \$<
-include \$(ecppSources:.ecpp=.deps)
EOF
echo create $project/main.cpp
cat >$project/main.cpp <<EOF
#include <iostream>
#include <configuration.h>
#include <tnt/tntnet.h>
#include <tnt/configurator.h>
#include <cxxtools/log.h>
log_define("${project}")
int main(int argc, char* argv[])
{
try
{
Configuration& configuration = Configuration::it();
if (argc > 1)
configuration.readConfiguration(argv[1]);
else
configuration.readConfiguration("${project}.conf");
log_init(configuration.loggingConfiguration());
tnt::Tntnet app;
app.listen(configuration.listenIp(), configuration.listenPort());
tnt::Configurator configurator(app);
if (configuration.sessionTimeout())
configurator.setSessionTimeout(configuration.sessionTimeout());
// configure static stuff - lookup files in static resources first
app.mapUrl("^/(.*)", "resources")
.setPathInfo("resources/\$1");
// index page
app.mapUrl("^/$", "webmain")
.setArg("next", "index");
// controller
app.mapUrl("^/(.*)$", "controller/\$1");
// view
app.mapUrl("^/(.*)$", "webmain")
.setArg("next", "view/\$1");
app.run();
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
EOF
echo create $project/webmain.ecpp
cat >$project/webmain.ecpp <<EOF
<%session scope="shared">
</%session>
<%request scope="shared">
</%request>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="${project} application">
<link rel="stylesheet" href="/${project}.css" type="text/css">
</head>
<body>
<div id="header">
<span id="title">${project} application</span>
</div>
<div id="menu">
<ul class="nav">
<li class="navitem"><a href="/">Home</a></li>
</ul>
</div>
<div id="content">
<& (request.getArg("next")) qparam >
</div>
</body>
</html>
EOF
echo create $project/${project}.conf
cat >$project/${project}.conf <<EOF
listen.port 8000
sessiontimeout 3600
# dburl postgresql:dbname=${project}
rootlogger INFO
logger.${project} INFO
EOF
echo create $project/index.ecpp
cat >$project/index.ecpp <<EOF
<h1>${project}</h1>
Welcome to the ${project} application.
EOF
echo create $project/configuration.cpp
cat >$project/configuration.cpp <<EOF
/*
* Copyright (C) 2013 Tommi Maekitalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <configuration.h>
#include <cxxtools/properties.h>
#include <fstream>
void operator>>= (const cxxtools::SerializationInfo& si, Configuration& config)
{
si.getMember("listen.ip", config._listenIp); // listen.ip is optional - default is to listen on all local interfaces
si.getMember("listen.port") >>= config._listenPort;
si.getMember("sessiontimeout", config._sessionTimeout); // sessiontimeout is optional
si.getMember("dburl", config._dburl);
config._loggingConfiguration = si;
}
Configuration::Configuration()
: _listenPort(0),
_sessionTimeout(0)
{ }
void Configuration::readConfiguration(const std::string& file)
{
std::ifstream in(file.c_str());
in >> cxxtools::Properties(Configuration::it());
}
Configuration& Configuration::it()
{
static Configuration theConfiguration;
return theConfiguration;
}
EOF
echo create $project/configuration.h
cat >$project/configuration.h <<EOF
#ifndef CONFIGURATION_H
#define CONFIGURATION_H
#include <string>
#include <cxxtools/serializationinfo.h>
class Configuration
{
friend void operator>>= (const cxxtools::SerializationInfo& si, Configuration& config);
public:
static Configuration& it();
void readConfiguration(const std::string& file);
const std::string& listenIp() const
{ return _listenIp; }
unsigned short listenPort() const
{ return _listenPort; }
unsigned sessionTimeout() const
{ return _sessionTimeout; }
const std::string& dburl() const
{ return _dburl; }
const cxxtools::SerializationInfo& loggingConfiguration() const
{ return _loggingConfiguration; }
private:
Configuration();
Configuration(const Configuration&); // no implementation
const Configuration& operator=(const Configuration&); // no implementation
std::string _listenIp;
unsigned short _listenPort;
unsigned _sessionTimeout;
std::string _dburl;
cxxtools::SerializationInfo _loggingConfiguration;
};
#endif // CONFIGURATION_H
EOF
echo create $project/$project.css
cat >$project/resources/$project.css <<EOF
/* add your css here */
EOF
echo project $project created
}
pages()
{
if [ ! -d view ]
then
echo ERROR: view directory not found
exit 1
fi
if [ ! -d controller ]
then
echo ERROR: controller directory not found
exit 1
fi
cp webmain.ecpp webmain.ecpp.new
cp Makefile.am Makefile.am.new
while [ "$1" ]
do
page=$1
shift
if [ -f view/$page.ecpp ]
then
echo $page already exists
continue
fi
echo create view/$page.ecpp
cat >view/$page.ecpp <<EOF
<h1>$page</h1>
EOF
echo create controller/$page.cpp
cat >controller/$page.cpp <<EOF
#include <tnt/component.h>
#include <tnt/componentfactory.h>
#include <tnt/httprequest.h>
#include <tnt/httpreply.h>
#include <cxxtools/log.h>
log_define("controller.page1")
namespace
{
class ${page}Controller : public tnt::Component
{
public:
unsigned operator() (tnt::HttpRequest& request, tnt::HttpReply& reply, tnt::QueryParams& qparam);
};
static tnt::ComponentFactoryImpl<${page}Controller> factory("controller/${page}");
unsigned ${page}Controller::operator() (tnt::HttpRequest& request, tnt::HttpReply& reply, tnt::QueryParams& qparam)
{
// TODO add code for processing user input
return DECLINED;
}
}
EOF
echo add view/$page.ecpp and controller/$page.cpp to Makefile.am
awk '
/ecppSources/{E=1}
E && /index\.ecpp/ { print "\tview/'$page'.ecpp \\"; E=0 }
/_SOURCES =/{S=1}
S && /main.cpp/ { print "\tcontroller/'$page'.cpp \\"; E=0 }
{ print }' <Makefile.am.new >Makefile.am.tmp
if diff Makefile.am.tmp Makefile.am.new >/dev/null
then
echo "failed to add page <$page> to Makefile.am"
rm Makefile.am.tmp
else
mv Makefile.am.tmp Makefile.am.new
fi
echo add menu item $page
awk '
/navitem/{M=1}
M && /<\/ul>/ { print " <li class=\"navitem\"><a href=\"/'$page'\">'$page'</a></li>"; M=0 }
{ print }' <webmain.ecpp.new >webmain.ecpp.tmp
mv webmain.ecpp.tmp webmain.ecpp.new
done
mv webmain.ecpp webmain.ecpp.old
mv webmain.ecpp.new webmain.ecpp
mv Makefile.am Makefile.am.old
mv Makefile.am.new Makefile.am
}
simpleproject()
{
project=$1
echo create directory $project
mkdir $project
echo create $project/configure.ac
cat >$project/configure.ac <<EOF
AC_INIT([${project}], [0.1], [your-email@somewhere])
AM_INIT_AUTOMAKE([foreign])
AC_PROG_CXX
AC_LANG(C++)
AC_CONFIG_HEADERS([config.h])
AC_CHECK_HEADER([cxxtools/mutex.h], , AC_MSG_ERROR([cxxtools headers not found]))
AC_CHECK_HEADER([tnt/tntnet.h], , AC_MSG_ERROR([tntnet headers not found]))
#AC_CHECK_HEADER([tntdb/connection.h], , AC_MSG_ERROR([tntdb headers not found]))
AC_CHECK_PROGS(ECPPC, [ecppc])
AC_PROG_SED
AC_OUTPUT([
Makefile
])
EOF
echo create $project/Makefile.am
cat >$project/Makefile.am <<EOF
AUTOMAKE_OPTIONS = subdir-objects
bin_PROGRAMS = ${project}
ecppSources = \\
${project}.ecpp
${project}_SOURCES = \\
main.cpp \\
\$(ecppSources)
${project}_LDFLAGS = -lcxxtools -ltntnet
CLEANFILES = \$(ecppSources:.ecpp=.cpp) \$(ecppSources:.ecpp=.deps) resources.cpp
#
# Rules for tntnet applications
#
SUFFIXES=.ecpp .ico .cpp
.ecpp.cpp:
\$(ECPPC) -I \$(top_srcdir)/src -n \$(basename \$<) -o \$@ \$<
.ecpp.deps:
\$(ECPPC) -M -n \$* -I \$(srcdir) -I \$(srcdir)/include \$< | \$(SED) '1s/\(.*\).cpp:/\1.cpp \1.deps:/' > \$@
.ico.cpp:
\$(ECPPC) -b -m image/x-icon -o \$@ \$<
-include \$(ecppSources:.ecpp=.deps)
EOF
echo create $project/main.cpp
cat >$project/main.cpp <<EOF
#include <iostream>
#include <tnt/tntnet.h>
#include <tnt/configurator.h>
#include <cxxtools/arg.h>
#include <cxxtools/log.h>
log_define("${project}")
int main(int argc, char* argv[])
{
try
{
cxxtools::Arg<std::string> ip(argc, argv, 'l');
cxxtools::Arg<unsigned short> port(argc, argv, 'p', 8000);
log_init();
tnt::Tntnet app;
app.listen(ip, port);
// map / to ${project}
app.mapUrl("^/$", "${project}");
// map /comp.* or /comp to comp
app.mapUrl("^/([^.]+)(\\..+)?", "\$1");
app.run();
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
EOF
echo create $project.ecpp
cat >$project/$project.ecpp <<EOF
<%pre>
// put your includes here
// #include "${project}.h"
</%pre>
<%args>
// define the query parameters
// bar;
</%args>
<%session>
// define your session scope variables here
// std::string mySessionState;
</%session>
<%cpp>
// put your C++ code here
</%cpp>
<!DOCTYPE html>
<html>
<head>
<title>ecpp application ${project}</title>
</head>
<body>
<h1>${project}</h1>
<p>Hello world!</p>
</body>
</html>
EOF
echo create log.properties
cat >$project/log.properties <<EOF
rootlogger INFO
logger.${project} INFO
#file = ${project} # this enables logging to a file
# setting maxfilesize and maxbackupindex enables rolling of file
# when file reaches maxfilesize, it is renamed to file.0 and a new file is created
# file.0 gets file.1 and so on up to maxbackupindex
#maxfilesize = 1M
#maxbackupindex = 2
#stdout = 1 # log to stdout instead of stderr if set
#loghost = hostname # send loging via udp to host
#broadcast = 1 # set when loghost is a network address
EOF
}
simplepages()
{
if [ ! -f Makefile.am ]
then
echo ERROR: Makefile.am not found
exit 1
fi
cp Makefile.am Makefile.am.new
while [ "$1" ]
do
page=$1
shift
if [ -f $page.ecpp ]
then
echo $page already exists
continue
fi
echo create $page.ecpp
cat >$page.ecpp <<EOF
<h1>$page</h1>
EOF
echo add $page.ecpp Makefile.am
awk '
/ecppSources/{E=1}
E && /\.ecpp$/ { print "\t'$page'.ecpp \\"; E=0 }
{ print }' <Makefile.am.new >Makefile.am.tmp
if diff Makefile.am.tmp Makefile.am.new >/dev/null
then
echo "failed to add page <$page> to Makefile.am"
rm Makefile.am.tmp
else
mv Makefile.am.tmp Makefile.am.new
fi
done
mv Makefile.am Makefile.am.old
mv Makefile.am.new Makefile.am
}
########################################################################
# main
#
while test $# -gt 0
do
case "$1" in
-*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
*) optarg= ;;
esac
case "$1" in
--simple=*)
project=$optarg
simple=1
;;
--simple|-s)
project=$2
simple=1
shift
;;
--mvc=*|-m=*)
project=$optarg
;;
--mvc|-m)
project=$2
shift
;;
--page=*)
pages="$pages $optarg"
;;
--page)
pages="$pages $2"
shift
;;
*)
usage
exit 1
;;
esac
shift
done
if [ ! "$project" -a ! "$pages" ]
then
echo failed
usage
exit 1
fi
echo project=$project pages="$pages"
if [ "$simple" ]
then
checkSystem $project
simpleproject $project
cd $project
if [ "$pages" ]
then
simplepages $pages
fi
echo run autoreconf -i
autoreconf -i
echo run configure
./configure
echo "============================================================"
echo "= project <$project> created"
echo "= change to directory <$project> and run <make> to build the application"
echo "= execute <./$project> there to run the application"
echo "============================================================"
elif [ "$project" ]
then
checkSystem $project
project $project
cd $project
if [ "$pages" ]
then
pages $pages
fi
echo run autoreconf -i
autoreconf -i
echo run configure
./configure
echo "============================================================"
echo "= project <$project> created"
echo "= change to directory <$project> and run <make> to build the application"
echo "= execute <./$project> there to run the application"
echo "============================================================"
else
if [ "$pages" ]
then
if [ -d view ]
then
pages $pages
else
simplepages $pages
fi
fi
fi