Artifact
3c3b10854f6287df5c4a85e467345b09f372ffd2e28d0f7c074828f302fff329:
- File
cmd/tome/op_serve.go
— part of check-in
[614cd7fa9e]
at
2023-08-05 03:53:06
on branch trunk
— use command/v2 package
(user:
dnc
size: 3292)
0000: 2f 2f 20 43 6f 70 79 72 69 67 68 74 20 28 43 29 // Copyright (C)
0010: 20 32 30 32 30 2d 32 30 32 32 20 20 44 61 76 69 2020-2022 Davi
0020: 64 20 4e 2e 20 43 6f 68 65 6e 0a 0a 2f 2f 20 54 d N. Cohen..// T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66 his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3a 20 79 6f ree software: yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75 u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 20 6d 6f 64 te it and/or mod
0070: 69 66 79 0a 2f 2f 20 69 74 20 75 6e 64 65 72 20 ify.// it under
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
0090: 20 47 4e 55 20 41 66 66 65 72 6f 20 47 65 6e 65 GNU Affero Gene
00a0: 72 61 6c 20 50 75 62 6c 69 63 20 4c 69 63 65 6e ral Public Licen
00b0: 73 65 20 61 73 20 70 75 62 6c 69 73 68 65 64 20 se as published
00c0: 62 79 0a 2f 2f 20 74 68 65 20 46 72 65 65 20 53 by.// the Free S
00d0: 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 61 74 69 oftware Foundati
00e0: 6f 6e 2c 20 65 69 74 68 65 72 20 76 65 72 73 69 on, either versi
00f0: 6f 6e 20 33 20 6f 66 20 74 68 65 20 4c 69 63 65 on 3 of the Lice
0100: 6e 73 65 2c 20 6f 72 0a 2f 2f 20 28 61 74 20 79 nse, or.// (at y
0110: 6f 75 72 20 6f 70 74 69 6f 6e 29 20 61 6e 79 20 our option) any
0120: 6c 61 74 65 72 20 76 65 72 73 69 6f 6e 2e 0a 0a later version...
0130: 2f 2f 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 // This program
0140: 69 73 20 64 69 73 74 72 69 62 75 74 65 64 20 69 is distributed i
0150: 6e 20 74 68 65 20 68 6f 70 65 20 74 68 61 74 20 n the hope that
0160: 69 74 20 77 69 6c 6c 20 62 65 20 75 73 65 66 75 it will be usefu
0170: 6c 2c 0a 2f 2f 20 62 75 74 20 57 49 54 48 4f 55 l,.// but WITHOU
0180: 54 20 41 4e 59 20 57 41 52 52 41 4e 54 59 3b 20 T ANY WARRANTY;
0190: 77 69 74 68 6f 75 74 20 65 76 65 6e 20 74 68 65 without even the
01a0: 20 69 6d 70 6c 69 65 64 20 77 61 72 72 61 6e 74 implied warrant
01b0: 79 20 6f 66 0a 2f 2f 20 4d 45 52 43 48 41 4e 54 y of.// MERCHANT
01c0: 41 42 49 4c 49 54 59 20 6f 72 20 46 49 54 4e 45 ABILITY or FITNE
01d0: 53 53 20 46 4f 52 20 41 20 50 41 52 54 49 43 55 SS FOR A PARTICU
01e0: 4c 41 52 20 50 55 52 50 4f 53 45 2e 20 20 53 65 LAR PURPOSE. Se
01f0: 65 20 74 68 65 0a 2f 2f 20 47 4e 55 20 41 66 66 e the.// GNU Aff
0200: 65 72 6f 20 47 65 6e 65 72 61 6c 20 50 75 62 6c ero General Publ
0210: 69 63 20 4c 69 63 65 6e 73 65 20 66 6f 72 20 6d ic License for m
0220: 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a 0a 2f 2f ore details...//
0230: 20 59 6f 75 20 73 68 6f 75 6c 64 20 68 61 76 65 You should have
0240: 20 72 65 63 65 69 76 65 64 20 61 20 63 6f 70 79 received a copy
0250: 20 6f 66 20 74 68 65 20 47 4e 55 20 41 66 66 65 of the GNU Affe
0260: 72 6f 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 ro General Publi
0270: 63 20 4c 69 63 65 6e 73 65 0a 2f 2f 20 61 6c 6f c License.// alo
0280: 6e 67 20 77 69 74 68 20 74 68 69 73 20 70 72 6f ng with this pro
0290: 67 72 61 6d 2e 20 20 49 66 20 6e 6f 74 2c 20 73 gram. If not, s
02a0: 65 65 20 3c 68 74 74 70 73 3a 2f 2f 77 77 77 2e ee <https://www.
02b0: 67 6e 75 2e 6f 72 67 2f 6c 69 63 65 6e 73 65 73 gnu.org/licenses
02c0: 2f 3e 2e 0a 0a 70 61 63 6b 61 67 65 20 6d 61 69 />...package mai
02d0: 6e 0a 0a 69 6d 70 6f 72 74 20 28 0a 09 22 66 6c n..import (.."fl
02e0: 61 67 22 0a 09 22 66 6d 74 22 0a 09 22 69 6f 2f ag".."fmt".."io/
02f0: 66 73 22 0a 09 22 6c 6f 67 22 0a 09 22 6e 65 74 fs".."log".."net
0300: 2f 68 74 74 70 22 0a 09 22 6f 73 22 0a 09 22 70 /http".."os".."p
0310: 61 74 68 22 0a 09 22 73 6f 72 74 22 0a 09 22 73 ath".."sort".."s
0320: 74 72 69 6e 67 73 22 0a 0a 09 22 73 72 63 2e 64 trings"..."src.d
0330: 31 30 2e 64 65 76 2f 63 6f 6d 6d 61 6e 64 2f 76 10.dev/command/v
0340: 32 22 0a 09 22 73 72 63 2e 64 31 30 2e 64 65 76 2".."src.d10.dev
0350: 2f 74 6f 6d 65 2f 69 6e 74 65 72 6e 61 6c 2f 73 /tome/internal/s
0360: 6c 61 74 65 22 0a 09 22 73 72 63 2e 64 31 30 2e late".."src.d10.
0370: 64 65 76 2f 74 6f 6d 65 2f 69 6e 74 65 72 6e 61 dev/tome/interna
0380: 6c 2f 74 6f 6d 65 22 0a 29 0a 0a 66 75 6e 63 20 l/tome".)..func
0390: 69 6e 69 74 28 29 20 7b 0a 0a 09 63 6f 6d 6d 61 init() {...comma
03a0: 6e 64 2e 4f 70 65 72 61 74 69 6f 6e 2e 41 64 64 nd.Operation.Add
03b0: 28 0a 09 09 63 6f 6d 6d 61 6e 64 2e 4e 65 77 52 (...command.NewR
03c0: 6f 75 74 65 28 22 73 65 72 76 65 22 2c 20 6f 70 oute("serve", op
03d0: 53 65 72 76 65 2c 20 22 73 65 72 76 65 20 5b 2d Serve, "serve [-
03e0: 61 64 64 72 3d 48 4f 53 54 3a 50 4f 52 54 5d 20 addr=HOST:PORT]
03f0: 5b 44 49 52 45 43 54 4f 52 59 20 2e 2e 2e 5d 22 [DIRECTORY ...]"
0400: 29 2e 0a 09 09 09 44 65 73 63 72 69 62 65 28 22 ).....Describe("
0410: 48 54 54 50 20 73 65 72 76 65 72 20 72 65 6e 64 HTTP server rend
0420: 65 72 69 6e 67 20 6d 61 72 6b 64 6f 77 6e 20 69 ering markdown i
0430: 6e 20 48 54 4d 4c 22 29 2c 0a 09 29 0a 7d 0a 0a n HTML"),..).}..
0440: 2f 2f 20 73 6f 72 74 20 68 65 6c 70 65 72 20 66 // sort helper f
0450: 75 6e 63 74 69 6f 6e 0a 2f 2f 20 54 4f 44 4f 28 unction.// TODO(
0460: 64 6e 63 29 3a 20 6d 6f 76 65 20 74 6f 20 61 6e dnc): move to an
0470: 6f 74 68 65 72 20 66 69 6c 65 0a 66 75 6e 63 20 other file.func
0480: 73 6f 72 74 41 6c 70 68 61 57 69 74 68 46 69 72 sortAlphaWithFir
0490: 73 74 28 6e 61 6d 65 20 5b 5d 73 74 72 69 6e 67 st(name []string
04a0: 2c 20 66 69 72 73 74 20 73 74 72 69 6e 67 29 20 , first string)
04b0: 7b 0a 09 73 6f 72 74 2e 53 6c 69 63 65 28 6e 61 {..sort.Slice(na
04c0: 6d 65 2c 20 66 75 6e 63 28 69 2c 20 6a 20 69 6e me, func(i, j in
04d0: 74 29 20 62 6f 6f 6c 20 7b 0a 0a 09 09 69 66 20 t) bool {....if
04e0: 6e 61 6d 65 5b 69 5d 20 3d 3d 20 66 69 72 73 74 name[i] == first
04f0: 20 7b 20 2f 2f 20 6f 76 65 72 61 6c 6c 20 66 69 { // overall fi
0500: 72 73 74 0a 09 09 09 72 65 74 75 72 6e 20 74 72 rst....return tr
0510: 75 65 0a 09 09 7d 0a 0a 09 09 69 66 20 73 74 72 ue...}....if str
0520: 69 6e 67 73 2e 48 61 73 53 75 66 66 69 78 28 6e ings.HasSuffix(n
0530: 61 6d 65 5b 69 5d 2c 20 66 69 72 73 74 29 20 26 ame[i], first) &
0540: 26 20 70 61 74 68 2e 44 69 72 28 6e 61 6d 65 5b & path.Dir(name[
0550: 69 5d 29 20 3d 3d 20 70 61 74 68 2e 44 69 72 28 i]) == path.Dir(
0560: 6e 61 6d 65 5b 6a 5d 29 20 7b 20 2f 2f 20 66 69 name[j]) { // fi
0570: 72 73 74 20 69 6e 20 73 75 62 64 69 72 0a 09 09 rst in subdir...
0580: 09 72 65 74 75 72 6e 20 74 72 75 65 0a 09 09 7d .return true...}
0590: 0a 0a 09 09 72 65 74 75 72 6e 20 6e 61 6d 65 5b ....return name[
05a0: 69 5d 20 3c 20 6e 61 6d 65 5b 6a 5d 0a 09 7d 29 i] < name[j]..})
05b0: 0a 7d 0a 0a 66 75 6e 63 20 6f 70 53 65 72 76 65 .}..func opServe
05c0: 28 29 20 7b 0a 0a 09 2f 2f 20 66 6c 61 67 73 0a () {...// flags.
05d0: 09 61 64 64 72 46 6c 61 67 20 3a 3d 20 66 6c 61 .addrFlag := fla
05e0: 67 2e 53 74 72 69 6e 67 28 22 61 64 64 72 22 2c g.String("addr",
05f0: 20 22 3a 38 30 38 30 22 2c 20 22 61 64 64 72 65 ":8080", "addre
0600: 73 73 20 61 6e 64 20 70 6f 72 74 20 74 6f 20 73 ss and port to s
0610: 65 72 76 65 20 48 54 54 50 22 29 0a 09 66 6c 61 erve HTTP")..fla
0620: 67 2e 50 61 72 73 65 28 29 0a 0a 09 61 72 67 20 g.Parse()...arg
0630: 3a 3d 20 66 6c 61 67 2e 41 72 67 73 28 29 0a 09 := flag.Args()..
0640: 69 66 20 6c 65 6e 28 61 72 67 29 20 3d 3d 20 30 if len(arg) == 0
0650: 20 7b 0a 09 09 77 64 2c 20 65 72 72 20 3a 3d 20 {...wd, err :=
0660: 6f 73 2e 47 65 74 77 64 28 29 0a 09 09 63 6f 6d os.Getwd()...com
0670: 6d 61 6e 64 2e 43 68 65 63 6b 46 61 74 61 6c 28 mand.CheckFatal(
0680: 65 72 72 29 0a 0a 09 09 61 72 67 20 3d 20 61 70 err)....arg = ap
0690: 70 65 6e 64 28 61 72 67 2c 20 77 64 29 20 2f 2f pend(arg, wd) //
06a0: 20 64 65 66 61 75 6c 74 20 66 69 6e 64 20 6d 61 default find ma
06b0: 72 6b 64 6f 77 6e 20 69 6e 20 63 75 72 72 65 6e rkdown in curren
06c0: 74 20 64 69 72 65 63 74 6f 72 79 0a 09 7d 0a 0a t directory..}..
06d0: 09 2f 2f 20 70 72 65 70 61 72 65 20 74 6f 20 73 .// prepare to s
06e0: 65 72 76 65 20 6e 6f 6e 2d 6d 61 72 6b 64 6f 77 erve non-markdow
06f0: 6e 20 61 73 73 65 74 73 0a 09 73 6c 61 74 65 46 n assets..slateF
0700: 53 2c 20 65 72 72 20 3a 3d 20 66 73 2e 53 75 62 S, err := fs.Sub
0710: 28 73 6c 61 74 65 2e 45 6d 62 65 64 2c 20 73 6c (slate.Embed, sl
0720: 61 74 65 2e 50 72 65 66 69 78 29 0a 09 63 6f 6d ate.Prefix)..com
0730: 6d 61 6e 64 2e 43 68 65 63 6b 46 61 74 61 6c 28 mand.CheckFatal(
0740: 65 72 72 29 0a 0a 09 73 6c 61 74 65 53 65 72 76 err)...slateServ
0750: 65 72 20 3a 3d 20 68 74 74 70 2e 46 69 6c 65 53 er := http.FileS
0760: 65 72 76 65 72 28 68 74 74 70 2e 46 53 28 73 6c erver(http.FS(sl
0770: 61 74 65 46 53 29 29 0a 0a 09 68 74 74 70 2e 48 ateFS))...http.H
0780: 61 6e 64 6c 65 46 75 6e 63 28 22 2f 22 2c 20 66 andleFunc("/", f
0790: 75 6e 63 28 77 20 68 74 74 70 2e 52 65 73 70 6f unc(w http.Respo
07a0: 6e 73 65 57 72 69 74 65 72 2c 20 72 20 2a 68 74 nseWriter, r *ht
07b0: 74 70 2e 52 65 71 75 65 73 74 29 20 7b 0a 09 09 tp.Request) {...
07c0: 69 66 20 72 2e 55 52 4c 2e 50 61 74 68 20 3d 3d if r.URL.Path ==
07d0: 20 22 2f 22 20 7b 0a 0a 09 09 09 2f 2f 20 73 63 "/" {.....// sc
07e0: 61 6e 20 6d 61 72 6b 64 6f 77 6e 20 77 69 74 68 an markdown with
07f0: 20 65 76 65 72 79 20 72 65 71 75 65 73 74 20 28 every request (
0800: 70 69 63 6b 20 75 70 20 63 68 61 6e 67 65 73 20 pick up changes
0810: 6f 6e 20 72 65 66 72 65 73 68 29 0a 09 09 09 6d on refresh)....m
0820: 64 46 69 6c 65 20 3a 3d 20 5b 5d 73 74 72 69 6e dFile := []strin
0830: 67 7b 7d 0a 09 09 09 66 6f 72 20 5f 2c 20 73 72 g{}....for _, sr
0840: 63 20 3a 3d 20 72 61 6e 67 65 20 61 72 67 20 7b c := range arg {
0850: 0a 09 09 09 09 6d 64 66 2c 20 65 72 72 20 3a 3d .....mdf, err :=
0860: 20 74 6f 6d 65 2e 46 69 6e 64 53 75 66 66 69 78 tome.FindSuffix
0870: 28 73 72 63 2c 20 53 75 66 66 69 78 29 0a 09 09 (src, Suffix)...
0880: 09 09 69 66 20 65 72 72 20 21 3d 20 6e 69 6c 20 ..if err != nil
0890: 7b 0a 09 09 09 09 09 65 72 72 20 3d 20 66 6d 74 {......err = fmt
08a0: 2e 45 72 72 6f 72 66 28 22 66 61 69 6c 65 64 20 .Errorf("failed
08b0: 74 6f 20 66 69 6e 64 20 6d 61 72 6b 64 6f 77 6e to find markdown
08c0: 20 69 6e 20 64 69 72 65 63 74 6f 72 79 20 28 25 in directory (%
08d0: 71 29 3a 20 25 77 22 2c 20 73 72 63 2c 20 65 72 q): %w", src, er
08e0: 72 29 0a 09 09 09 09 09 68 74 74 70 2e 45 72 72 r)......http.Err
08f0: 6f 72 28 77 2c 20 65 72 72 2e 45 72 72 6f 72 28 or(w, err.Error(
0900: 29 2c 20 68 74 74 70 2e 53 74 61 74 75 73 49 6e ), http.StatusIn
0910: 74 65 72 6e 61 6c 53 65 72 76 65 72 45 72 72 6f ternalServerErro
0920: 72 29 0a 09 09 09 09 09 72 65 74 75 72 6e 0a 09 r)......return..
0930: 09 09 09 7d 0a 0a 09 09 09 09 2f 2f 20 73 6f 72 ...}......// sor
0940: 74 20 66 6f 75 6e 64 20 6d 61 72 6b 64 6f 77 6e t found markdown
0950: 20 66 69 6c 65 73 0a 09 09 09 09 2f 2f 20 54 4f files.....// TO
0960: 44 4f 28 64 6e 63 29 3a 20 6d 61 6b 65 20 63 6f DO(dnc): make co
0970: 6e 66 69 67 75 72 61 62 6c 65 20 77 68 69 63 68 nfigurable which
0980: 20 66 69 6c 65 6e 61 6d 65 20 63 6f 6d 65 73 20 filename comes
0990: 66 69 72 73 74 20 61 6e 64 20 6c 61 73 74 0a 09 first and last..
09a0: 09 09 09 2f 2f 20 54 4f 44 4f 28 64 6e 63 29 3a ...// TODO(dnc):
09b0: 20 73 75 70 70 6f 72 74 20 73 6f 72 74 20 62 79 support sort by
09c0: 20 61 6c 70 68 61 20 6f 72 20 66 69 6c 65 20 61 alpha or file a
09d0: 67 65 0a 09 09 09 09 73 6f 72 74 41 6c 70 68 61 ge.....sortAlpha
09e0: 57 69 74 68 46 69 72 73 74 28 6d 64 66 2c 20 22 WithFirst(mdf, "
09f0: 52 45 41 44 4d 45 2e 6d 64 22 29 0a 0a 09 09 09 README.md").....
0a00: 09 2f 2f 20 6f 76 65 72 61 6c 6c 20 6c 69 73 74 .// overall list
0a10: 20 6f 66 20 6d 61 72 6b 64 6f 77 6e 20 66 69 6c of markdown fil
0a20: 65 73 20 68 6f 6e 6f 72 73 20 6f 72 64 65 72 20 es honors order
0a30: 6f 66 20 61 72 67 73 0a 09 09 09 09 6d 64 46 69 of args.....mdFi
0a40: 6c 65 20 3d 20 61 70 70 65 6e 64 28 6d 64 46 69 le = append(mdFi
0a50: 6c 65 2c 20 6d 64 66 2e 2e 2e 29 0a 09 09 09 7d le, mdf...)....}
0a60: 0a 09 09 09 2f 2f 20 54 4f 44 4f 28 64 6e 63 29 ....// TODO(dnc)
0a70: 3a 20 63 6f 6e 73 69 64 65 72 20 72 65 6d 6f 76 : consider remov
0a80: 69 6e 67 20 64 75 70 6c 69 63 61 74 65 73 2c 20 ing duplicates,
0a90: 69 66 20 61 6e 79 2c 20 69 6e 20 6d 64 46 69 6c if any, in mdFil
0aa0: 65 20 6c 69 73 74 0a 0a 09 09 09 69 66 20 56 28 e list.....if V(
0ab0: 32 29 20 7b 0a 09 09 09 09 66 6f 72 20 69 20 3a 2) {.....for i :
0ac0: 3d 20 72 61 6e 67 65 20 6d 64 46 69 6c 65 20 7b = range mdFile {
0ad0: 0a 09 09 09 09 09 56 28 32 29 2e 4c 6f 67 66 28 ......V(2).Logf(
0ae0: 22 25 64 20 25 71 22 2c 20 69 2c 20 6d 64 46 69 "%d %q", i, mdFi
0af0: 6c 65 5b 69 5d 29 0a 09 09 09 09 7d 0a 09 09 09 le[i]).....}....
0b00: 7d 0a 09 09 09 69 66 20 6c 65 6e 28 6d 64 46 69 }....if len(mdFi
0b10: 6c 65 29 20 3d 3d 20 30 20 7b 0a 09 09 09 09 6c le) == 0 {.....l
0b20: 6f 67 2e 50 72 69 6e 74 66 28 22 6e 6f 20 66 69 og.Printf("no fi
0b30: 6c 65 73 20 65 6e 64 69 6e 67 20 25 71 20 66 6f les ending %q fo
0b40: 75 6e 64 20 69 6e 20 25 76 22 2c 20 53 75 66 66 und in %v", Suff
0b50: 69 78 2c 20 61 72 67 29 0a 09 09 09 7d 0a 0a 09 ix, arg)....}...
0b60: 09 09 65 72 72 20 3a 3d 20 74 6f 6d 65 2e 4d 61 ..err := tome.Ma
0b70: 72 6b 64 6f 77 6e 54 6f 53 6c 61 74 65 28 77 2c rkdownToSlate(w,
0b80: 20 6d 64 46 69 6c 65 29 0a 09 09 09 69 66 20 65 mdFile)....if e
0b90: 72 72 20 21 3d 20 6e 69 6c 20 7b 0a 09 09 09 09 rr != nil {.....
0ba0: 68 74 74 70 2e 45 72 72 6f 72 28 77 2c 20 65 72 http.Error(w, er
0bb0: 72 2e 45 72 72 6f 72 28 29 2c 20 68 74 74 70 2e r.Error(), http.
0bc0: 53 74 61 74 75 73 49 6e 74 65 72 6e 61 6c 53 65 StatusInternalSe
0bd0: 72 76 65 72 45 72 72 6f 72 29 0a 09 09 09 09 72 rverError).....r
0be0: 65 74 75 72 6e 0a 09 09 09 7d 0a 0a 09 09 09 72 eturn....}.....r
0bf0: 65 74 75 72 6e 0a 09 09 7d 0a 0a 09 09 2f 2f 20 eturn...}....//
0c00: 6f 74 68 65 72 20 72 65 71 75 65 73 74 73 2c 20 other requests,
0c10: 64 65 66 65 72 20 74 6f 20 73 6c 61 74 65 20 66 defer to slate f
0c20: 69 6c 65 73 0a 09 09 73 6c 61 74 65 53 65 72 76 iles...slateServ
0c30: 65 72 2e 53 65 72 76 65 48 54 54 50 28 77 2c 20 er.ServeHTTP(w,
0c40: 72 29 0a 09 7d 29 0a 0a 09 56 28 31 29 2e 4c 6f r)..})...V(1).Lo
0c50: 67 66 28 22 73 65 72 76 69 6e 67 20 63 6f 6e 74 gf("serving cont
0c60: 65 6e 74 20 28 25 76 29 20 6f 6e 20 25 71 22 2c ent (%v) on %q",
0c70: 20 61 72 67 2c 20 2a 61 64 64 72 46 6c 61 67 29 arg, *addrFlag)
0c80: 20 2f 2f 20 76 65 72 62 6f 73 65 0a 09 63 6f 6d // verbose..com
0c90: 6d 61 6e 64 2e 43 68 65 63 6b 28 68 74 74 70 2e mand.Check(http.
0ca0: 4c 69 73 74 65 6e 41 6e 64 53 65 72 76 65 28 2a ListenAndServe(*
0cb0: 61 64 64 72 46 6c 61 67 2c 20 6e 6f 73 74 6f 72 addrFlag, nostor
0cc0: 65 28 68 74 74 70 2e 44 65 66 61 75 6c 74 53 65 e(http.DefaultSe
0cd0: 72 76 65 4d 75 78 29 29 29 0a 7d 0a rveMux))).}.