commit b548faa258777bb8a0002308ecb6ef3b6e3b18c7 Author: Veillax <126523099+Veillax@users.noreply.github.com> Date: Sat Oct 31 20:16:33 2026 -0600 Basking: a place to dump thoughts, jot notes and keep a journal Plain markdown files, tags by typing #hashtags, a page per day, search, pin and trash with undo, autosave with conflict warnings, API tokens, zip export, and the Lightwell dashboard. Includes install.sh, CI and release workflows. Co-Authored-By: Claude Sonnet 5.5 diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..b9806df --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,42 @@ +name: CI + +on: + push: + branches: [main, master] + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + + - name: Formatting + run: | + unformatted=$(gofmt -l .) + if [ -n "$unformatted" ]; then + echo "These files need gofmt:" + echo "$unformatted" + exit 1 + fi + + - name: Design rules + run: node scripts/lint-design.mjs + + - name: Vet + run: go vet ./... + + - name: Test + run: go test ./... + + # Catches platform-specific breakage before a release is tagged. + - name: Every release platform compiles + run: | + for t in linux/amd64 linux/arm64 windows/amd64; do + echo "building for $t" + GOOS=${t%/*} GOARCH=${t#*/} CGO_ENABLED=0 go build -o /dev/null ./cmd/basking + done diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml new file mode 100644 index 0000000..d11b9bd --- /dev/null +++ b/.gitea/workflows/release.yml @@ -0,0 +1,34 @@ +name: Release + +# Push a tag such as v0.1.0 to build and publish a release: +# git tag -a v0.1.0 -m "basking v0.1.0" && git push origin v0.1.0 +on: + push: + tags: ['v*'] + +jobs: + release: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # release notes come from the history since the previous tag + + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + + - name: Test + run: | + go vet ./... + go test ./... + + - name: Build release archives + run: sh deploy/build.sh "${GITHUB_REF_NAME}" + + # Optional secret RELEASE_TOKEN: an access token that can write releases, for + # servers where the automatic job token can't. + - name: Publish the release + env: + TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} + run: sh deploy/publish-release.sh "${GITHUB_REF_NAME}" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bda7484 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +bin/ +dist/ +*.exe +data/ +basking.json +.env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2221011 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +# Build: docker build -t basking . +FROM golang:1.27-alpine AS build +WORKDIR /src +COPY . . +RUN CGO_ENABLED=0 go build -trimpath -ldflags "-s -w" -o /basking ./cmd/basking + +FROM alpine:3.20 +RUN apk add --no-cache tzdata ca-certificates && adduser -D -u 10001 basking && mkdir /config && chown basking /config +COPY --from=build /basking /usr/local/bin/basking +USER basking +# The config file and the notes both live under /config. +ENV BASKING_CONFIG=/config/basking.json +WORKDIR /config +EXPOSE 8490 +HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://127.0.0.1:8490/healthz || exit 1 +ENTRYPOINT ["basking", "serve", "-listen", "0.0.0.0:8490"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ed16188 --- /dev/null +++ b/LICENSE @@ -0,0 +1,235 @@ +GNU AFFERO GENERAL PUBLIC LICENSE +Version 3, 19 November 2007 + +Copyright (C) 2007 Free Software Foundation, Inc. + +Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. + + Preamble + +The GNU Affero General Public License is a free, copyleft license for software and other kinds of works, specifically designed to ensure cooperation with the community in the case of network server software. + +The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. + +When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. + +Developers that use our General Public Licenses protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License which gives you legal permission to copy, distribute and/or modify the software. + +A secondary benefit of defending all users' freedom is that improvements made in alternate versions of the program, if they receive widespread use, become available for other developers to incorporate. Many developers of free software are heartened and encouraged by the resulting cooperation. However, in the case of software used on network servers, this result may fail to come about. The GNU General Public License permits making a modified version and letting the public access it on a server without ever releasing its source code to the public. + +The GNU Affero General Public License is designed specifically to ensure that, in such cases, the modified source code becomes available to the community. It requires the operator of a network server to provide the source code of the modified version running there to the users of that server. Therefore, public use of a modified version, on a publicly accessible server, gives the public access to the source code of the modified version. + +An older license, called the Affero General Public License and published by Affero, was designed to accomplish similar goals. This is a different license, not a version of the Affero GPL, but Affero has released a new version of the Affero GPL which permits relicensing under this license. + +The precise terms and conditions for copying, distribution and modification follow. + + TERMS AND CONDITIONS + +0. Definitions. + +"This License" refers to version 3 of the GNU Affero General Public License. + +"Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. + +"The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. + +To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. + +A "covered work" means either the unmodified Program or a work based on the Program. + +To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. + +To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. + +An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. + +1. Source Code. +The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. + +A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. + +The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. + +The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those +subprograms and other parts of the work. + +The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. + +The Corresponding Source for a work in source code form is that same work. + +2. Basic Permissions. +All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. + +You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. + +Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. + +3. Protecting Users' Legal Rights From Anti-Circumvention Law. +No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. + +When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. + +4. Conveying Verbatim Copies. +You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. + +You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. + +5. Conveying Modified Source Versions. +You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". + + c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. + +A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. + +6. Conveying Non-Source Forms. +You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: + + a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. + + d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. + +A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. + +A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. + +"Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. + +If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). + +The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. + +Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. + +7. Additional Terms. +"Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. + +When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. + +Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or authors of the material; or + + e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. + +All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. + +If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. + +Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. + +8. Termination. + +You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). + +However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. + +Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. + +Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. + +9. Acceptance Not Required for Having Copies. + +You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. + +10. Automatic Licensing of Downstream Recipients. + +Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. + +An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. + +You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. + +11. Patents. + +A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". + +A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. + +Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. + +In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. + +If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. + +If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. + +A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. + +Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. + +12. No Surrender of Others' Freedom. + +If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. + +13. Remote Network Interaction; Use with the GNU General Public License. + +Notwithstanding any other provision of this License, if you modify the Program, your modified version must prominently offer all users interacting with it remotely through a computer network (if your version supports such interaction) an opportunity to receive the Corresponding Source of your version by providing access to the Corresponding Source from a network server at no charge, through some standard or customary means of facilitating copying of software. This Corresponding Source shall include the Corresponding Source for any work covered by version 3 of the GNU General Public License that is incorporated pursuant to the following paragraph. + +Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the work with which it is combined will remain governed by version 3 of the GNU General Public License. + +14. Revised Versions of this License. + +The Free Software Foundation may publish revised and/or new versions of the GNU Affero General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU Affero General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU Affero General Public License, you may choose any version ever published by the Free Software Foundation. + +If the Program specifies that a proxy can decide which future versions of the GNU Affero General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. + +Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. + +15. Disclaimer of Warranty. + +THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + +16. Limitation of Liability. + +IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +17. Interpretation of Sections 15 and 16. + +If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. + +END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + +If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. + +To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. + + Hammerhead + Copyright (C) 2026 Veillax + + This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + +If your software can interact with users remotely through a computer network, you should also make sure that it provides a way for users to get its source. For example, if your program is a web application, its interface could display a "Source" link that leads users to an archive of the code. There are many ways you could offer source, and different solutions will be better for different programs; see section 13 for the specific requirements. + +You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU AGPL, see . diff --git a/README.md b/README.md new file mode 100644 index 0000000..b244950 --- /dev/null +++ b/README.md @@ -0,0 +1,156 @@ +# Basking + +A place to dump thoughts, jot notes and keep a journal. Named for the basking shark, which swims +along with its mouth open and takes in whatever drifts by. One small binary, no database, no +dependencies. + +Your notes are plain markdown files in a folder you own. Basking is just a fast, comfortable way to +write them and find them again, from a browser on your phone or your desk. + +## What it does + +- **Dump first, sort later.** The box at the top of Notes is always ready. Type, press Ctrl+Enter, + keep typing. An unsaved draft survives a closed tab. +- **Tags by typing.** Write `#errands` or `#projects/basking` anywhere in a note and it becomes a tag. + The sidebar lists them. Numbers such as `#12` and anything inside code are left alone. +- **A page for each day.** Today opens the day's journal page, with an Insert time button to stamp + `20:15 ` where you are typing. Move between days, or read back over the Journal. +- **Search** across everything as you type: every word must match, and `#tag` words filter by tag. +- **Markdown**, rendered safely: headings, lists, checkboxes you can tick in place, quotes, code, + links and bold and italic. +- **Pin** what matters. **Delete** goes to a trash first, with an Undo, and nothing is lost until you + empty it. +- **Autosave** while you edit, and a warning (not a silent overwrite) if the same note was changed on + another device. +- **Send notes from anywhere.** Make an API token and post text from a script, a phone shortcut or the + `basking add` command. +- **Take it with you.** Download everything as a zip of `.md` files, or just copy the folder. + +## Installing on Linux (systemd) + +```sh +curl -fsSL https://git.veillax.com/Veillax/basking/raw/branch/main/deploy/install.sh | sudo sh +``` + +This downloads the latest release (checking its SHA-256), installs `/usr/local/bin/basking`, creates +a `basking` user, `/etc/basking/` (settings) and `/var/lib/basking/` (your notes), and writes the +systemd service and `/etc/basking/basking.env`. It then tells you what to do next: + +```sh +sudoedit /etc/basking/basking.env # optional: the address and where notes are kept +sudo systemctl start basking +journalctl -u basking -f # the first start prints your password here, once +``` + +Run it again to upgrade; your settings and notes are kept and a running service is restarted. + +Options: `--start` (start the service straight away), `--version v0.1.0`, `--listen 0.0.0.0:8490`, +`--source` (build from the repository with Go instead), and `--uninstall` (add `--purge` to delete +settings and notes). Download the script and read it first if you prefer: it is +[`deploy/install.sh`](deploy/install.sh). + +## Running it by hand + +```sh +# download a release, or: go build ./cmd/basking +./basking serve +``` + +On first run it writes `basking.json` and prints a password **once** in the log. Open +http://127.0.0.1:8490 and sign in. Notes go in `./data/notes/`. + +By default it only listens on `127.0.0.1`. To reach it from your phone, put it behind an HTTPS +reverse proxy, or expose it through [Hammerhead](https://git.veillax.com/Veillax/hammerhead), and set +`"cookie_secure": true` and `"trust_proxy": true` in `basking.json`. Don't put it on the open +internet over plain HTTP: it holds your private thoughts. + +Docker: see [`Dockerfile`](Dockerfile) and [`deploy/docker-compose.yml`](deploy/docker-compose.yml). + +### Commands + +``` +basking serve [-config basking.json] [-listen addr] [-data dir] +basking password [-config ...] set a new password (BASKING_PASSWORD or stdin); restart afterwards +basking token [-config ...] NAME make an API token (printed once) +basking add [TEXT...] send a note to a running Basking (needs BASKING_URL and BASKING_TOKEN) +``` + +## Settings + +`basking.json` holds `listen` (default `127.0.0.1:8490`), `data_dir` (default `data`), the password +hash, API token hashes, a signing secret, and `cookie_secure` and `trust_proxy` for use behind a +proxy. The environment variables `BASKING_CONFIG`, `BASKING_LISTEN` and `BASKING_DATA` override the +file location, address and notes folder. + +## Where your notes are + +``` +data/ + notes/20261031-143205-a1b2c3.md one file per note + notes/daily-2026-10-31.md the journal page for a day + trash/ deleted notes, until the trash is emptied +``` + +A note file looks like this, so you can edit it with anything: + +``` +--- +created: 2026-10-31T14:32:05.123Z +updated: 2026-10-31T14:35:00.456Z +pinned: true +--- + +Call the dentist #errands +``` + +A plain `.md` file you drop into `notes/` is picked up too, with its file time as its date (name it +with lower-case letters, numbers and dashes). Restart Basking to see files added by hand. + +## API + +Sign in from a browser, or send `Authorization: Bearer bsk_...`. + +```sh +curl -H "Authorization: Bearer $TOKEN" -H "Content-Type: text/plain" \ + -d "Call the dentist #errands" http://127.0.0.1:8490/api/notes +``` + +`GET /api/notes?q=&tag=&view=all|pinned|daily|trash&offset=&limit=`, `POST /api/notes` (JSON +`{"body": "..."}` or plain text), `GET|PUT|DELETE /api/notes/{id}`, `POST /api/notes/{id}/restore`, +`DELETE /api/trash`, `POST /api/daily` (`{"date": "2026-10-31"}`), `GET /api/summary`, +`GET /api/export`. + +## Security notes + +- The password is stored as a PBKDF2-SHA256 hash and API tokens as SHA-256 hashes. `basking.json` is + written `0600`. Failed sign-ins are rate-limited per address. +- Sessions are `HttpOnly`, `SameSite=Strict` cookies that last 30 days, signed with a secret from the + settings file. They are not kept on the server, so a restart does not sign you out, and signing out + only clears this browser's cookie. Changing the password signs out every browser. +- Every browser change needs a header a page on another site can't send. The page has a strict + Content-Security-Policy and notes are never rendered as HTML. +- Notes are **not encrypted** on disk. Anyone who can read the folder can read your notes. + +## Honest limits + +- One person, one password. There are no accounts, sharing or real-time collaboration. +- No attachments or images, and no offline mode: it needs the server to be reachable. +- Searching holds every note in memory, which is comfortable for tens of thousands of notes and not + built for millions. +- Notes are only reloaded from disk when Basking starts. + +## Design + +The dashboard follows the Lightwell design language, the same one Hammerhead and Megalodon use. See +[docs/design.md](docs/design.md). `node scripts/lint-design.mjs` checks the rules a machine can check. + +## Development + +```sh +go test ./... +go run ./cmd/basking serve +``` + +Releases are built by the Gitea workflow when a `v*` tag is pushed. + +Licensed under the AGPL-3.0 (see [LICENSE](LICENSE)). diff --git a/cmd/basking/main.go b/cmd/basking/main.go new file mode 100644 index 0000000..93001c9 --- /dev/null +++ b/cmd/basking/main.go @@ -0,0 +1,245 @@ +// Command basking is a place to dump thoughts, jot notes and keep a journal. Notes are plain +// markdown files; the dashboard is one small binary. +package main + +import ( + "bufio" + "bytes" + "context" + "encoding/json" + "errors" + "flag" + "fmt" + "io" + "log" + "net/http" + "os" + "os/signal" + "strings" + "syscall" + "time" + + "git.veillax.com/Veillax/basking/internal/config" + "git.veillax.com/Veillax/basking/internal/store" + "git.veillax.com/Veillax/basking/internal/web" +) + +var version = "dev" + +const usage = `basking is a place to dump thoughts, jot notes and keep a journal. + + basking serve [-config basking.json] [-listen addr] [-data dir] + run the dashboard + basking password [-config ...] set the password (reads BASKING_PASSWORD or a line on stdin) + basking token [-config ...] NAME create an API token for scripts (shown once) + basking add [TEXT...] send a note to a running Basking; reads stdin if no text + basking version + +"add" needs BASKING_URL (such as http://127.0.0.1:8490) and BASKING_TOKEN. +The settings path can also be set with BASKING_CONFIG, the address with BASKING_LISTEN and the +notes folder with BASKING_DATA. +` + +func main() { + log.SetFlags(log.LstdFlags) + if len(os.Args) < 2 { + fmt.Fprint(os.Stderr, usage) + os.Exit(2) + } + var err error + switch os.Args[1] { + case "serve": + err = serve(os.Args[2:]) + case "password": + err = passwordCmd(os.Args[2:]) + case "token": + err = tokenCmd(os.Args[2:]) + case "add": + err = addCmd(os.Args[2:]) + case "version", "-v", "--version": + fmt.Println("basking", version) + case "help", "-h", "--help": + fmt.Print(usage) + default: + fmt.Fprintf(os.Stderr, "unknown command %q\n\n%s", os.Args[1], usage) + os.Exit(2) + } + if err != nil { + fmt.Fprintln(os.Stderr, "basking:", err) + os.Exit(1) + } +} + +func configFlag(fs *flag.FlagSet) *string { + def := os.Getenv("BASKING_CONFIG") + if def == "" { + def = "basking.json" + } + return fs.String("config", def, "path of the settings file") +} + +func serve(args []string) error { + fs := flag.NewFlagSet("serve", flag.ExitOnError) + path := configFlag(fs) + listen := fs.String("listen", os.Getenv("BASKING_LISTEN"), "override the listen address") + data := fs.String("data", os.Getenv("BASKING_DATA"), "override the folder the notes are kept in") + fs.Parse(args) + + h, err := config.Load(*path) + if err != nil { + return err + } + // First run: make a password so the dashboard can be opened, and show it once. + if h.Get().PasswordHash == "" { + pw := config.RandomPassword() + hash, err := config.HashPassword(pw) + if err != nil { + return err + } + if err := h.Update(func(c *config.Config) error { c.PasswordHash = hash; return nil }); err != nil { + return err + } + log.Printf("first run: your password is %s (change it in Settings; written to %s)", pw, *path) + } + cfg := h.Get() + if *listen != "" { + cfg.Listen = *listen + } + if *data != "" { + cfg.DataDir = *data + } + + st, err := store.Open(cfg.DataDir) + if err != nil { + return err + } + counts, _ := st.Summary() + + srv := &http.Server{ + Addr: cfg.Listen, + Handler: web.New(h, st, version).Handler(), + ReadHeaderTimeout: 10 * time.Second, + ReadTimeout: 30 * time.Second, + WriteTimeout: 60 * time.Second, + IdleTimeout: 2 * time.Minute, + } + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + errc := make(chan error, 1) + go func() { errc <- srv.ListenAndServe() }() + log.Printf("basking %s: %d note(s) in %s; open http://%s", version, counts.All, cfg.DataDir, cfg.Listen) + if !strings.HasPrefix(cfg.Listen, "127.") && !strings.HasPrefix(cfg.Listen, "localhost") && !strings.HasPrefix(cfg.Listen, "[::1]") { + log.Print("note: this is not a loopback address; put it behind HTTPS (a reverse proxy or Hammerhead) and set cookie_secure") + } + + select { + case err := <-errc: + return err + case <-ctx.Done(): + } + sctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + return srv.Shutdown(sctx) +} + +func readPassword() (string, error) { + if pw := os.Getenv("BASKING_PASSWORD"); pw != "" { + return pw, nil + } + fmt.Fprint(os.Stderr, "new password: ") + line, err := bufio.NewReader(os.Stdin).ReadString('\n') + if err != nil && line == "" { + return "", errors.New("no password given") + } + return strings.TrimRight(line, "\r\n"), nil +} + +func passwordCmd(args []string) error { + fs := flag.NewFlagSet("password", flag.ExitOnError) + path := configFlag(fs) + fs.Parse(args) + pw, err := readPassword() + if err != nil { + return err + } + if len(pw) < 8 { + return errors.New("the password must be at least 8 characters") + } + hash, err := config.HashPassword(pw) + if err != nil { + return err + } + h, err := config.Load(*path) + if err != nil { + return err + } + if err := h.Update(func(c *config.Config) error { c.PasswordHash = hash; return nil }); err != nil { + return err + } + fmt.Println("password saved; restart Basking if it is running, and everyone will be signed out") + return nil +} + +func tokenCmd(args []string) error { + fs := flag.NewFlagSet("token", flag.ExitOnError) + path := configFlag(fs) + fs.Parse(args) + if fs.NArg() != 1 { + return errors.New("expected NAME") + } + token, hash, err := config.NewToken() + if err != nil { + return err + } + h, err := config.Load(*path) + if err != nil { + return err + } + err = h.Update(func(c *config.Config) error { + c.Tokens = append(c.Tokens, config.Token{Name: fs.Arg(0), Hash: hash, Created: time.Now().UTC()}) + return nil + }) + if err == nil { + fmt.Println(token) + fmt.Fprintln(os.Stderr, "this is the only time the token is shown; send it as 'Authorization: Bearer '") + fmt.Fprintln(os.Stderr, "a running Basking picks it up after a restart") + } + return err +} + +func addCmd(args []string) error { + url, token := os.Getenv("BASKING_URL"), os.Getenv("BASKING_TOKEN") + if url == "" || token == "" { + return errors.New("set BASKING_URL and BASKING_TOKEN (make a token in Settings)") + } + text := strings.Join(args, " ") + if text == "" { + b, err := io.ReadAll(os.Stdin) + if err != nil { + return err + } + text = string(b) + } + if strings.TrimSpace(text) == "" { + return errors.New("nothing to add") + } + body, _ := json.Marshal(map[string]string{"body": text}) + req, err := http.NewRequest("POST", strings.TrimRight(url, "/")+"/api/notes", bytes.NewReader(body)) + if err != nil { + return err + } + req.Header.Set("Authorization", "Bearer "+token) + req.Header.Set("Content-Type", "application/json") + resp, err := (&http.Client{Timeout: 15 * time.Second}).Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusCreated { + var e struct{ Error string } + json.NewDecoder(resp.Body).Decode(&e) + return fmt.Errorf("%s: %s", resp.Status, e.Error) + } + fmt.Println("saved") + return nil +} diff --git a/deploy/build.sh b/deploy/build.sh new file mode 100644 index 0000000..c1d7389 --- /dev/null +++ b/deploy/build.sh @@ -0,0 +1,44 @@ +#!/bin/sh +# Builds basking for every release platform and packs the archives into dist/. +# +# deploy/build.sh [version] version defaults to `git describe` +# +# Output: +# dist/basking___.tar.gz or .zip +# dist/SHA256SUMS +# +# Releases are published for linux/amd64, linux/arm64 and windows/amd64. Set TARGETS="linux/arm64 darwin/arm64" +# (any GOOS/GOARCH pair Go supports) to build something else by hand. +set -eu +NAME=basking +CMDS="basking" +cd "$(dirname "$0")/.." +VERSION=${1:-$(git describe --tags --always 2>/dev/null || echo dev)} +TARGETS=${TARGETS:-"linux/amd64 linux/arm64 windows/amd64"} + +rm -rf dist +mkdir -p dist/stage +for t in $TARGETS; do + os=${t%/*} + arch=${t#*/} + ext="" + [ "$os" = windows ] && ext=".exe" + pkg="${NAME}_${VERSION}_${os}_${arch}" + stage="dist/stage/$pkg" + mkdir -p "$stage" + for c in $CMDS; do + echo "building $c $VERSION for $os/$arch" + CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath -ldflags "-s -w -X main.version=$VERSION" -o "$stage/$c$ext" "./cmd/$c" + done + for f in LICENSE README.md; do + [ -f "$f" ] && cp "$f" "$stage/" + done + if [ "$os" = windows ] && command -v zip >/dev/null 2>&1; then + (cd dist/stage && zip -qr "../$pkg.zip" "$pkg") + else + tar -C dist/stage -czf "dist/$pkg.tar.gz" "$pkg" + fi +done +rm -rf dist/stage +(cd dist && sha256sum $(ls | grep -E '\.(tar\.gz|zip)$') > SHA256SUMS) +echo "done: $(ls dist | grep -cE '\.(tar\.gz|zip)$') archives in dist/" diff --git a/deploy/docker-compose.yml b/deploy/docker-compose.yml new file mode 100644 index 0000000..9b9bce9 --- /dev/null +++ b/deploy/docker-compose.yml @@ -0,0 +1,10 @@ +# Basking. Reach it through a reverse proxy or Hammerhead (HTTPS), and set "cookie_secure": true +# and "trust_proxy": true in basking.json when you do. +services: + basking: + image: git.veillax.com/veillax/basking:latest # or: build: .. + restart: unless-stopped + ports: + - "127.0.0.1:8490:8490" + volumes: + - ./config:/config # basking.json and data/ (your notes) live here diff --git a/deploy/install.sh b/deploy/install.sh new file mode 100644 index 0000000..a1f0436 --- /dev/null +++ b/deploy/install.sh @@ -0,0 +1,211 @@ +#!/bin/sh +# Installs (or upgrades) Basking as a systemd service. +# +# curl -fsSL https://git.veillax.com/Veillax/basking/raw/branch/main/deploy/install.sh | sudo sh +# sudo sh install.sh [--start] [--version v0.1.0] [--listen 127.0.0.1:8490] [--source] +# sudo sh install.sh --uninstall [--purge] +# +# What it does: +# 1. downloads the release archive for this machine and checks its SHA-256 (or, with --source, +# clones the repository and builds it with Go), +# 2. installs the binary to /usr/local/bin/basking, +# 3. creates a "basking" user, /etc/basking (settings) and /var/lib/basking (data), +# 4. writes /etc/basking/basking.env and the systemd service, +# 5. tells you what to do next: edit the env file, start the service, watch the log. +# +# Running it again upgrades the binary and keeps your settings; a service that is already running +# is restarted. Add --start to start it straight away instead of editing the env file first. +set -eu + +REPO_URL=${BASKING_REPO_URL:-https://git.veillax.com/Veillax/basking} +API_URL=${BASKING_API_URL:-https://git.veillax.com/api/v1/repos/Veillax/basking} +BIN=/usr/local/bin/basking +CONF_DIR=/etc/basking +DATA_DIR=/var/lib/basking +ENV_FILE=$CONF_DIR/basking.env +CONF_FILE=$CONF_DIR/basking.json +UNIT=/etc/systemd/system/basking.service +USER_NAME=basking + +VERSION="" +LISTEN="127.0.0.1:8490" +SOURCE=0 +START=0 +UNINSTALL=0 +PURGE=0 + +say() { printf '%s\n' "$*"; } +die() { printf 'install.sh: %s\n' "$*" >&2; exit 1; } + +while [ $# -gt 0 ]; do + case $1 in + --version) VERSION=${2:?--version needs a value}; shift ;; + --listen) LISTEN=${2:?--listen needs a value}; shift ;; + --source) SOURCE=1 ;; + --start) START=1 ;; + --uninstall) UNINSTALL=1 ;; + --purge) PURGE=1 ;; + -h|--help) sed -n '2,19p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; + *) die "unknown option $1" ;; + esac + shift +done + +[ "$(id -u)" -eq 0 ] || die "run as root (sudo sh install.sh)" +[ "$(uname -s)" = Linux ] || die "this installer is for Linux; on Windows, download the .zip from the releases page" +command -v systemctl >/dev/null 2>&1 || HAVE_SYSTEMD=0 +HAVE_SYSTEMD=${HAVE_SYSTEMD:-1} + +if [ "$UNINSTALL" -eq 1 ]; then + if [ "$HAVE_SYSTEMD" -eq 1 ]; then + systemctl disable --now basking 2>/dev/null || true + rm -f "$UNIT" + systemctl daemon-reload + fi + rm -f "$BIN" + if [ "$PURGE" -eq 1 ]; then + rm -rf "$CONF_DIR" "$DATA_DIR" + userdel "$USER_NAME" 2>/dev/null || true + say "Basking removed, including its settings and data." + else + say "Basking removed. Settings ($CONF_DIR) and data ($DATA_DIR) were kept; add --purge to delete them." + fi + exit 0 +fi + +fetch() { # fetch URL DEST + if command -v curl >/dev/null 2>&1; then curl -fsSL -o "$2" "$1" + elif command -v wget >/dev/null 2>&1; then wget -qO "$2" "$1" + else die "need curl or wget"; fi +} + +TMP=$(mktemp -d) +trap 'rm -rf "$TMP"' EXIT + +# ---- 1. get the binary ---- +if [ "$SOURCE" -eq 1 ]; then + command -v git >/dev/null 2>&1 || die "--source needs git" + command -v go >/dev/null 2>&1 || die "--source needs Go (https://go.dev/dl/)" + say "Cloning $REPO_URL ..." + git clone --quiet ${VERSION:+--branch "$VERSION"} --depth 1 "$REPO_URL" "$TMP/src" + (cd "$TMP/src" && CGO_ENABLED=0 go build -trimpath -ldflags "-s -w -X main.version=${VERSION:-source}" -o "$TMP/basking" ./cmd/basking) +else + case $(uname -m) in + x86_64|amd64) ARCH=amd64 ;; + aarch64|arm64) ARCH=arm64 ;; + *) die "no release build for $(uname -m); try --source" ;; + esac + if [ -z "$VERSION" ]; then + fetch "$API_URL/releases/latest" "$TMP/latest.json" + VERSION=$(sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$TMP/latest.json" | head -n 1) + [ -n "$VERSION" ] || die "could not find the latest release; use --version or --source" + fi + ARCHIVE="basking_${VERSION}_linux_${ARCH}.tar.gz" + say "Downloading Basking $VERSION ($ARCH) ..." + fetch "$REPO_URL/releases/download/$VERSION/$ARCHIVE" "$TMP/$ARCHIVE" + fetch "$REPO_URL/releases/download/$VERSION/SHA256SUMS" "$TMP/SHA256SUMS" + want=$(grep " $ARCHIVE\$" "$TMP/SHA256SUMS" | cut -d' ' -f1) + [ -n "$want" ] || die "$ARCHIVE is not listed in SHA256SUMS" + got=$(sha256sum "$TMP/$ARCHIVE" | cut -d' ' -f1) + [ "$want" = "$got" ] || die "checksum mismatch for $ARCHIVE; refusing to install" + tar -xzf "$TMP/$ARCHIVE" -C "$TMP" + cp "$TMP/basking_${VERSION}_linux_${ARCH}/basking" "$TMP/basking" +fi + +# ---- 2. install it ---- +UPGRADE=0 +[ -x "$BIN" ] && UPGRADE=1 +if [ "$UPGRADE" -eq 1 ] && [ "$HAVE_SYSTEMD" -eq 1 ]; then systemctl stop basking 2>/dev/null || true; fi +install -m 0755 "$TMP/basking" "$BIN" +say "Installed $("$BIN" version) to $BIN" + +# ---- 3. user and folders ---- +id "$USER_NAME" >/dev/null 2>&1 || useradd --system --home-dir "$DATA_DIR" --shell /usr/sbin/nologin "$USER_NAME" 2>/dev/null \ + || adduser --system --home "$DATA_DIR" --shell /usr/sbin/nologin "$USER_NAME" 2>/dev/null \ + || die "could not create the $USER_NAME user" +# The dashboard rewrites its settings file, so the service must own the folder that holds it. +mkdir -p "$CONF_DIR" "$DATA_DIR" +chown "$USER_NAME" "$CONF_DIR" "$DATA_DIR" +chmod 0750 "$CONF_DIR" "$DATA_DIR" + +# ---- 4. settings and environment file ---- +if [ ! -f "$CONF_FILE" ]; then + printf '{\n "listen": "%s",\n "data_dir": "%s"\n}\n' "$LISTEN" "$DATA_DIR" > "$CONF_FILE" + chown "$USER_NAME" "$CONF_FILE" + chmod 0600 "$CONF_FILE" +fi +if [ ! -f "$ENV_FILE" ]; then + cat > "$ENV_FILE" < "$UNIT" </dev/null 2>&1 + +if [ "$START" -eq 1 ] || systemctl is-active --quiet basking; then + systemctl restart basking + sleep 3 + if systemctl is-active --quiet basking; then + say "Basking is running." + else + say "Basking did not start. Look at: journalctl -u basking -n 50" + exit 1 + fi +fi + +say "Installed $("$BIN" version)." +say "Next:" +say " 1. sudoedit $ENV_FILE (optional: the address to listen on and where notes are kept)" +say " 2. systemctl start basking (or run this again with --start)" +say " 3. journalctl -u basking -f to watch it; a first install prints the password here, once" +say "" +say "The dashboard is at http://$LISTEN" +case $LISTEN in +127.*|localhost*|[::1]*) say "It only listens on this machine. To reach it elsewhere, put an HTTPS reverse proxy (or Hammerhead) in front of it." ;; +esac diff --git a/deploy/publish-release.sh b/deploy/publish-release.sh new file mode 100644 index 0000000..605a0bb --- /dev/null +++ b/deploy/publish-release.sh @@ -0,0 +1,71 @@ +#!/bin/sh +# Creates (or updates) the Gitea release for a tag and uploads what deploy/build.sh made. +# +# TOKEN= deploy/publish-release.sh v0.2.0 +# +# In Gitea Actions GITHUB_SERVER_URL and GITHUB_REPOSITORY are already set. Safe to +# re-run: an existing release is reused and assets with the same name are replaced. +set -eu +TAG=${1:?usage: publish-release.sh } +: "${TOKEN:?set TOKEN to a Gitea access token that can write releases}" +SERVER=${GITHUB_SERVER_URL:-${GITEA_SERVER_URL:-https://git.veillax.com}} +REPO=${GITHUB_REPOSITORY:-Veillax/basking} +API="$SERVER/api/v1/repos/$REPO" +AUTH="Authorization: token $TOKEN" + +# a JSON helper without needing jq: any python will do +PY=python3 +python3 -c 'pass' > /dev/null 2>&1 || PY=python +json() { "$PY" -c "$1"; } + +# release notes: the commit subjects since the previous tag +prev=$(git describe --tags --abbrev=0 "$TAG^" 2>/dev/null || true) +if [ -n "$prev" ]; then + notes=$(git log --pretty='format:- %s' "$prev..$TAG") +else + notes=$(git log --pretty='format:- %s' "$TAG") +fi +prerelease=false +case "$TAG" in *-*) prerelease=true ;; esac + +body=$(NOTES="$notes" TAG="$TAG" PRE="$prerelease" json ' +import json, os +print(json.dumps({"tag_name": os.environ["TAG"], "name": os.environ["TAG"], "body": os.environ["NOTES"], + "draft": False, "prerelease": os.environ["PRE"] == "true"}))') + +# reuse the release when a failed job is re-run +id=$(curl -fsS -H "$AUTH" "$API/releases/tags/$TAG" 2>/dev/null | json 'import sys, json; print(json.load(sys.stdin)["id"])' 2>/dev/null || true) +if [ -z "$id" ]; then + echo "creating release $TAG" + id=$(curl -fsS -X POST -H "$AUTH" -H 'Content-Type: application/json' -d "$body" "$API/releases" | json 'import sys, json; print(json.load(sys.stdin)["id"])') +else + echo "release $TAG already exists (id $id): replacing its files" +fi + +# the ids of the release's assets with a given name (a failed upload can leave a partial one behind) +asset_ids() { + curl -fsS -H "$AUTH" "$API/releases/$id/assets" | "$PY" -c 'import sys, json +for a in json.load(sys.stdin): + if a["name"] == sys.argv[1]: + print(a["id"])' "$1" +} + +for f in dist/*.tar.gz dist/*.zip dist/SHA256SUMS; do + [ -f "$f" ] || continue + name=$(basename "$f") + n=1 + while :; do + for aid in $(asset_ids "$name"); do + curl -fsS -X DELETE -H "$AUTH" "$API/releases/$id/assets/$aid" > /dev/null + done + echo "uploading $name (attempt $n)" + if curl -fsS --max-time 1200 -X POST -H "$AUTH" -F "attachment=@$f" "$API/releases/$id/assets?name=$name" > /dev/null; then + break + fi + # large uploads can hit a proxy timeout (HTTP 502); try again + [ "$n" -lt 5 ] || { echo "giving up on $name after $n attempts" >&2; exit 1; } + n=$((n + 1)) + sleep $((n * 10)) + done +done +echo "published $SERVER/$REPO/releases/tag/$TAG" diff --git a/docs/design.md b/docs/design.md new file mode 100644 index 0000000..da4b9ea --- /dev/null +++ b/docs/design.md @@ -0,0 +1,31 @@ +# Basking design language + +Basking follows the Lightwell design language, the same one Hammerhead and Megalodon use. The reference is `docs/design.md` in the Lightwell repository. When this page and Lightwell disagree, Lightwell wins. + +## Shared with Lightwell + +* **Tokens.** Colors, spacing (4px scale), radius, type sizes, the single shadow and the dark theme are the Lightwell values. The accent is teal, `#1f5c5c`. Raw colors live only between `tokens:begin` and `tokens:end` in `style.css`. +* **Voice.** No emoji, no em or en dashes, no exclamation marks, sentence case, buttons that name the result, and errors that say what happened and what to do next. Server errors are capitalized in one place (`fail` in `internal/web/server.go`). +* **Status carries text.** "Pinned", "Saving", "Saved" and "Not saved" are words, never a colored dot. +* **Structure.** Sidebar and drawer shell, cards, dialogs with a focus trap, `confirmDialog` for anything that cannot be undone, skeletons for loading, empty states that say why a list is empty. + +## Where Basking differs + +* **The writing box is the primary action.** Lightwell puts the one primary button in the page header. On Notes the header has none: the primary button, Save note, sits in the composer, because writing is the page. Elsewhere the rule holds: Write today on the Journal, Done while editing a note. +* **Deleting a note is not confirmed, it is undoable.** Delete moves a note to the trash and offers Undo. Only Delete for good and Empty trash ask first, naming what is lost. +* **Script and styles are separate files**, because the Content-Security-Policy forbids inline script and `style` attributes. Widths come from classes. The one use of `element.style` is sizing a text box to its text, which the policy allows. +* **Rendered notes** use a small safe markdown renderer in `app.js`. It builds elements, never HTML, and only `http(s)` links are links. + +## Icons + +Lucide style, outline only, stroke 1.75, round caps, `currentColor`, sizes 16, 18 and 22. An icon goes with a text label, and icon-only buttons carry an `aria-label`. + +Approved names: `fin`, `notes`, `today`, `journal`, `pin`, `tag`, `trash`, `edit`, `restore`, `clock`, `view`, `download`, `copy`, `prev`, `next`, `settings`, `out`, `menu`, `close`, `add`, `check`, `light`, `dark`, `alert`. Add a name here and in `ICONS` together. + +## Checking a change + +``` +node scripts/lint-design.mjs +``` + +It fails on em and en dashes, emoji, and raw colors outside the token block. Then check the page in light and dark, at phone and desktop width, using only the keyboard. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1e90df4 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.veillax.com/Veillax/basking + +go 1.27.3 diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..c1f172a --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,212 @@ +// Package config holds Basking's settings file: where to listen, where notes live, the password +// hash and the API tokens. The notes themselves are not in here. +package config + +import ( + "crypto/pbkdf2" + "crypto/rand" + "crypto/sha256" + "crypto/subtle" + "encoding/base64" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "regexp" + "strconv" + "strings" + "sync" + "time" +) + +type Token struct { + Name string `json:"name"` + Hash string `json:"hash"` + Created time.Time `json:"created"` +} + +type Config struct { + Listen string `json:"listen"` + DataDir string `json:"data_dir"` + + // PasswordHash is the one password that opens the dashboard. + PasswordHash string `json:"password_hash,omitempty"` + // Secret signs session cookies, so a restart does not sign you out. It is made on first run. + Secret string `json:"secret,omitempty"` + Tokens []Token `json:"tokens,omitempty"` + + // CookieSecure and TrustProxy are for running behind an HTTPS reverse proxy. + CookieSecure bool `json:"cookie_secure,omitempty"` + TrustProxy bool `json:"trust_proxy,omitempty"` +} + +var tokenName = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9 ._-]{0,39}$`) + +// Defaults fills in what is missing. +func (c *Config) Defaults() { + if c.Listen == "" { + c.Listen = "127.0.0.1:8490" + } + if c.DataDir == "" { + c.DataDir = "data" + } + if c.Secret == "" { + b := make([]byte, 32) + rand.Read(b) + c.Secret = base64.RawURLEncoding.EncodeToString(b) + } +} + +func (c *Config) Validate() error { + seen := map[string]bool{} + for _, t := range c.Tokens { + if !tokenName.MatchString(t.Name) { + return fmt.Errorf("token name %q must be 1 to 40 letters, numbers, spaces, dots, dashes or underscores", t.Name) + } + if seen[strings.ToLower(t.Name)] { + return fmt.Errorf("there is already a token called %q", t.Name) + } + seen[strings.ToLower(t.Name)] = true + } + return nil +} + +// Holder keeps the live configuration and writes every change to disk. +type Holder struct { + path string + mu sync.RWMutex + cfg Config +} + +// Load reads the file. A missing file gives the defaults (and is created by the first Update). +func Load(path string) (*Holder, error) { + h := &Holder{path: path} + b, err := os.ReadFile(path) + switch { + case errors.Is(err, os.ErrNotExist): + case err != nil: + return nil, err + default: + if err := json.Unmarshal(b, &h.cfg); err != nil { + return nil, fmt.Errorf("%s: %w", path, err) + } + } + h.cfg.Defaults() + if err := h.cfg.Validate(); err != nil { + return nil, fmt.Errorf("%s: %w", path, err) + } + return h, nil +} + +func (h *Holder) Path() string { return h.path } + +// Exists says whether the file is on disk yet. +func (h *Holder) Exists() bool { _, err := os.Stat(h.path); return err == nil } + +// Get returns a private copy; change it freely. +func (h *Holder) Get() Config { + h.mu.RLock() + defer h.mu.RUnlock() + return clone(h.cfg) +} + +func clone(c Config) Config { + b, _ := json.Marshal(c) + var out Config + json.Unmarshal(b, &out) + return out +} + +// Update applies fn to a copy, validates it, saves it and only then makes it current. +func (h *Holder) Update(fn func(*Config) error) error { + h.mu.Lock() + defer h.mu.Unlock() + c := clone(h.cfg) + if err := fn(&c); err != nil { + return err + } + c.Defaults() + if err := c.Validate(); err != nil { + return err + } + if err := save(h.path, c); err != nil { + return err + } + h.cfg = c + return nil +} + +func save(path string, c Config) error { + b, err := json.MarshalIndent(c, "", " ") + if err != nil { + return err + } + if dir := filepath.Dir(path); dir != "." { + os.MkdirAll(dir, 0o755) + } + tmp := path + ".tmp" + if err := os.WriteFile(tmp, append(b, '\n'), 0o600); err != nil { + return err + } + return os.Rename(tmp, path) +} + +// ---- passwords and tokens ---- + +const pbkdf2Rounds = 600_000 + +// HashPassword returns "pbkdf2$rounds$salt$key". +func HashPassword(password string) (string, error) { + salt := make([]byte, 16) + if _, err := rand.Read(salt); err != nil { + return "", err + } + key, err := pbkdf2.Key(sha256.New, password, salt, pbkdf2Rounds, 32) + if err != nil { + return "", err + } + return fmt.Sprintf("pbkdf2$%d$%s$%s", pbkdf2Rounds, base64.RawStdEncoding.EncodeToString(salt), base64.RawStdEncoding.EncodeToString(key)), nil +} + +// CheckPassword compares in constant time. An unparseable hash never matches. +func CheckPassword(hash, password string) bool { + parts := strings.Split(hash, "$") + if len(parts) != 4 || parts[0] != "pbkdf2" { + return false + } + rounds, err := strconv.Atoi(parts[1]) + if err != nil || rounds < 1 || rounds > 10_000_000 { + return false + } + salt, err1 := base64.RawStdEncoding.DecodeString(parts[2]) + want, err2 := base64.RawStdEncoding.DecodeString(parts[3]) + if err1 != nil || err2 != nil { + return false + } + got, err := pbkdf2.Key(sha256.New, password, salt, rounds, len(want)) + return err == nil && subtle.ConstantTimeCompare(got, want) == 1 +} + +// NewToken makes an API token and the hash to store for it. The token itself is shown once. +func NewToken() (token, hash string, err error) { + b := make([]byte, 32) + if _, err = rand.Read(b); err != nil { + return + } + token = "bsk_" + base64.RawURLEncoding.EncodeToString(b) + return token, HashToken(token), nil +} + +func HashToken(token string) string { + sum := sha256.Sum256([]byte(token)) + return hex.EncodeToString(sum[:]) +} + +// RandomPassword makes a password for a first run. +func RandomPassword() string { + b := make([]byte, 12) + rand.Read(b) + return base64.RawURLEncoding.EncodeToString(b) +} diff --git a/internal/store/store.go b/internal/store/store.go new file mode 100644 index 0000000..8ad3cee --- /dev/null +++ b/internal/store/store.go @@ -0,0 +1,543 @@ +// Package store keeps notes as plain markdown files, one per note, so they stay readable and +// portable with or without Basking. An in-memory index makes listing and searching quick. +package store + +import ( + "archive/zip" + "crypto/rand" + "encoding/hex" + "errors" + "fmt" + "io" + "os" + "path/filepath" + "regexp" + "sort" + "strings" + "sync" + "time" +) + +// MaxBody is the longest note, in bytes. +const MaxBody = 256 << 10 + +var ( + ErrNotFound = errors.New("note not found") + ErrBadID = errors.New("bad note id") + ErrEmpty = errors.New("a note needs some text") + ErrTooLong = errors.New("that note is too long") + ErrConflict = errors.New("this note changed somewhere else") + ErrBadDate = errors.New("the date must look like 2026-10-31") + ErrNotTrash = errors.New("only a note in the trash can be deleted for good") +) + +type Note struct { + ID string `json:"id"` + Created time.Time `json:"created"` + Updated time.Time `json:"updated"` + Pinned bool `json:"pinned"` + Daily string `json:"daily,omitempty"` // the date, for a daily note + Trashed bool `json:"trashed,omitempty"` + Body string `json:"body"` + Tags []string `json:"tags"` + + lower string // lower-cased body, for searching +} + +type Store struct { + dir string + mu sync.RWMutex + by map[string]*Note +} + +var idRe = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,63}$`) + +// ValidID says whether s can be used as a note id (and so as a file name). +func ValidID(s string) bool { return idRe.MatchString(s) } + +// Open reads every note under dir, creating the folders if needed. +func Open(dir string) (*Store, error) { + s := &Store{dir: dir, by: map[string]*Note{}} + for _, sub := range []string{"notes", "trash"} { + if err := os.MkdirAll(filepath.Join(dir, sub), 0o755); err != nil { + return nil, err + } + entries, err := os.ReadDir(filepath.Join(dir, sub)) + if err != nil { + return nil, err + } + for _, e := range entries { + id := strings.TrimSuffix(e.Name(), ".md") + if e.IsDir() || id == e.Name() || !ValidID(id) { + continue + } + path := filepath.Join(dir, sub, e.Name()) + b, err := os.ReadFile(path) + if err != nil { + return nil, err + } + info, err := e.Info() + if err != nil { + return nil, err + } + n := parse(id, string(b), info.ModTime()) + n.Trashed = sub == "trash" + s.by[id] = n + } + } + return s, nil +} + +func (s *Store) path(n *Note) string { + sub := "notes" + if n.Trashed { + sub = "trash" + } + return filepath.Join(s.dir, sub, n.ID+".md") +} + +// ---- file format ---- +// +// --- +// created: 2026-10-31T14:32:05.123Z +// updated: 2026-10-31T14:35:00.456Z +// pinned: true +// daily: 2026-10-31 +// --- +// +// the note, in markdown + +func format(n *Note) []byte { + var b strings.Builder + b.WriteString("---\n") + fmt.Fprintf(&b, "created: %s\n", n.Created.UTC().Format(time.RFC3339Nano)) + fmt.Fprintf(&b, "updated: %s\n", n.Updated.UTC().Format(time.RFC3339Nano)) + if n.Pinned { + b.WriteString("pinned: true\n") + } + if n.Daily != "" { + fmt.Fprintf(&b, "daily: %s\n", n.Daily) + } + b.WriteString("---\n\n") + b.WriteString(n.Body) + b.WriteString("\n") + return []byte(b.String()) +} + +// parse reads a note file. A file with no header is still a note: it takes its time from the file. +func parse(id, text string, mod time.Time) *Note { + text = strings.ReplaceAll(text, "\r\n", "\n") + n := &Note{ID: id, Created: mod.UTC(), Updated: mod.UTC()} + if rest, ok := strings.CutPrefix(text, "---\n"); ok { + if head, body, ok := strings.Cut(rest, "\n---\n"); ok || strings.HasSuffix(rest, "\n---") { + if !ok { + head, body = strings.TrimSuffix(rest, "\n---"), "" + } + for _, line := range strings.Split(head, "\n") { + k, v, _ := strings.Cut(line, ":") + v = strings.TrimSpace(v) + switch strings.TrimSpace(k) { + case "created": + if t, err := time.Parse(time.RFC3339Nano, v); err == nil { + n.Created = t.UTC() + } + case "updated": + if t, err := time.Parse(time.RFC3339Nano, v); err == nil { + n.Updated = t.UTC() + } + case "pinned": + n.Pinned = v == "true" + case "daily": + if _, err := time.Parse("2006-01-02", v); err == nil { + n.Daily = v + } + } + } + text = strings.TrimPrefix(body, "\n") + } + } + n.set(text) + return n +} + +// set stores the body (without trailing blank space) and works out the tags. +func (n *Note) set(body string) { + n.Body = strings.TrimRight(strings.ReplaceAll(body, "\r\n", "\n"), " \t\n") + n.lower = strings.ToLower(n.Body) + n.Tags = Tags(n.Body) +} + +var ( + tagRe = regexp.MustCompile(`(?:^|[\s(])#([\p{L}\p{N}_][\p{L}\p{N}_/-]*)`) + inlineCodeRe = regexp.MustCompile("`[^`\n]*`") +) + +// Tags returns the #tags in a note, lower-cased and without repeats. Code is skipped, and so are +// numbers such as #12. +func Tags(body string) []string { + var lines []string + fence := false + for _, l := range strings.Split(body, "\n") { + if strings.HasPrefix(strings.TrimSpace(l), "```") { + fence = !fence + continue + } + if !fence { + lines = append(lines, inlineCodeRe.ReplaceAllString(l, "")) + } + } + seen := map[string]bool{} + out := []string{} + for _, m := range tagRe.FindAllStringSubmatch(strings.Join(lines, "\n"), -1) { + t := strings.ToLower(strings.TrimRight(m[1], "/-")) + if t == "" || len(t) > 40 || isNumber(t) || seen[t] { + continue + } + seen[t] = true + out = append(out, t) + } + return out +} + +func isNumber(s string) bool { + for _, r := range s { + if r < '0' || r > '9' { + return false + } + } + return true +} + +func (s *Store) write(n *Note) error { + path := s.path(n) + tmp := filepath.Join(filepath.Dir(path), "."+n.ID+".tmp") + if err := os.WriteFile(tmp, format(n), 0o644); err != nil { + return err + } + return os.Rename(tmp, path) +} + +func newID(t time.Time) string { + b := make([]byte, 3) + rand.Read(b) + return t.UTC().Format("20060102-150405") + "-" + hex.EncodeToString(b) +} + +func (n *Note) copy() Note { + c := *n + c.Tags = append([]string{}, n.Tags...) + return c +} + +func checkBody(body string) error { + if len(body) > MaxBody { + return ErrTooLong + } + return nil +} + +// ---- changes ---- + +// Create saves a new note. +func (s *Store) Create(body string) (Note, error) { + if err := checkBody(body); err != nil { + return Note{}, err + } + if strings.TrimSpace(body) == "" { + return Note{}, ErrEmpty + } + now := time.Now().UTC() + n := &Note{ID: newID(now), Created: now, Updated: now} + n.set(body) + s.mu.Lock() + defer s.mu.Unlock() + if err := s.write(n); err != nil { + return Note{}, err + } + s.by[n.ID] = n + return n.copy(), nil +} + +// Daily returns the note for a date (like 2026-10-31), making it if it is not there yet. +func (s *Store) Daily(date string) (Note, error) { + if _, err := time.Parse("2006-01-02", date); err != nil { + return Note{}, ErrBadDate + } + id := "daily-" + date + s.mu.Lock() + defer s.mu.Unlock() + if n, ok := s.by[id]; ok { + if n.Trashed { // asking for the day brings it back + old := s.path(n) + n.Trashed = false + if err := os.Rename(old, s.path(n)); err != nil { + n.Trashed = true + return Note{}, err + } + } + return n.copy(), nil + } + now := time.Now().UTC() + n := &Note{ID: id, Created: now, Updated: now, Daily: date} + if err := s.write(n); err != nil { + return Note{}, err + } + s.by[id] = n + return n.copy(), nil +} + +func (s *Store) Get(id string) (Note, error) { + s.mu.RLock() + defer s.mu.RUnlock() + n, ok := s.by[id] + if !ok { + return Note{}, ErrNotFound + } + return n.copy(), nil +} + +// Update changes the text and/or the pin. When base is given, the text is only replaced if the +// note has not been changed since then; otherwise the current note comes back with ErrConflict. +func (s *Store) Update(id string, body *string, pinned *bool, base time.Time) (Note, error) { + s.mu.Lock() + defer s.mu.Unlock() + n, ok := s.by[id] + if !ok { + return Note{}, ErrNotFound + } + if body != nil { + if err := checkBody(*body); err != nil { + return Note{}, err + } + if !base.IsZero() && !n.Updated.Equal(base) { + return n.copy(), ErrConflict + } + } + next := n.copy() + if body != nil { + next.set(*body) + next.Updated = time.Now().UTC() + if !next.Updated.After(n.Updated) { // keep updated moving forward, so a stale tab is always noticed + next.Updated = n.Updated.Add(time.Microsecond) + } + } + if pinned != nil { + next.Pinned = *pinned + } + if err := s.write(&next); err != nil { + return Note{}, err + } + *n = next + return n.copy(), nil +} + +func (s *Store) move(id string, toTrash bool) (Note, error) { + s.mu.Lock() + defer s.mu.Unlock() + n, ok := s.by[id] + if !ok { + return Note{}, ErrNotFound + } + if n.Trashed == toTrash { + return n.copy(), nil + } + old := s.path(n) + n.Trashed = toTrash + if err := os.Rename(old, s.path(n)); err != nil { + n.Trashed = !toTrash + return Note{}, err + } + return n.copy(), nil +} + +// Trash moves a note to the trash. Restore brings it back. +func (s *Store) Trash(id string) (Note, error) { return s.move(id, true) } +func (s *Store) Restore(id string) (Note, error) { return s.move(id, false) } + +// Purge deletes a note that is already in the trash, for good. +func (s *Store) Purge(id string) error { + s.mu.Lock() + defer s.mu.Unlock() + n, ok := s.by[id] + if !ok { + return ErrNotFound + } + if !n.Trashed { + return ErrNotTrash + } + if err := os.Remove(s.path(n)); err != nil && !errors.Is(err, os.ErrNotExist) { + return err + } + delete(s.by, id) + return nil +} + +// EmptyTrash deletes everything in the trash for good and says how many notes that was. +func (s *Store) EmptyTrash() (int, error) { + s.mu.Lock() + defer s.mu.Unlock() + count := 0 + for id, n := range s.by { + if !n.Trashed { + continue + } + if err := os.Remove(s.path(n)); err != nil && !errors.Is(err, os.ErrNotExist) { + return count, err + } + delete(s.by, id) + count++ + } + return count, nil +} + +// ---- reading ---- + +type Query struct { + Text string // words that must all appear; #tag words must be tags + Tag string + View string // "" or "all", "pinned", "daily", "trash" + Offset int + Limit int +} + +// List returns one page of matching notes, newest first, and how many matched in all. +func (s *Store) List(q Query) ([]Note, int) { + var words, tags []string + for _, w := range strings.Fields(strings.ToLower(q.Text)) { + if t := strings.TrimPrefix(w, "#"); t != w && t != "" { + tags = append(tags, t) + } else { + words = append(words, w) + } + } + if t := strings.ToLower(strings.TrimPrefix(q.Tag, "#")); t != "" { + tags = append(tags, t) + } + + s.mu.RLock() + var hits []*Note + for _, n := range s.by { + if n.Trashed != (q.View == "trash") || (q.View == "pinned" && !n.Pinned) || (q.View == "daily" && n.Daily == "") { + continue + } + if matches(n, words, tags) { + hits = append(hits, n) + } + } + sort.Slice(hits, func(i, j int) bool { + a, b := hits[i], hits[j] + if q.View == "daily" && a.Daily != b.Daily { + return a.Daily > b.Daily + } + if !a.Created.Equal(b.Created) { + return a.Created.After(b.Created) + } + return a.ID > b.ID + }) + total := len(hits) + if q.Offset > 0 { + hits = hits[min(q.Offset, len(hits)):] + } + if q.Limit > 0 && len(hits) > q.Limit { + hits = hits[:q.Limit] + } + out := make([]Note, len(hits)) + for i, n := range hits { + out[i] = n.copy() + } + s.mu.RUnlock() + return out, total +} + +func matches(n *Note, words, tags []string) bool { + for _, w := range words { + if !strings.Contains(n.lower, w) { + return false + } + } + for _, want := range tags { + found := false + for _, have := range n.Tags { + if have == want || strings.HasPrefix(have, want+"/") { + found = true + break + } + } + if !found { + return false + } + } + return true +} + +type TagCount struct { + Tag string `json:"tag"` + Count int `json:"count"` +} + +type Counts struct { + All int `json:"all"` + Pinned int `json:"pinned"` + Daily int `json:"daily"` + Trash int `json:"trash"` +} + +// Summary counts the notes and the tags in use (trashed notes do not count towards tags). +func (s *Store) Summary() (Counts, []TagCount) { + s.mu.RLock() + defer s.mu.RUnlock() + var c Counts + per := map[string]int{} + for _, n := range s.by { + if n.Trashed { + c.Trash++ + continue + } + c.All++ + if n.Pinned { + c.Pinned++ + } + if n.Daily != "" { + c.Daily++ + } + for _, t := range n.Tags { + per[t]++ + } + } + tags := make([]TagCount, 0, len(per)) + for t, k := range per { + tags = append(tags, TagCount{t, k}) + } + sort.Slice(tags, func(i, j int) bool { + if tags[i].Count != tags[j].Count { + return tags[i].Count > tags[j].Count + } + return tags[i].Tag < tags[j].Tag + }) + return c, tags +} + +// WriteZip writes every note that is not in the trash as a .md file in a zip archive. +func (s *Store) WriteZip(w io.Writer) error { + s.mu.RLock() + defer s.mu.RUnlock() + ids := make([]string, 0, len(s.by)) + for id, n := range s.by { + if !n.Trashed { + ids = append(ids, id) + } + } + sort.Strings(ids) + zw := zip.NewWriter(w) + for _, id := range ids { + n := s.by[id] + f, err := zw.CreateHeader(&zip.FileHeader{Name: "basking/" + id + ".md", Method: zip.Deflate, Modified: n.Updated}) + if err != nil { + return err + } + if _, err := f.Write(format(n)); err != nil { + return err + } + } + return zw.Close() +} diff --git a/internal/store/store_test.go b/internal/store/store_test.go new file mode 100644 index 0000000..0e77d5c --- /dev/null +++ b/internal/store/store_test.go @@ -0,0 +1,273 @@ +package store + +import ( + "archive/zip" + "bytes" + "errors" + "os" + "path/filepath" + "reflect" + "strings" + "testing" + "time" +) + +func open(t *testing.T) (*Store, string) { + t.Helper() + dir := t.TempDir() + s, err := Open(dir) + if err != nil { + t.Fatal(err) + } + return s, dir +} + +func TestTags(t *testing.T) { + cases := []struct { + in string + want []string + }{ + {"buy milk #errands #Errands and #home/garden", []string{"errands", "home/garden"}}, + {"issue #12 is not a tag, nor is a#b", []string{}}, + {"# Heading is not a tag\n#idea at the start", []string{"idea"}}, + {"`#code` and\n```\n#fenced\n```\n#real", []string{"real"}}, + {"(#paren) and #trailing-", []string{"paren", "trailing"}}, + {"#ünï and #日本語", []string{"ünï", "日本語"}}, + } + for _, c := range cases { + if got := Tags(c.in); !reflect.DeepEqual(got, c.want) { + t.Errorf("Tags(%q) = %v, want %v", c.in, got, c.want) + } + } +} + +func TestCreateRoundTripsThroughDisk(t *testing.T) { + s, dir := open(t) + n, err := s.Create("first thought #idea\n\nwith a second paragraph \n\n") + if err != nil { + t.Fatal(err) + } + if n.Body != "first thought #idea\n\nwith a second paragraph" { + t.Fatalf("body %q", n.Body) + } + if !ValidID(n.ID) { + t.Fatalf("id %q", n.ID) + } + if _, err := os.Stat(filepath.Join(dir, "notes", n.ID+".md")); err != nil { + t.Fatal(err) + } + + s2, err := Open(dir) + if err != nil { + t.Fatal(err) + } + got, err := s2.Get(n.ID) + if err != nil { + t.Fatal(err) + } + if got.Body != n.Body || !got.Created.Equal(n.Created) || !got.Updated.Equal(n.Updated) || !reflect.DeepEqual(got.Tags, []string{"idea"}) { + t.Fatalf("reloaded %+v, want %+v", got, n) + } +} + +func TestCreateRejectsEmptyAndHuge(t *testing.T) { + s, _ := open(t) + if _, err := s.Create(" \n\t"); !errors.Is(err, ErrEmpty) { + t.Fatalf("empty: %v", err) + } + if _, err := s.Create(strings.Repeat("x", MaxBody+1)); !errors.Is(err, ErrTooLong) { + t.Fatalf("huge: %v", err) + } +} + +func TestPlainMarkdownFileIsANote(t *testing.T) { + s, dir := open(t) + path := filepath.Join(dir, "notes", "from-elsewhere.md") + os.WriteFile(path, []byte("# Dropped in\r\nby hand #manual\r\n"), 0o644) + mod := time.Date(2025, 3, 4, 5, 6, 7, 0, time.UTC) + os.Chtimes(path, mod, mod) + s2, err := Open(dir) + if err != nil { + t.Fatal(err) + } + n, err := s2.Get("from-elsewhere") + if err != nil { + t.Fatal(err) + } + if n.Body != "# Dropped in\nby hand #manual" || !n.Created.Equal(mod) || !reflect.DeepEqual(n.Tags, []string{"manual"}) { + t.Fatalf("%+v", n) + } + _ = s +} + +func TestUpdateConflictAndPin(t *testing.T) { + s, _ := open(t) + n, _ := s.Create("one") + body := "two" + n2, err := s.Update(n.ID, &body, nil, n.Updated) + if err != nil || n2.Body != "two" || !n2.Updated.After(n.Updated) { + t.Fatalf("update: %v %+v", err, n2) + } + stale := "three" + cur, err := s.Update(n.ID, &stale, nil, n.Updated) + if !errors.Is(err, ErrConflict) || cur.Body != "two" { + t.Fatalf("stale write: %v %+v", err, cur) + } + yes := true + n3, err := s.Update(n.ID, nil, &yes, time.Time{}) + if err != nil || !n3.Pinned || !n3.Updated.Equal(n2.Updated) { + t.Fatalf("pin should not move updated: %v %+v", err, n3) + } + // a write without a base always goes through (scripts appending) + if _, err := s.Update(n.ID, &stale, nil, time.Time{}); err != nil { + t.Fatal(err) + } + if _, err := s.Update("nope", &stale, nil, time.Time{}); !errors.Is(err, ErrNotFound) { + t.Fatalf("missing: %v", err) + } +} + +func TestTrashRestorePurge(t *testing.T) { + s, dir := open(t) + n, _ := s.Create("to be thrown away") + if err := s.Purge(n.ID); !errors.Is(err, ErrNotTrash) { + t.Fatalf("purge of a live note: %v", err) + } + if _, err := s.Trash(n.ID); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(dir, "trash", n.ID+".md")); err != nil { + t.Fatal("not moved to trash:", err) + } + if list, _ := s.List(Query{}); len(list) != 0 { + t.Fatal("trashed note still listed") + } + if list, total := s.List(Query{View: "trash"}); len(list) != 1 || total != 1 { + t.Fatal("trash view empty") + } + // survives a restart in the trash + s2, _ := Open(dir) + if list, _ := s2.List(Query{View: "trash"}); len(list) != 1 { + t.Fatal("trash lost on reload") + } + if _, err := s2.Restore(n.ID); err != nil { + t.Fatal(err) + } + if list, _ := s2.List(Query{}); len(list) != 1 { + t.Fatal("restore failed") + } + s2.Trash(n.ID) + if err := s2.Purge(n.ID); err != nil { + t.Fatal(err) + } + if _, err := s2.Get(n.ID); !errors.Is(err, ErrNotFound) { + t.Fatal("purged note is still there") + } + a, _ := s2.Create("a") + b, _ := s2.Create("b") + s2.Trash(a.ID) + s2.Trash(b.ID) + if k, err := s2.EmptyTrash(); err != nil || k != 2 { + t.Fatalf("empty trash: %d %v", k, err) + } +} + +func TestDaily(t *testing.T) { + s, _ := open(t) + d1, err := s.Daily("2026-10-31") + if err != nil || d1.ID != "daily-2026-10-31" || d1.Daily != "2026-10-31" { + t.Fatalf("%v %+v", err, d1) + } + d2, _ := s.Daily("2026-10-31") + if d1.ID != d2.ID || !d1.Created.Equal(d2.Created) { + t.Fatal("asking twice should give the same note") + } + for _, bad := range []string{"", "2026-13-01", "../etc/passwd", "today"} { + if _, err := s.Daily(bad); !errors.Is(err, ErrBadDate) { + t.Errorf("%q: %v", bad, err) + } + } + s.Trash(d1.ID) + d3, _ := s.Daily("2026-10-31") + if d3.Trashed { + t.Fatal("asking for a trashed day should bring it back") + } + s.Daily("2026-10-30") + list, _ := s.List(Query{View: "daily"}) + if len(list) != 2 || list[0].Daily != "2026-10-31" { + t.Fatalf("daily view order: %+v", list) + } +} + +func TestSearchAndTags(t *testing.T) { + s, _ := open(t) + s.Create("Buy oat MILK #errands") + time.Sleep(2 * time.Millisecond) + s.Create("Call the dentist #errands #health") + time.Sleep(2 * time.Millisecond) + s.Create("Idea: a #projects/basking dashboard") + + if list, _ := s.List(Query{Text: "milk"}); len(list) != 1 { + t.Fatalf("case-insensitive search: %d", len(list)) + } + if list, _ := s.List(Query{Text: "the dentist"}); len(list) != 1 { + t.Fatalf("all words must match: %d", len(list)) + } + if list, _ := s.List(Query{Text: "milk dentist"}); len(list) != 0 { + t.Fatalf("words in different notes: %d", len(list)) + } + if list, _ := s.List(Query{Tag: "errands"}); len(list) != 2 { + t.Fatalf("tag filter: %d", len(list)) + } + if list, _ := s.List(Query{Text: "#errands #health"}); len(list) != 1 { + t.Fatalf("two tags in the query: %d", len(list)) + } + if list, _ := s.List(Query{Tag: "projects"}); len(list) != 1 { + t.Fatalf("a parent tag finds its children: %d", len(list)) + } + all, total := s.List(Query{Limit: 2}) + if len(all) != 2 || total != 3 || !strings.HasPrefix(all[0].Body, "Idea") { + t.Fatalf("newest first with a page size: %d of %d, first %q", len(all), total, all[0].Body) + } + if page2, _ := s.List(Query{Limit: 2, Offset: 2}); len(page2) != 1 { + t.Fatalf("second page: %d", len(page2)) + } + + counts, tags := s.Summary() + if counts.All != 3 || tags[0].Tag != "errands" || tags[0].Count != 2 { + t.Fatalf("summary %+v %+v", counts, tags) + } +} + +func TestIDsCannotEscape(t *testing.T) { + for _, id := range []string{"", "..", "../x", "a/b", `a\b`, "A", ".hidden", strings.Repeat("a", 65)} { + if ValidID(id) { + t.Errorf("%q should not be a valid id", id) + } + } + s, dir := open(t) + os.WriteFile(filepath.Join(dir, "notes", "Bad Name.md"), []byte("ignored"), 0o644) + s2, _ := Open(dir) + if list, _ := s2.List(Query{}); len(list) != 0 { + t.Fatal("a file whose name is not a valid id should be ignored") + } + _ = s +} + +func TestZip(t *testing.T) { + s, _ := open(t) + a, _ := s.Create("keep me") + b, _ := s.Create("bin me") + s.Trash(b.ID) + var buf bytes.Buffer + if err := s.WriteZip(&buf); err != nil { + t.Fatal(err) + } + zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len())) + if err != nil { + t.Fatal(err) + } + if len(zr.File) != 1 || zr.File[0].Name != "basking/"+a.ID+".md" { + t.Fatalf("zip has %d files", len(zr.File)) + } +} diff --git a/internal/web/api.go b/internal/web/api.go new file mode 100644 index 0000000..7364e54 --- /dev/null +++ b/internal/web/api.go @@ -0,0 +1,254 @@ +package web + +import ( + "errors" + "fmt" + "io" + "mime" + "net/http" + "strconv" + "strings" + "time" + + "git.veillax.com/Veillax/basking/internal/config" + "git.veillax.com/Veillax/basking/internal/store" +) + +func (s *Server) me(w http.ResponseWriter, r *http.Request, p principal) { + writeJSON(w, map[string]any{"version": s.version, "api": p.API}) +} + +func (s *Server) summary(w http.ResponseWriter, r *http.Request, p principal) { + counts, tags := s.st.Summary() + writeJSON(w, map[string]any{"counts": counts, "tags": tags}) +} + +func (s *Server) listNotes(w http.ResponseWriter, r *http.Request, p principal) { + q := r.URL.Query() + limit, _ := strconv.Atoi(q.Get("limit")) + if limit <= 0 || limit > 200 { + limit = 50 + } + offset, _ := strconv.Atoi(q.Get("offset")) + text := q.Get("q") + if len(text) > 200 { + text = text[:200] + } + notes, total := s.st.List(store.Query{Text: text, Tag: q.Get("tag"), View: q.Get("view"), Offset: offset, Limit: limit}) + writeJSON(w, map[string]any{"notes": notes, "total": total}) +} + +// createNote takes {"body": "..."} as JSON, or the note itself as plain text, so a script can +// just post what it has: curl -d 'buy milk' -H 'Content-Type: text/plain' ... +func (s *Server) createNote(w http.ResponseWriter, r *http.Request, p principal) { + var body string + if mt, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); mt == "application/json" { + var in struct{ Body, Text string } + if !decode(w, r, &in) { + return + } + body = in.Body + in.Text + } else { + b, err := io.ReadAll(r.Body) + if err != nil { + fail(w, http.StatusRequestEntityTooLarge, "request too large") + return + } + body = string(b) + } + n, err := s.st.Create(body) + if err != nil { + storeFail(w, err) + return + } + writeJSONStatus(w, http.StatusCreated, n) +} + +func (s *Server) getNote(w http.ResponseWriter, r *http.Request, p principal) { + n, err := s.st.Get(r.PathValue("id")) + if err != nil { + storeFail(w, err) + return + } + writeJSON(w, n) +} + +func (s *Server) updateNote(w http.ResponseWriter, r *http.Request, p principal) { + var in struct { + Body *string `json:"body"` + Pinned *bool `json:"pinned"` + Base string `json:"base"` + } + if !decode(w, r, &in) { + return + } + var base time.Time + if in.Base != "" { + t, err := time.Parse(time.RFC3339Nano, in.Base) + if err != nil { + fail(w, http.StatusBadRequest, "base must be the note's updated time") + return + } + base = t + } + n, err := s.st.Update(r.PathValue("id"), in.Body, in.Pinned, base) + if errors.Is(err, store.ErrConflict) { + writeJSONStatus(w, http.StatusConflict, map[string]any{"error": "This note changed somewhere else", "note": n}) + return + } + if err != nil { + storeFail(w, err) + return + } + writeJSON(w, n) +} + +func (s *Server) deleteNote(w http.ResponseWriter, r *http.Request, p principal) { + id := r.PathValue("id") + var err error + if r.URL.Query().Get("purge") == "1" { + err = s.st.Purge(id) + } else { + _, err = s.st.Trash(id) + } + if err != nil { + storeFail(w, err) + return + } + writeJSON(w, map[string]bool{"ok": true}) +} + +func (s *Server) restoreNote(w http.ResponseWriter, r *http.Request, p principal) { + n, err := s.st.Restore(r.PathValue("id")) + if err != nil { + storeFail(w, err) + return + } + writeJSON(w, n) +} + +func (s *Server) emptyTrash(w http.ResponseWriter, r *http.Request, p principal) { + n, err := s.st.EmptyTrash() + if err != nil { + storeFail(w, err) + return + } + writeJSON(w, map[string]int{"deleted": n}) +} + +func (s *Server) daily(w http.ResponseWriter, r *http.Request, p principal) { + var in struct{ Date string } + if !decode(w, r, &in) { + return + } + n, err := s.st.Daily(in.Date) + if err != nil { + storeFail(w, err) + return + } + writeJSON(w, n) +} + +func (s *Server) export(w http.ResponseWriter, r *http.Request, p principal) { + w.Header().Set("Content-Type", "application/zip") + w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="basking-%s.zip"`, time.Now().Format("2006-01-02"))) + if err := s.st.WriteZip(w); err != nil { + // The download has started, so all that can be done is to cut it short. + panic(http.ErrAbortHandler) + } +} + +// ---- password and tokens ---- + +func (s *Server) changePassword(w http.ResponseWriter, r *http.Request, p principal) { + var in struct{ Current, New string } + if !decode(w, r, &in) { + return + } + if !config.CheckPassword(s.cfg.Get().PasswordHash, in.Current) { + fail(w, http.StatusForbidden, "the current password is wrong") + return + } + if len(in.New) < 8 { + fail(w, http.StatusBadRequest, "the new password must be at least 8 characters") + return + } + hash, err := config.HashPassword(in.New) + if err != nil { + fail(w, http.StatusInternalServerError, err.Error()) + return + } + if err := s.cfg.Update(func(c *config.Config) error { c.PasswordHash = hash; return nil }); err != nil { + fail(w, http.StatusInternalServerError, err.Error()) + return + } + s.startSession(w, r) // the old cookie no longer matches, so give this browser a new one + writeJSON(w, map[string]bool{"ok": true}) +} + +func (s *Server) listTokens(w http.ResponseWriter, r *http.Request, p principal) { + type row struct { + Name string `json:"name"` + Created time.Time `json:"created"` + } + out := []row{} + for _, t := range s.cfg.Get().Tokens { + out = append(out, row{t.Name, t.Created}) + } + writeJSON(w, out) +} + +func (s *Server) newToken(w http.ResponseWriter, r *http.Request, p principal) { + if p.API { + fail(w, http.StatusForbidden, "tokens are managed from the dashboard") + return + } + var in struct{ Name string } + if !decode(w, r, &in) { + return + } + token, hash, err := config.NewToken() + if err != nil { + fail(w, http.StatusInternalServerError, err.Error()) + return + } + name := strings.TrimSpace(in.Name) + err = s.cfg.Update(func(c *config.Config) error { + c.Tokens = append(c.Tokens, config.Token{Name: name, Hash: hash, Created: time.Now().UTC()}) + return nil + }) + if err != nil { + fail(w, http.StatusBadRequest, err.Error()) + return + } + writeJSON(w, map[string]string{"name": name, "token": token}) +} + +func (s *Server) deleteToken(w http.ResponseWriter, r *http.Request, p principal) { + if p.API { + fail(w, http.StatusForbidden, "tokens are managed from the dashboard") + return + } + name := r.PathValue("name") + found := false + err := s.cfg.Update(func(c *config.Config) error { + keep := c.Tokens[:0] + for _, t := range c.Tokens { + if t.Name == name { + found = true + continue + } + keep = append(keep, t) + } + c.Tokens = keep + return nil + }) + switch { + case err != nil: + fail(w, http.StatusInternalServerError, err.Error()) + case !found: + fail(w, http.StatusNotFound, "no such token") + default: + writeJSON(w, map[string]bool{"ok": true}) + } +} diff --git a/internal/web/server.go b/internal/web/server.go new file mode 100644 index 0000000..8f3c7b6 --- /dev/null +++ b/internal/web/server.go @@ -0,0 +1,290 @@ +// Package web serves Basking's dashboard and its JSON API. +package web + +import ( + "crypto/hmac" + "crypto/sha256" + "crypto/subtle" + "embed" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io/fs" + "net" + "net/http" + "strconv" + "strings" + "sync" + "time" + + "git.veillax.com/Veillax/basking/internal/config" + "git.veillax.com/Veillax/basking/internal/store" +) + +//go:embed static +var staticFiles embed.FS + +const ( + cookieName = "basking_session" + sessionTTL = 30 * 24 * time.Hour + maxLoginFails = 6 + loginFailSpan = 5 * time.Minute + maxRequestBody = store.MaxBody + 4096 +) + +type Server struct { + cfg *config.Holder + st *store.Store + version string + + mu sync.Mutex + fails map[string]failure + + dummyOnce sync.Once + dummyHash string +} + +type failure struct { + n int + until time.Time +} + +// principal is who is making a request. +type principal struct { + Name string + API bool // came with an API token rather than a browser session +} + +func New(h *config.Holder, st *store.Store, version string) *Server { + return &Server{cfg: h, st: st, version: version, fails: map[string]failure{}} +} + +// Handler returns the whole site. +func (s *Server) Handler() http.Handler { + mux := http.NewServeMux() + mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "ok") }) + + mux.HandleFunc("POST /api/login", s.login) + mux.HandleFunc("POST /api/logout", s.api(s.logout)) + mux.HandleFunc("GET /api/me", s.api(s.me)) + mux.HandleFunc("GET /api/summary", s.api(s.summary)) + + mux.HandleFunc("GET /api/notes", s.api(s.listNotes)) + mux.HandleFunc("POST /api/notes", s.api(s.createNote)) + mux.HandleFunc("GET /api/notes/{id}", s.api(s.getNote)) + mux.HandleFunc("PUT /api/notes/{id}", s.api(s.updateNote)) + mux.HandleFunc("DELETE /api/notes/{id}", s.api(s.deleteNote)) + mux.HandleFunc("POST /api/notes/{id}/restore", s.api(s.restoreNote)) + mux.HandleFunc("DELETE /api/trash", s.api(s.emptyTrash)) + mux.HandleFunc("POST /api/daily", s.api(s.daily)) + mux.HandleFunc("GET /api/export", s.api(s.export)) + + mux.HandleFunc("POST /api/password", s.api(s.changePassword)) + mux.HandleFunc("GET /api/tokens", s.api(s.listTokens)) + mux.HandleFunc("POST /api/tokens", s.api(s.newToken)) + mux.HandleFunc("DELETE /api/tokens/{name}", s.api(s.deleteToken)) + + static, _ := fs.Sub(staticFiles, "static") + mux.Handle("GET /", http.FileServerFS(static)) + return secure(mux) +} + +// secure adds headers that keep the page from being framed, sniffed or fed outside scripts. +func secure(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := w.Header() + h.Set("Content-Security-Policy", "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'") + h.Set("X-Content-Type-Options", "nosniff") + h.Set("X-Frame-Options", "DENY") + h.Set("Referrer-Policy", "no-referrer") + if strings.HasPrefix(r.URL.Path, "/api/") { + h.Set("Cache-Control", "no-store") + } + next.ServeHTTP(w, r) + }) +} + +// ---- authentication ---- + +type handlerFunc func(w http.ResponseWriter, r *http.Request, p principal) + +// api wraps a handler with sign-in and CSRF protection. +func (s *Server) api(fn handlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + p, ok := s.authenticate(r) + if !ok { + fail(w, http.StatusUnauthorized, "sign in required") + return + } + // Browser sessions must send a header a web page on another site cannot add. + if !p.API && r.Method != http.MethodGet && r.Header.Get("X-Basking") != "1" { + fail(w, http.StatusForbidden, "missing X-Basking header") + return + } + r.Body = http.MaxBytesReader(w, r.Body, maxRequestBody) + fn(w, r, p) + } +} + +// A session cookie is "expiry.signature". The signature covers the password hash, so changing +// the password signs every browser out, and nothing has to be remembered on the server. +func sign(c config.Config, exp int64) string { + m := hmac.New(sha256.New, []byte(c.Secret)) + fmt.Fprintf(m, "session|%d|%s", exp, c.PasswordHash) + return hex.EncodeToString(m.Sum(nil)) +} + +func (s *Server) authenticate(r *http.Request) (principal, bool) { + c := s.cfg.Get() + if c.PasswordHash == "" { + return principal{}, false + } + if h := r.Header.Get("Authorization"); strings.HasPrefix(h, "Bearer ") { + sum := config.HashToken(strings.TrimPrefix(h, "Bearer ")) + for _, t := range c.Tokens { + if subtle.ConstantTimeCompare([]byte(t.Hash), []byte(sum)) == 1 { + return principal{Name: "token:" + t.Name, API: true}, true + } + } + return principal{}, false + } + ck, err := r.Cookie(cookieName) + if err != nil { + return principal{}, false + } + expStr, sig, ok := strings.Cut(ck.Value, ".") + exp, err := strconv.ParseInt(expStr, 10, 64) + if !ok || err != nil || time.Now().Unix() > exp { + return principal{}, false + } + if !hmac.Equal([]byte(sig), []byte(sign(c, exp))) { + return principal{}, false + } + return principal{Name: "you"}, true +} + +func (s *Server) clientIP(r *http.Request) string { + if s.cfg.Get().TrustProxy { + if f := r.Header.Get("X-Forwarded-For"); f != "" { + return strings.TrimSpace(strings.Split(f, ",")[0]) + } + } + host, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + return r.RemoteAddr + } + return host +} + +func (s *Server) secureCookie(r *http.Request) bool { + c := s.cfg.Get() + return c.CookieSecure || r.TLS != nil || (c.TrustProxy && r.Header.Get("X-Forwarded-Proto") == "https") +} + +func (s *Server) startSession(w http.ResponseWriter, r *http.Request) { + c := s.cfg.Get() + exp := time.Now().Add(sessionTTL).Unix() + http.SetCookie(w, &http.Cookie{Name: cookieName, Value: fmt.Sprintf("%d.%s", exp, sign(c, exp)), Path: "/", HttpOnly: true, + SameSite: http.SameSiteStrictMode, Secure: s.secureCookie(r), MaxAge: int(sessionTTL.Seconds())}) +} + +func (s *Server) login(w http.ResponseWriter, r *http.Request) { + r.Body = http.MaxBytesReader(w, r.Body, 4096) + if r.Header.Get("X-Basking") != "1" { + fail(w, http.StatusForbidden, "missing X-Basking header") + return + } + var in struct{ Password string } + if json.NewDecoder(r.Body).Decode(&in) != nil { + fail(w, http.StatusBadRequest, "bad request") + return + } + ip := s.clientIP(r) + s.mu.Lock() + f := s.fails[ip] + if f.n >= maxLoginFails && time.Now().Before(f.until) { + s.mu.Unlock() + fail(w, http.StatusTooManyRequests, "too many failed attempts; wait a few minutes") + return + } + s.mu.Unlock() + + hash := s.cfg.Get().PasswordHash + if hash == "" { + // Spend the same time as for a real check. + s.dummyOnce.Do(func() { s.dummyHash, _ = config.HashPassword("dummy") }) + hash = s.dummyHash + } + ok := config.CheckPassword(hash, in.Password) && s.cfg.Get().PasswordHash != "" + s.mu.Lock() + if !ok { + if time.Now().After(f.until) { + f = failure{} + } + f.n++ + f.until = time.Now().Add(loginFailSpan) + s.fails[ip] = f + s.mu.Unlock() + fail(w, http.StatusUnauthorized, "wrong password") + return + } + delete(s.fails, ip) + s.mu.Unlock() + s.startSession(w, r) + writeJSON(w, map[string]bool{"ok": true}) +} + +func (s *Server) logout(w http.ResponseWriter, r *http.Request, p principal) { + http.SetCookie(w, &http.Cookie{Name: cookieName, Value: "", Path: "/", MaxAge: -1, HttpOnly: true}) + writeJSON(w, map[string]bool{"ok": true}) +} + +// ---- helpers ---- + +func writeJSON(w http.ResponseWriter, v any) { writeJSONStatus(w, http.StatusOK, v) } + +func writeJSONStatus(w http.ResponseWriter, code int, v any) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(code) + json.NewEncoder(w).Encode(v) +} + +// fail answers with an error. Messages are capitalized here, once, so they read as sentences. +func fail(w http.ResponseWriter, code int, msg string) { + if r := []rune(msg); len(r) > 0 { + msg = strings.ToUpper(string(r[:1])) + string(r[1:]) + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(code) + json.NewEncoder(w).Encode(map[string]string{"error": msg}) +} + +func decode(w http.ResponseWriter, r *http.Request, v any) bool { + if err := json.NewDecoder(r.Body).Decode(v); err != nil { + var mbe *http.MaxBytesError + if errors.As(err, &mbe) { + fail(w, http.StatusRequestEntityTooLarge, "request too large") + } else { + fail(w, http.StatusBadRequest, "bad request: "+err.Error()) + } + return false + } + return true +} + +// storeFail turns a store error into the right status. +func storeFail(w http.ResponseWriter, err error) { + switch { + case errors.Is(err, store.ErrNotFound): + fail(w, http.StatusNotFound, err.Error()) + case errors.Is(err, store.ErrConflict): + fail(w, http.StatusConflict, err.Error()) + case errors.Is(err, store.ErrEmpty), errors.Is(err, store.ErrBadDate), errors.Is(err, store.ErrNotTrash), errors.Is(err, store.ErrBadID): + fail(w, http.StatusBadRequest, err.Error()) + case errors.Is(err, store.ErrTooLong): + fail(w, http.StatusRequestEntityTooLarge, err.Error()) + default: + fail(w, http.StatusInternalServerError, "could not save: "+err.Error()) + } +} diff --git a/internal/web/static/app.js b/internal/web/static/app.js new file mode 100644 index 0000000..a412eb2 --- /dev/null +++ b/internal/web/static/app.js @@ -0,0 +1,710 @@ +'use strict'; + +// ---------- tiny toolkit ---------- +// h builds an element. Strings become text nodes, never HTML, so note text can never inject markup. +function h(tag, props, ...kids) { + const el = document.createElement(tag); + let value; + for (const [k, v] of Object.entries(props || {})) { + if (v == null || v === false) continue; + if (k.startsWith('on')) el.addEventListener(k.slice(2), v); + else if (k === 'class') el.className = v; + else if (k === 'value') value = v; + else if (k === 'checked') el.checked = true; + else el.setAttribute(k, v === true ? '' : v); + } + for (const k of kids.flat(Infinity)) if (k != null && k !== false) el.append(k.nodeType ? k : document.createTextNode(String(k))); + if (value !== undefined) el.value = value; + return el; +} +const $app = document.getElementById('app'); + +// theme: follows the system unless the person picked one +try { const t = localStorage.getItem('theme'); if (t) document.documentElement.dataset.theme = t; } catch (e) { /* storage is off */ } +function toggleTheme() { + const dark = document.documentElement.dataset.theme ? document.documentElement.dataset.theme === 'dark' : matchMedia('(prefers-color-scheme: dark)').matches; + const next = dark ? 'light' : 'dark'; + document.documentElement.dataset.theme = next; + try { localStorage.setItem('theme', next); } catch (e) { /* storage is off */ } +} +const store = { + get(k) { try { return localStorage.getItem(k) || ''; } catch (e) { return ''; } }, + set(k, v) { try { v ? localStorage.setItem(k, v) : localStorage.removeItem(k); } catch (e) { /* storage is off */ } }, +}; + +// short confirmations at the bottom right; errors stay longer and are announced as alerts +let toastBox; +function toast(msg, bad, action) { + if (!toastBox) { toastBox = h('div', { class: 'toasts', 'aria-live': 'polite' }); document.body.append(toastBox); } + const t = h('div', { class: 'toast' + (bad ? ' toast-error' : ''), role: bad ? 'alert' : 'status' }, h('span', {}, msg), + action ? h('button', { class: 'toast-act', onclick: () => { t.remove(); action.fn(); } }, action.label) : null); + toastBox.append(t); + setTimeout(() => t.remove(), bad ? 7000 : action ? 6000 : 3000); +} +const cap = s => s ? s[0].toUpperCase() + s.slice(1) : s; +const SIGNED_OUT = 'Your session ended. Sign in again to continue.'; + +async function api(method, url, body) { + const opt = { method, headers: { 'X-Basking': '1' }, credentials: 'same-origin' }; + if (body !== undefined) { opt.headers['Content-Type'] = 'application/json'; opt.body = JSON.stringify(body); } + const r = await fetch(url, opt); + const j = await r.json().catch(() => ({})); + if (r.status === 401 && url !== '/api/login' && url !== '/api/me') { + if (S.ready) { S.ready = false; boot(); } + throw new Error(SIGNED_OUT); + } + if (!r.ok) throw Object.assign(new Error(cap(j.error || r.statusText)), { status: r.status, data: j }); + return j; +} +// run an action; show errors as a toast instead of throwing into the void +const act = fn => async (...a) => { try { await fn(...a); } catch (e) { if (e.message !== SIGNED_OUT) toast(e.message, true); } }; + +const pad = n => String(n).padStart(2, '0'); +const hhmm = d => { const t = new Date(d); return `${pad(t.getHours())}:${pad(t.getMinutes())}`; }; +const fmtTime = d => new Date(d).toLocaleString(undefined, { month: 'short', day: 'numeric', year: 'numeric', hour: 'numeric', minute: '2-digit' }); +const isoDay = d => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`; +const parseDay = s => { const [y, m, d] = s.split('-').map(Number); return new Date(y, m - 1, d); }; +const addDays = (s, n) => { const d = parseDay(s); d.setDate(d.getDate() + n); return isoDay(d); }; +function dayLabel(d) { + const day = isoDay(d instanceof Date ? d : parseDay(d)), now = new Date(); + if (day === isoDay(now)) return 'Today'; + if (day === addDays(isoDay(now), -1)) return 'Yesterday'; + const dt = parseDay(day); + return dt.toLocaleDateString(undefined, { weekday: 'long', month: 'long', day: 'numeric', year: dt.getFullYear() === now.getFullYear() ? undefined : 'numeric' }); +} +const field = (label, el, hint) => h('label', {}, label, el, hint ? h('span', { class: 'sub' }, hint) : null); +const chk = (label, checked) => { const i = h('input', { type: 'checkbox', checked: !!checked }); return Object.assign(h('label', { class: 'chk' }, i, label), { box: i }); }; +// status pill: color always travels with a text label +const badge = (text, tone) => h('span', { class: 'badge' + (tone ? ' badge-' + tone : '') }, text); +const emptyState = (name, title, text, action) => h('div', { class: 'empty' }, icon(name, 22), h('h3', {}, title), h('p', {}, text), action || null); +// placeholder rows while a page loads, so the layout does not jump +const skeleton = () => h('div', { class: 'card', role: 'status', 'aria-busy': 'true' }, h('span', { class: 'sr-only' }, 'Loading'), + ['w60', 'w100', 'w90', 'w100', 'w75'].map(w => h('div', { class: 'skeleton ' + w }))); +// replaceChildren stringifies null and arrays, so everything goes through here +const fill = (el, ...kids) => el.replaceChildren(...kids.flat(Infinity).filter(k => k != null && k !== false)); +const card = (title, ...kids) => h('div', { class: 'card' }, title ? h('h2', {}, title) : null, ...kids); + +// ---------- icons (lucide-style, static strings; see docs/design.md for the approved names) ---------- +const ICONS = { + fin: '', + notes: '', + today: '', + journal: '', + pin: '', + tag: '', + trash: '', + edit: '', + restore: '', + clock: '', + view: '', + download: '', + copy: '', + prev: '', + next: '', + settings: '', + out: '', + menu: '', + close: '', + add: '', + check: '', + light: '', + dark: '', + alert: '', +}; +function icon(name, size = 18) { + const s = h('span', { class: 'inline-icon', 'aria-hidden': 'true' }); + s.innerHTML = `${ICONS[name] || ''}`; + return s; +} +const brand = () => h('div', { class: 'brand' }, icon('fin', 22), 'Basking'); +const btn = (label, ic, onclick, cls = 'ghost') => h('button', { class: cls, onclick }, ic ? icon(ic, 16) : null, label); + +// ---------- dialogs ---------- +// A dialog: focus moves in, Tab stays inside, Escape closes, and focus goes back to what opened it. +let dialogSeq = 0; +function modal(title, body, opts = {}) { + const opener = document.activeElement, id = 'dialog-title-' + (++dialogSeq); + const shade = h('div', { class: 'shade' }); + const box = h('div', { class: 'modal' + (opts.wide ? ' wide' : ''), role: 'dialog', 'aria-modal': 'true', 'aria-labelledby': id, tabindex: '-1' }, h('h2', { id }, title), body); + let closed = false; + const close = () => { + if (closed) return; closed = true; + document.removeEventListener('keydown', onKey, true); shade.remove(); + if (opener && opener.isConnected && opener.focus) opener.focus(); + if (opts.onClose) opts.onClose(); + }; + const focusables = () => [...box.querySelectorAll('a[href],button:not([disabled]),input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex="-1"])')].filter(e => e.offsetParent !== null); + function onKey(e) { + if ([...document.querySelectorAll('.shade')].pop() !== shade) return; + if (e.key === 'Escape') { e.stopPropagation(); close(); } + else if (e.key === 'Tab') { + const f = focusables(); if (!f.length) { e.preventDefault(); return; } + const first = f[0], last = f[f.length - 1]; + if (e.shiftKey && (document.activeElement === first || document.activeElement === box)) { e.preventDefault(); last.focus(); } + else if (!e.shiftKey && document.activeElement === last) { e.preventDefault(); first.focus(); } + } + } + shade.addEventListener('mousedown', e => { if (e.target === shade && !opts.sticky) close(); }); + shade.append(box); + document.body.append(shade); + document.addEventListener('keydown', onKey, true); + (box.querySelector('input,select,textarea') || focusables()[0] || box).focus(); + return close; +} +const modalOpen = () => !!document.querySelector('.shade'); + +// A confirmation that names what it affects and says what is lost. Resolves true only when confirmed. +function confirmDialog({ title, body, confirm = 'Confirm', cancel = 'Cancel', danger = true }) { + return new Promise(resolve => { + let close; + const done = v => { resolve(v); close(); }; + close = modal(title, h('div', { class: 'stack' }, h('p', { class: 'dim' }, body), + h('div', { class: 'actions form-actions' }, h('button', { class: 'ghost', onclick: () => done(false) }, cancel), + h('button', { class: danger ? 'ghost danger' : 'btn', onclick: () => done(true) }, confirm))), { onClose: () => resolve(false) }); + }); +} + +// formDialog shows a form and resolves with its values, or null if it was cancelled. +// Field types: text, password, textarea, note. +function formDialog({ title, intro, fields, submit = 'Save' }) { + return new Promise(resolve => { + const parts = {}; + const err = h('p', { class: 'err', role: 'alert' }); + const rows = fields.map(f => { + if (f.type === 'note') return h('p', { class: 'dim' }, f.text); + let el, get; + if (f.type === 'textarea') { + el = h('textarea', { placeholder: f.placeholder, rows: 5 }); el.value = f.value || ''; get = () => el.value.trim(); + } else { + el = h('input', { type: f.type || 'text', placeholder: f.placeholder, autocomplete: f.type === 'password' ? 'new-password' : 'off' }); + el.value = f.value ?? ''; get = () => f.type === 'password' ? el.value : el.value.trim(); + } + parts[f.name] = { f, get, el }; + return field(f.label, el, f.hint); + }); + let close; + const send = () => { + const out = {}; + for (const [name, p] of Object.entries(parts)) { + const v = p.get(); + if (p.f.required && !v) { err.textContent = `${p.f.label} is required.`; p.el.setAttribute('aria-invalid', 'true'); p.el.focus(); return; } + out[name] = v; + } + done(out); + }; + const done = v => { resolve(v); close(); }; + const form = h('form', { class: 'stack', onsubmit: e => { e.preventDefault(); send(); } }, intro ? h('p', { class: 'dim' }, intro) : null, rows, err, + h('div', { class: 'actions form-actions' }, h('button', { type: 'button', class: 'ghost', onclick: () => done(null) }, 'Cancel'), h('button', { type: 'submit', class: 'btn' }, submit))); + close = modal(title, form, { sticky: true, onClose: () => resolve(null) }); + }); +} + +// ---------- markdown ---------- +// A small, safe renderer. It builds elements (never HTML strings), and only http(s) links are links. +const INLINE_SRC = '`([^`\\n]+)`|\\*\\*([^*\\n]+)\\*\\*|(? h('a', { class: 'chip', href: '#/tag/' + encodeURIComponent(t) }, '#' + t); +const extLink = (url, label) => h('a', { href: url, target: '_blank', rel: 'noopener noreferrer' }, label); + +function inline(text) { + const re = new RegExp(INLINE_SRC, 'gu'), out = []; + let last = 0, m; + while ((m = re.exec(text))) { + if (m.index > last) out.push(text.slice(last, m.index)); + last = re.lastIndex; + if (m[1] != null) out.push(h('code', {}, m[1])); + else if (m[2] != null) out.push(h('strong', {}, inline(m[2]))); + else if (m[3] != null) out.push(h('em', {}, inline(m[3]))); + else if (m[4] != null) out.push(extLink(m[5], m[4])); + else if (m[6] != null) out.push(extLink(m[6], m[6])); + else { + const t = m[7].replace(/[/-]+$/, ''); + if (!t || /^\d+$/.test(t)) out.push('#' + m[7]); + else out.push(tagChip(t.toLowerCase()), m[7].slice(t.length)); + } + } + if (last < text.length) out.push(text.slice(last)); + return out; +} + +// md renders a note. onTask(lineIndex, checked) makes the checkboxes work; without it they are read-only. +function md(src, onTask) { + const root = h('div', { class: 'md' }), lines = src.split('\n'); + let para = [], i = 0, m; + const flush = () => { + if (!para.length) return; + const p = h('p'); + para.forEach((t, k) => { if (k) p.append(h('br')); p.append(...inline(t)); }); + root.append(p); para = []; + }; + while (i < lines.length) { + const line = lines[i]; + if (/^\s*```/.test(line)) { + flush(); const code = []; i++; + while (i < lines.length && !/^\s*```/.test(lines[i])) code.push(lines[i++]); + i++; root.append(h('pre', {}, h('code', {}, code.join('\n')))); continue; + } + if (!line.trim()) { flush(); i++; continue; } + if ((m = /^(#{1,3})\s+(.+)$/.exec(line))) { flush(); root.append(h('h' + (m[1].length + 2), { class: 'md-h' }, inline(m[2]))); i++; continue; } + if (/^\s*(-{3,}|\*{3,})\s*$/.test(line)) { flush(); root.append(h('hr')); i++; continue; } + if (/^>/.test(line)) { + flush(); const q = []; + while (i < lines.length && /^>/.test(lines[i])) q.push(lines[i++].replace(/^>\s?/, '')); + const bq = h('blockquote'); q.forEach((t, k) => { if (k) bq.append(h('br')); bq.append(...inline(t)); }); root.append(bq); continue; + } + if (/^(\s*)([-*+]|\d+[.)])\s+/.test(line)) { + flush(); const ordered = /^\s*\d/.test(line), list = h(ordered ? 'ol' : 'ul'); + while (i < lines.length && (m = /^(\s*)([-*+]|\d+[.)])\s+(?:\[([ xX])\]\s+)?(.*)$/.exec(lines[i]))) { + const lvl = Math.min(3, Math.floor(m[1].replace(/\t/g, ' ').length / 2)), at = i; + const li = h('li', { class: 'ind-' + lvl + (m[3] != null ? ' task' : '') }); + if (m[3] != null) { + const box = h('input', { type: 'checkbox', checked: m[3] !== ' ', disabled: !onTask, 'aria-label': m[4] || 'Task' }); + if (onTask) box.addEventListener('change', () => onTask(at, box.checked)); + li.append(box, h('span', { class: m[3] !== ' ' ? 'done' : '' }, inline(m[4]))); + } else li.append(...inline(m[4])); + list.append(li); i++; + } + root.append(list); continue; + } + para.push(line); i++; + } + flush(); + return root; +} + +// ---------- state ---------- +const S = { ready: false, counts: { all: 0, pinned: 0, daily: 0, trash: 0 }, tags: [], view: null, q: '', pending: new Set(), nav: {} }; + +async function refreshSummary() { + const r = await api('GET', '/api/summary'); + S.counts = r.counts; S.tags = r.tags; updateNav(); +} +// Anything still waiting to autosave is saved before the page changes or the tab is hidden. +const flushAll = () => Promise.all([...S.pending].map(f => f())); +document.addEventListener('visibilitychange', () => { if (document.hidden) flushAll(); }); +window.addEventListener('pagehide', () => { flushAll(); }); + +async function boot() { + S.view = null; S.pending.clear(); + try { await api('GET', '/api/me'); } catch { return $app.replaceChildren(loginView()); } + await refreshSummary().catch(() => { }); + S.ready = true; + render(); +} + +function loginView() { + const pw = h('input', { type: 'password', autocomplete: 'current-password' }); + const go = act(async () => { + await api('POST', '/api/login', { password: pw.value }); + if (toastBox) toastBox.replaceChildren(); // an earlier failed attempt no longer applies + boot(); + }); + pw.addEventListener('keydown', e => e.key === 'Enter' && go()); + setTimeout(() => pw.focus()); + return h('div', { class: 'center' }, card('', brand(), h('h2', {}, 'Sign in'), h('div', { class: 'stack' }, + field('Password', pw, 'Basking printed it in its log the first time it started.'), h('button', { class: 'btn', onclick: go }, 'Sign in')))); +} + +// ---------- shell ---------- +function setDrawer(open) { + const sb = document.getElementById('sidebar'), bd = document.getElementById('backdrop'), mb = document.getElementById('menu-btn'); + if (!sb) return; + sb.classList.toggle('open', open); bd.classList.toggle('open', open); mb.setAttribute('aria-expanded', open); + if (open) sb.querySelector('a,button')?.focus(); else mb.focus(); +} +document.addEventListener('keydown', e => { if (e.key === 'Escape' && !modalOpen()) document.getElementById('sidebar')?.classList.contains('open') && setDrawer(false); }); + +const route = () => { const p = location.hash.slice(2).split('/'); return { name: p[0] || 'notes', arg: decodeURIComponent(p.slice(1).join('/')) }; }; +const routeKey = r => r.name === 'tag' ? 'tag/' + r.arg : r.name === 'day' ? (r.arg === isoDay(new Date()) ? 'today' : 'journal') : r.name; + +function navItem(href, key, label, ic, count) { + const cur = routeKey(route()) === key; + const el = h('a', { class: 'navitem' + (cur ? ' on' : ''), href, 'aria-current': cur ? 'page' : null, onclick: () => setDrawer(false) }, + icon(ic), h('span', { class: 'label' }, label), count == null ? null : h('span', { class: 'count' }, count)); + return el; +} +// The sidebar's lists and counts change as notes do, so they are rebuilt on their own. +function updateNav() { + const box = document.getElementById('nav-dynamic'); + if (!box) return; + fill(box, + navItem('#/', 'notes', 'Notes', 'notes', S.counts.all), + navItem('#/today', 'today', 'Today', 'today'), + navItem('#/journal', 'journal', 'Journal', 'journal', S.counts.daily), + navItem('#/pinned', 'pinned', 'Pinned', 'pin', S.counts.pinned), + S.tags.length ? h('div', { class: 'navsec' }, 'Tags') : null, + S.tags.map(t => navItem('#/tag/' + encodeURIComponent(t.tag), 'tag/' + t.tag, t.tag, 'tag', t.count)), + h('div', { class: 'navsec' }, 'Other'), + navItem('#/trash', 'trash', 'Trash', 'trash', S.counts.trash || null)); +} + +async function render() { + await flushAll(); + S.pending.clear(); + const main = h('main', { id: 'view', tabindex: '-1' }); + $app.replaceChildren(h('a', { class: 'skip', href: '#view', onclick: e => { e.preventDefault(); main.focus(); } }, 'Skip to content'), h('div', { class: 'shell' }, + h('div', { class: 'topbar' }, brand(), h('button', { class: 'ghost quiet btn-icon', id: 'menu-btn', 'aria-label': 'Open menu', 'aria-controls': 'sidebar', 'aria-expanded': 'false', onclick: () => setDrawer(true) }, icon('menu'))), + h('div', { class: 'backdrop', id: 'backdrop', onclick: () => setDrawer(false) }), + h('aside', { class: 'sidebar', id: 'sidebar', 'aria-label': 'Main' }, + h('div', { class: 'sidebar-head' }, brand(), h('button', { class: 'ghost quiet btn-icon sidebar-close', 'aria-label': 'Close menu', onclick: () => setDrawer(false) }, icon('close'))), + h('nav', { id: 'nav-dynamic', class: 'nav', 'aria-label': 'Notes' }), + h('div', { class: 'grow' }), + h('div', { class: 'userbox' }, + navItemLink('#/settings', 'settings', 'Settings', 'settings'), + h('button', { class: 'navitem', onclick: toggleTheme }, icon('dark'), 'Switch theme'), + h('button', { class: 'navitem', onclick: act(async () => { await flushAll(); await api('POST', '/api/logout'); S.ready = false; boot(); }) }, icon('out'), 'Sign out'))), + h('div', { class: 'content' }, main))); + updateNav(); + show(); +} +function navItemLink(href, key, label, ic) { return navItem(href, key, label, ic); } +window.addEventListener('hashchange', () => { if (S.ready) render(); }); + +function pageHeader(title, desc, primary) { + return h('div', { class: 'page-h' }, h('div', {}, h('h1', {}, title), desc ? h('p', {}, desc) : null), + primary ? h('button', { class: 'btn', onclick: primary[1] }, icon(primary[2] || 'add', 16), primary[0]) : null); +} + +function show() { + const r = route(), main = document.getElementById('view'); + S.q = ''; + let page; + switch (r.name) { + case 'today': location.replace('#/day/' + isoDay(new Date())); return; + case 'day': page = /^\d{4}-\d{2}-\d{2}$/.test(r.arg) ? dayPage(r.arg) : notesPage({ view: 'all' }); break; + case 'journal': page = journalPage(); break; + case 'pinned': page = notesPage({ view: 'pinned', title: 'Pinned', desc: 'Notes you pinned to keep close.' }); break; + case 'tag': page = notesPage({ view: 'all', tag: r.arg, title: '#' + r.arg, desc: 'Every note with this tag.' }); break; + case 'trash': page = notesPage({ view: 'trash', title: 'Trash', desc: 'Deleted notes wait here until you empty the trash.' }); break; + case 'settings': page = settingsPage(); break; + default: page = notesPage({ view: 'all', composer: true, title: 'Notes', desc: 'Write anything down. Sort it out later, or never.' }); + } + S.view = page; + main.replaceChildren(page.el); + document.title = (page.title ? page.title + ' · ' : '') + 'Basking'; + page.start?.(); +} + +// ---------- the editor: autosaves, and notices when a note changed somewhere else ---------- +function makeEditor(note, { rows = 4, onSaved, big } = {}) { + let cur = note, dirty = false, saving = false, queued = false, blocked = false, timer = null; + const ta = h('textarea', { class: 'write' + (big ? ' big' : ''), rows, 'aria-label': 'Note text', placeholder: big ? 'Write about your day...' : '' }); + ta.value = note.body; + const status = h('span', { class: 'sub', role: 'status' }); + const notice = h('div', { class: 'notice notice-warning', hidden: true }); + const fit = () => { if (big) return; ta.style.height = 'auto'; ta.style.height = Math.min(ta.scrollHeight + 2, window.innerHeight * 0.7) + 'px'; }; + + async function save() { + clearTimeout(timer); timer = null; + if (!dirty || blocked) return; + if (saving) { queued = true; return; } + saving = true; dirty = false; status.textContent = 'Saving'; + try { + cur = await api('PUT', '/api/notes/' + cur.id, { body: ta.value, base: cur.updated }); + if (!dirty) status.textContent = 'Saved'; + onSaved?.(cur); + } catch (e) { + dirty = true; + if (e.status === 409) conflict(e.data.note); + else { status.textContent = 'Not saved'; if (e.message !== SIGNED_OUT) toast(e.message, true); } + } finally { + saving = false; + if (queued) { queued = false; save(); } + } + } + function conflict(theirs) { + blocked = true; status.textContent = 'Not saved'; + notice.hidden = false; + notice.replaceChildren(icon('alert', 16), h('span', { class: 'grow' }, 'This note changed somewhere else, probably on another device.'), + h('button', { class: 'ghost', onclick: () => { cur = theirs; blocked = false; notice.hidden = true; save(); } }, 'Keep my version'), + h('button', { class: 'ghost', onclick: () => { cur = theirs; ta.value = theirs.body; dirty = false; blocked = false; notice.hidden = true; status.textContent = 'Saved'; fit(); onSaved?.(cur); } }, 'Use the other version')); + } + ta.addEventListener('input', () => { + dirty = true; status.textContent = 'Unsaved'; fit(); + clearTimeout(timer); timer = setTimeout(save, 700); + }); + S.pending.add(save); + return { + ta, status, notice, save, + get note() { return cur; }, + flush: async () => { await save(); }, + close() { S.pending.delete(save); }, + fit, + }; +} + +// ---------- note cards ---------- +const LONG_LINES = 14, LONG_CHARS = 1200; + +function noteCard(n, ctx) { + const el = h('article', { class: 'note' + (n.pinned ? ' pinned' : ''), 'data-id': n.id }); + let ed = null; + + const toggleTask = act(async (line, checked) => { + const lines = n.body.split('\n'); + lines[line] = lines[line].replace(/\[( |x|X)\]/, checked ? '[x]' : '[ ]'); + n = await api('PUT', '/api/notes/' + n.id, { body: lines.join('\n'), base: n.updated }); + ctx.changed(n, true); + }); + + function view() { + ed = null; + const long = n.body.split('\n').length > LONG_LINES || n.body.length > LONG_CHARS; + let open = false; + const body = n.body.trim() ? md(n.body, n.trashed ? null : toggleTask) : h('p', { class: 'dim' }, 'Empty note'); + const wrap = h('div', { class: 'note-body' + (long ? ' clamp' : '') }, body); + const more = long ? h('button', { class: 'ghost quiet small-btn', 'aria-expanded': 'false', onclick: e => { + open = !open; wrap.classList.toggle('clamp', !open); e.currentTarget.setAttribute('aria-expanded', open); e.currentTarget.textContent = open ? 'Show less' : 'Show more'; + } }, 'Show more') : null; + const when = n.daily ? h('a', { href: '#/day/' + n.daily }, dayLabel(n.daily)) : h('time', { datetime: n.created, title: 'Written ' + fmtTime(n.created) + (n.updated !== n.created ? '. Edited ' + fmtTime(n.updated) : '') }, hhmm(n.created)); + const actions = n.trashed + ? [btn('Restore', 'restore', act(() => ctx.restore(n))), btn('Delete for good', 'trash', act(() => ctx.purge(n)), 'ghost danger')] + : [btn('Edit', 'edit', edit), btn(n.pinned ? 'Unpin' : 'Pin', 'pin', act(async () => { n = await api('PUT', '/api/notes/' + n.id, { pinned: !n.pinned }); ctx.changed(n, true); })), btn('Delete', 'trash', act(() => ctx.trash(n)))]; + el.className = 'note' + (n.pinned ? ' pinned' : ''); + fill(el, wrap, more, h('div', { class: 'note-foot' }, when, n.pinned ? badge('Pinned', 'accent') : null, h('span', { class: 'grow' }), h('div', { class: 'actions' }, actions))); + } + + function edit() { + ed = makeEditor(n, { rows: 3, onSaved: s => { n = s; ctx.changed(n, false); } }); + el.className = 'note editing'; + el.replaceChildren(ed.notice, ed.ta, h('div', { class: 'note-foot' }, ed.status, h('span', { class: 'grow' }), h('div', { class: 'actions' }, + btn('Done', 'check', act(done), 'btn')))); + ed.fit(); ed.ta.focus(); ed.ta.setSelectionRange(ed.ta.value.length, ed.ta.value.length); + ed.ta.addEventListener('keydown', e => { + if (e.key === 'Escape' || (e.key === 'Enter' && (e.ctrlKey || e.metaKey))) { e.preventDefault(); e.stopPropagation(); done(); } + }); + } + async function done() { + const e = ed; await e.flush(); e.close(); n = e.note; + view(); ctx.changed(n, true); el.querySelector('button')?.focus(); + } + + view(); + el.edit = edit; + return el; +} + +// ---------- the composer ---------- +function composer(onSaved) { + const ta = h('textarea', { class: 'write', rows: 3, placeholder: 'What is on your mind?', 'aria-label': 'New note' }); + ta.value = store.get('draft'); + const fit = () => { ta.style.height = 'auto'; ta.style.height = Math.min(Math.max(ta.scrollHeight + 2, 84), window.innerHeight * 0.5) + 'px'; }; + const go = h('button', { class: 'btn' }, icon('check', 16), 'Save note'); + const save = async () => { + const text = ta.value.trim(); + if (!text) { ta.focus(); return; } + go.disabled = true; + try { + await api('POST', '/api/notes', { body: text }); + ta.value = ''; store.set('draft', ''); fit(); + await onSaved(); + } catch (e) { if (e.message !== SIGNED_OUT) toast(e.message, true); } + go.disabled = false; ta.focus(); + }; + go.addEventListener('click', save); + ta.addEventListener('input', () => { store.set('draft', ta.value); fit(); }); + ta.addEventListener('keydown', e => { if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) { e.preventDefault(); save(); } }); + setTimeout(fit); + return Object.assign(h('div', { class: 'card composer' }, ta, h('div', { class: 'note-foot' }, h('span', { class: 'sub' }, 'Ctrl+Enter saves. Add #tags to find things later.'), h('span', { class: 'grow' }), go)), { ta }); +} + +// ---------- notes list ---------- +function notesPage({ view, tag = '', title, desc, composer: withComposer }) { + const list = h('div', { class: 'notes' }), moreBox = h('div', { class: 'more' }); + const search = h('input', { type: 'search', placeholder: 'Search notes, or type #tag', 'aria-label': 'Search notes' }); + let items = [], total = 0, loaded = false, timer = null, token = 0; + + async function load(reset) { + const mine = ++token, offset = reset ? 0 : items.length; + const q = new URLSearchParams({ view, tag, q: search.value.trim(), offset, limit: 50 }); + const r = await api('GET', '/api/notes?' + q); + if (mine !== token) return; + items = reset ? r.notes : items.concat(r.notes); total = r.total; loaded = true; S.q = search.value.trim(); + draw(); + } + const reload = act(() => Promise.all([load(true), refreshSummary()])); + + const ctx = { + changed(n, redraw) { + const k = items.findIndex(x => x.id === n.id); + if (k >= 0) items[k] = n; + if (view === 'pinned' && !n.pinned) { items.splice(k, 1); total--; } + if (redraw) { draw(); } refreshSummary().catch(() => { }); + }, + async trash(n) { + await api('DELETE', '/api/notes/' + n.id); + toast('Moved to the trash', false, { label: 'Undo', fn: act(async () => { await api('POST', `/api/notes/${n.id}/restore`); await reload(); }) }); + await reload(); + }, + async restore(n) { await api('POST', `/api/notes/${n.id}/restore`); toast('Note restored'); await reload(); }, + async purge(n) { + const ok = await confirmDialog({ title: 'Delete this note for good?', body: `"${preview(n.body)}" will be deleted and cannot be brought back.`, confirm: 'Delete note' }); + if (!ok) return; + await api('DELETE', `/api/notes/${n.id}?purge=1`); toast('Note deleted'); await reload(); + }, + }; + + function draw() { + list.replaceChildren(); moreBox.replaceChildren(); + if (!items.length) { + const searching = search.value.trim() || tag; + const [ic, t, p] = view === 'trash' ? ['trash', 'The trash is empty', 'Notes you delete wait here until you empty the trash.'] + : view === 'pinned' ? ['pin', 'Nothing pinned', 'Pin a note to keep it here.'] + : searching ? ['view', 'No notes match', 'Try fewer words, or clear the search.'] + : ['notes', 'No notes yet', 'Write anything in the box above. It is saved when you press Ctrl+Enter.']; + list.append(emptyState(ic, t, p)); + } else { + let day = '', group; + for (const n of items) { + const d = isoDay(new Date(n.created)); + if (d !== day && view !== 'trash') { + day = d; group = h('section', { class: 'day', 'aria-label': dayLabel(d) }, h('h2', { class: 'day-h' }, dayLabel(d))); + list.append(group); + } else if (view === 'trash' && !group) { group = h('section', { class: 'day' }); list.append(group); } + group.append(noteCard(n, ctx)); + } + } + if (items.length < total) moreBox.append(h('button', { class: 'ghost', onclick: act(() => load(false)) }, `Show more (${total - items.length} left)`)); + } + + const comp = withComposer ? composer(async () => { search.value = ''; await load(true); await refreshSummary(); }) : null; + const emptyBtn = view === 'trash' ? btn('Empty trash', 'trash', act(async () => { + if (!total) { toast('The trash is already empty'); return; } + const ok = await confirmDialog({ title: 'Empty the trash?', body: `${total} ${total === 1 ? 'note' : 'notes'} will be deleted for good. This cannot be undone.`, confirm: 'Empty trash' }); + if (!ok) return; + await api('DELETE', '/api/trash'); toast('Trash emptied'); await reload(); + }), 'ghost danger') : null; + + search.addEventListener('input', () => { clearTimeout(timer); timer = setTimeout(act(() => load(true)), 250); }); + search.addEventListener('keydown', e => { if (e.key === 'Escape') { search.value = ''; load(true); search.blur(); } }); + + const el = h('div', {}, pageHeader(title || 'Notes', desc), comp, + h('div', { class: 'toolbar' }, search, emptyBtn ? h('span', { class: 'push' }, emptyBtn) : null), list, moreBox); + list.append(skeleton()); + return { el, title, search, composer: comp, start: act(() => load(true)), newNote: () => comp?.ta.focus() }; +} +const preview = s => { const t = s.replace(/\s+/g, ' ').trim(); return t.length > 60 ? t.slice(0, 60) + '...' : t; }; + +// ---------- journal ---------- +function journalPage() { + const list = h('div', { class: 'notes' }); + return { + title: 'Journal', + el: h('div', {}, pageHeader('Journal', 'One page for each day. Open a day to write it, or read back over the ones before.', ['Write today', () => { location.hash = '#/day/' + isoDay(new Date()); }, 'edit']), list), + start: act(async () => { + const r = await api('GET', '/api/notes?view=daily&limit=200'); + list.replaceChildren(); + const filled = r.notes.filter(n => n.body.trim()); + if (!filled.length) { list.append(emptyState('journal', 'No journal entries yet', 'Open today and write a few lines. Each day gets its own page.')); return; } + for (const n of filled) { + list.append(h('article', { class: 'note' }, h('h2', { class: 'day-h' }, h('a', { href: '#/day/' + n.daily }, dayLabel(n.daily))), + h('div', { class: 'note-body clamp' }, md(n.body)), + h('div', { class: 'note-foot' }, h('span', { class: 'grow' }), h('div', { class: 'actions' }, btn('Open', 'edit', () => { location.hash = '#/day/' + n.daily; }))))); + } + }), + }; +} + +// ---------- one day ---------- +function dayPage(date) { + const body = h('div'), today = isoDay(new Date()); + let ed = null, previewing = false; + const nav = h('div', { class: 'actions' }, + btn('Previous day', 'prev', () => { location.hash = '#/day/' + addDays(date, -1); }), + date === today ? null : btn('Today', 'today', () => { location.hash = '#/day/' + today; }), + date >= today ? null : btn('Next day', 'next', () => { location.hash = '#/day/' + addDays(date, 1); })); + + function stamp() { + const t = ed.ta, at = t.selectionStart, before = t.value.slice(0, at), line = before.slice(before.lastIndexOf('\n') + 1); + const text = (line.trim() ? '\n\n' : before && !before.endsWith('\n\n') && before.endsWith('\n') ? '\n' : '') + hhmm(new Date()) + ' '; + t.setRangeText(text, at, t.selectionEnd, 'end'); t.focus(); t.dispatchEvent(new Event('input')); + } + function draw(n) { + ed = makeEditor(n, { rows: 14, big: true }); + const rendered = h('div', { class: 'card note-body', hidden: true }); + const toggle = btn('Preview', 'view', () => { + previewing = !previewing; + if (previewing) { rendered.replaceChildren(ed.ta.value.trim() ? md(ed.ta.value) : h('p', { class: 'dim' }, 'Nothing written yet')); } + rendered.hidden = !previewing; ed.ta.hidden = previewing; + toggle.replaceChildren(icon(previewing ? 'edit' : 'view', 16), previewing ? 'Edit' : 'Preview'); + }); + body.replaceChildren(ed.notice, ed.ta, rendered, h('div', { class: 'note-foot' }, ed.status, h('span', { class: 'grow' }), + h('div', { class: 'actions' }, btn('Insert time', 'clock', stamp), toggle))); + setTimeout(() => ed.ta.focus()); + } + return { + title: dayLabel(date), + el: h('div', {}, pageHeader(dayLabel(date), date === today ? parseDay(date).toLocaleDateString(undefined, { dateStyle: 'full' }) : 'Your page for this day. It saves as you type.'), + h('div', { class: 'toolbar' }, nav), h('div', { class: 'card' }, body)), + start: act(async () => { body.replaceChildren(skeleton()); draw(await api('POST', '/api/daily', { date })); }), + }; +} + +// ---------- settings ---------- +async function copyText(text, input) { + try { await navigator.clipboard.writeText(text); toast('Copied'); } + catch { input.select(); toast('Press Ctrl+C to copy'); } +} + +function settingsPage() { + const tokens = h('div'); + const loadTokens = act(async () => { + const rows = await api('GET', '/api/tokens'); + tokens.replaceChildren(rows.length ? h('div', { class: 'scroll' }, h('table', {}, h('thead', {}, h('tr', {}, h('th', {}, 'Name'), h('th', {}, 'Created'), h('th', {}))), + h('tbody', {}, rows.map(t => h('tr', {}, h('td', {}, t.name), h('td', {}, fmtTime(t.created)), h('td', { class: 'actions-cell' }, btn('Revoke', null, act(async () => { + const ok = await confirmDialog({ title: `Revoke ${t.name}?`, body: 'Anything using this token stops working at once. You can make another one later.', confirm: 'Revoke token' }); + if (!ok) return; + await api('DELETE', '/api/tokens/' + encodeURIComponent(t.name)); toast('Token revoked'); loadTokens(); + }), 'ghost danger'))))))) : emptyState('tag', 'No tokens yet', 'Make a token to send notes from a script or a phone shortcut.')); + }); + + const newToken = act(async () => { + const r = await formDialog({ title: 'New API token', submit: 'Make token', fields: [{ name: 'name', label: 'Name', required: true, placeholder: 'phone', hint: 'Something that tells you where it is used.' }] }); + if (!r) return; + const t = await api('POST', '/api/tokens', r); + const box = h('input', { type: 'text', readonly: true, class: 'mono' }); box.value = t.token; + let close; + close = modal('Your new token', h('div', { class: 'stack' }, h('p', { class: 'dim' }, 'This is the only time it is shown. Copy it now.'), box, + h('div', { class: 'actions form-actions' }, btn('Copy token', 'copy', () => copyText(t.token, box)), h('button', { class: 'btn', onclick: () => close() }, 'Done'))), { sticky: true }); + box.select(); loadTokens(); + }); + + const changePassword = act(async () => { + const r = await formDialog({ title: 'Change password', submit: 'Change password', fields: [ + { name: 'current', label: 'Current password', type: 'password', required: true }, + { name: 'next', label: 'New password', type: 'password', required: true, hint: 'At least 8 characters.' }, + { name: 'again', label: 'New password again', type: 'password', required: true }] }); + if (!r) return; + if (r.next !== r.again) { toast('The two new passwords do not match', true); return; } + await api('POST', '/api/password', { current: r.current, new: r.next }); + toast('Password changed'); + }); + + const origin = location.origin; + const curl = `curl -H "Authorization: Bearer $TOKEN" \\\n -H "Content-Type: text/plain" \\\n -d "Call the dentist #errands" \\\n ${origin}/api/notes`; + return { + title: 'Settings', + el: h('div', {}, pageHeader('Settings', 'Your password, tokens for scripts, and a copy of everything.'), + card('Password', h('p', { class: 'dim' }, 'Changing it signs out every other browser.'), btn('Change password', 'edit', changePassword)), + card('', h('div', { class: 'card-h' }, h('h2', {}, 'API tokens'), btn('New token', 'add', newToken)), + h('p', { class: 'dim' }, 'A token lets a script or a phone shortcut add notes without signing in.'), tokens), + card('Send a note from anywhere', h('p', { class: 'dim' }, 'With a token, post the text of a note to Basking:'), h('pre', { class: 'snippet' }, h('code', {}, curl)), + h('p', { class: 'dim' }, 'Or, from a terminal with the basking command:'), h('pre', { class: 'snippet' }, h('code', {}, `BASKING_URL=${origin} BASKING_TOKEN=... basking add "an idea"`))), + card('Your notes', h('p', { class: 'dim' }, 'Every note is a plain markdown file in the folder Basking keeps its data in, so you can back that folder up or sync it as it is. This downloads the same files as one zip.'), + btn('Download all notes', 'download', () => { location.href = '/api/export'; }))), + start: loadTokens, + }; +} + +// ---------- keyboard ---------- +document.addEventListener('keydown', e => { + if (!S.ready || modalOpen() || e.ctrlKey || e.metaKey || e.altKey) return; + const t = e.target; + if (t.matches?.('input,textarea,select,[contenteditable]')) return; + if (e.key === '/' && S.view?.search) { e.preventDefault(); S.view.search.focus(); } + else if (e.key === 'n') { + e.preventDefault(); + if (S.view?.composer) S.view.composer.ta.focus(); else { location.hash = '#/'; setTimeout(() => S.view?.composer?.ta.focus(), 400); } + } +}); + +boot(); diff --git a/internal/web/static/index.html b/internal/web/static/index.html new file mode 100644 index 0000000..4ec4d1c --- /dev/null +++ b/internal/web/static/index.html @@ -0,0 +1,15 @@ + + + + + + +Basking + + + + +
+ + + diff --git a/internal/web/static/style.css b/internal/web/static/style.css new file mode 100644 index 0000000..af240d4 --- /dev/null +++ b/internal/web/static/style.css @@ -0,0 +1,239 @@ + /* tokens:begin Design tokens, the Lightwell design language (docs/design.md). This is the only place raw colors and sizes are defined. */ + :root { + --accent:#1f5c5c; --accent-contrast:#ffffff; + --bg:#f6f5f3; --surface:#ffffff; --surface-2:#efede9; --border:#e0ddd7; --border-strong:#c4c0b8; + --text:#1d1c1a; --text-muted:#5f5b54; --text-faint:#7a766e; + --accent-text:var(--accent); + --accent-soft:color-mix(in srgb, var(--accent) 9%, var(--surface)); + --accent-hover:color-mix(in srgb, var(--accent) 88%, #000000); + --focus:color-mix(in srgb, var(--accent) 70%, #ffffff); + --success:#1c6b3a; --success-bg:#e3f1e8; --warning:#85560a; --warning-bg:#f9eed6; --danger:#a3271e; --danger-bg:#f9e6e3; --neutral-bg:#eceae6; + --overlay:rgb(20 19 17 / 0.55); --shadow:0 8px 24px rgb(20 19 17 / 0.14); + --s1:4px; --s2:8px; --s3:12px; --s4:16px; --s5:24px; --s6:32px; --s7:48px; --s8:64px; + --radius:6px; --radius-lg:10px; --border-w:1px; + --font:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif; + --font-mono:ui-monospace,"SF Mono",Menlo,Consolas,monospace; + --text-xs:12px; --text-sm:13px; --text-base:15px; --text-lg:18px; --text-xl:22px; --text-2xl:28px; --leading:1.5; + --sidebar-w:232px; --content-max:1120px; --touch:44px; --control-h:38px; + --z-sticky:20; --z-overlay:100; --z-toast:200; --ease:140ms ease; + color-scheme:light; + } + @media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { + --bg:#131312; --surface:#1b1b19; --surface-2:#252523; --border:#33322e; --border-strong:#4a4943; + --text:#ecebe7; --text-muted:#adaaa2; --text-faint:#8f8c84; + --accent-text:color-mix(in srgb, var(--accent) 45%, #ffffff); + --accent-soft:color-mix(in srgb, var(--accent) 22%, var(--surface)); + --accent-hover:color-mix(in srgb, var(--accent) 85%, #ffffff); + --success:#86d3a0; --success-bg:#17301f; --warning:#e8bd6b; --warning-bg:#33280f; --danger:#f19a92; --danger-bg:#3a1b18; --neutral-bg:#2b2a27; + --overlay:rgb(0 0 0 / 0.65); --shadow:0 8px 24px rgb(0 0 0 / 0.5); color-scheme:dark; + } } + :root[data-theme="dark"] { + --bg:#131312; --surface:#1b1b19; --surface-2:#252523; --border:#33322e; --border-strong:#4a4943; + --text:#ecebe7; --text-muted:#adaaa2; --text-faint:#8f8c84; + --accent-text:color-mix(in srgb, var(--accent) 45%, #ffffff); + --accent-soft:color-mix(in srgb, var(--accent) 22%, var(--surface)); + --accent-hover:color-mix(in srgb, var(--accent) 85%, #ffffff); + --success:#86d3a0; --success-bg:#17301f; --warning:#e8bd6b; --warning-bg:#33280f; --danger:#f19a92; --danger-bg:#3a1b18; --neutral-bg:#2b2a27; + --overlay:rgb(0 0 0 / 0.65); --shadow:0 8px 24px rgb(0 0 0 / 0.5); color-scheme:dark; + } + /* tokens:end */ + + *, *::before, *::after { box-sizing:border-box; } + html, body { height:100%; } + html { -webkit-text-size-adjust:100%; } + body { margin:0; background:var(--bg); color:var(--text); font:var(--text-base)/var(--leading) var(--font); -webkit-font-smoothing:antialiased; } + h1, h2, h3, h4 { margin:0; line-height:1.25; font-weight:600; letter-spacing:-.01em; } + h1 { font-size:var(--text-2xl); } h2 { font-size:var(--text-xl); } h3 { font-size:var(--text-lg); } + p { margin:0 0 var(--s3); } + a { color:var(--accent-text); text-underline-offset:2px; } + a:hover { text-decoration-thickness:2px; } + :focus-visible { outline:2px solid var(--focus); outline-offset:2px; border-radius:var(--radius); } + [hidden] { display:none !important; } + .sr-only { position:absolute; width:1px; height:1px; margin:-1px; padding:0; overflow:hidden; clip:rect(0 0 0 0); white-space:nowrap; border:0; } + .skip { position:absolute; left:var(--s3); top:-100px; z-index:var(--z-toast); background:var(--surface); border:var(--border-w) solid var(--border-strong); border-radius:var(--radius); padding:var(--s2) var(--s3); } + .skip:focus { top:var(--s3); } + .dim { color:var(--text-muted); } .err { color:var(--danger); } .small { font-size:var(--text-sm); } + .num { font-variant-numeric:tabular-nums; } + .clip { display:-webkit-box; -webkit-line-clamp:3; -webkit-box-orient:vertical; overflow:hidden; overflow-wrap:anywhere; max-width:340px; } + code, .mono { font-family:var(--font-mono); font-size:var(--text-sm); } + .icon { width:18px; height:18px; flex:none; stroke-width:1.75; } + .inline-icon { display:inline-flex; } + + /* shell */ + .shell { display:grid; grid-template-columns:var(--sidebar-w) minmax(0,1fr); min-height:100vh; } + .sidebar { position:sticky; top:0; height:100vh; display:flex; flex-direction:column; gap:var(--s3); padding:var(--s5) var(--s3); background:var(--surface); border-right:var(--border-w) solid var(--border); overflow-y:auto; } + .sidebar-head { display:flex; align-items:center; justify-content:space-between; gap:var(--s2); } + .sidebar button.sidebar-close { display:none; } + .brand { display:flex; align-items:center; gap:var(--s2); font-size:var(--text-lg); font-weight:700; letter-spacing:-.02em; padding:0 var(--s3); color:var(--text); text-decoration:none; } + .brand .icon { color:var(--accent-text); } + .navsec { font-size:var(--text-xs); font-weight:600; letter-spacing:.06em; text-transform:uppercase; color:var(--text-muted); padding:var(--s4) var(--s3) var(--s1); } + .nav { display:flex; flex-direction:column; gap:2px; } + .navitem { display:flex; align-items:center; gap:var(--s3); width:100%; min-height:36px; padding:var(--s2) var(--s3); border-radius:var(--radius); color:var(--text-muted); text-decoration:none; font:inherit; font-weight:500; background:none; border:0; cursor:pointer; text-align:left; } + .navitem:hover { background:var(--surface-2); color:var(--text); } + .navitem.on { background:var(--accent-soft); color:var(--accent-text); } + .navitem .label { flex:1 1 auto; min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } + .grow { flex:1 1 auto; min-width:0; } + .userbox { border-top:var(--border-w) solid var(--border); padding-top:var(--s3); } + .userbox .who { padding:var(--s1) var(--s3) var(--s2); font-size:var(--text-sm); color:var(--text-muted); overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } + .content { min-width:0; } + main { max-width:var(--content-max); margin:0 auto; padding:var(--s6) var(--s5) var(--s8); } + main:focus { outline:none; } + .topbar, .backdrop { display:none; } + .page-h { display:flex; align-items:flex-start; justify-content:space-between; gap:var(--s4); margin-bottom:var(--s5); flex-wrap:wrap; } + .page-h h1 { display:flex; align-items:center; gap:var(--s3); flex-wrap:wrap; } + .page-h p { margin:var(--s1) 0 0; color:var(--text-muted); max-width:70ch; } + .page-h .btn { white-space:nowrap; } + .back { display:inline-block; margin-bottom:var(--s3); font-size:var(--text-sm); } + + /* cards, layout */ + .card { background:var(--surface); border:var(--border-w) solid var(--border); border-radius:var(--radius-lg); padding:var(--s5); margin-bottom:var(--s4); overflow-x:auto; } + .card.flush { padding:0; overflow:hidden; } + .card > h2 { font-size:var(--text-lg); margin:0 0 var(--s3); } + .card-h { display:flex; align-items:center; justify-content:space-between; gap:var(--s3); margin-bottom:var(--s3); flex-wrap:wrap; } + .card-h h2 { font-size:var(--text-lg); margin:0; } + .meta { color:var(--text-muted); font-size:var(--text-sm); } + .stack { display:flex; flex-direction:column; gap:var(--s4); } + .scroll { overflow-x:auto; } + .actions { display:flex; gap:var(--s2); flex-wrap:wrap; } + td.actions-cell { text-align:right; white-space:nowrap; } + td.actions-cell button + button { margin-left:var(--s2); } + .form-actions { justify-content:flex-end; margin-top:var(--s2); } + .toolbar { display:flex; align-items:center; gap:var(--s3); margin-bottom:var(--s3); flex-wrap:wrap; } + .toolbar .push { margin-left:auto; } + .center { max-width:440px; margin:var(--s8) auto; padding:0 var(--s4); } + .center .brand { justify-content:center; font-size:var(--text-xl); padding-bottom:var(--s4); } + + /* forms */ + label { display:flex; flex-direction:column; gap:var(--s1); font-size:var(--text-sm); font-weight:600; color:var(--text); } + label.chk { flex-direction:row; align-items:flex-start; gap:var(--s2); font-size:var(--text-base); font-weight:400; } + input, select, textarea { background:var(--surface); color:var(--text); border:var(--border-w) solid var(--border-strong); border-radius:var(--radius); padding:var(--s2) var(--s3); min-height:var(--control-h); font:inherit; font-weight:400; min-width:0; width:100%; } + textarea { min-height:96px; resize:vertical; font-family:var(--font-mono); font-size:var(--text-sm); } + input[type=checkbox] { width:16px; height:16px; min-height:0; padding:0; margin-top:3px; accent-color:var(--accent); } + input:focus, select:focus, textarea:focus { outline:2px solid var(--focus); outline-offset:0; border-color:var(--accent); } + input:disabled, select:disabled { opacity:.6; } + input[aria-invalid="true"] { border-color:var(--danger); } + .sub { color:var(--text-muted); font-size:var(--text-sm); font-weight:400; } + fieldset { border:var(--border-w) solid var(--border); border-radius:var(--radius-lg); padding:var(--s4); margin:0; min-width:0; } + legend { font-size:var(--text-sm); font-weight:600; color:var(--text-muted); padding:0 var(--s2); } + .pillrow { display:flex; flex-wrap:wrap; gap:var(--s2) var(--s4); } + + /* buttons: btn is the primary action, ghost the default, danger and quiet the others */ + button.btn, button.ghost { display:inline-flex; align-items:center; justify-content:center; gap:var(--s2); min-height:var(--control-h); padding:0 var(--s4); border-radius:var(--radius); font:inherit; font-weight:500; cursor:pointer; white-space:nowrap; text-decoration:none; transition:background var(--ease), border-color var(--ease); } + button.btn { background:var(--accent); color:var(--accent-contrast); border:var(--border-w) solid var(--accent); } + button.btn:hover { background:var(--accent-hover); border-color:var(--accent-hover); } + button.ghost { background:var(--surface); border:var(--border-w) solid var(--border-strong); color:var(--text); } + button.ghost:hover { background:var(--surface-2); } + button.quiet { background:transparent; border-color:transparent; } + button.danger { color:var(--danger); border-color:var(--danger); } + button.danger:hover { background:var(--danger-bg); } + button.btn-icon { width:var(--control-h); padding:0; } + td button.ghost, .actions button.ghost { min-height:30px; padding:0 var(--s3); font-size:var(--text-sm); } + button:disabled { opacity:.55; cursor:not-allowed; } + + /* tables */ + table { width:100%; border-collapse:collapse; } + th { text-align:left; font-size:var(--text-sm); color:var(--text-muted); font-weight:600; padding:var(--s3) var(--s4); border-bottom:var(--border-w) solid var(--border); white-space:nowrap; background:var(--surface); } + td { padding:var(--s3) var(--s4); border-bottom:var(--border-w) solid var(--border); vertical-align:middle; } + tbody tr:hover td { background:var(--surface-2); } + tr:last-child td { border-bottom:0; } + td.wrap { overflow-wrap:anywhere; min-width:150px; max-width:320px; } + + /* badges: status always carries a text label, never color alone */ + .badge { display:inline-flex; align-items:center; gap:var(--s1); padding:2px var(--s2); border-radius:999px; font-size:var(--text-xs); font-weight:600; background:var(--neutral-bg); color:var(--text-muted); white-space:nowrap; } + .badge-success { background:var(--success-bg); color:var(--success); } + .badge-warning { background:var(--warning-bg); color:var(--warning); } + .badge-danger { background:var(--danger-bg); color:var(--danger); } + .badge-accent { background:var(--accent-soft); color:var(--accent-text); } + .tag { display:inline-block; padding:1px var(--s2); border:var(--border-w) solid var(--border); border-radius:var(--radius); font-size:var(--text-xs); color:var(--text-muted); } + + /* the event and chat feed */ + + /* empty state, notice, loading */ + .empty { text-align:center; padding:var(--s7) var(--s5); color:var(--text-muted); border:var(--border-w) dashed var(--border-strong); border-radius:var(--radius-lg); margin:var(--s3); } + .empty h3 { color:var(--text); margin-bottom:var(--s1); } + .empty p { margin:0 auto var(--s3); max-width:56ch; } + .empty .icon { width:28px; height:28px; margin:0 auto var(--s3); color:var(--text-faint); display:block; } + .card > .empty { margin:0; } + .notice { padding:var(--s3) var(--s4); border-radius:var(--radius); border:var(--border-w) solid var(--border); background:var(--surface-2); font-size:var(--text-sm); margin-bottom:var(--s4); display:flex; gap:var(--s3); align-items:center; flex-wrap:wrap; } + .notice-warning { background:var(--warning-bg); border-color:var(--warning); color:var(--warning); } + .notice-danger { background:var(--danger-bg); border-color:var(--danger); color:var(--danger); } + .skeleton { background:linear-gradient(90deg, var(--surface-2), var(--border), var(--surface-2)); background-size:200% 100%; animation:shimmer 1.3s infinite; border-radius:var(--radius); height:16px; margin-bottom:14px; } + .w60 { width:60%; } .w75 { width:75%; } .w90 { width:90%; } .w100 { width:100%; } + @keyframes shimmer { to { background-position:-200% 0; } } + + /* dialogs, tabs, toasts */ + .shade { position:fixed; inset:0; background:var(--overlay); display:grid; grid-template-columns:minmax(0,1fr); place-items:start center; padding:var(--s7) var(--s4); overflow:auto; z-index:var(--z-overlay); } + .modal { background:var(--surface); border-radius:var(--radius-lg); box-shadow:var(--shadow); padding:var(--s5); width:min(480px,100%); } + .modal.wide { width:min(640px,100%); } + .modal h2 { margin:0 0 var(--s3); font-size:var(--text-lg); } + .toasts { position:fixed; right:var(--s4); bottom:var(--s4); z-index:var(--z-toast); display:flex; flex-direction:column; gap:var(--s2); max-width:min(360px, calc(100vw - 32px)); } + .toast { background:var(--text); color:var(--bg); padding:var(--s3) var(--s4); border-radius:var(--radius); box-shadow:var(--shadow); font-size:var(--text-sm); overflow-wrap:anywhere; } + .toast-error { background:var(--danger); color:var(--accent-contrast); } + + @media (prefers-reduced-motion: reduce) { .skeleton { animation:none; } * { transition:none !important; } html { scroll-behavior:auto; } } + @media (pointer: coarse) { + button.btn, button.ghost, .navitem, input, select, .tabs2 a { min-height:var(--touch); } + td button.ghost, .actions button.ghost { min-height:36px; } + input, select, textarea { font-size:16px; } + } + @media (max-width:860px) { + .shell { grid-template-columns:minmax(0,1fr); grid-template-rows:auto minmax(0,1fr); } + .sidebar { display:none; } + .sidebar.open { display:flex; position:fixed; inset:0 auto 0 0; width:min(280px,85vw); height:100dvh; z-index:var(--z-overlay); box-shadow:var(--shadow); } + .sidebar button.sidebar-close { display:inline-flex; } + .topbar { display:flex; align-items:center; justify-content:space-between; min-height:52px; padding:var(--s2) var(--s4); background:var(--surface); border-bottom:var(--border-w) solid var(--border); position:sticky; top:0; z-index:var(--z-sticky); } + .backdrop.open { display:block; position:fixed; inset:0; background:var(--overlay); z-index:calc(var(--z-overlay) - 1); } + main { padding:var(--s5) var(--s4) var(--s7); } + .ev { grid-template-columns:4.5rem minmax(0,1fr); } .ev .badge { display:none; } + } + @media (max-width:640px) { + .modal { padding:var(--s4); } + .shade { padding:var(--s4) var(--s3); } + .toasts { left:var(--s3); right:var(--s3); bottom:var(--s3); max-width:none; } + .page-h > .btn { width:100%; } + } + .icon-16 { width:16px; height:16px; } .icon-22 { width:22px; height:22px; } + + /* notes */ + textarea.write { width:100%; min-height:84px; font-family:var(--font); font-size:var(--text-base); line-height:1.6; resize:none; border:0; padding:0; background:transparent; } + textarea.write:focus { outline:none; border:0; } + textarea.write.big { min-height:50vh; resize:vertical; } + .composer, .note { padding:var(--s4) var(--s5); } + .composer:focus-within, .note.editing { border-color:var(--accent); } + .note { background:var(--surface); border:var(--border-w) solid var(--border); border-radius:var(--radius-lg); margin-bottom:var(--s3); overflow:visible; } + .note.pinned { border-left:3px solid var(--accent); } + .note-body { overflow-wrap:anywhere; } + .note-body.clamp { max-height:15em; overflow:hidden; -webkit-mask-image:linear-gradient(black 70%, transparent); mask-image:linear-gradient(black 70%, transparent); } + .note-foot { display:flex; align-items:center; gap:var(--s3); margin-top:var(--s3); flex-wrap:wrap; font-size:var(--text-sm); color:var(--text-muted); } + .note-foot a, .note-foot time { color:var(--text-muted); } + .notes .empty { margin:0; } + .day-h { font-size:var(--text-sm); font-weight:600; color:var(--text-muted); text-transform:none; margin:var(--s5) 0 var(--s2); letter-spacing:0; } + .day-h a { color:inherit; } + .day:first-child .day-h { margin-top:var(--s2); } + .more { text-align:center; margin:var(--s4) 0; } + button.small-btn { min-height:28px; padding:0 var(--s2); font-size:var(--text-sm); } + .navitem .count { font-size:var(--text-xs); color:var(--text-faint); font-variant-numeric:tabular-nums; } + .chip { display:inline-block; padding:0 var(--s2); border-radius:999px; background:var(--accent-soft); color:var(--accent-text); text-decoration:none; font-size:var(--text-sm); font-weight:500; } + .chip:hover { text-decoration:underline; } + .toast { display:flex; gap:var(--s3); align-items:center; justify-content:space-between; } + .toast-act { background:transparent; border:0; color:inherit; font:inherit; font-weight:600; text-decoration:underline; cursor:pointer; padding:0; } + .toolbar .push { margin-left:auto; } + .toolbar input[type=search] { max-width:360px; } + .snippet { margin:0 0 var(--s4); padding:var(--s3); background:var(--bg); border:var(--border-w) solid var(--border); border-radius:var(--radius); overflow-x:auto; } + .snippet code { white-space:pre; } + + /* rendered markdown */ + .md > *:first-child { margin-top:0; } .md > *:last-child { margin-bottom:0; } + .md p { margin:0 0 var(--s3); } + .md .md-h { margin:var(--s4) 0 var(--s2); font-size:var(--text-lg); } + .md h4.md-h { font-size:var(--text-base); } .md h5.md-h { font-size:var(--text-base); color:var(--text-muted); } + .md ul, .md ol { margin:0 0 var(--s3); padding-left:var(--s5); } + .md li.task { list-style:none; margin-left:calc(var(--s5) * -1); display:flex; gap:var(--s2); align-items:flex-start; } + .md li.task input { margin-top:5px; } + .md li .done { color:var(--text-muted); text-decoration:line-through; } + .md li.ind-1 { margin-left:var(--s4); } .md li.ind-2 { margin-left:var(--s6); } .md li.ind-3 { margin-left:var(--s7); } + .md li.task.ind-1 { margin-left:calc(var(--s5) * -1 + var(--s4)); } .md li.task.ind-2 { margin-left:calc(var(--s5) * -1 + var(--s6)); } .md li.task.ind-3 { margin-left:calc(var(--s5) * -1 + var(--s7)); } + .md blockquote { margin:0 0 var(--s3); padding:var(--s1) var(--s4); border-left:3px solid var(--border-strong); color:var(--text-muted); } + .md pre { margin:0 0 var(--s3); padding:var(--s3); background:var(--bg); border:var(--border-w) solid var(--border); border-radius:var(--radius); overflow-x:auto; } + .md code { background:var(--surface-2); padding:1px 4px; border-radius:4px; } .md pre code { background:none; padding:0; } + .md hr { border:0; border-top:var(--border-w) solid var(--border); margin:var(--s4) 0; } diff --git a/internal/web/web_test.go b/internal/web/web_test.go new file mode 100644 index 0000000..a9e7ca3 --- /dev/null +++ b/internal/web/web_test.go @@ -0,0 +1,339 @@ +package web + +import ( + "archive/zip" + "bytes" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + + "git.veillax.com/Veillax/basking/internal/config" + "git.veillax.com/Veillax/basking/internal/store" +) + +const password = "correct horse battery" + +type rig struct { + t *testing.T + srv *httptest.Server + cfg *config.Holder + cookie *http.Cookie +} + +func newRig(t *testing.T) *rig { + t.Helper() + dir := t.TempDir() + h, err := config.Load(filepath.Join(dir, "basking.json")) + if err != nil { + t.Fatal(err) + } + hash, _ := config.HashPassword(password) + if err := h.Update(func(c *config.Config) error { c.PasswordHash = hash; return nil }); err != nil { + t.Fatal(err) + } + st, err := store.Open(filepath.Join(dir, "data")) + if err != nil { + t.Fatal(err) + } + srv := httptest.NewServer(New(h, st, "test").Handler()) + t.Cleanup(srv.Close) + return &rig{t: t, srv: srv, cfg: h} +} + +func (r *rig) do(method, path, ctype, body string, headers ...string) (*http.Response, []byte) { + r.t.Helper() + req, _ := http.NewRequest(method, r.srv.URL+path, strings.NewReader(body)) + if ctype != "" { + req.Header.Set("Content-Type", ctype) + } + for i := 0; i+1 < len(headers); i += 2 { + req.Header.Set(headers[i], headers[i+1]) + } + if r.cookie != nil { + req.AddCookie(r.cookie) + } + resp, err := http.DefaultClient.Do(req) + if err != nil { + r.t.Fatal(err) + } + defer resp.Body.Close() + b, _ := io.ReadAll(resp.Body) + return resp, b +} + +// js sends JSON as a browser would, with the CSRF header. +func (r *rig) js(method, path string, v any) (*http.Response, map[string]any) { + r.t.Helper() + var body string + if v != nil { + b, _ := json.Marshal(v) + body = string(b) + } + resp, b := r.do(method, path, "application/json", body, "X-Basking", "1") + var out map[string]any + json.Unmarshal(b, &out) + return resp, out +} + +func (r *rig) signIn() { + r.t.Helper() + resp, _ := r.js("POST", "/api/login", map[string]string{"password": password}) + if resp.StatusCode != 200 { + r.t.Fatalf("login: %d", resp.StatusCode) + } + for _, c := range resp.Cookies() { + if c.Name == cookieName { + r.cookie = c + } + } + if r.cookie == nil { + r.t.Fatal("no session cookie") + } +} + +func TestRequiresSignIn(t *testing.T) { + r := newRig(t) + for _, p := range []string{"/api/notes", "/api/summary", "/api/export", "/api/tokens", "/api/me"} { + if resp, _ := r.do("GET", p, "", ""); resp.StatusCode != 401 { + t.Errorf("GET %s without signing in: %d", p, resp.StatusCode) + } + } + if resp, _ := r.do("GET", "/healthz", "", ""); resp.StatusCode != 200 { + t.Error("healthz should be open") + } + if resp, _ := r.do("GET", "/", "", ""); resp.StatusCode != 200 { + t.Error("the page itself should load") + } +} + +func TestLoginAndHeaders(t *testing.T) { + r := newRig(t) + if resp, _ := r.do("POST", "/api/login", "application/json", `{"password":"`+password+`"}`); resp.StatusCode != 403 { + t.Errorf("login without the CSRF header: %d", resp.StatusCode) + } + if resp, _ := r.js("POST", "/api/login", map[string]string{"password": "wrong"}); resp.StatusCode != 401 { + t.Errorf("wrong password: %d", resp.StatusCode) + } + r.signIn() + resp, _ := r.do("GET", "/api/me", "", "") + if resp.StatusCode != 200 { + t.Fatalf("me: %d", resp.StatusCode) + } + if csp := resp.Header.Get("Content-Security-Policy"); !strings.Contains(csp, "default-src 'none'") { + t.Errorf("CSP %q", csp) + } + if !r.cookie.HttpOnly || r.cookie.SameSite != http.SameSiteStrictMode { + t.Errorf("cookie flags %+v", r.cookie) + } + // a browser write needs the header + if resp, _ := r.do("POST", "/api/notes", "application/json", `{"body":"x"}`); resp.StatusCode != 403 { + t.Errorf("write without the header: %d", resp.StatusCode) + } + // a forged cookie does nothing + r.cookie = &http.Cookie{Name: cookieName, Value: "9999999999.deadbeef"} + if resp, _ := r.do("GET", "/api/me", "", ""); resp.StatusCode != 401 { + t.Errorf("forged cookie: %d", resp.StatusCode) + } +} + +func TestLoginIsRateLimited(t *testing.T) { + r := newRig(t) + var last int + for i := 0; i < maxLoginFails+2; i++ { + resp, _ := r.js("POST", "/api/login", map[string]string{"password": "nope"}) + last = resp.StatusCode + } + if last != 429 { + t.Fatalf("after many failures got %d", last) + } + if resp, _ := r.js("POST", "/api/login", map[string]string{"password": password}); resp.StatusCode != 429 { + t.Fatalf("even the right password waits: %d", resp.StatusCode) + } +} + +func TestNoteLifecycle(t *testing.T) { + r := newRig(t) + r.signIn() + + resp, n := r.js("POST", "/api/notes", map[string]string{"body": "remember the milk #errands"}) + if resp.StatusCode != 201 { + t.Fatalf("create: %d %v", resp.StatusCode, n) + } + id, _ := n["id"].(string) + if id == "" || n["body"] != "remember the milk #errands" { + t.Fatalf("created %v", n) + } + if resp, _ := r.js("POST", "/api/notes", map[string]string{"body": " "}); resp.StatusCode != 400 { + t.Errorf("empty note: %d", resp.StatusCode) + } + + _, list := r.js("GET", "/api/notes?q=milk", nil) + if list["total"] != float64(1) { + t.Fatalf("search: %v", list) + } + _, list = r.js("GET", "/api/notes?tag=errands", nil) + if list["total"] != float64(1) { + t.Fatalf("tag: %v", list) + } + + // edit with the version we have, then again with a stale one + resp, n2 := r.js("PUT", "/api/notes/"+id, map[string]any{"body": "remember the oat milk", "base": n["updated"]}) + if resp.StatusCode != 200 || n2["body"] != "remember the oat milk" { + t.Fatalf("update: %d %v", resp.StatusCode, n2) + } + resp, c := r.js("PUT", "/api/notes/"+id, map[string]any{"body": "stale", "base": n["updated"]}) + if resp.StatusCode != 409 { + t.Fatalf("stale update: %d", resp.StatusCode) + } + if cur, _ := c["note"].(map[string]any); cur["body"] != "remember the oat milk" { + t.Errorf("conflict should return the current note: %v", c) + } + if resp, _ := r.js("PUT", "/api/notes/"+id, map[string]any{"pinned": true}); resp.StatusCode != 200 { + t.Errorf("pin: %d", resp.StatusCode) + } + _, list = r.js("GET", "/api/notes?view=pinned", nil) + if list["total"] != float64(1) { + t.Errorf("pinned view: %v", list) + } + + if resp, _ := r.js("DELETE", "/api/notes/"+id+"?purge=1", nil); resp.StatusCode != 400 { + t.Errorf("purge of a live note: %d", resp.StatusCode) + } + if resp, _ := r.js("DELETE", "/api/notes/"+id, nil); resp.StatusCode != 200 { + t.Errorf("trash: %d", resp.StatusCode) + } + _, sum := r.js("GET", "/api/summary", nil) + if counts := sum["counts"].(map[string]any); counts["trash"] != float64(1) || counts["all"] != float64(0) { + t.Errorf("summary after trashing: %v", sum) + } + if resp, _ := r.js("POST", "/api/notes/"+id+"/restore", nil); resp.StatusCode != 200 { + t.Errorf("restore: %d", resp.StatusCode) + } + r.js("DELETE", "/api/notes/"+id, nil) + if resp, _ := r.js("DELETE", "/api/trash", nil); resp.StatusCode != 200 { + t.Errorf("empty trash: %d", resp.StatusCode) + } + if resp, _ := r.js("GET", "/api/notes/"+id, nil); resp.StatusCode != 404 { + t.Errorf("gone: %d", resp.StatusCode) + } + for _, bad := range []string{"..%2f..%2fbasking.json", "UPPER", "a.b"} { + if resp, _ := r.js("GET", "/api/notes/"+bad, nil); resp.StatusCode != 404 { + t.Errorf("odd id %q: %d", bad, resp.StatusCode) + } + } +} + +func TestDailyEndpoint(t *testing.T) { + r := newRig(t) + r.signIn() + resp, n := r.js("POST", "/api/daily", map[string]string{"date": "2026-10-31"}) + if resp.StatusCode != 200 || n["id"] != "daily-2026-10-31" { + t.Fatalf("%d %v", resp.StatusCode, n) + } + if resp, _ := r.js("POST", "/api/daily", map[string]string{"date": "yesterday"}); resp.StatusCode != 400 { + t.Errorf("bad date: %d", resp.StatusCode) + } +} + +func TestTokensAndPlainTextCapture(t *testing.T) { + r := newRig(t) + r.signIn() + resp, tok := r.js("POST", "/api/tokens", map[string]string{"name": "phone"}) + if resp.StatusCode != 200 { + t.Fatalf("new token: %d %v", resp.StatusCode, tok) + } + token := tok["token"].(string) + if !strings.HasPrefix(token, "bsk_") { + t.Fatalf("token %q", token) + } + if resp, _ := r.js("POST", "/api/tokens", map[string]string{"name": "PHONE"}); resp.StatusCode != 400 { + t.Errorf("duplicate name: %d", resp.StatusCode) + } + if b, _ := json.Marshal(r.cfg.Get()); strings.Contains(string(b), token) { + t.Error("the token itself must not be stored") + } + + // a script with only the token: no cookie, no CSRF header, plain text body + api := &rig{t: t, srv: r.srv, cfg: r.cfg} + resp, b := api.do("POST", "/api/notes", "text/plain", "call mum #family", "Authorization", "Bearer "+token) + if resp.StatusCode != 201 { + t.Fatalf("capture: %d %s", resp.StatusCode, b) + } + if !strings.Contains(string(b), `"body":"call mum #family"`) { + t.Errorf("body %s", b) + } + if resp, _ := api.do("POST", "/api/notes", "text/plain", "x", "Authorization", "Bearer bsk_wrong"); resp.StatusCode != 401 { + t.Errorf("wrong token: %d", resp.StatusCode) + } + // a token cannot mint more tokens + if resp, _ := api.do("POST", "/api/tokens", "application/json", `{"name":"x"}`, "Authorization", "Bearer "+token); resp.StatusCode != 403 { + t.Errorf("token making a token: %d", resp.StatusCode) + } + + _, list := r.js("GET", "/api/tokens", nil) + _ = list + if resp, _ := r.js("DELETE", "/api/tokens/phone", nil); resp.StatusCode != 200 { + t.Errorf("revoke: %d", resp.StatusCode) + } + if resp, _ := api.do("GET", "/api/notes", "", "", "Authorization", "Bearer "+token); resp.StatusCode != 401 { + t.Errorf("revoked token: %d", resp.StatusCode) + } +} + +func TestChangePassword(t *testing.T) { + r := newRig(t) + r.signIn() + old := r.cookie + if resp, _ := r.js("POST", "/api/password", map[string]string{"current": "wrong", "new": "a brand new password"}); resp.StatusCode != 403 { + t.Errorf("wrong current: %d", resp.StatusCode) + } + if resp, _ := r.js("POST", "/api/password", map[string]string{"current": password, "new": "short"}); resp.StatusCode != 400 { + t.Errorf("short: %d", resp.StatusCode) + } + resp, _ := r.js("POST", "/api/password", map[string]string{"current": password, "new": "a brand new password"}) + if resp.StatusCode != 200 { + t.Fatalf("change: %d", resp.StatusCode) + } + // the old cookie stops working, the new one (sent back in the response) works + if resp2, _ := r.do("GET", "/api/me", "", ""); resp2.StatusCode != 401 { + t.Errorf("old cookie after a password change: %d", resp2.StatusCode) + } + for _, c := range resp.Cookies() { + if c.Name == cookieName { + r.cookie = c + } + } + if r.cookie == old { + t.Fatal("no new cookie") + } + if resp2, _ := r.do("GET", "/api/me", "", ""); resp2.StatusCode != 200 { + t.Errorf("new cookie: %d", resp2.StatusCode) + } +} + +func TestExport(t *testing.T) { + r := newRig(t) + r.signIn() + r.js("POST", "/api/notes", map[string]string{"body": "one"}) + r.js("POST", "/api/notes", map[string]string{"body": "two"}) + resp, b := r.do("GET", "/api/export", "", "") + if resp.StatusCode != 200 || !strings.Contains(resp.Header.Get("Content-Disposition"), "attachment") { + t.Fatalf("export: %d %v", resp.StatusCode, resp.Header) + } + zr, err := zip.NewReader(bytes.NewReader(b), int64(len(b))) + if err != nil || len(zr.File) != 2 { + t.Fatalf("zip: %v, %d files", err, len(zr.File)) + } +} + +func TestFailCapitalizes(t *testing.T) { + w := httptest.NewRecorder() + fail(w, 400, "écrit mal") + if !strings.Contains(w.Body.String(), "Écrit mal") { + t.Errorf("%s", w.Body.String()) + } +} diff --git a/scripts/lint-design.mjs b/scripts/lint-design.mjs new file mode 100644 index 0000000..0a6a031 --- /dev/null +++ b/scripts/lint-design.mjs @@ -0,0 +1,47 @@ +// Style gate for the dashboard, following the Lightwell design language (see docs/design.md). +// Fails on em or en dashes, emoji, exclamation marks in interface copy, and raw colors outside the +// token block of the dashboard stylesheet. Run with: node scripts/lint-design.mjs +import { readdirSync, readFileSync, statSync } from "node:fs"; +import { extname, join, relative, sep } from "node:path"; + +const root = new URL("..", import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, "$1"); +const skipDirs = new Set(["node_modules", ".git", "dist", "bin", "data"]); +const exts = new Set([".html", ".css", ".js", ".md", ".go", ".sh", ".mjs", ".json", ".yml"]); + +const dash = /[–—−]/; +const emoji = /[\u{1F300}-\u{1FAFF}\u{2600}-\u{27BF}\u{1F900}-\u{1F9FF}\u{FE0F}\u{2B50}\u{2705}\u{274C}]/u; +const rawColor = /#[0-9a-fA-F]{3,8}\b|\brgba?\(|\bhsla?\(/; + +function* walk(p) { + const st = statSync(p, { throwIfNoEntry: false }); + if (!st) return; + if (st.isFile()) return yield p; + for (const name of readdirSync(p)) if (!skipDirs.has(name)) yield* walk(join(p, name)); +} + +const problems = []; +for (const file of walk(root)) { + if (!exts.has(extname(file))) continue; + const rel = relative(root, file).split(sep).join("/"); + if (rel === "scripts/lint-design.mjs" || rel === "basking.json") continue; + const isPage = rel.startsWith("internal/web/static/"); + let inTokens = false; + readFileSync(file, "utf8").split("\n").forEach((line, i) => { + const at = (msg) => problems.push(`${rel}:${i + 1}: ${msg}`); + if (line.includes("tokens:begin")) inTokens = true; + if (dash.test(line)) at("em dash, en dash or minus sign. Use a comma, colon, period or the word to."); + if (emoji.test(line)) at("emoji or pictographic symbol. Use an approved outline icon."); + if (isPage && !inTokens && rawColor.test(line.replace(/\/\*.*?\*\//g, "")) && !/placeholder|href="#|&#/.test(line)) { + at("raw color outside the token block. Use a token."); + } + if (isPage && /(?:toast|textContent\s*=|class:\s*'(?:empty|notice)')[^;\n]*'[^']*[A-Za-z]![^']*'/.test(line)) at("exclamation mark in interface copy."); + if (line.includes("tokens:end")) inTokens = false; + }); +} + +if (problems.length) { + console.error(problems.join("\n")); + console.error(`\n${problems.length} style problem(s).`); + process.exit(1); +} +console.log("Style checks passed.");