├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── assets.go
├── assets
├── 404.md
├── 500.md
├── icon.png
├── wiki.css
└── wiki.md
├── assets_test.go
├── conf.go
├── go.mod
├── go.sum
├── handlers.go
├── handlers_test.go
├── http.go
├── init.go
├── main.go
├── md.go
├── md_test.go
├── pages.go
├── pages
├── example.md
├── test1.md
└── test2.md
├── pages_test.go
├── revive.toml
├── setup.sh
├── tildewiki.yaml
├── tools
└── racefind.sh
└── types.go
/.gitignore:
--------------------------------------------------------------------------------
1 | tildewiki
2 | trace.out
3 | tildewiki.sh
4 | local/
5 | *.tar.gz
6 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: go
2 |
3 | go:
4 | - 1.11.x
5 | - 1.12.x
6 |
7 | os:
8 | - linux
9 |
10 | dist: xenial
11 |
12 | env:
13 | - GO111MODULE=on
14 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # TildeWiki [](https://goreportcard.com/report/github.com/gbmor/tildewiki) [](https://travis-ci.org/gbmor/tildewiki)
2 | TildeWiki is a memory-caching static site server. The possible uses of TildeWiki range from blogs to wikis, and more.
3 | Let me know if you adapt it to a new use-case, I'm always interested!
4 |
5 | Originally designed around the needs of the [tildeverse](https://tildeverse.org).1
6 |
7 | [\[Features\]](#features) | [\[Installation\]](#installation) | [\[Benchmarks\]](#benchmarks) | [\[Notes\]](#notes)
8 |
9 | ## [v0.6.4](https://github.com/gbmor/tildewiki/releases/tag/v0.6.4)
10 | Version 0.6.4 Changes:
11 | * Cleaned up code so it's nicer to read. Less extraneous whitespace, unnecessary comments, etc.
12 | * Refactored index and page cache a bit
13 |
14 | ### Currently powering the [tilde.institute](https://tilde.institute) wiki:
15 | * [https://wiki.tilde.institute](https://wiki.tilde.institute)
16 | * [gtmetrix report](https://gtmetrix.com/reports/wiki.tilde.institute/F1tzxEch)
17 |
18 | ## Features
19 | * Speed is a priority
20 | * Mobile-friendly pages
21 | * Markdown!2
22 | * Compressed responses (gzip)
23 | * Uses [kognise/water.css](https://github.com/kognise/water.css) dark theme by
24 | default (and includes as an example, a simple but nice local CSS file)3
25 | * `YAML` configuration
26 | * Automatically reloads config file when a change is detected.
27 | * Generates list of pages, then places at an anchor comment in the index page
28 | * Caches pages to memory and only re-renders when the file changes
29 | * Very configurable. For example:
30 | * URL path for viewing pages
31 | * Directory for page data
32 | * File to use for index page
33 | * Logging output (file, `stdout`, `null`) and file location
34 | * Runs as a multithreaded service, rather than via CGI
35 | * Easily use [Caddy](https://caddyserver.com) or Nginx to proxy requests to it. This allows you to use your
36 | existing SSL certificates (or, in the case of Caddy, painlessly generate new ones).
37 |
38 | ## Installation
39 |
40 | The installation script uses `bash`, and the startup script uses `daemonize`. Both should
41 | be available in any Linux distribution's package repositories. However, they are not
42 | required to use TildeWiki.
43 |
44 | ### Using the scripts
45 |
46 | First, clone the repository or download and untar a release archive, then enter the directory.
47 |
48 | ```
49 | $ git clone git://github.com/gbmor/tildewiki.git && cd tildewiki
50 |
51 | $ curl -L https://github.com/gbmor/tildewiki/archive/v0.6.3.tar.gz | tar xzvf - && cd tildewiki-v0.6.3
52 | ```
53 |
54 | If you used `git`, the master branch will be the most recent release. Development work stays
55 | in the `dev` branch, so there's no need to look for a tag.
56 |
57 | Execute `setup.sh` as root, with the `install` argument:
58 |
59 | ```
60 | $ sudo ./setup.sh install
61 | ```
62 |
63 | Once you receive the confirmation message, and no errors have appeared, you may run the
64 | startup script as root to test the installation:
65 |
66 | ```
67 | $ sudo tildewiki
68 | ```
69 |
70 | TildeWiki will drop privileges to the `tildewiki` user, which was created by the script.
71 |
72 | I'm going to add a `systemd` service file soon. For now, it'll need to be started like this.
73 |
74 | ### Building manually
75 |
76 | If you prefer, you can install it this way. Clone the repository or download a source archive
77 | like above, and enter the directory. Once in the directory, you'll need to build the binary.
78 |
79 | ```
80 | $ go build
81 | ```
82 |
83 | It won't take long. Also, it doesn't need to live in your `GOPATH` as
84 | it's been set up to use Go Modules.
85 |
86 | After it finishes, you can leave the binary where it is or move it somewhere else. Remember
87 | to move the `pages` and `assets` directories with it, along with `tildewiki.yaml`.
88 |
89 | ### Setting up TildeWIki
90 |
91 | Begin by combing through `tildewiki.yaml` (if you used the scripts, it's in `/usr/local/tildewiki`)
92 | and changing the options to something appropriate to your site. Afterwards, place your markdown-formatted
93 | pages into the directory specified by `PageDir` in the config and place your markdown-formatted
94 | index file, containing the anchor comment ``, into the `AssetsDir`. Feel free to
95 | change the favicon and CSS to your liking.
96 |
97 | Once that's all done, either run `/usr/local/bin/tildewiki` (if you've used the scripts) or run
98 | the binary manually.
99 |
100 | ### Serving TildeWiki
101 |
102 | Unless you plan on serving directly from :8080 (which is fine!), or whichever port you chose in
103 | `tildewiki.yaml`, I recommend proxying requests to TildeWiki so it can be served from a subdomain,
104 | for example. There are several options for this, namely [Caddy](https://caddyserver.com/) and
105 | [nginx](https://nginx.org). The best option is for you to use Caddy: it integrates TLS certificate
106 | renewal and has a *very* easy configuration syntax.
107 |
108 | If you're going to use Nginx, here's an example server block for you to start with. Note: this
109 | example uses TLS and http2. [LetsEncrypt](https://letsencrypt.org) is awesome, and free.
110 | Their `certbot` tool is really easy to use.
111 |
112 | ```
113 | server {
114 | server_name wiki.example.com;
115 | listen [::]:443 ssl http2;
116 | listen 0.0.0.0:443 ssl http2;
117 | ssl_certificate /etc/letsencrypt/live/wiki.example.com/fullchain.pem;
118 | ssl_certificate_key /etc/letsencrypt/live/wiki.example.com/privkey.pem;
119 | include /etc/letsencrypt/options-ssl-nginx.conf;
120 | ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
121 | location / {
122 | proxy_set_header Host $host;
123 | proxy_set_header X-Forwarded-For $remote_addr;
124 | proxy_pass http://127.0.0.1:8080;
125 | }
126 | }
127 | server {
128 | if ($host = wiki.example.com) {
129 | return 301 https://$host$request_uri;
130 | }
131 | listen 80;
132 | server_name wiki.example.com;
133 | return 404;
134 | }
135 | ```
136 |
137 | ## Benchmarks
138 |
139 | * [bombardier](https://github.com/codesenberg/bombardier)
140 |
141 | ```
142 | $ bombardier -c 100 -n 200000 http://localhost:8080
143 |
144 | Bombarding http://localhost:8080 with 200000 request(s) using 100 connection(s)
145 | 200000 / 200000 [===========================================] 100.00% 7512/s 26s
146 | Done!
147 | Statistics Avg Stdev Max
148 | Reqs/sec 7548.57 663.04 10453.06
149 | Latency 13.24ms 2.38ms 49.32ms
150 | HTTP codes:
151 | 1xx - 0, 2xx - 200000, 3xx - 0, 4xx - 0, 5xx - 0
152 | others - 0
153 | Throughput: 8.55MB/s
154 |
155 | ```
156 |
157 | * [baton](https://github.com/americanexpress/baton)
158 | ```
159 | $ baton -u http://localhost:8080 -c 100 -r 200000
160 |
161 | ...
162 |
163 | =========================== Results ========================================
164 |
165 | Total requests: 200000
166 | Time taken to complete requests: 27.270626274s
167 | Requests per second: 7334
168 | Max response time (ms): 52
169 | Min response time (ms): 0
170 | Avg response time (ms): 13.11
171 |
172 | ========= Percentage of responses by status code ==========================
173 |
174 | Number of connection errors: 0
175 | Number of 1xx responses: 0
176 | Number of 2xx responses: 200000
177 | Number of 3xx responses: 0
178 | Number of 4xx responses: 0
179 | Number of 5xx responses: 0
180 |
181 | ========= Percentage of responses received within a certain time (ms)======
182 |
183 | 9% : 5 ms
184 | 13% : 10 ms
185 | 79% : 15 ms
186 | 95% : 20 ms
187 | 98% : 25 ms
188 | 99% : 30 ms
189 | 99% : 35 ms
190 | 99% : 40 ms
191 | 99% : 45 ms
192 | 100% : 52 ms
193 |
194 | ===========================================================================
195 |
196 | ```
197 |
198 | ## Notes
199 | * Builds with `Go 1.11` and `Go 1.12`.
200 | * Tested on Linux (Ubuntu 18.04LTS, Debian 9, 10, and Sid) and OpenBSD 6.4
201 |
202 | 1. For [tildeverse](https://tildeverse.org) projects, we tend to use a PR
203 | workflow for collaboration. For example, wiki pages are submitted to the repo via pull
204 | request. I'm currently evaluating other options for page creation and editing.
205 |
206 | 2. Uses a patched copy of [russross/blackfriday](https://github.com/russross/blackfriday)
207 | ([gopkg](https://gopkg.in/russross/blackfriday.v2)) as the markdown
208 | parser. The patch allows injection of various `` tags into
209 | the document header during the `markdown->html` translation.
210 |
211 | * The patched `v2` repository lives at:
212 | [gbmor-forks/blackfriday.v2-patched](https://github.com/gbmor-forks/blackfriday.v2-patched)
213 |
214 | * The patched `master` repo lives at:
215 | [gbmor-forks/blackfriday](https://github.com/gbmor-forks/blackfriday).
216 |
217 | * The PR can be found here: [allow writing of user-specified
218 | <meta.../>...](https://github.com/russross/blackfriday/pull/541)
219 |
220 | 3. The local CSS provided is the "58 bytes of CSS" from [https://jrl.ninja/etc/1/](https://jrl.ninja/etc/1/)
221 |
222 |
--------------------------------------------------------------------------------
/assets.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "bytes"
5 | "fmt"
6 | )
7 |
8 | // displays on startup
9 | func setUpUsTheWiki() {
10 | fmt.Printf(`
11 | __ _ __ __ _ __ _
12 | / /_(_) /___/ /__ _ __(_) /__(_)
13 | / __/ / / __ / _ \ | /| / / / //_/ /
14 | / /_/ / / /_/ / __/ |/ |/ / / ,< / /
15 | \__/_/_/\__,_/\___/|__/|__/_/_/|_/_/
16 |
17 | :: TildeWiki ` + twvers + ` ::
18 | (c)2019 Ben Morrison (gbmor)
19 | GPL v3
20 | https://github.com/gbmor/tildewiki
21 | All Contributions Appreciated!
22 | `)
23 | fmt.Printf("\n")
24 | }
25 |
26 | // determine if using local or remote css
27 | // by checking if it's a URL or not
28 | func cssLocal(css []byte) bool {
29 | if bytes.HasPrefix(css, []byte("http://")) || bytes.HasPrefix(css, []byte("https://")) {
30 | return false
31 | }
32 | return true
33 | }
34 |
--------------------------------------------------------------------------------
/assets/404.md:
--------------------------------------------------------------------------------
1 | # 404
2 |
3 | ## Not the area code
4 |
5 | ## I mean I can't find that :(
6 |
--------------------------------------------------------------------------------
/assets/500.md:
--------------------------------------------------------------------------------
1 | # 500
2 |
3 | ## Oh noez
4 |
5 | ## Something went wrong!
6 |
--------------------------------------------------------------------------------
/assets/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tildeinstitute/tildewiki/7410ca83f79b25e3e9207acd3be3a9bc1421f19b/assets/icon.png
--------------------------------------------------------------------------------
/assets/wiki.css:
--------------------------------------------------------------------------------
1 | body {
2 | max-width: 38rem;
3 | padding: 1.5rem;
4 | margin: auto;
5 | background-color: #b3b3cc;
6 | }
7 |
--------------------------------------------------------------------------------
/assets/wiki.md:
--------------------------------------------------------------------------------
1 | # henlo
2 |
3 | this is a wiki
4 |
5 | ## welcome
6 |
7 | to the wiki
8 |
9 |
10 |
11 | after the page list
12 |
--------------------------------------------------------------------------------
/assets_test.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "testing"
5 | )
6 |
7 | var cssLocalTests = []struct {
8 | name []byte
9 | want bool
10 | }{
11 | {
12 | name: []byte("https://google.com/test.css"),
13 | want: false,
14 | },
15 | {
16 | name: []byte("style.css"),
17 | want: true,
18 | },
19 | }
20 |
21 | // Make sure it's parsing the CSS location correctly
22 | // and returning the correct bool
23 | func Test_cssLocal(t *testing.T) {
24 | for _, tt := range cssLocalTests {
25 | t.Run(string(tt.name), func(t *testing.T) {
26 | if got := cssLocal(tt.name); got != tt.want {
27 | t.Errorf("cssLocal() = %v, want %v", got, tt.want)
28 | }
29 | })
30 | }
31 | }
32 | func Benchmark_cssLocal(b *testing.B) {
33 | for i := 0; i < b.N; i++ {
34 | for _, c := range cssLocalTests {
35 | cssLocal(c.name)
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/conf.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "log"
5 | "regexp"
6 |
7 | "github.com/fsnotify/fsnotify"
8 | "github.com/spf13/viper"
9 | )
10 |
11 | // content-type constants
12 | const htmlutf8 = "text/html; charset=utf-8"
13 | const cssutf8 = "text/css; charset=utf-8"
14 |
15 | // Config object initialization
16 | var confVars = &confParams{}
17 |
18 | // (Re-)Populates config object
19 | func setConfVars() {
20 | confVars.port = ":" + viper.GetString("Port")
21 | confVars.pageDir = viper.GetString("PageDir")
22 | confVars.assetsDir = viper.GetString("AssetsDir")
23 | confVars.cssPath = viper.GetString("CSS")
24 | confVars.viewPath = "/" + viper.GetString("ViewPath") + "/"
25 | confVars.indexRefreshInterval = viper.GetString("IndexRefreshInterval")
26 | confVars.wikiName = viper.GetString("Name")
27 | confVars.wikiDesc = viper.GetString("ShortDesc")
28 | confVars.descSep = viper.GetString("DescSeparator")
29 | confVars.titleSep = viper.GetString("TitleSeparator")
30 | confVars.iconPath = viper.GetString("Icon")
31 | confVars.indexFile = viper.GetString("Index")
32 | confVars.reverseTally = viper.GetBool("ReverseTally")
33 | confVars.validPath = regexp.MustCompile(viper.GetString("ValidPath"))
34 | confVars.quietLogging = viper.GetBool("QuietLogging")
35 | confVars.fileLogging = viper.GetBool("FileLogging")
36 | confVars.logFile = viper.GetString("LogFile")
37 | }
38 |
39 | // Sets the basic parameters for the default viper (config library) instance
40 | func initConfigParams() {
41 | conf := viper.GetViper()
42 |
43 | conf.SetConfigType("yaml")
44 | conf.SetConfigName("tildewiki")
45 |
46 | conf.AddConfigPath(".")
47 | conf.AddConfigPath("$HOME/.config/")
48 | conf.AddConfigPath("/usr/local/tildewiki/")
49 | conf.AddConfigPath("/etc/")
50 | conf.AddConfigPath("/usr/local/etc/")
51 |
52 | err := conf.ReadInConfig()
53 | if err != nil {
54 | log.Fatalf("Config file error: %s\n", err.Error())
55 | }
56 |
57 | setConfVars()
58 |
59 | conf.WatchConfig()
60 | conf.OnConfigChange(func(e fsnotify.Event) {
61 | log.Println("**NOTICE** Config file change detected: ", e.Name)
62 | setConfVars()
63 | triggerRecache()
64 | })
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/go.mod:
--------------------------------------------------------------------------------
1 | module github.com/gbmor/tildewiki
2 |
3 | go 1.11
4 |
5 | require (
6 | github.com/fsnotify/fsnotify v1.4.7
7 | github.com/gbmor-forks/blackfriday.v2-patched v0.0.0-20190422230759-91071f2561f1
8 | github.com/gorilla/handlers v1.4.0
9 | github.com/gorilla/mux v1.7.2
10 | github.com/kr/pretty v0.1.0 // indirect
11 | github.com/pelletier/go-toml v1.4.0 // indirect
12 | github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
13 | github.com/spf13/viper v1.3.2
14 | github.com/stretchr/testify v1.3.0 // indirect
15 | golang.org/x/sys v0.0.0-20190508220229-2d0786266e9c // indirect
16 | golang.org/x/text v0.3.2 // indirect
17 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
18 | )
19 |
--------------------------------------------------------------------------------
/go.sum:
--------------------------------------------------------------------------------
1 | github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
2 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3 | github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
4 | github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
5 | github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
6 | github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
7 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
9 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10 | github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
11 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
12 | github.com/gbmor-forks/blackfriday.v2-patched v0.0.0-20190422230759-91071f2561f1 h1:O1zej9wdZX4GP26nEWlabIYF8tBzxLwdGWNTo/XgOg0=
13 | github.com/gbmor-forks/blackfriday.v2-patched v0.0.0-20190422230759-91071f2561f1/go.mod h1:aklyD3jeUevHhApmpQeRMGPD10BF9l3bD/s1vNeBHyI=
14 | github.com/gorilla/handlers v1.4.0 h1:XulKRWSQK5uChr4pEgSE4Tc/OcmnU9GJuSwdog/tZsA=
15 | github.com/gorilla/handlers v1.4.0/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
16 | github.com/gorilla/mux v1.7.2 h1:zoNxOV7WjqXptQOVngLmcSQgXmgk4NMz1HibBchjl/I=
17 | github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
18 | github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
19 | github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
20 | github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
21 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
22 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
23 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
24 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
25 | github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
26 | github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
27 | github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
28 | github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
29 | github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
30 | github.com/pelletier/go-toml v1.4.0 h1:u3Z1r+oOXJIkxqw34zVhyPgjBsm6X2wn21NWs/HfSeg=
31 | github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo=
32 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
33 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
34 | github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
35 | github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
36 | github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
37 | github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
38 | github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
39 | github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
40 | github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
41 | github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
42 | github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
43 | github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
44 | github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M=
45 | github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
46 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
47 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
48 | github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
49 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
50 | github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
51 | github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
52 | golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
53 | golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
54 | golang.org/x/sys v0.0.0-20190508220229-2d0786266e9c h1:hDn6jm7snBX2O7+EeTk6Q4WXJfKt7MWgtiCCRi1rBoY=
55 | golang.org/x/sys v0.0.0-20190508220229-2d0786266e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
56 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
57 | golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
58 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
59 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
60 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
61 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
62 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
63 | gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
64 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
65 |
--------------------------------------------------------------------------------
/handlers.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "crypto/sha256"
5 | "fmt"
6 | "io/ioutil"
7 | "log"
8 | "net/http"
9 | "os"
10 |
11 | "github.com/gorilla/mux"
12 | )
13 |
14 | // handler for viewing content pages (not the index page)
15 | func pageHandler(w http.ResponseWriter, r *http.Request) {
16 | vars := mux.Vars(r)
17 | filename := vars["pageReq"]
18 | filename += ".md"
19 |
20 | page, err := pullFromCache(filename)
21 | if err != nil {
22 | log.Printf("%v\n", err)
23 | }
24 |
25 | pingCache(page)
26 |
27 | if page.Body == nil {
28 | http.Redirect(w, r, "/", http.StatusFound)
29 | return
30 | }
31 |
32 | etag := fmt.Sprintf("%x", sha256.Sum256([]byte(page.Modtime.String())))
33 |
34 | w.Header().Set("ETag", "\""+etag+"\"")
35 | w.Header().Set("Content-Type", htmlutf8)
36 | w.Header().Set("Link", ">; rel=\"contents\", ; rel=\"stylesheet\"")
37 | _, err = w.Write(page.Body)
38 | if err != nil {
39 | log500(w, r, err)
40 | return
41 | }
42 | log200(r)
43 | }
44 |
45 | // Handler for viewing the index page.
46 | func indexHandler(w http.ResponseWriter, r *http.Request) {
47 | pingCache(indexCache)
48 |
49 | etag := fmt.Sprintf("%x", sha256.Sum256([]byte(indexCache.page.Modtime.String())))
50 |
51 | w.Header().Set("ETag", "\""+etag+"\"")
52 | w.Header().Set("Content-Type", htmlutf8)
53 | w.Header().Set("Link", ">; rel=\"contents\", ; rel=\"stylesheet\"")
54 | _, err := w.Write(indexCache.page.Body)
55 | if err != nil {
56 | log500(w, r, err)
57 | return
58 | }
59 | log200(r)
60 | }
61 |
62 | // Serves the favicon as a URL.
63 | // This is due to the default behavior of
64 | // not serving naked paths but virtual ones.
65 | func iconHandler(w http.ResponseWriter, r *http.Request) {
66 | confVars.mu.RLock()
67 | assetsDir := confVars.assetsDir
68 | iconPath := confVars.iconPath
69 | confVars.mu.RUnlock()
70 |
71 | longname := assetsDir + "/" + iconPath
72 | icon, err := ioutil.ReadFile(longname)
73 | if err != nil {
74 | if os.IsNotExist(err) {
75 | log.Printf("Favicon file specified in config does not exist: /icon request 404\n")
76 | error404(w, r)
77 | return
78 | }
79 | log500(w, r, err)
80 | return
81 | }
82 |
83 | stat, err := os.Stat(longname)
84 | if err != nil {
85 | log.Printf("Couldn't stat icon to send ETag header: %v\n", err.Error())
86 | }
87 |
88 | etag := fmt.Sprintf("%x", sha256.Sum256([]byte(stat.ModTime().String())))
89 |
90 | w.Header().Set("ETag", "\""+etag+"\"")
91 | w.Header().Set("Content-Type", http.DetectContentType(icon))
92 | _, err = w.Write(icon)
93 | if err != nil {
94 | log500(w, r, err)
95 | return
96 | }
97 | log200(r)
98 | }
99 |
100 | // Serves the local css file as a url.
101 | // This is due to the default behavior of
102 | // not serving naked paths but virtual ones.
103 | func cssHandler(w http.ResponseWriter, r *http.Request) {
104 | confVars.mu.RLock()
105 | cssPath := confVars.cssPath
106 | confVars.mu.RUnlock()
107 |
108 | // check if using local or remote CSS.
109 | // if remote, don't bother doing anything
110 | // and redirect requests to /
111 | if !cssLocal([]byte(cssPath)) {
112 | http.Redirect(w, r, "/", http.StatusFound)
113 | return
114 | }
115 |
116 | css, err := ioutil.ReadFile(cssPath)
117 | if err != nil {
118 | if os.IsNotExist(err) {
119 | log.Printf("CSS file specified in config does not exist: /css request 404\n")
120 | error404(w, r)
121 | return
122 | }
123 | log500(w, r, err)
124 | return
125 | }
126 |
127 | stat, err := os.Stat(cssPath)
128 | if err != nil {
129 | log.Printf("Couldn't stat CSS file to send ETag header: %v\n", err.Error())
130 | }
131 |
132 | etag := fmt.Sprintf("%x", sha256.Sum256([]byte(stat.ModTime().String())))
133 |
134 | w.Header().Set("ETag", "\""+etag+"\"")
135 | w.Header().Set("Content-Type", cssutf8)
136 | _, err = w.Write(css)
137 | if err != nil {
138 | log500(w, r, err)
139 | return
140 | }
141 | log200(r)
142 | }
143 |
--------------------------------------------------------------------------------
/handlers_test.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "bytes"
5 | "io/ioutil"
6 | "net/http/httptest"
7 | "testing"
8 | )
9 |
10 | // This is a pretty strict test. Make sure the
11 | // output of pageHandler is byte-for-byte what
12 | // I'm expecting it to be.
13 | /*
14 | func Test_pageHandler(t *testing.T) {
15 | tests := []struct {
16 | name string
17 | }{
18 | {
19 | name: "example",
20 | },
21 | {
22 | name: "test1",
23 | },
24 | }
25 |
26 | hush, _ := os.Open("/dev/null")
27 | log.SetOutput(hush)
28 | initConfigParams()
29 | genPageCache()
30 |
31 | for _, tt := range tests {
32 | t.Run(tt.name, func(t *testing.T) {
33 | w := httptest.NewRecorder()
34 | req := httptest.NewRequest("GET", "localhost:8080/w/"+tt.name, nil)
35 | pageHandler(w, req)
36 | resp := w.Result()
37 | body, _ := ioutil.ReadAll(resp.Body)
38 | if resp.StatusCode != 200 {
39 | t.Errorf("pageHandler(): %v\n", resp.StatusCode)
40 | }
41 | if !bytes.Equal(body, cachedPages[tt.name+".md"].Body) {
42 | t.Errorf("pageHandler(): Byte mismatch\n")
43 | }
44 | })
45 | }
46 | }
47 | */
48 |
49 | // This is the same test type as pageHandler
50 | func Test_indexHandler(t *testing.T) {
51 | name := "Index Handler Test"
52 | w := httptest.NewRecorder()
53 | r := httptest.NewRequest("GET", "localhost:8080", nil)
54 | t.Run(name, func(t *testing.T) {
55 | indexHandler(w, r)
56 | resp := w.Result()
57 | body, _ := ioutil.ReadAll(resp.Body)
58 | if resp.StatusCode != 200 {
59 | t.Errorf("indexHandler(): %v\n", resp.StatusCode)
60 | }
61 | if !bytes.Equal(body, indexCache.page.Body) {
62 | t.Errorf("indexHandler(): Byte mismatch\n")
63 | }
64 | })
65 | }
66 |
67 | // This is the same test type as pageHandler
68 | func Test_iconHandler(t *testing.T) {
69 | name := "Icon Handler Test"
70 | initConfigParams()
71 |
72 | confVars.mu.RLock()
73 | icon, _ := ioutil.ReadFile(confVars.assetsDir + "/" + confVars.iconPath)
74 | confVars.mu.RUnlock()
75 |
76 | w := httptest.NewRecorder()
77 | r := httptest.NewRequest("GET", "localhost:8080/icon", nil)
78 | t.Run(name, func(t *testing.T) {
79 | iconHandler(w, r)
80 | resp := w.Result()
81 | body, _ := ioutil.ReadAll(resp.Body)
82 | if resp.StatusCode != 200 {
83 | t.Errorf("iconHandler(): %v\n", resp.StatusCode)
84 | }
85 | if !bytes.Equal(body, icon) {
86 | t.Errorf("iconHandler(): Byte mismatch\n")
87 | }
88 | })
89 | }
90 |
91 | // This is the same test type as pageHandler
92 | func Test_cssHandler(t *testing.T) {
93 | name := "CSS Handler Test"
94 | initConfigParams()
95 | if !cssLocal([]byte(confVars.cssPath)) {
96 | t.Skipf("cssHandler(): Set to use remote CSS in config, skipping test ...\n")
97 | }
98 | css, _ := ioutil.ReadFile(confVars.cssPath)
99 | w := httptest.NewRecorder()
100 | r := httptest.NewRequest("GET", "localhost:8080/css", nil)
101 | t.Run(name, func(t *testing.T) {
102 | cssHandler(w, r)
103 | resp := w.Result()
104 | body, _ := ioutil.ReadAll(resp.Body)
105 | if resp.StatusCode != 200 {
106 | t.Errorf("cssHandler(): %v\n", resp.StatusCode)
107 | }
108 | if !bytes.Equal(body, css) {
109 | t.Errorf("cssHandler(): Byte mismatch\n")
110 | }
111 | })
112 | }
113 |
114 | // Tests if /500 returns a status 200, which means
115 | // the handler is working. Doesn't test for 500-triggering
116 | // situations yet.
117 | func Test_error500(t *testing.T) {
118 | name := "Error 500 Handler Test"
119 | initConfigParams()
120 | w := httptest.NewRecorder()
121 | r := httptest.NewRequest("GET", "localhost:8080/500", nil)
122 | t.Run(name, func(t *testing.T) {
123 | error500(w, r)
124 | resp := w.Result()
125 | if resp.StatusCode != 200 {
126 | t.Errorf("error500(): %v\n", resp.StatusCode)
127 | }
128 | })
129 | }
130 |
131 | // Tests for a 200 status code because it serves requests
132 | // that fail the regex path validation, rather than a traditional
133 | // 404 status code.
134 | func Test_error404(t *testing.T) {
135 | name := "Error 404 Handler Test"
136 | initConfigParams()
137 | w := httptest.NewRecorder()
138 | r := httptest.NewRequest("GET", "localhost:8080"+confVars.viewPath+"?@$#$", nil)
139 | t.Run(name, func(t *testing.T) {
140 | error404(w, r)
141 | resp := w.Result()
142 | if resp.StatusCode != 200 {
143 | t.Errorf("error404(): %v\n", resp.StatusCode)
144 | }
145 | })
146 | }
147 |
--------------------------------------------------------------------------------
/http.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "io/ioutil"
7 | "log"
8 | "net"
9 | "net/http"
10 | "strings"
11 | )
12 |
13 | // Attach requester's IP address to context value
14 | func newCtxUserIP(ctx context.Context, r *http.Request) context.Context {
15 | base := strings.Split(r.RemoteAddr, ":")
16 | uip := base[0]
17 |
18 | if _, ok := r.Header["X-Forwarded-For"]; ok {
19 | proxied := r.Header["X-Forwarded-For"]
20 | base = strings.Split(proxied[len(proxied)-1], ":")
21 | uip = base[0]
22 | }
23 |
24 | return context.WithValue(ctx, ctxKey, uip)
25 | }
26 |
27 | // Retrieve an IP address from context passed with the request
28 | func getIPfromCtx(ctx context.Context) net.IP {
29 | uip, ok := ctx.Value(ctxKey).(string)
30 | if !ok {
31 | log.Printf("Error retrieving IP from request.\n")
32 | }
33 |
34 | return net.ParseIP(uip)
35 | }
36 |
37 | func ipMiddleware(hop http.Handler) http.Handler {
38 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39 | ctx := newCtxUserIP(r.Context(), r)
40 | hop.ServeHTTP(w, r.WithContext(ctx))
41 | })
42 | }
43 |
44 | func log200(r *http.Request) {
45 | useragent := r.Header["User-Agent"]
46 | uip := getIPfromCtx(r.Context())
47 | log.Printf("**** %v :: 200 :: %v %v :: %v\n", uip, r.Method, r.URL, useragent)
48 | }
49 |
50 | // wrapper for testing 500 pages via /500
51 | func error500(w http.ResponseWriter, r *http.Request) {
52 | log500(w, r, fmt.Errorf("500 Page Accessed Directly, No Error"))
53 | }
54 |
55 | // this is a custom 500 page using a markdown doc
56 | // in the assets directory.
57 | // if the markdown doc can't be read, default to
58 | // net/http's error handling
59 | func log500(w http.ResponseWriter, r *http.Request, topErr error) {
60 | useragent := r.Header["User-Agent"]
61 | uip := getIPfromCtx(r.Context())
62 | log.Printf("**** %v :: 500 :: %v %v :: %v :: %v\n", uip, r.Method, r.URL, useragent, topErr.Error())
63 |
64 | confVars.mu.RLock()
65 | e500 := confVars.assetsDir + "/500.md"
66 | confVars.mu.RUnlock()
67 |
68 | file, err := ioutil.ReadFile(e500)
69 | if err != nil {
70 | log.Printf("Tried to read 500.md: %v\n", err.Error())
71 | http.Error(w, err.Error(), http.StatusInternalServerError)
72 | return
73 | }
74 |
75 | w.Header().Set("Content-Type", htmlutf8)
76 | _, err = w.Write(render(file, "500: Internal Server Error"))
77 | if err != nil {
78 | log.Printf("Failed to write to HTTP stream: %v\n", err.Error())
79 | http.Error(w, err.Error(), http.StatusInternalServerError)
80 | }
81 | }
82 |
83 | // this is a custom 404 page using a markdown doc
84 | // in the assets directory.
85 | // if the markdown doc can't be read, default to
86 | // net/http's error handling
87 | func error404(w http.ResponseWriter, r *http.Request) {
88 | confVars.mu.RLock()
89 | e404 := confVars.assetsDir + "/404.md"
90 | confVars.mu.RUnlock()
91 |
92 | file, err := ioutil.ReadFile(e404)
93 | if err != nil {
94 | log.Printf("Tried to read 404.md: %v\n", err.Error())
95 | http.Error(w, err.Error(), http.StatusNotFound)
96 | return
97 | }
98 |
99 | w.Header().Set("Content-Type", htmlutf8)
100 | _, err = w.Write(render(file, "404: Not Found"))
101 | if err != nil {
102 | log.Printf("Failed to write to HTTP stream: %v\n", err.Error())
103 | error500(w, r)
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/init.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "log"
5 | "os"
6 | )
7 |
8 | func init() {
9 | // show the logo, repo link, etc
10 | setUpUsTheWiki()
11 |
12 | // initialize the configuration
13 | initConfigParams()
14 |
15 | // set up logging if the config file params
16 | // are set
17 | confVars.mu.RLock()
18 | filog := confVars.fileLogging
19 | qlog := confVars.quietLogging
20 | logfi := confVars.logFile
21 | confVars.mu.RUnlock()
22 | if filog && !qlog {
23 | if llogfile, err := os.OpenFile(logfi, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600); err == nil {
24 | log.SetOutput(llogfile)
25 |
26 | go func() {
27 | <-closelog
28 | log.Printf("Closing log file ...\n")
29 | err := llogfile.Close()
30 | if err != nil {
31 | log.Printf("Couldn't close log file: %v\n", err.Error())
32 | }
33 | }()
34 |
35 | } else {
36 | log.Printf("Couldn't log to file: %v\n", err.Error())
37 | }
38 | }
39 |
40 | // Tell TildeWiki to be quiet,
41 | if qlog {
42 | if llogfile, err := os.Open("/dev/null"); err == nil {
43 | log.SetOutput(llogfile)
44 |
45 | go func() {
46 | // I don't know why I'm bothering to do this for /dev/null
47 | // ...
48 | // whatever
49 | <-closelog
50 | log.Printf("Closing log file ...\n")
51 | err := llogfile.Close()
52 | if err != nil {
53 | log.Printf("Couldn't close log file: %v\n", err.Error())
54 | }
55 | }()
56 |
57 | } else {
58 | log.Printf("Couldn't quiet logging: %v\n", err.Error())
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/main.go:
--------------------------------------------------------------------------------
1 | package main // import "github.com/gbmor/tildewiki"
2 |
3 | import (
4 | "log"
5 | "net/http"
6 | "os"
7 | "os/signal"
8 | "time"
9 |
10 | "github.com/gorilla/handlers"
11 | "github.com/gorilla/mux"
12 | )
13 |
14 | // TildeWiki version
15 | const twvers = "0.6.4"
16 |
17 | // Makes the deferred close functions for the log file
18 | // block until exit
19 | var closelog = make(chan struct{}, 1)
20 |
21 | func main() {
22 | confVars.mu.RLock()
23 | filog := confVars.fileLogging
24 | portnum := confVars.port
25 | qlog := confVars.quietLogging
26 | reversed := confVars.reverseTally
27 | viewPath := confVars.viewPath
28 | confVars.mu.RUnlock()
29 |
30 | // watch for SIGINT aka ^C
31 | // close the log file then exit
32 | c := make(chan os.Signal, 1)
33 | signal.Notify(c, os.Interrupt)
34 | go func() {
35 | for sigint := range c {
36 | log.Printf("\n\nCaught %v. Cleaning up ...\n", sigint)
37 |
38 | if filog {
39 | // signal to close the log file
40 | closelog <- struct{}{}
41 | time.Sleep(50 * time.Millisecond)
42 | }
43 |
44 | close(closelog)
45 | os.Exit(0)
46 | }
47 | }()
48 |
49 | // fill the page cache
50 | log.Println("**NOTICE** Building initial cache ...")
51 | genPageCache()
52 |
53 | serv := mux.NewRouter().StrictSlash(true)
54 |
55 | serv.Path("/").HandlerFunc(indexHandler)
56 | serv.Path(viewPath + "{pageReq:[a-zA-Z0-9_-]+}").HandlerFunc(pageHandler)
57 | serv.Path("/css").HandlerFunc(cssHandler)
58 | serv.Path("/icon").HandlerFunc(iconHandler)
59 | serv.Path("/500").HandlerFunc(error500)
60 | serv.Path("/404").HandlerFunc(error404)
61 |
62 | if reversed {
63 | log.Printf("**NOTICE** Using reversed page listings on index ... \n")
64 | }
65 |
66 | log.Println("**NOTICE** Binding to " + portnum)
67 | server := &http.Server{
68 | Handler: handlers.CompressHandler(ipMiddleware(serv)),
69 | Addr: portnum,
70 | WriteTimeout: 15 * time.Second,
71 | ReadTimeout: 15 * time.Second,
72 | }
73 |
74 | err := server.ListenAndServe()
75 | if err != nil {
76 | log.Printf("%v\n", err.Error())
77 | }
78 |
79 | // signal to close the log file
80 | if filog || qlog {
81 | closelog <- struct{}{}
82 | close(closelog)
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/md.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | bf "github.com/gbmor-forks/blackfriday.v2-patched"
5 | )
6 |
7 | // Sets parameters for the markdown->html renderer
8 | func setupMarkdown(css, title string) *bf.HTMLRenderer {
9 | // if using local CSS file, use the virtually-served css
10 | // path rather than the actual file name
11 | confVars.mu.RLock()
12 | if cssLocal([]byte(confVars.cssPath)) {
13 | css = "/css"
14 | }
15 | confVars.mu.RUnlock()
16 |
17 | var params = bf.HTMLRendererParameters{
18 | CSS: css,
19 | Title: title,
20 | Icon: "/icon",
21 | Meta: map[string]string{
22 | "name=\"application-name\"": "TildeWiki " + twvers + " :: https://github.com/gbmor/tildewiki",
23 | "name=\"viewport\"": "width=device-width, initial-scale=1.0",
24 | },
25 | Flags: bf.CompletePage | bf.Safelink,
26 | }
27 | return bf.NewHTMLRenderer(params)
28 | }
29 |
30 | // Wrapper function to generate the parameters above and
31 | // pass them to the blackfriday library's parsing function
32 | func render(data []byte, title string) []byte {
33 | confVars.mu.RLock()
34 | cssPath := confVars.cssPath
35 | confVars.mu.RUnlock()
36 | return bf.Run(data, bf.WithRenderer(setupMarkdown(cssPath, title)))
37 | }
38 |
--------------------------------------------------------------------------------
/md_test.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "io/ioutil"
5 | "reflect"
6 | "testing"
7 |
8 | bf "github.com/gbmor-forks/blackfriday.v2-patched"
9 | )
10 |
11 | var mdTestData1, _ = ioutil.ReadFile("pages/example.md")
12 | var mdTestData2, _ = ioutil.ReadFile("pages/test1.md")
13 | var markdownTests = []struct {
14 | name string
15 | css string
16 | title string
17 | data []byte
18 | }{
19 | {
20 | name: "one",
21 | css: "assets/wiki.css",
22 | title: "Example Page",
23 | data: mdTestData1,
24 | },
25 | {
26 | name: "two",
27 | css: "assets/wiki.css",
28 | title: "No Description",
29 | data: mdTestData2,
30 | },
31 | }
32 |
33 | // Make sure setupMarkdown is returning a valid
34 | // blackfriday.HTMLRenderer type
35 | func Test_setupMarkdown(t *testing.T) {
36 | for _, tt := range markdownTests {
37 | t.Run(string(tt.name), func(t *testing.T) {
38 | var got interface{} = setupMarkdown(tt.css, tt.title)
39 | if _, ok := got.(*bf.HTMLRenderer); !ok {
40 | t.Errorf("setupMarkdown() returned incorrect type: %v", reflect.TypeOf(got))
41 | }
42 | })
43 | }
44 | }
45 | func Benchmark_setupMarkdown(b *testing.B) {
46 | for i := 0; i < b.N; i++ {
47 | for _, c := range markdownTests {
48 | setupMarkdown(c.css, c.title)
49 | }
50 | }
51 | }
52 |
53 | // Previously, I was using bytes.Equal(a, b) to test the
54 | // output of render. However, I can't control for variations
55 | // in blackfriday's output, so I'm just testing to make sure
56 | // it's returning *something*
57 | func Test_render(t *testing.T) {
58 | for _, tt := range markdownTests {
59 | t.Run(string(tt.name), func(t *testing.T) {
60 | var got []byte
61 | if got = render(tt.data, tt.title); got == nil {
62 | t.Errorf("render() outputting nil bytes\n")
63 | }
64 | })
65 | }
66 | }
67 | func Benchmark_render(b *testing.B) {
68 | for i := 0; i < b.N; i++ {
69 | for _, c := range markdownTests {
70 | render(c.data, c.title)
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/pages.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "bufio"
5 | "bytes"
6 | "errors"
7 | "fmt"
8 | "io/ioutil"
9 | "log"
10 | "os"
11 | "path/filepath"
12 | "sync"
13 | "time"
14 |
15 | "github.com/spf13/viper"
16 | )
17 |
18 | // Loads a given wiki page and returns a page object.
19 | // Used for building the initial cache and re-caching.
20 | func buildPage(filename string) (*Page, error) {
21 | file, err := os.Open(filename)
22 | if err != nil {
23 | log.Printf("%v\n", err.Error())
24 | return nil, err
25 | }
26 |
27 | defer func() {
28 | err = file.Close()
29 | if err != nil {
30 | log.Printf("%v\n", err.Error())
31 | }
32 | }()
33 |
34 | stat, err := file.Stat()
35 | if err != nil {
36 | log.Printf("Couldn't stat %s: %v\n", filename, err.Error())
37 | }
38 |
39 | var body pagedata
40 | body, err = ioutil.ReadAll(file)
41 | if err != nil {
42 | log.Printf("%v\n", err.Error())
43 | }
44 |
45 | _, shortname := filepath.Split(filename)
46 |
47 | // get meta info on file from the header comment
48 | title, desc, author := body.getMeta()
49 | if title == "" {
50 | title = shortname
51 | }
52 | if desc != "" {
53 | confVars.mu.RLock()
54 | desc = confVars.descSep + " " + desc
55 | confVars.mu.RUnlock()
56 | }
57 | if author != "" {
58 | author = "`by " + author + "`"
59 | }
60 |
61 | // longtitle is used in the tags of the output html
62 | confVars.mu.RLock()
63 | longtitle := title + " " + confVars.titleSep + " " + confVars.wikiName
64 | confVars.mu.RUnlock()
65 |
66 | // store the raw bytes of the document after parsing
67 | // from markdown to HTML.
68 | // keep the unparsed markdown for future use (maybe gopher?)
69 | bodydata := render(body, longtitle)
70 | return newPage(filename, shortname, title, author, desc, stat.ModTime(), bodydata, body, false), nil
71 | }
72 |
73 | // Scan the page until reaching following fields in the
74 | // header comment:
75 | // title:
76 | // author:
77 | // description:
78 | func (body pagedata) getMeta() (string, string, string) {
79 | // a bit redundant, but scanner is simpler to use
80 | bytereader := bytes.NewReader(body)
81 | metafinder := bufio.NewScanner(bytereader)
82 | var title, desc, author string
83 |
84 | for metafinder.Scan() {
85 | splitter := bytes.Split(metafinder.Bytes(), []byte(":"))
86 |
87 | switch string(bytes.ToLower(splitter[0])) {
88 | case "title":
89 | title = string(bytes.TrimSpace(splitter[1]))
90 | case "description":
91 | desc = string(bytes.TrimSpace(splitter[1]))
92 | case "author":
93 | author = string(bytes.TrimSpace(splitter[1]))
94 | default:
95 | continue
96 | }
97 |
98 | if title != "" && desc != "" && author != "" {
99 | break
100 | }
101 | }
102 |
103 | return title, desc, author
104 | }
105 |
106 | // Checks the index page's cache. Returns true if the
107 | // index needs to be re-cached.
108 | // This method helps satisfy the cacher interface.
109 | func (indexCache *indexCacheBlk) checkCache() bool {
110 | // if the last tally time is past the
111 | // interval in the config file, re-cache
112 | if interval, err := time.ParseDuration(viper.GetString("IndexRefreshInterval")); err == nil {
113 | indexCache.mu.RLock()
114 | if time.Since(indexCache.page.LastTally) > interval {
115 | indexCache.mu.RUnlock()
116 | return true
117 | }
118 | indexCache.mu.RUnlock()
119 | } else {
120 | log.Printf("Couldn't parse index refresh interval: %v\n", err.Error())
121 | }
122 |
123 | // if the stored mod time is different
124 | // from the file's modtime, re-cache
125 | confVars.mu.RLock()
126 | if stat, err := os.Stat(confVars.assetsDir + "/" + confVars.indexFile); err == nil {
127 | indexCache.mu.RLock()
128 | if stat.ModTime() != indexCache.page.Modtime {
129 | indexCache.mu.RUnlock()
130 | confVars.mu.RUnlock()
131 | return true
132 | }
133 | indexCache.mu.RUnlock()
134 | } else {
135 | log.Printf("Couldn't stat index page: %v\n", err.Error())
136 | }
137 | confVars.mu.RUnlock()
138 |
139 | // if the last tally time or stored mod time is zero, signal
140 | // to re-cache the index
141 | indexCache.mu.RLock()
142 | if indexCache.page.LastTally.IsZero() || indexCache.page.Modtime.IsZero() {
143 | indexCache.mu.RUnlock()
144 | return true
145 | }
146 | indexCache.mu.RUnlock()
147 |
148 | return false
149 | }
150 |
151 | // Re-caches the index page.
152 | // This method helps satisfy the cacher interface.
153 | func (indexCache *indexCacheBlk) cache() error {
154 | confVars.mu.RLock()
155 | body := render(genIndex(), confVars.wikiName+" "+confVars.titleSep+" "+confVars.wikiDesc)
156 | confVars.mu.RUnlock()
157 | if body == nil {
158 | return errors.New("indexPage.cache(): getting nil bytes")
159 | }
160 | indexCache.mu.Lock()
161 | indexCache.page.Body = body
162 | indexCache.mu.Unlock()
163 | return nil
164 | }
165 |
166 | // Generate the front page of the wiki
167 | func genIndex() []byte {
168 | var err error
169 | confVars.mu.RLock()
170 | indexpath := confVars.assetsDir + "/" + confVars.indexFile
171 | confVars.mu.RUnlock()
172 |
173 | stat, err := os.Stat(indexpath)
174 | if err != nil {
175 | log.Printf("Couldn't stat index: %v\n", err.Error())
176 | }
177 |
178 | indexCache.mu.RLock()
179 | if indexCache.page.Modtime != stat.ModTime() {
180 | indexCache.mu.RUnlock()
181 | indexCache.mu.Lock()
182 | indexCache.page.Raw, err = ioutil.ReadFile(indexpath)
183 | indexCache.mu.Unlock()
184 | if err != nil {
185 | return []byte("Could not open \"" + indexpath + "\"")
186 | }
187 | } else {
188 | indexCache.mu.RUnlock()
189 | }
190 |
191 | body := make([]byte, 0)
192 | buf := bytes.NewBuffer(body)
193 |
194 | // scan the file line by line until it finds the anchor
195 | // comment. replace the anchor comment with a list of
196 | // wiki pages sorted alphabetically by title.
197 | indexCache.mu.RLock()
198 | builder := bufio.NewScanner(bytes.NewReader(indexCache.page.Raw))
199 | indexCache.mu.RUnlock()
200 | builder.Split(bufio.ScanLines)
201 |
202 | for builder.Scan() {
203 | if bytes.Equal(builder.Bytes(), []byte("")) {
204 | tallyPages(buf)
205 | } else {
206 | n, err := buf.Write(append(builder.Bytes(), byte('\n')))
207 | if err != nil || n == 0 {
208 | log.Printf("Error writing to buffer: %v\n", err.Error())
209 | }
210 | }
211 | }
212 |
213 | // the LastTally field lets us know
214 | // when the index was last generated
215 | // by this function.
216 | indexCache.mu.Lock()
217 | indexCache.page.LastTally = time.Now()
218 | indexCache.mu.Unlock()
219 |
220 | return buf.Bytes()
221 | }
222 |
223 | // Generate a list of pages for the index.
224 | // Called by genIndex() when the anchor
225 | // comment has been found.
226 | func tallyPages(buf *bytes.Buffer) {
227 | // get a list of files in the directory specified
228 | // in the config file parameter "PageDir"
229 | confVars.mu.RLock()
230 | if files, err := ioutil.ReadDir(confVars.pageDir); err == nil {
231 | // entry is used in the loop to construct the markdown
232 | // link to the given page
233 | if len(files) == 0 {
234 | n, err := buf.WriteString("*No wiki pages! Add some content.*\n")
235 | if err != nil || n == 0 {
236 | log.Printf("Error writing to buffer: %v\n", err.Error())
237 | }
238 | confVars.mu.RUnlock()
239 | return
240 | }
241 |
242 | if confVars.reverseTally {
243 | for i := len(files) - 1; i >= 0; i-- {
244 | writeIndexLinks(files[i], buf)
245 | }
246 | } else {
247 | for _, f := range files {
248 | writeIndexLinks(f, buf)
249 | }
250 | }
251 | } else {
252 | n, err := buf.WriteString("*PageDir can't be read.*\n")
253 | if err != nil || n == 0 {
254 | log.Printf("Error writing to buffer: %v\n", err.Error())
255 | }
256 | }
257 |
258 | err := buf.WriteByte(byte('\n'))
259 | if err != nil {
260 | log.Printf("Error writing to buffer: %v\n", err.Error())
261 | }
262 | confVars.mu.RUnlock()
263 | }
264 |
265 | // Takes in a file and outputs a markdown link to it.
266 | // Called by tallyPages() for each file in the pages
267 | // directory.
268 | func writeIndexLinks(f os.FileInfo, buf *bytes.Buffer) {
269 | var page *Page
270 | var err error
271 | if _, exists := pageCache.pool[f.Name()]; exists {
272 | page, err = pullFromCache(f.Name())
273 | if err != nil {
274 | log.Printf("%v\n", err.Error())
275 | }
276 | } else {
277 | // if it hasn't been cached, cache it.
278 | // usually means the page is new.
279 | confVars.mu.RLock()
280 | newpage := newBarePage(confVars.pageDir+"/"+f.Name(), f.Name())
281 | confVars.mu.RUnlock()
282 | if err := newpage.cache(); err != nil {
283 | log.Printf("While caching page %v during the index generation, caught an error: %v\n", f.Name(), err.Error())
284 | }
285 | page, err = pullFromCache(f.Name())
286 | if err != nil {
287 | log.Printf("%v\n", err.Error())
288 | }
289 | }
290 | // get the URI path from the file name
291 | // and write the formatted link to the
292 | // bytes.Buffer
293 | linkname := bytes.TrimSuffix([]byte(page.Shortname), []byte(".md"))
294 | confVars.mu.RLock()
295 | n, err := buf.WriteString("* [" + page.Title + "](" + confVars.viewPath + string(linkname) + ") " + page.Desc + " " + page.Author + "\n")
296 | confVars.mu.RUnlock()
297 | if err != nil || n == 0 {
298 | log.Printf("Error writing to buffer: %v\n", err.Error())
299 | }
300 | }
301 |
302 | // Caches a page.
303 | // This method helps satisfy the cacher interface.
304 | func (page *Page) cache() error {
305 | // If buildPage() successfully returns a page
306 | // object ptr, then push it into the cache
307 | if newpage, err := buildPage(page.Longname); err == nil {
308 | pageCache.mu.Lock()
309 | pageCache.pool[newpage.Shortname] = newpage
310 | pageCache.mu.Unlock()
311 | } else {
312 | log.Printf("Couldn't cache %v: %v", page.Longname, err.Error())
313 | return err
314 | }
315 | return nil
316 | }
317 |
318 | // Compare the recorded modtime of a cached page to the
319 | // modtime of the file on disk. If they're different,
320 | // return `true`, indicating the cache needs
321 | // to be refreshed. Also returns `true` if the
322 | // page.Recache field is set to `true`.
323 | // This method helps satisfy the cacher interface.
324 | func (page *Page) checkCache() bool {
325 | if page == nil {
326 | return true
327 | }
328 |
329 | if newpage, err := os.Stat(page.Longname); err == nil {
330 | if newpage.ModTime() != page.Modtime || page.Recache {
331 | return true
332 | }
333 | } else {
334 | log.Println("Can't stat " + page.Longname + ". Using cached copy...")
335 | }
336 |
337 | return false
338 | }
339 |
340 | // When TildeWiki first starts, pull all available pages
341 | // into cache, saving their modification time as well to
342 | // detect changes to a page.
343 | func genPageCache() {
344 | // spawn a new goroutine for each entry, to cache
345 | // everything as quickly as possible
346 | confVars.mu.RLock()
347 | if wikipages, err := ioutil.ReadDir(confVars.pageDir); err == nil {
348 | var wg sync.WaitGroup
349 | for _, f := range wikipages {
350 | wg.Add(1)
351 | go func(f os.FileInfo) {
352 | confVars.mu.RLock()
353 | page := newBarePage(confVars.pageDir+"/"+f.Name(), f.Name())
354 | confVars.mu.RUnlock()
355 | if err := page.cache(); err != nil {
356 | log.Printf("While generating initial cache, caught error for %v: %v\n", f.Name(), err.Error())
357 | }
358 | log.Printf("Cached page %v\n", page.Shortname)
359 |
360 | wg.Done()
361 | }(f)
362 | }
363 | wg.Wait()
364 | } else {
365 | log.Printf("Initial cache build :: Can't read directory: %s\n", err.Error())
366 | log.Printf("**NOTICE** TildeWiki's cache may not function correctly until this is resolved.\n")
367 | log.Printf("\tPlease verify the directory in tildewiki.yml is correct and restart TildeWiki\n")
368 | }
369 | confVars.mu.RUnlock()
370 | }
371 |
372 | // Wrapper function to check the cache
373 | // of any cacher type, and if true,
374 | // re-cache the data
375 | func pingCache(c cacher) {
376 | if c.checkCache() {
377 | if err := c.cache(); err != nil {
378 | log.Printf("Pinged cache, received error while caching: %v\n", err.Error())
379 | }
380 | }
381 | }
382 |
383 | // Pulling from cache is its own function.
384 | // Less worrying about mutexes.
385 | func pullFromCache(filename string) (*Page, error) {
386 | pageCache.mu.RLock()
387 | if page, ok := pageCache.pool[filename]; ok {
388 | pageCache.mu.RUnlock()
389 | return page, nil
390 | }
391 | pageCache.mu.RUnlock()
392 |
393 | return nil, fmt.Errorf("error pulling %v from cache", filename)
394 | }
395 |
396 | // Blanks stored modtimes for the page cache.
397 | // Used to trigger a forced re-cache on the
398 | // next page load.
399 | func triggerRecache() {
400 | for _, v := range pageCache.pool {
401 | v.Recache = true
402 | }
403 | }
404 |
--------------------------------------------------------------------------------
/pages/example.md:
--------------------------------------------------------------------------------
1 |
6 |
7 | # template heading
8 |
9 | this is a test page. it is the first test page of the wiki
10 |
11 | ## sub header
12 |
13 | sub header text
14 |
15 | [back](/)
16 |
--------------------------------------------------------------------------------
/pages/test1.md:
--------------------------------------------------------------------------------
1 |
5 |
6 | # test page 1
7 |
8 | this is a test page. it is the first test page of the wiki
9 |
10 | ## sub header
11 |
12 | sub header text
13 |
14 | [back](/)
15 |
--------------------------------------------------------------------------------
/pages/test2.md:
--------------------------------------------------------------------------------
1 |
6 |
7 | # test page 1
8 |
9 | this is a test page. it is the first test page of the wiki
10 |
11 | ## sub header
12 |
13 | sub header text
14 |
15 | [back](/)
16 |
--------------------------------------------------------------------------------
/pages_test.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "bufio"
5 | "bytes"
6 | "io/ioutil"
7 | "log"
8 | "os"
9 | "sync"
10 | "testing"
11 | "time"
12 | )
13 |
14 | // to quiet the function output during
15 | // testing and benchmarks
16 | var hush, _ = os.Open("/dev/null")
17 |
18 | var buildPageCases = []struct {
19 | name string
20 | filename string
21 | want *Page
22 | wantErr bool
23 | }{
24 | {
25 | name: "example.md",
26 | filename: "pages/example.md",
27 | want: &Page{},
28 | wantErr: false,
29 | },
30 | {
31 | name: "fake.md",
32 | filename: "pages/fake.md",
33 | want: &Page{},
34 | wantErr: true,
35 | },
36 | }
37 |
38 | func Test_buildPage(t *testing.T) {
39 | log.SetOutput(hush)
40 | for _, tt := range buildPageCases {
41 | t.Run(tt.name, func(t *testing.T) {
42 | testpage, err := buildPage(tt.filename)
43 | if (err != nil) != tt.wantErr {
44 | t.Errorf("buildPage() error = %v, wantErr %v\n", err, tt.wantErr)
45 | }
46 | if testpage == nil && !tt.wantErr {
47 | t.Errorf("buildPage() returned nil bytes when it wasn't expected.\n")
48 | }
49 | })
50 | }
51 | }
52 | func Benchmark_buildPage(b *testing.B) {
53 | log.SetOutput(hush)
54 | b.ResetTimer()
55 | for i := 0; i < b.N; i++ {
56 | for _, c := range buildPageCases {
57 | _, err := buildPage(c.filename)
58 | if (err != nil) != c.wantErr {
59 | b.Errorf("buildPage benchmark failed: %v\n", err)
60 | }
61 | }
62 | }
63 | }
64 |
65 | var metaBytes, _ = ioutil.ReadFile("pages/example.md")
66 | var metaTestBytes pagedata = metaBytes
67 | var getMetaCases = []struct {
68 | name string
69 | data pagedata
70 | titlewant string
71 | descwant string
72 | authwant string
73 | }{
74 | {
75 | name: "example",
76 | data: metaTestBytes,
77 | titlewant: "Example Page",
78 | descwant: "Example page for the wiki",
79 | authwant: "gbmor",
80 | },
81 | }
82 |
83 | func Test_getMeta(t *testing.T) {
84 | for _, tt := range getMetaCases {
85 | t.Run(tt.name, func(t *testing.T) {
86 | if title, desc, auth := tt.data.getMeta(); title != tt.titlewant || desc != tt.descwant || auth != tt.authwant {
87 | t.Errorf("getMeta() = %v, %v, %v .. want %v, %v, %v", title, desc, auth, tt.titlewant, tt.descwant, tt.authwant)
88 | }
89 | })
90 | }
91 | }
92 | func Benchmark_getMeta(b *testing.B) {
93 | for i := 0; i < b.N; i++ {
94 | for _, tt := range getMetaCases {
95 | tt.data.getMeta()
96 | }
97 | }
98 | }
99 |
100 | func Test_genIndex(t *testing.T) {
101 | initConfigParams()
102 | log.SetOutput(hush)
103 | genPageCache()
104 | t.Run("genIndex() test", func(t *testing.T) {
105 | if got := genIndex(); got == nil {
106 | t.Errorf("genIndex(), got %v bytes.", got)
107 | }
108 | })
109 | }
110 | func Benchmark_genIndex(b *testing.B) {
111 | initConfigParams()
112 | log.SetOutput(hush)
113 | genPageCache()
114 | b.ResetTimer()
115 | for i := 0; i < b.N; i++ {
116 | indexCache.page.Modtime = time.Time{}
117 | genIndex()
118 | }
119 | }
120 |
121 | var tallyPagesPagelist = make([]byte, 0, 1)
122 | var tallyPagesBuf = bytes.NewBuffer(tallyPagesPagelist)
123 |
124 | // Currently tests for whether the buffer is being written to.
125 | // Also checks if the anchor tag was replaced in the buffer.
126 | func Test_tallyPages(t *testing.T) {
127 | t.Run("tallyPages test", func(t *testing.T) {
128 | if tallyPages(tallyPagesBuf); tallyPagesBuf == nil {
129 | t.Errorf("tallyPages() wrote nil to buffer\n")
130 | }
131 | bufscan := bufio.NewScanner(tallyPagesBuf)
132 | for bufscan.Scan() {
133 | if bufscan.Text() == "" {
134 | t.Errorf("tallyPages() - Did not replace anchor tag with page listing.\n")
135 | }
136 | }
137 | })
138 | }
139 | func Benchmark_tallyPages(b *testing.B) {
140 | for i := 0; i < b.N; i++ {
141 | // I'm not blanking the *Page values
142 | // before every run of tallyPages here
143 | // because the likelihood of
144 | // tallyPages calling page.cache() for
145 | // every page is near-zero
146 | if tallyPages(tallyPagesBuf); tallyPagesBuf == nil {
147 | b.Errorf("tallyPages() benchmark failed, got nil bytes\n")
148 | }
149 | }
150 | }
151 |
152 | type fields struct {
153 | Longname string
154 | Shortname string
155 | Title string
156 | Desc string
157 | Author string
158 | Modtime time.Time
159 | Body []byte
160 | Raw []byte
161 | }
162 |
163 | type indexFields struct {
164 | Modtime time.Time
165 | LastTally time.Time
166 | }
167 |
168 | var IndexCacheCases = []struct {
169 | name string
170 | fields indexFields
171 | want bool
172 | }{
173 | {
174 | name: "test1",
175 | fields: indexFields{
176 | LastTally: time.Now(),
177 | Modtime: time.Time{},
178 | },
179 | want: false,
180 | },
181 | {
182 | name: "test2",
183 | fields: indexFields{
184 | Modtime: time.Time{},
185 | LastTally: time.Time{},
186 | },
187 | want: true,
188 | },
189 | }
190 |
191 | var testIndex = indexCacheBlk{
192 | mu: &sync.RWMutex{},
193 | page: &indexPage{
194 | Modtime: time.Time{},
195 | LastTally: time.Time{},
196 | },
197 | }
198 |
199 | // Check if checkCache() method on indexPage type
200 | // is returning the expected bool
201 | func Test_indexPage_checkCache(t *testing.T) {
202 | initConfigParams()
203 | testindexstat, err := os.Stat(confVars.assetsDir + "/" + confVars.indexFile)
204 | if err != nil {
205 | t.Errorf("Test_indexPage_checkCache(): Couldn't stat file for first test case: %v\n", err)
206 | }
207 |
208 | for _, tt := range IndexCacheCases {
209 | t.Run(tt.name, func(t *testing.T) {
210 | if tt.name == "test1" {
211 | tt.fields.Modtime = testindexstat.ModTime()
212 | }
213 | testIndex.page.Modtime = tt.fields.Modtime
214 | testIndex.page.LastTally = tt.fields.LastTally
215 | if got := testIndex.checkCache(); got != tt.want {
216 | t.Errorf("indexPage.checkCache() - got %v, want %v\n", got, tt.want)
217 | }
218 | })
219 | }
220 | }
221 |
222 | func Benchmark_indexPage_checkCache(b *testing.B) {
223 | for i := 0; i < b.N; i++ {
224 | for range IndexCacheCases {
225 | testIndex.checkCache()
226 | }
227 | }
228 | }
229 |
230 | // Make sure indexPage.cache() is returning
231 | // non-nil bytes for indexPage.Body field
232 | func Test_indexPage_cache(t *testing.T) {
233 | for _, tt := range IndexCacheCases {
234 | t.Run(tt.name, func(t *testing.T) {
235 | testIndex.page.Modtime = tt.fields.Modtime
236 | testIndex.page.LastTally = tt.fields.LastTally
237 | testIndex.cache()
238 | if testIndex.page.Body == nil {
239 | t.Errorf("indexPage_cache(): Returning nil for field Body.\n")
240 | }
241 | })
242 | }
243 | }
244 | func Benchmark_indexPage_cache(b *testing.B) {
245 | for i := 0; i < b.N; i++ {
246 | for range IndexCacheCases {
247 | if err := testIndex.cache(); err != nil {
248 | b.Errorf("testIndex.cache() - %v\n", err)
249 | }
250 | }
251 | }
252 | }
253 |
254 | var pageCacheCase2stat, _ = os.Stat("pages/example.md")
255 | var pageCacheCase2bytes, _ = ioutil.ReadFile("pages/example.md")
256 | var pageCacheCase1bytes, _ = ioutil.ReadFile("pages/test1.md")
257 | var PageCacheCases = []struct {
258 | name string
259 | fields fields
260 | wantErr bool
261 | needCache bool
262 | }{
263 | {
264 | name: "test1.md",
265 | fields: fields{
266 | Longname: "pages/test1.md",
267 | Shortname: "test1.md",
268 | Modtime: time.Time{},
269 | Raw: pageCacheCase1bytes,
270 | },
271 | wantErr: false,
272 | needCache: true,
273 | },
274 | {
275 | name: "example.md",
276 | fields: fields{
277 | Longname: "pages/example.md",
278 | Shortname: "example.md",
279 | Modtime: pageCacheCase2stat.ModTime(),
280 | Raw: pageCacheCase2bytes,
281 | },
282 | wantErr: false,
283 | needCache: false,
284 | },
285 | {
286 | name: "doesn't exist",
287 | fields: fields{
288 | Longname: "pages/fake.md",
289 | Shortname: "fake.md",
290 | Modtime: time.Time{},
291 | },
292 | wantErr: true,
293 | needCache: false,
294 | },
295 | }
296 |
297 | // Tests that the raw field matches
298 | // what's been pulled from disk.
299 | func TestPage_cache(t *testing.T) {
300 | log.SetOutput(hush)
301 | for _, tt := range PageCacheCases {
302 | t.Run(tt.name, func(t *testing.T) {
303 | page := &Page{
304 | Longname: tt.fields.Longname,
305 | Shortname: tt.fields.Shortname,
306 | Raw: tt.fields.Raw,
307 | }
308 | if err := page.cache(); !tt.wantErr {
309 | cachedpage := pageCache.pool[tt.fields.Shortname]
310 | if !bytes.Equal(cachedpage.Raw, tt.fields.Raw) {
311 | t.Errorf("page.cache(): byte mismatch for %v: %v\n", page.Shortname, err)
312 | }
313 | }
314 | })
315 | }
316 | }
317 | func Benchmark_Page_cache(b *testing.B) {
318 | log.SetOutput(hush)
319 | b.ResetTimer()
320 | for i := 0; i < b.N; i++ {
321 | for _, tt := range PageCacheCases {
322 | page := &Page{
323 | Longname: tt.fields.Longname,
324 | Shortname: tt.fields.Shortname,
325 | }
326 | if err := page.cache(); err != nil && !tt.wantErr {
327 | b.Errorf("While benchmarking page.cache, caught: %v\n", err)
328 | }
329 | }
330 | }
331 | }
332 |
333 | // Make sure it's returning the appropriate
334 | // bool for zeroed modtime and current modtime
335 | func TestPage_checkCache(t *testing.T) {
336 | for _, tt := range PageCacheCases {
337 | t.Run(tt.name, func(t *testing.T) {
338 | page := &Page{
339 | Longname: tt.fields.Longname,
340 | Shortname: tt.fields.Shortname,
341 | Modtime: tt.fields.Modtime,
342 | }
343 | got := page.checkCache()
344 | if got != tt.needCache {
345 | t.Errorf("Page.checkCache() = %v", got)
346 | }
347 | })
348 | }
349 | }
350 | func Benchmark_Page_checkCache(b *testing.B) {
351 | for i := 0; i < b.N; i++ {
352 | for _, tt := range PageCacheCases {
353 | page := &Page{
354 | Longname: tt.fields.Longname,
355 | Shortname: tt.fields.Shortname,
356 | }
357 | page.checkCache()
358 | }
359 | }
360 | }
361 |
362 | // Check that the fields are filled
363 | // for each page in the cache
364 | func Test_genPageCache(t *testing.T) {
365 | initConfigParams()
366 | log.SetOutput(hush)
367 | genPageCache()
368 | t.Run("genPageCache", func(t *testing.T) {
369 | for k, v := range pageCache.pool {
370 | if v.Body == nil || v.Raw == nil || v.Longname == "" {
371 | t.Errorf("Test_genPageCache(): %v holds incorrect data or nil bytes\n", k)
372 | }
373 | }
374 | })
375 | }
376 | func Benchmark_genPageCache(b *testing.B) {
377 | initConfigParams()
378 | log.SetOutput(hush)
379 | b.ResetTimer()
380 | for i := 0; i < b.N; i++ {
381 | genPageCache()
382 | }
383 | }
384 |
385 | // Ensure pullFromCache() doesn't return a
386 | // nil page from the cache
387 | func Test_pullFromCache(t *testing.T) {
388 | initConfigParams()
389 | log.SetOutput(hush)
390 | genPageCache()
391 | t.Run("pullFromCache", func(t *testing.T) {
392 | for k := range pageCache.pool {
393 | page, err := pullFromCache(k)
394 | if page == nil || err != nil {
395 | t.Errorf("%v returned nil\n", k)
396 | }
397 | }
398 | })
399 | }
400 | func Benchmark_pullFromCache(b *testing.B) {
401 | initConfigParams()
402 | log.SetOutput(hush)
403 | genPageCache()
404 | b.ResetTimer()
405 | for i := 0; i < b.N; i++ {
406 | for k := range pageCache.pool {
407 | pullFromCache(k)
408 | }
409 | }
410 | }
411 |
412 | // tests if triggerRecache sets the trip bool
413 | // on all pages in the cache
414 | func Test_triggerRecache(t *testing.T) {
415 | initConfigParams()
416 | log.SetOutput(hush)
417 | genPageCache()
418 | t.Run("triggerRecache", func(t *testing.T) {
419 | triggerRecache()
420 | for k, v := range pageCache.pool {
421 | if !v.Recache {
422 | t.Errorf("Recache didn't trip for %v\n", k)
423 | }
424 | }
425 | })
426 | }
427 |
--------------------------------------------------------------------------------
/revive.toml:
--------------------------------------------------------------------------------
1 | ignoreGeneratedHeader = false
2 | severity = "warning"
3 | confidence = 0.8
4 | errorCode = 0
5 | warningCode = 0
6 |
7 | [rule.blank-imports]
8 | [rule.context-as-argument]
9 | [rule.context-keys-type]
10 | [rule.dot-imports]
11 | [rule.error-return]
12 | [rule.error-strings]
13 | [rule.error-naming]
14 | [rule.exported]
15 | [rule.if-return]
16 | [rule.increment-decrement]
17 | [rule.var-naming]
18 | [rule.var-declaration]
19 | [rule.package-comments]
20 | [rule.range]
21 | [rule.receiver-naming]
22 | [rule.time-naming]
23 | [rule.unexported-return]
24 | [rule.indent-error-flow]
25 | [rule.errorf]
26 | [rule.empty-block]
27 | [rule.superfluous-else]
28 | [rule.unused-parameter]
29 | [rule.unreachable-code]
30 | [rule.redefines-builtin-id]
31 |
--------------------------------------------------------------------------------
/setup.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 |
4 | install_tildewiki()
5 | {
6 | [[ $(id -u) != 0 ]] && noroot_die "I can't be installed unless this is run as root"
7 | display_logo
8 | echo
9 | echo Building TildeWiki ...
10 | go clean
11 | go build
12 | echo Creating user/group ...
13 | setup_usergrp
14 | echo Copying files ...
15 | copy_files
16 | install_startup_script
17 | echo Cleaning up ...
18 | go clean
19 | cat > /dev/stdout << EOF
20 |
21 | TildeWiki data has been installed to
22 | /usr/local/tildewiki
23 |
24 | You may now feel free to add content!
25 |
26 | To start TildeWiki, run:
27 | /usr/local/bin/tildewiki
28 |
29 | EOF
30 | }
31 |
32 | noroot_die()
33 | {
34 | echo
35 | echo -e "${1:-"Error"}" >&2
36 | exit 1
37 | }
38 |
39 | setup_usergrp()
40 | {
41 | adduser --home /usr/local/tildewiki --system --group tildewiki
42 | }
43 |
44 | copy_files()
45 | {
46 | mkdir -p /usr/local/tildewiki/
47 | cp ./tildewiki /usr/local/tildewiki/
48 | cp -r pages /usr/local/tildewiki/
49 | cp -r assets /usr/local/tildewiki/
50 | cp tildewiki.yaml /usr/local/tildewiki/
51 | chown -R tildewiki:tildewiki /usr/local/tildewiki
52 | }
53 |
54 | install_startup_script()
55 | {
56 | cat > /usr/local/bin/tildewiki << EOF
57 | #!/usr/bin/env bash
58 | error_exit() {
59 | echo -e "\${1:-"Unknown Error"}" >&2
60 | exit 1
61 | }
62 | [[ \$(id -u) != 0 ]] && error_exit "I can't daemonize unless I'm run as root :("
63 | /usr/sbin/daemonize -c /usr/local/tildewiki -u tildewiki /usr/local/tildewiki/tildewiki
64 | EOF
65 | chmod 755 /usr/local/bin/tildewiki
66 | }
67 |
68 | display_logo()
69 | {
70 | cat > /dev/stdout << EOF
71 | __ _ __ __ _ __ _
72 | / /_(_) /___/ /__ _ __(_) /__(_)
73 | / __/ / / __ / _ \ | /| / / / //_/ /
74 | / /_/ / / /_/ / __/ |/ |/ / / ,< / /
75 | \__/_/_/\__,_/\___/|__/|__/_/_/|_/_/
76 |
77 | :: TildeWiki v0.6.4 ::
78 | (c)2019 Ben Morrison (gbmor)
79 | GPL v3
80 | https://github.com/gbmor/tildewiki
81 | All Contributions Appreciated!
82 | EOF
83 | }
84 |
85 | uninstall_tildewiki()
86 | {
87 | [[ $(id -u) != 0 ]] && noroot_die "I can't be uninstalled unless this is run as root"
88 | display_logo
89 | echo
90 | echo Removing files ...
91 | rm -rf /usr/local/tildewiki
92 | rm -f /usr/local/bin/tildewiki
93 | echo Removing user/group ...
94 | userdel tildewiki
95 | echo TildeWiki successfully uninstalled!
96 | echo
97 | }
98 |
99 | display_help()
100 | {
101 | display_logo
102 | cat >/dev/stdout< tag between name and description
50 | TitleSeparator: "::"
51 |
52 | # Used between page names and descriptions
53 | DescSeparator: "::"
54 |
55 | # Little blurb for the tag
56 | ShortDesc: "Wiki for the Tildeverse"
57 |
58 | # Location of the CSS file. Can be relative or remote
59 | #CSS: "assets/wiki.css"
60 | CSS: "https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.css"
61 |
62 | # AssetsDir holds some of the configuration files
63 | # Like the `index` markdown document (front page), the error pages,
64 | # the icon / favicon, etc.
65 | AssetsDir: "assets"
66 |
67 | # Use the file name, not the full path
68 | Index: "wiki.md"
69 |
70 | # Again, just the file name. Currently, must be PNG, JPEG, or GIF.
71 | Icon: "icon.png"
72 |
73 | # PageDir holds the actual content pages for the wiki
74 | PageDir: "pages"
75 |
76 | # ReverseTally is false for Alphabetical, true for Reverse Alphabetical.
77 | # Set to reverse if you want to title your pages with dates and sort
78 | # the newest first.
79 | ReverseTally: false
80 |
81 | # Regex to validate the URLs. You probably don't want to change this.
82 | ValidPath: "^/(w)/([a-zA-Z0-9-_]+)$"
83 |
84 | # URL path used to delineate the wiki pages.
85 | # The default is "w", which would appear publicly as:
86 | # example.com/w/page
87 | # If you change this, change the (w) in ValidPath above to match.
88 | ViewPath: "w"
89 |
90 |
--------------------------------------------------------------------------------
/tools/racefind.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env -S bash -e
2 |
3 | go test -c -race
4 | PKG=$(basename $(pwd))
5 |
6 | while true ; do
7 | export GOMAXPROCS=$[ 1 + $[ RANDOM % 128 ]]
8 | ./$PKG.test $@ 2>&1
9 | done
10 |
--------------------------------------------------------------------------------
/types.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "regexp"
5 | "sync"
6 | "time"
7 | )
8 |
9 | // The in-memory page cache
10 | var pageCache = &pagesCache{
11 | mu: new(sync.RWMutex),
12 | pool: make(map[string]*Page),
13 | }
14 |
15 | // The in-memory index cache
16 | var indexCache = &indexCacheBlk{
17 | mu: new(sync.RWMutex),
18 | page: new(indexPage),
19 | }
20 |
21 | // indexPage and Page types implement
22 | // this interface, currently.
23 | type cacher interface {
24 | cache() error
25 | checkCache() bool
26 | }
27 |
28 | type ipCtxKey int
29 |
30 | const ctxKey ipCtxKey = iota
31 |
32 | type pagesCache struct {
33 | mu *sync.RWMutex
34 | pool map[string]*Page
35 | }
36 |
37 | type indexCacheBlk struct {
38 | mu *sync.RWMutex
39 | page *indexPage
40 | }
41 |
42 | type confParams struct {
43 | mu sync.RWMutex
44 | port string
45 | pageDir string
46 | assetsDir string
47 | cssPath string
48 | viewPath string
49 | indexRefreshInterval string
50 | wikiName string
51 | wikiDesc string
52 | descSep string
53 | titleSep string
54 | iconPath string
55 | indexFile string
56 | reverseTally bool
57 | validPath *regexp.Regexp
58 | quietLogging bool
59 | fileLogging bool
60 | logFile string
61 | }
62 |
63 | // Page cache object definition
64 | type Page struct {
65 | Longname string
66 | Shortname string
67 | Title string
68 | Desc string
69 | Author string
70 | Modtime time.Time
71 | Body []byte
72 | Raw pagedata
73 | Recache bool
74 | }
75 |
76 | // Index cache object definition
77 | type indexPage struct {
78 | Modtime time.Time
79 | LastTally time.Time
80 | Body []byte
81 | Raw pagedata
82 | }
83 |
84 | // Type alias for methods and readability
85 | // in certain situations.
86 | type pagedata []byte
87 |
88 | // Creates a filled page object
89 | func newPage(longname, shortname, title, author, desc string, modtime time.Time, body []byte, raw pagedata, recache bool) *Page {
90 | return &Page{
91 | Longname: longname,
92 | Shortname: shortname,
93 | Title: title,
94 | Author: author,
95 | Desc: desc,
96 | Modtime: modtime,
97 | Body: body,
98 | Raw: raw,
99 | Recache: recache}
100 |
101 | }
102 |
103 | // Creates a page object with the minimal number of fields filled
104 | func newBarePage(longname, shortname string) *Page {
105 | return &Page{
106 | Longname: longname,
107 | Shortname: shortname,
108 | Title: "",
109 | Author: "",
110 | Desc: "",
111 | Modtime: time.Time{},
112 | Body: nil,
113 | Raw: nil,
114 | }
115 | }
116 |
--------------------------------------------------------------------------------