-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.xml
More file actions
2968 lines (2333 loc) · 141 KB
/
Copy pathindex.xml
File metadata and controls
2968 lines (2333 loc) · 141 KB
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Blue Jay | Go Toolkit for the Web</title>
<link>https://blue-jay.github.io/</link>
<description>Recent content on Blue Jay | Go Toolkit for the Web</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language>
<atom:link href="https://blue-jay.github.io/index.xml" rel="self" type="application/rss+xml" />
<item>
<title>Blue Jay Overview</title>
<link>https://blue-jay.github.io/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://blue-jay.github.io/</guid>
<description>
<p>Blue Jay is a web toolkit for <a href="https://golang.org/">Go</a>. It&rsquo;s a collection of command-line tools and a web blueprint that allows you to easily structure your web application. There is no rigid framework to which you have to conform and the tools are easy to start using.</p>
<p>There are a few components:</p>
<ul>
<li><a href="https://github.com/blue-jay/blueprint"><strong>Blueprint</strong></a> is a model-view-controller (MVC) style web skeleton.</li>
<li><a href="https://github.com/blue-jay/jay"><strong>Jay</strong></a> is a command line tool with find/replace, database migrations, and code generation.</li>
<li><a href="https://github.com/blue-jay/core"><strong>Core</strong></a> is a collection of packages used
by Blueprint and Jay that can also be used by other projects.</li>
</ul>
<h2 id="high-level">High Level</h2>
<p>Blueprint is a web application with a built-in web server and MySQL integration.
The application has a public home page, authenticated home page, login page,
register page, about page, and a simple notepad to demonstrate GET, POST,
PATCH, and DELETE operations.</p>
<p>The entrypoint for the web app is <strong>blueprint.go</strong> which uses the <strong>boot</strong>
package to load the application settings, create the session store,
connect to the database, set up the views, load the routes, attach the
middleware, and then start the web server.</p>
<p>The front end is built using <a href="http://getbootstrap.com/">Bootstrap</a> with a few
small changes to fonts and
spacing. The flash messages are customized so they show up at the bottom right
corner of the screen. All of the error and warning messages should display to
the user or in the console. Informational messages are displayed to the user
via flash messages that disappear after 4 seconds.</p>
<p>Blueprint also works well with <a href="https://www.npmjs.com/">npm</a> and
<a href="http://gulpjs.com/">Gulp</a>. A Gulp script is
included that automates the compiling of SASS, concatenation of JavaScript,
generation of favicons, and copying of static assets like Bootstrap and jQuery
(which are managed by npm) to the <strong>asset/static</strong> folder.</p>
<p>Jay is a command-line tool that pairs nicely with Blueprint. It has find/replace
functionality to make code refactoring is a little easier. It performs database
migration to easily update your database when sharing code
between teams. Jay provides template-based code generation that allows you to
build files like controllers, models, middleware, or even multiple views.
All templates (*.gen files) are parsed using
the <strong>text/template</strong> package from the Go standard library and all generation
instructions (*.json files) allow you to specify which variables to pass via
<strong>Jay</strong> as well as in which folder to create the templates. You can also build
collections of templates to generate more than one file set which is great when
you want to scaffold out a component with CRUD (create, read, update, and delete).</p>
<h2 id="quick-start-website-with-jay">Quick Start Website with Jay</h2>
<ol>
<li>To download Blueprint, run the following command: <code>go get github.com/blue-jay/blueprint</code></li>
<li>To download Jay, run the following command: <code>go get github.com/blue-jay/jay</code></li>
<li>In your terminal, CD to the <strong>blueprint</strong> folder.</li>
<li>Run this command to create the env.json file from env.json.example: <code>jay env make</code></li>
<li>Set the environment variable, JAYCONFIG, to the env.json file path. For example:
<ul>
<li>On Windows: <code>SET JAYCONFIG=C:\bluejay\workspace\src\github.com\blue-jay\blueprint\env.json</code></li>
<li>On Linux/OS X: <code>export JAYCONFIG=$HOME/workspace/src/github.com/blue-jay/blueprint/env.json</code></li>
</ul></li>
<li>Start a MySQL instance.</li>
<li>Edit the <strong>MySQL</strong> section of env.json to match your database login information.</li>
<li>Create the database and tables using the command: <code>jay migrate:mysql all</code></li>
<li>Run the application: <code>go run blueprint.go</code></li>
<li>Open your web browser to <a href="http://localhost">http://localhost</a> and you should see the welcome page.</li>
<li>Navigate to the register page at <a href="http://localhost/register">http://localhost/register</a> and create a new user.</li>
<li>You can now login at <a href="http://localhost/login">http://localhost/login</a>.</li>
</ol>
<h3 id="os-specific-instructions">OS Specific Instructions</h3>
<p>There are also more detailed guides available by operating system:</p>
<ul>
<li><a href="https://github.com/blue-jay/blueprint/wiki/Blueprint-Setup-for-Amazon-AMI">Setup for Amazon AMI</a></li>
<li><a href="https://github.com/blue-jay/blueprint/wiki/Blueprint-Setup-for-Ubuntu-AMI">Setup for Ubuntu AMI</a></li>
<li><a href="https://github.com/blue-jay/blueprint/wiki/Blueprint-Setup-for-OS-X">Setup for OS X</a></li>
<li><a href="https://github.com/blue-jay/blueprint/wiki/Blueprint-Setup-for-Windows">Setup for Windows</a></li>
</ul>
<h2 id="quick-start-website-without-jay">Quick Start Website without Jay</h2>
<ol>
<li>To download Blueprint, run the following command: <code>go get github.com/blue-jay/blueprint</code></li>
<li>Start a MySQL instance.</li>
<li>Make a copy of env.json.example and name it: <strong>env.json</strong></li>
<li>Edit the <strong>MySQL</strong> section in <strong>env.json</strong> so the connection information matches your MySQL instance.</li>
<li>In the <strong>Session</strong> section, you should generate new passwords for the following keys:
<ul>
<li>AuthKey should be a 64 byte password and then base64 encoded</li>
<li>EncryptKey should be a 32 byte password and then base64 encoded</li>
<li>CSRFKey should be a 32 byte password and then base64 encoded</li>
</ul></li>
<li>Create a database called <strong>blueprint</strong> in MySQL.</li>
<li>Import <strong>migration/mysql/20160630_020000.000000_init.up.sql</strong> to create the tables.</li>
<li>In your terminal, CD to the <strong>blueprint</strong> folder.</li>
<li>Run the application using the command: <code>go run blueprint.go</code></li>
<li>Open your web browser to <a href="http://localhost">http://localhost</a> and you should see the welcome page.</li>
<li>Navigate to the register page at <a href="http://localhost/register">http://localhost/register</a> and create a new user.</li>
<li>You can now login at <a href="http://localhost/login">http://localhost/login</a>.</li>
</ol>
<h2 id="why-blue-jay">Why Blue Jay?</h2>
<p>After 300 stars on GitHub, I realized people really liked the boilerplate
Model-View-Controller (MVC) web application in Go called
<a href="https://github.com/josephspurrier/gowebapp">gowebapp</a> so I rewrote it with
better documentation.</p>
<p>Go is a blast to code in and it&rsquo;s great being part of a helpful community.
Blue Jay provides a quickstart for developers with a lean web skeleton called
<strong>Blueprint</strong> that demonstrates how to structure a web application with sample
code.</p>
<p>One of the things you&rsquo;ll notice while using Blueprint is how to abstract out
external packages to make it easy to swap out components. Ultimately, you should
be able to write code once and use it in all of your other projects. The <strong>lib</strong>
folder is a great place for all these packages with very few dependencies.</p>
<p>You&rsquo;ll also notice certain packages need to be thread-safe when building web applications.
An example is the <strong>github.com/core/view</strong> package which provides thread-safe template caching.</p>
<p>The other reason for Blue Jay is the command-line tool, <strong>jay</strong>, which provides an easy way
to find/replace in a project when refactoring, migrate your database forwards or backwards, and
generate a file or sets or files using the Go <a href="https://golang.org/pkg/html/template/">html/template</a>
package. Code generation can help you build faster and more efficiently with less mistakes.</p>
<h2 id="why-go">Why Go?</h2>
<p>One of the big draws to Go is the rich standard library. The standard library includes a web server,
web-safe templating, and
many other tools necessary to build a web application. Any features missing from the standard library are
written by other Go developers who are happy to contribute to the thriving community.</p>
<p>Go allows you to write code that compiles to the majority of the architectures we use today so all your
code is pretty much portable. Go excels when you want to write command line apps instead of just scripts,
but that&rsquo;s not the only niche.
The designers of Go wanted to build a language that solved problems between the Google development teams.
It&rsquo;s a modern language that allows you to easily multi-thread your applications. It&rsquo;s a &ldquo;get stuff done&rdquo;
language.</p>
<h2 id="screenshots">Screenshots</h2>
<h3 id="public-home">Public Home</h3>
<p><img src="https://blue-jay.github.io/images/home_anon.png" alt="Image of Public Home" /></p>
<h3 id="about">About</h3>
<p><img src="https://blue-jay.github.io/images/about.png" alt="Image of About" /></p>
<h3 id="register">Register</h3>
<p><img src="https://blue-jay.github.io/images/register.png" alt="Image of Register" /></p>
<h3 id="login">Login</h3>
<p><img src="https://blue-jay.github.io/images/login.png" alt="Image of Login" /></p>
<h3 id="authenticated-home">Authenticated Home</h3>
<p><img src="https://blue-jay.github.io/images/home_auth.png" alt="Image of Auth Home" /></p>
<h3 id="view-all-notes">View All Notes</h3>
<p><img src="https://blue-jay.github.io/images/notepad_index.png" alt="Image of Notepad View" /></p>
<h3 id="add-note">Add Note</h3>
<p><img src="https://blue-jay.github.io/images/notepad_create.png" alt="Image of Notepad Add" /></p>
<h3 id="view-one-note">View One Note</h3>
<p><img src="https://blue-jay.github.io/images/notepad_view.png" alt="Image of Notepad Edit" /></p>
<h3 id="edit-note">Edit Note</h3>
<p><img src="https://blue-jay.github.io/images/notepad_edit.png" alt="Image of Notepad Edit" /></p>
<h2 id="feedback">Feedback</h2>
<p>All feedback is welcome. Let me know if you have any suggestions, questions, or criticisms.
If something is not idiomatic to Go, please let me know know so we can make it better.</p>
</description>
</item>
<item>
<title>Structure</title>
<link>https://blue-jay.github.io/structure/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://blue-jay.github.io/structure/</guid>
<description>
<p>It helps to understand the folder structure so you know where each of the
components lives.</p>
<h2 id="blueprint-structure">Blueprint Structure</h2>
<p>The project is organized into the following folders:</p>
<pre><code class="language-text">asset/
|----dynamic/ - private assets like SASS files, single JavaScript files, and logo.png for favicon generation
|----static/ - public assets like CSS, JavaScript, and favicon.ico for Android, Apple, etc
controller/ - packages with routes and application logic
filestorage/ - files uploaded from an HTML form
generate/ - template pairs (.gen and .json) for generating code using jay
lib/ - packages you'll build that are used by the application (recommended to build with minimum dependencies)
|----boot/ - package for registering services and setting up all the components
|----env/ - package that holds the structure for the application settings
|----flight/ - package with a context that can be used by controllers to access application settings
middleware/ - packages that return a http.Handler to wrap around routes for ACL, request logging, etc
migration/ - migration database files
|----mysql/ - MySQL files for migrating database up and down
model/ - packages with database queries and structs matching tables
view/ - HTML templates parsed using the Go html/template package
viewfunc/ - packages that return a template.FuncMap for use in views
viewmodify/ - packages that modify view prior to rendering to add varibles like CSRF token and auth level
</code></pre>
<p>The following files exist at the project root:</p>
<pre><code class="language-text">blueprint.go - entrypoint for the application
env.json.example - application config template for variables
gulpfile.js - Gulp configuration that compiles SASS, concatenates JavaScript, etc
package.json - npm configuration that loads Gulp, Boostrap, Underscore.js, etc
</code></pre>
<h2 id="blueprint-external-go-packages">Blueprint External Go Packages</h2>
<p>There are a few external packages used in Blueprint:</p>
<pre><code class="language-text">github.com/gorilla/context - registry for global request variables
github.com/gorilla/csrf - CSRF protection for gorilla sessions
github.com/gorilla/sessions - cookie and filesystem sessions
github.com/go-sql-driver/mysql - MySQL driver
github.com/husobee/vestigo - HTTP router with wildcards
github.com/jmoiron/sqlx - MySQL general purpose extensions
github.com/justinas/alice - middleware chaining
golang.org/x/crypto/bcrypt - password hashing algorithm
</code></pre>
<h2 id="jay-structure">Jay Structure</h2>
<p>The project is simply a command-line interface for packages in the Core library
<a href="https://github.com/blue-jay/core">https://github.com/blue-jay/core</a>. The packages that Jay uses from the Core
library are:</p>
<pre><code class="language-text">env/ - package that creates and updates the env.json config file
find/ - package that finds case-sensitive matched text in files
generate/ - package that generates code from template pairs
migrate/ - package that handles the database migrations
replace/ - package that replaces case-sensitive matched text in files
</code></pre>
<p>The following file exists at the project root of Jay:</p>
<pre><code class="language-text">jay.go - entrypoint for the application
</code></pre>
<h2 id="jay-external-go-packages">Jay External Go Packages</h2>
<p>There is only one external packages used in Jay (not including the Core
library):</p>
<pre><code class="language-text">gopkg.in/alecthomas/kingpin.v2 - command-line and flag parser
</code></pre>
<h2 id="core-structure">Core Structure</h2>
<p>The Core project contains many packages that are all divided into individual
folders. Each is well documented so the
<a href="https://godoc.org/github.com/blue-jay/core">GoDoc</a> page should provide enough
information on each one.</p>
</description>
</item>
<item>
<title>Configuration</title>
<link>https://blue-jay.github.io/configuration/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://blue-jay.github.io/configuration/</guid>
<description>
<h2 id="basic-usage">Basic Usage</h2>
<p>Throughout this documentation, keep in mind everything in Blueprint is configurable.
You are not using a framework so don&rsquo;t be afraid to change code. You don&rsquo;t need to
use any of the components included with Blueprint, but it does give you a nice foundation to
start from. If you want to use YAML instead of JSON, it&rsquo;s recommended to create a wrapper
library in the <strong>lib</strong> folder and then load your env.yaml file via the <strong>blueprint.go</strong>
file.</p>
<h2 id="jay-command-env">Jay Command: env</h2>
<p>One of the first steps before using Blueprint is to create <strong>env.json</strong>. You can make
a copy of <strong>env.json.example</strong> and then name it <strong>env.json</strong>, just be sure to
generate a new <strong>AuthKey</strong>, <strong>EncryptKey</strong>, and <strong>CSRFKey</strong> in the <strong>Session</strong> section.</p>
<p>You can also use <strong>jay</strong> to create the env.json file with new session keys.
Just CD to the <strong>blueprint</strong> folder and then run: <code>jay env make</code></p>
<p>Here are the commands you can use with <code>jay env</code>:</p>
<pre><code class="language-bash"># Create a new env.json file from env.json.example with newly generated session keys
jay env make
# Show a new set of session keys that can be copied and pasted into env.json
jay env keyshow
# Generate a new set of session keys and automatically apply them to env.json
env env keyupdate
</code></pre>
<p>The <strong>env.json</strong> file is a good place to set variables for the application so
you don&rsquo;t have to hardcode them. If you want to add any of your own settings,
you can add them to <strong>env.json</strong> and update the <strong>Info</strong> struct
in the <strong>lib/env</strong> package. Here is an example <strong>env.json</strong>:</p>
<pre><code class="language-json">{
&quot;Asset&quot;:{
&quot;Folder&quot;:&quot;asset&quot;
},
&quot;Email&quot;:{
&quot;Username&quot;:&quot;&quot;,
&quot;Password&quot;:&quot;&quot;,
&quot;Hostname&quot;:&quot;&quot;,
&quot;Port&quot;:25,
&quot;From&quot;:&quot;&quot;
},
&quot;Form&quot;:{
&quot;FileStorageFolder&quot;:&quot;filestorage&quot;
},
&quot;Generation&quot;:{
&quot;TemplateFolder&quot;:&quot;generate&quot;
},
&quot;MySQL&quot;:{
&quot;Username&quot;:&quot;root&quot;,
&quot;Password&quot;:&quot;&quot;,
&quot;Database&quot;:&quot;blueprint&quot;,
&quot;Charset&quot;:&quot;utf8mb4&quot;,
&quot;Collation&quot;:&quot;utf8mb4_unicode_ci&quot;,
&quot;Hostname&quot;:&quot;127.0.0.1&quot;,
&quot;Port&quot;:3306,
&quot;Parameter&quot;:&quot;parseTime=true&quot;,
&quot;Migration&quot;:{
&quot;Folder&quot;:&quot;migration/mysql&quot;,
&quot;Table&quot;:&quot;migration_blueprint&quot;,
&quot;Extension&quot;:&quot;sql&quot;
}
},
&quot;Server&quot;:{
&quot;Hostname&quot;:&quot;&quot;,
&quot;UseHTTP&quot;:true,
&quot;UseHTTPS&quot;:false,
&quot;RedirectToHTTPS&quot;:false,
&quot;HTTPPort&quot;:80,
&quot;HTTPSPort&quot;:443,
&quot;CertFile&quot;:&quot;tls/server.crt&quot;,
&quot;KeyFile&quot;:&quot;tls/server.key&quot;
},
&quot;Session&quot;:{
&quot;AuthKey&quot;:&quot;PzCh6FNAB7/jhmlUQ0+25sjJ+WgcJeKR2bAOtnh9UnfVN+WJSBvY/YC80Rs+rbMtwfmSP4FUSxKPtpYKzKFqFA==&quot;,
&quot;EncryptKey&quot;:&quot;3oTKCcKjDHMUlV+qur2Ve664SPpSuviyGQ/UqnroUD8=&quot;,
&quot;CSRFKey&quot;:&quot;xULAGF5FcWvqHsXaovNFJYfgCt6pedRPROqNvsZjU18=&quot;,
&quot;Name&quot;:&quot;sess&quot;,
&quot;Options&quot;:{
&quot;Path&quot;:&quot;/&quot;,
&quot;Domain&quot;:&quot;&quot;,
&quot;MaxAge&quot;:28800,
&quot;Secure&quot;:false,
&quot;HttpOnly&quot;:true
}
},
&quot;Template&quot;:{
&quot;Root&quot;:&quot;base&quot;,
&quot;Children&quot;:[
&quot;partial/favicon&quot;,
&quot;partial/menu&quot;,
&quot;partial/footer&quot;
]
},
&quot;View&quot;:{
&quot;BaseURI&quot;:&quot;/&quot;,
&quot;Extension&quot;:&quot;tmpl&quot;,
&quot;Folder&quot;:&quot;view&quot;,
&quot;Caching&quot;:true
}
}
</code></pre>
<h2 id="production">Production</h2>
<p>When you move your application to production, you should make the following
changes:</p>
<ul>
<li>Set <strong>Server</strong>.<strong>Hostname</strong> to the server</li>
<li>Set <strong>Server</strong>.<strong>UseHTTPS</strong> to true</li>
<li>Generate a certificate and key for HTTPS and place in the <strong>tls</strong> folder</li>
<li>Set <strong>Session</strong>.<strong>Secure</strong> to true</li>
</ul>
<h2 id="configuration-structure">Configuration Structure</h2>
<p>The <strong>env.json</strong> file contains the configuration for Blueprint and Jay. It removes the need
to hardcode any of these values and makes it easy to move Blueprint to another system
with a different setup. The <strong>env.json</strong> file is parsed to the
<strong>Info</strong> struct from the <strong>lib/env</strong> package:</p>
<p><a href="https://github.com/blue-jay/blueprint/blob/master/lib/env/env.go">Source</a></p>
<pre><code class="language-go">// Info contains the application settings.
type Info struct {
Asset asset.Info `json:&quot;Asset&quot;`
Email email.Info `json:&quot;Email&quot;`
Form form.Info `json:&quot;Form&quot;`
Generation generate.Info `json:&quot;Generation&quot;`
MySQL mysql.Info `json:&quot;MySQL&quot;`
Server server.Info `json:&quot;Server&quot;`
Session session.Info `json:&quot;Session&quot;`
Template view.Template `json:&quot;Template&quot;`
View view.Info `json:&quot;View&quot;`
path string
}
</code></pre>
<p>The <strong>Info</strong> struct is a container that nests structs from packages in
the <strong>Core</strong> library that need variables configured. The <strong>Path</strong> variable is
the location of the env.json file. Here is a list mapping the JSON keys to
structs:</p>
<pre><code class="language-text">Asset - Info struct in core/asset
Email - Info struct in core/email
Form - Info struct in core/form
Generate - Info struct in core/generate
MySQL - Info struct in core/mysql
Server - Info struct in core/server
Session - Info struct in core/session
Template - Template struct in core/view
View - Info struct in core/view
</code></pre>
<h2 id="using-flight">Using Flight</h2>
<p>The <strong>flight</strong> package provides access to the session variables, env.json settings,
database connections, views, and general shortcuts. It&rsquo;s mostly designed for
controllers, but it can be used by other packages as well.</p>
<p>To use package, you would call the <strong>flight.Context()</strong> function like this:</p>
<pre><code class="language-go">// Index displays the items.
func Index(w http.ResponseWriter, r *http.Request) {
c := flight.Context(w, r)
items, _, err := note.ByUserID(c.DB, c.UserID)
if err != nil {
c.FlashError(err)
items = []note.Item{}
}
v := c.View.New(&quot;note/index&quot;)
v.Vars[&quot;items&quot;] = items
v.Render(w, r)
}
</code></pre>
<p>Flight needs a <strong>http.ResponseWriter</strong> and a <strong>http.Request</strong> so it can access
the sessions variables and provide shortcuts for the following tasks:</p>
<ul>
<li>reading URL parameters: c.Param(name string)</li>
<li>redirecting to a page: c.Redirect(urlStr string)</li>
<li>validation a form and sets a flash message: c.FormValid(fields &hellip;string)</li>
<li>repopulating a form: c.Repopulate(v map[string]interface{}, fields &hellip;string)</li>
<li>setting flash messages and then saving the session: c.FlashSuccess(message string)</li>
</ul>
<p>Flight also provides access to the following:</p>
<ul>
<li>configuration settings from env.json: <code>c.Config.Asset.Folder</code></li>
<li>session: <code>c.Sess.Values[&quot;email&quot;]</code></li>
<li>current user ID: <code>c.UserID</code></li>
<li>view package: <code>c.View.New(&quot;home/index&quot;)</code></li>
<li>database connection: <code>items, _, err := note.ByUserID(c.DB, c.UserID)</code></li>
</ul>
<p>It is not a requirement to use the <strong>flight</strong> package, but it makes working
with the different web components much easier. Feel free to modify the package
to fit your needs. Just make sure it is thread-safe.</p>
<h2 id="enable-https">Enable HTTPS</h2>
<p>To enable HTTPS:</p>
<ol>
<li>Set <strong>UseHTTPS</strong> to <strong>true</strong> in the <strong>env.json</strong> file</li>
<li>Create a folder called <strong>tls</strong> in the project root folder</li>
<li>Place your own certificate and key files in the <strong>tls</strong> folder</li>
</ol>
<p><strong>Note:</strong> If you want to redirect HTTP to HTTPS, you can set <strong>RedirectToHTTPS</strong> to <strong>true</strong> in the <strong>env.json</strong> file as well.</p>
<h2 id="tip-add-a-section">Tip: Add a Section</h2>
<p>To add a new key called <strong>Captcha</strong>, your workflow would consist of the
following:</p>
<ol>
<li>Create a new package in the <strong>lib</strong> folder called <strong>captcha</strong></li>
<li>Create a struct called <strong>Info</strong> in the <strong>lib/captcha</strong> package</li>
<li>Add the struct to the <strong>Info</strong> struct in the <strong>lib/env</strong> package</li>
<li>Add the <strong>Captcha</strong> key and any values to the <strong>env.json</strong> file</li>
<li>Add code to the <strong>RegisterServices()</strong> function in the <strong>lib/boot</strong> package to pass the any additional settings to the <strong>lib/flight</strong> package at start up</li>
<li>Add code to your controllers that references uses <strong>flight.Context()</strong> to retrieve your <strong>lib/captcha</strong> package settings</li>
</ol>
<h2 id="tip-remove-a-section">Tip: Remove a Section</h2>
<p>To remove the <strong>Email</strong> key, your workflow would consist of the following:</p>
<ol>
<li>Remove the <strong>Email</strong> key and value from the <strong>env.json</strong> file</li>
<li>Remove the <strong>Email</strong> nested struct from the <strong>Info</strong> struct in the <strong>lib/env</strong> package</li>
<li>Remove any code setting up the package from the <strong>RegisterServices()</strong> function in the <strong>boot</strong> package</li>
<li>Remove the <strong>lib/email</strong> package from the filesystem</li>
<li>Find any references to the <strong>lib/email</strong> package in your code using the jay command line, <code>jay find . &quot;lib/email&quot;</code>,
then delete the imports and referencing code</li>
</ol>
</description>
</item>
<item>
<title>Assets</title>
<link>https://blue-jay.github.io/assets/</link>
<pubDate>Thu, 30 Jun 2016 21:07:13 +0100</pubDate>
<guid>https://blue-jay.github.io/assets/</guid>
<description>
<h2 id="basic-usage">Basic Usage</h2>
<p>Out of the box, all of the static assets like CSS and JavaScript are ready to
demo. If you want to make changes to the file, it&rsquo;s best to use the tools
provided. The <strong>asset</strong> folder contains a <strong>dynamic</strong> folder and a <strong>static</strong>
folder.</p>
<p>The <strong>dynamic</strong> folder contains the Syntactically Awesome Style Sheets (SASS),
individual JavaScript files, and a large PNG image which is used to generate
favicons for different platforms like Android, iPhone, etc. <em>This is the folder
in which you want to make your changes.</em> The dynamic folder holds some of the
assets required to generate the assets in the <strong>static</strong> folder.</p>
<p>The <strong>static</strong> folder contains the minified CSS and JavaScript as well as
the generated favicons. The <strong>static</strong> folder is designed to be served up
so the files can be accessed like this:</p>
<pre><code class="language-html">&lt;!-- Favicons --&gt;
&lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;57x57&quot; href=&quot;/static/favicon/apple-touch-icon-57x57.png?v1.0=3eepn6WlLO&quot;&gt;
&lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;60x60&quot; href=&quot;/static/favicon/apple-touch-icon-60x60.png?v1.0=3eepn6WlLO&quot;&gt;
&lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;72x72&quot; href=&quot;/static/favicon/apple-touch-icon-72x72.png?v1.0=3eepn6WlLO&quot;&gt;
&lt;!-- CSS and Fonts --&gt;
&lt;link media=&quot;all&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/static/css/bootstrap.min.css?1466973904&quot; /&gt;
&lt;link media=&quot;all&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;//fonts.googleapis.com/css?family=Open+Sans:300,400,bold,italic&quot; /&gt;
&lt;link media=&quot;all&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/static/css/all.css?1466973904&quot; /&gt;
</code></pre>
<p>Check out the <a href="https://blue-jay.github.io/controllers">Controllers</a> and
<a href="https://blue-jay.github.io/views/#included-functions">Views</a> pages for how
the files are served and how timestamps are append to them for cach management.</p>
<p>If you make changes to any of the files in the <strong>dynamic</strong> folder, you still
need a way to compile/minify them and then move them to the <strong>static</strong> folder
so we&rsquo;ll use Gulp to do that.</p>
<h2 id="install-npm">Install npm</h2>
<p>The Node Package Manager (npm) helps install packages that work with NodeJS.
If you don&rsquo;t have NodeJS and npm installed, you can install the latest version
from <a href="https://nodejs.org">https://nodejs.org</a>.</p>
<h2 id="install-gulp-and-dependencies">Install Gulp and Dependencies</h2>
<p>Once npm is installed, you can open your terminal and CD to the root of the
project folder. You can then run these commands:</p>
<pre><code class="language-bash"># Install Gulp globally
npm install -g gulp-cli
# Install Gulp locally and dependencies from package.json
npm install
</code></pre>
<h2 id="gulp">Gulp</h2>
<p>Once the environment is set up, you should have your terminal open to the root
of the project folder. There are a couple commands you can use with Gulp that
are in the <a href="https://github.com/blue-jay/blueprint/blob/master/gulpfile.js">gulpfile.js</a>.</p>
<pre><code class="language-bash"># Compile the SASS from asset/dynamic/sass and store CSS in asset/static/css/all.css
gulp sass
# Concat the JavaScript from asset/dynamic/js and store JS in asset/static/js/all.js
gulp javascript
# Copy the jQuery files from node_modules/jquery to asset/static/js
gulp jquery
# Copy the Bootstrap files from node_modules/bootstrap to asset/static
gulp bootstrap
# Copy the Underscore files from note_modules/underscore to asset/static/js
gulp underscore
# Run tasks favicon-generate and favicon-inject
gulp favicon
# Generate favicons from asset/dynamic/logo.png and copy to /asset/static/favicon
gulp favicon-generate
# Generate view/partial/favicon.tmpl with favicon tags
gulp favicon-inject
# Update the asset/dynamic/favicon/data.json file with the latest version from the RealFaviconGenerator website
gulp favicon-update
# Run the sass and javascript tasks when any of the files change
gulp watch
# Run all the tasks once
gulp init
# Run just the sass and javascript tasks once
gulp default
</code></pre>
<p>It is best to run <code>gulp watch</code> so when you are working with the SASS and
JavaScript files so they will automatically generate in the <strong>static</strong> folder
for you.</p>
</description>
</item>
<item>
<title>Routing</title>
<link>https://blue-jay.github.io/routing/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://blue-jay.github.io/routing/</guid>
<description>
<h2 id="basic-usage">Basic Usage</h2>
<p>When a user requests a page from your application, the routes determine which
page is shown. The route is a URL that is mapped to a controller function.
To simplify the organization, the routes are stored in the controller files.
The controller files are all organized under the <strong>controller</strong> folder.</p>
<h2 id="routing">Routing</h2>
<p>In the <strong>boot</strong> package, the <strong>RegisterServices()</strong> function
calls the <strong>controller.LoadRoutes()</strong> function. The <strong>LoadRoutes()</strong> function in
the <strong>controller</strong> package loads the routes for each of the individual
controllers:</p>
<pre><code class="language-go">// LoadRoutes loads the routes for each of the controllers.
func LoadRoutes() {
about.Load()
debug.Load()
register.Load()
login.Load()
home.Load()
static.Load()
status.Load()
notepad.Load()
}
</code></pre>
<p>Here is the <strong>Load()</strong> function from the <strong>controller/notepad</strong> package:</p>
<p><a href="https://github.com/blue-jay/blueprint/blob/master/controller/static/static.go">Source</a></p>
<pre><code class="language-go">// Package static serves static files like CSS, JavaScript, and images.
package static
import (
&quot;net/http&quot;
&quot;os&quot;
&quot;path&quot;
&quot;github.com/blue-jay/blueprint/controller/status&quot;
&quot;github.com/blue-jay/blueprint/lib/flight&quot;
&quot;github.com/blue-jay/core/router&quot;
)
// Load the routes.
func Load() {
// Serve static files
router.Get(&quot;/static/*filepath&quot;, Index)
}
// Index maps static files.
func Index(w http.ResponseWriter, r *http.Request) {
c := flight.Context(w, r)
// File path
path := path.Join(c.Config.Asset.Folder, r.URL.Path[1:])
// Only serve files
if fi, err := os.Stat(path); err == nil &amp;&amp; !fi.IsDir() {
http.ServeFile(w, r, path)
return
}
status.Error404(w, r)
}
</code></pre>
<h2 id="error-pages">Error Pages</h2>
<p>A few errors pages are already defined for you like the <strong>404</strong> (Page Not Found)
and <strong>405</strong> (Method Not Allowed) pages.</p>
<p><a href="https://github.com/blue-jay/blueprint/blob/master/controller/status/status.go">Source</a></p>
<pre><code class="language-go">// Package status provides all the error pages like 404, 405, 500, 501,
// and the page when a CSRF token is invalid.
package status
import (
&quot;net/http&quot;
&quot;github.com/blue-jay/blueprint/lib/flight&quot;
&quot;github.com/blue-jay/core/router&quot;
)
// Load the routes.
func Load() {
router.MethodNotAllowed(Error405)
router.NotFound(Error404)
}
// Error404 - Page Not Found.
func Error404(w http.ResponseWriter, r *http.Request) {
c := flight.Context(w, r)
w.WriteHeader(http.StatusNotFound)
v := c.View.New(&quot;status/index&quot;)
v.Vars[&quot;title&quot;] = &quot;404 Not Found&quot;
v.Vars[&quot;message&quot;] = &quot;Page could not be found.&quot;
v.Render(w, r)
}
// Error405 - Method Not Allowed.
func Error405(allowedMethods string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
c := flight.Context(w, r)
w.WriteHeader(http.StatusMethodNotAllowed)
v := c.View.New(&quot;status/index&quot;)
v.Vars[&quot;title&quot;] = &quot;405 Method Not Allowed&quot;
v.Vars[&quot;message&quot;] = &quot;Method is not allowed.&quot;
v.Render(w, r)
}
}
...
</code></pre>
</description>
</item>
<item>
<title>Middleware</title>
<link>https://blue-jay.github.io/middleware/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://blue-jay.github.io/middleware/</guid>
<description>
<h2 id="basic-usage">Basic Usage</h2>
<p>Middleware, in the context of Go, is applied during routing to provide
features like request/response logging, access controls lists (ACLs), and
header modification. Middleware is either applied to every request (like for
request logging) or specified routes (like for ACLs).</p>
<p>There are a few pieces of middleware included. The package called <strong>csrf</strong>
protects against Cross-Site Request Forgery attacks.
The <strong>logrequest</strong> package will log every request made against the
website to the console. The <strong>rest</strong> package allows the HTTP method to be
changed during a form submission to DELETE or PATCH instead of POST.</p>
<h2 id="creating-middleware">Creating Middleware</h2>
<p>An example of a piece of middleware that is applied to every request is
<strong>middleware/logrequest</strong>. When a page is requested, the middleware will
print to the console: the time of the request, remote IP address, HTTP method,
and the URL requested.</p>
<p><a href="https://github.com/blue-jay/blueprint/blob/master/middleware/logrequest/logrequest.go">Source</a></p>
<pre><code class="language-go">// Package logrequest provides an http.Handler that logs when a request is
// made to the application and lists the remote address, the HTTP method,
// and the URL.
package logrequest
import (
&quot;fmt&quot;
&quot;net/http&quot;
&quot;time&quot;
)
// Handler will log the HTTP requests.
func Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println(time.Now().Format(&quot;2006-01-02 03:04:05 PM&quot;), r.RemoteAddr, r.Method, r.URL)
next.ServeHTTP(w, r)
})
}
</code></pre>
<p>This is an example of the minimum code required for middleware:</p>
<pre><code class="language-go">// Handler
func Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Logic BEFORE the other handlers and function goes here
next.ServeHTTP(w, r)
// Logic AFTER the other handlers and function goes here
})
}
</code></pre>
<h2 id="chaining">Chaining</h2>
<p>The more middleware you use, the more it stacks up like this and makes it hard
to read:</p>
<pre><code class="language-go">return context.ClearHandler(rest.Handler(logrequest.Handler(setUpCSRF)))
</code></pre>
<p>Before <a href="https://github.com/justinas/alice">justinas/alice</a>, a workaround was to
use a variable and reassign it multiple times like this:</p>
<pre><code class="language-go">h = setUpCSRF(h)
h = logrequest.Handler(h)
h = rest.Handler(h)
return context.ClearHandler(h)
</code></pre>
<p>You can see chaining in action in <a href="https://github.com/blue-jay/blueprint/blob/master/controller/notepad/notepad.go">controller/notepad</a>
where the controller uses the <strong>router.ChainHandler()</strong> function.
The function is a wrapper for
the <a href="https://github.com/justinas/alice">justinas/alice</a> package which makes
using middleware more scalable and a little &ldquo;prettier&rdquo;. If you look at the
<a href="https://github.com/blue-jay/blueprint/blob/master/lib/boot/middleware.go">lib/boot</a>
package, you&rsquo;ll see the <strong>ChainHandler()</strong> function. There is also a <strong>Chain()</strong>
function that can be used to chain middleware for routes or to pass to
<strong>ChainHandler()</strong>.</p>
<pre><code class="language-go">// Apply middleware to routes individually
router.Get(&quot;/notepad&quot;, Index, acl.DisallowAnon, logrequest.Handler)
router.Get(&quot;/notepad/create&quot;, Create, acl.DisallowAnon, logrequest.Handler)
// Use Chain() to apply middleware
c := router.Chain(acl.DisallowAnon, logrequest.Handler)
router.Get(&quot;/notepad&quot;, Index, c...)
router.Get(&quot;/notepad/create&quot;, Create, c...)
// Pass Chain() to ChainHandler()
c := router.Chain( // Chain middleware, bottom runs first
h, // Handler to wrap
setUpCSRF, // Prevent CSRF
rest.Handler, // Support changing HTTP method sent via query string
logrequest.Handler, // Log every request
context.ClearHandler, // Prevent memory leak with gorilla.sessions
)
return router.ChainHandler(c...)
</code></pre>
<p><strong>ChainHandler()</strong> accepts one or more of the http.Handler type and returns a
http.Handler.</p>
<p><strong>Chain()</strong> accepts one or more of the http.Handler type and returns an array of
the alice.Constructor type.</p>
<h2 id="apply-to-every-request">Apply to Every Request</h2>
<p>In <a href="https://github.com/blue-jay/blueprint/blob/master/blueprint.go">blueprint.go</a>,
the application calls <strong>boot.SetUpMiddleware(router.Instance())</strong> which
applies the middleware to the router. The middleware is called on every
request.</p>
<p><a href="https://github.com/blue-jay/blueprint/blob/master/lib/boot/middleware.go">Source</a></p>
<pre><code class="language-go">// SetUpMiddleware contains the middleware that applies to every request.
func SetUpMiddleware(h http.Handler) http.Handler {
return router.ChainHandler( // Chain middleware, top middlware runs first
h, // Handler to wrap
setUpCSRF, // Prevent CSRF
rest.Handler, // Support changing HTTP method sent via query string
logrequest.Handler, // Log every request
context.ClearHandler, // Prevent memory leak with gorilla.sessions
)
</code></pre>
<h2 id="apply-to-specific-routes">Apply to Specific Routes</h2>
<p>In <a href="https://github.com/blue-jay/blueprint/blob/master/controller/notepad/notepad.go">controller/notepad</a>,
the application creates a chain of middleware and then
applies it to only certain routes. In this scenario, the pages are only
accessible if the user is authenticated.</p>
<p><a href="https://github.com/blue-jay/blueprint/blob/master/controller/notepad/notepad.go">Source</a></p>
<pre><code class="language-go">func Load() {
// Add middleware that disallows anonymous access
c := router.Chain(acl.DisallowAnon)
// Map HTTP methods and URLs to functions wrapped in the middleware chain
router.Get(&quot;/notepad&quot;, Index, c...)
router.Get(&quot;/notepad/create&quot;, Create, c...)
router.Post(&quot;/notepad&quot;, Store, c...)
router.Get(&quot;/notepad/view/:id&quot;, Show, c...)
router.Get(&quot;/notepad/edit/:id&quot;, Edit, c...)
router.Patch(&quot;/notepad/edit/:id&quot;, Update, c...)
router.Delete(&quot;/notepad/:id&quot;, Destroy, c...)
}
</code></pre>
</description>
</item>
<item>
<title>Controllers</title>
<link>https://blue-jay.github.io/controllers/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://blue-jay.github.io/controllers/</guid>
<description>
<h2 id="basic-usage">Basic Usage</h2>
<p>The controller files are all organized under the <strong>controller</strong> folder. The
controllers handle the interactions between the models and the views as well as
specify which routes to map to which functions.</p>
<p>It&rsquo;s a good idea to follow a naming convention for the different pieces.
Laravel developers will notice it&rsquo;s very similar, but with a few changes.</p>
<table>
<thead>
<tr>
<th align="center">Method</th>
<th align="center">Path</th>
<th align="center">Function</th>
<th align="center">View</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">GET</td>
<td align="center">/notepad</td>
<td align="center">Index</td>
<td align="center">index.tmpl</td>
</tr>
<tr>
<td align="center">GET</td>
<td align="center">/notepad/create</td>
<td align="center">Create</td>
<td align="center">create.tmpl</td>
</tr>
<tr>
<td align="center">POST</td>
<td align="center">/notepad/create</td>
<td align="center">Store</td>
<td align="center"></td>
</tr>