├── .gitattributes ├── .gitignore ├── LICENSE ├── ShortLink App Plan.pdf ├── architecture.md ├── favicon.ico ├── go.mod ├── go.sum ├── handler └── handler.go ├── helper ├── constant.go └── functions.go ├── main.go ├── readme.md └── rest.http /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /db/badger 3 | /db/pogreb -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /ShortLink App Plan.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/go-awesome/shortlink/9dd9c43e4fce7f861fcb2d972f482e83b7a7a068/ShortLink App Plan.pdf -------------------------------------------------------------------------------- /architecture.md: -------------------------------------------------------------------------------- 1 | We have multiple option here based on traffic in-flow 2 | 3 | ### Single Server: 4 | 5 | - Running combined main binary and forget everything 6 | 7 | ### Or, Separate DB server: 8 | 9 | - We will compile and run "db" app separately which will take request from the remote/local app for read and write operation 10 | - We can therefore execute multiple instance of app in shared basis architecture for the app "read", "write" operation. 11 | 12 | ### Or, Centralized DB server with multiple machine for API: 13 | 14 | - Each machine has ID hard coded in the constant variable inside `constant.go` or we can also get one from `os.Getenv("MachineID")` 15 | - Runing multiple instance of server with different Machine ID 16 | - Running centralized db server 17 | 18 | ### Or, Each Node own DB server with multiple machine for API. 19 | 20 | Limitation: Each URL has analytics different on each Node as each one of them has their own DB. Useful to run on region based deployment 21 | 22 | - Each machine has ID hard coded in the constant variable or we can also get one from 'os.Getenv("MachineID")' 23 | - Runing multiple instance of server with different Machine ID 24 | - Each Instance has their own DB file with unique identifier for "unique id" with value of "Machine ID" 25 | - When "Analytics" fetch, check uniqueID - Machine ID value and then communicating with the "Fetched" machine server. -------------------------------------------------------------------------------- /favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/go-awesome/shortlink/9dd9c43e4fce7f861fcb2d972f482e83b7a7a068/favicon.ico -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/go-awesome/shortlink 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/akrylysov/pogreb v0.10.1 7 | github.com/dgraph-io/badger/v3 v3.2011.1 8 | github.com/gofiber/fiber/v2 v2.52.5 9 | github.com/rs/xid v1.3.0 10 | github.com/speps/go-hashids/v2 v2.0.1 11 | ) 12 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 3 | github.com/DataDog/zstd v1.4.1 h1:3oxKN3wbHibqx897utPC2LTQU4J+IHWWJO+glkAkpFM= 4 | github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo= 5 | github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= 6 | github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= 7 | github.com/akrylysov/pogreb v0.10.1 h1:FqlR8VR7uCbJdfUob916tPM+idpKgeESDXOA1K0DK4w= 8 | github.com/akrylysov/pogreb v0.10.1/go.mod h1:pNs6QmpQ1UlTJKDezuRWmaqkgUE2TuU0YTWyqJZ7+lI= 9 | github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= 10 | github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= 11 | github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= 12 | github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= 13 | github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= 14 | github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= 15 | github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= 16 | github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= 17 | github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= 18 | github.com/cosiner/argv v0.1.0/go.mod h1:EusR6TucWKX+zFgtdUsKT2Cvg45K5rtpCcWz4hK06d8= 19 | github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= 20 | github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 21 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 22 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 23 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 24 | github.com/dgraph-io/badger/v3 v3.2011.1 h1:Hmyof0WMEF/QtutX5SQHzIMnJQxb/IrSzhjckV2SD6g= 25 | github.com/dgraph-io/badger/v3 v3.2011.1/go.mod h1:0rLLrQpKVQAL0or/lBLMQznhr6dWWX7h5AKnmnqx268= 26 | github.com/dgraph-io/ristretto v0.0.4-0.20210122082011-bb5d392ed82d h1:eQYOG6A4td1tht0NdJB9Ls6DsXRGb2Ft6X9REU/MbbE= 27 | github.com/dgraph-io/ristretto v0.0.4-0.20210122082011-bb5d392ed82d/go.mod h1:tv2ec8nA7vRpSYX7/MbP52ihrUMXIHit54CQMq8npXQ= 28 | github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA= 29 | github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= 30 | github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= 31 | github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= 32 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 33 | github.com/go-delve/delve v1.5.0/go.mod h1:c6b3a1Gry6x8a4LGCe/CWzrocrfaHvkUxCj3k4bvSUQ= 34 | github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo= 35 | github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= 36 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= 37 | github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I= 38 | github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= 39 | github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= 40 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 41 | github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= 42 | github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 43 | github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= 44 | github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= 45 | github.com/google/flatbuffers v1.12.0 h1:/PtAHvnBY4Kqnx/xCQ3OIV9uYcSFGScBsWI3Oogeh6w= 46 | github.com/google/flatbuffers v1.12.0/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= 47 | github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 48 | github.com/google/go-dap v0.2.0/go.mod h1:5q8aYQFnHOAZEMP+6vmq25HKYAEwE+LF5yh7JKrrhSQ= 49 | github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= 50 | github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 51 | github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= 52 | github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= 53 | github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= 54 | github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM= 55 | github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= 56 | github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 57 | github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 58 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 59 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 60 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= 61 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 62 | github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= 63 | github.com/mattn/go-colorable v0.0.0-20170327083344-ded68f7a9561/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= 64 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= 65 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 66 | github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= 67 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 68 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 69 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 70 | github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= 71 | github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 72 | github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= 73 | github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= 74 | github.com/mmcloughlin/avo v0.0.0-20201105074841-5d2f697d268f/go.mod h1:6aKT4zZIrpGqB3RpFU14ByCSSyKY6LfJz4J/JJChHfI= 75 | github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= 76 | github.com/peterh/liner v0.0.0-20170317030525-88609521dc4b/go.mod h1:xIteQHvHuaLYG9IFj6mSxM0fCKrs34IrEQUhOYuGPHc= 77 | github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= 78 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 79 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 80 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 81 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 82 | github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= 83 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 84 | github.com/rs/xid v1.3.0 h1:6NjYksEUlhurdVehpc7S7dk6DAmcKv8V9gG0FsVN2U4= 85 | github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= 86 | github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= 87 | github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= 88 | github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= 89 | github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= 90 | github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= 91 | github.com/speps/go-hashids/v2 v2.0.1 h1:ViWOEqWES/pdOSq+C1SLVa8/Tnsd52XC34RY7lt7m4g= 92 | github.com/speps/go-hashids/v2 v2.0.1/go.mod h1:47LKunwvDZki/uRVD6NImtyk712yFzIs3UF3KlHohGw= 93 | github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= 94 | github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= 95 | github.com/spf13/cobra v0.0.0-20170417170307-b6cb39589372/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= 96 | github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= 97 | github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= 98 | github.com/spf13/pflag v0.0.0-20170417173400-9e4c21054fa1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= 99 | github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= 100 | github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= 101 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 102 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 103 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 104 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 105 | github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw= 106 | github.com/twitchyliquid64/golang-asm v0.15.0/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= 107 | github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= 108 | github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 109 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 110 | github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= 111 | github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= 112 | github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= 113 | github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= 114 | github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= 115 | github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= 116 | github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 117 | go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0= 118 | go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= 119 | go.starlark.net v0.0.0-20190702223751-32f345186213/go.mod h1:c1/X6cHgvdXj6pUlmWKMkuqRnW4K8x2vwt6JAaaircg= 120 | golang.org/x/arch v0.0.0-20190927153633-4e8777c89be4/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4= 121 | golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4= 122 | golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 123 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 124 | golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 125 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 126 | golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 127 | golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= 128 | golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= 129 | golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 130 | golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= 131 | golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 132 | golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= 133 | golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 134 | golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 135 | golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 136 | golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 137 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 138 | golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 139 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 140 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 141 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 142 | golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= 143 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 144 | golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 145 | golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= 146 | golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= 147 | golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= 148 | golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= 149 | golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= 150 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 151 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 152 | golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 153 | golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 154 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 155 | golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 156 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 157 | golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 158 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 159 | golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 160 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 161 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 162 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 163 | golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 164 | golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 165 | golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 166 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 167 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 168 | golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 169 | golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 170 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 171 | golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 172 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 173 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 174 | golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 175 | golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 176 | golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= 177 | golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 178 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 179 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 180 | golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= 181 | golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= 182 | golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= 183 | golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= 184 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 185 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 186 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 187 | golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 188 | golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 189 | golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= 190 | golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= 191 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 192 | golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 193 | golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= 194 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 195 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 196 | golang.org/x/tools v0.0.0-20191127201027-ecd32218bd7f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 197 | golang.org/x/tools v0.0.0-20201105001634-bc3cf281b174/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= 198 | golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= 199 | golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= 200 | golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= 201 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 202 | golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 203 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 204 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 205 | google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= 206 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 207 | google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= 208 | google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= 209 | google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= 210 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 211 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= 212 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 213 | gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 214 | gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= 215 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 216 | honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 217 | rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= 218 | -------------------------------------------------------------------------------- /handler/handler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "encoding/json" 5 | "strconv" 6 | "strings" 7 | "time" 8 | 9 | "github.com/go-awesome/shortlink/helper" 10 | 11 | "github.com/akrylysov/pogreb" 12 | "github.com/dgraph-io/badger/v3" 13 | "github.com/gofiber/fiber/v2" 14 | "github.com/rs/xid" 15 | ) 16 | 17 | func IndexHandler(c *fiber.Ctx) error { 18 | return c.JSON(fiber.Map{"node": helper.NodeID, "message": "Short URL Service Provider"}) 19 | } 20 | 21 | func CreateHandler(n int, bdb *badger.DB, db *pogreb.DB) fiber.Handler { 22 | return func(c *fiber.Ctx) error { 23 | post := new(helper.CreateURL) 24 | if err := c.BodyParser(&post); err != nil { 25 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.CO101)}) 26 | } 27 | // should do better URL validation here 28 | if !helper.ValidateURL(post.URL) { 29 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": "Invalid URL | Minimum URL Length should be 5"}) 30 | } 31 | // Get the API Token from Authorization Bearer Header 32 | token := helper.ParseToken(c.Get("Authorization")) 33 | 34 | // Basic Checking without Real time API Token checking 35 | if len(token) != helper.APITokenLength { 36 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(helper.ID103, helper.ID103)}) 37 | } 38 | msgTime := time.Now().Format("2006-01-02-15:04:05") 39 | // Create MD5 Hash of URL 40 | md5URL := helper.CreateMD5Hash(post.URL) 41 | // Check if user already has the URL 42 | val, err := helper.FindBDB([]byte(token+"-"+md5URL), bdb) 43 | if err == nil { 44 | return c.JSON(fiber.Map{"error": "false", "message": val}) 45 | } 46 | // Checking increment of N variable for new ShortID Token 47 | if n == 1 { 48 | num, err := helper.FindBDB([]byte("lastID"), bdb) 49 | if err != nil && err.Error() == "Key not found" { 50 | // do nothing as it is first entry 51 | } else if err != nil && err.Error() != "Key not found" { 52 | return c.Status(400).JSON(fiber.Map{"error": "true", "Message": helper.ErrorPrint(err.Error(), helper.ID101)}) 53 | } else { 54 | byteToInt, err := strconv.Atoi(num) 55 | if err != nil { 56 | return c.Status(400).JSON(fiber.Map{"error": "true", "Message": helper.ErrorPrint(err.Error(), helper.ID102)}) 57 | } 58 | if byteToInt > 1 { 59 | n = byteToInt + 1 60 | } 61 | } 62 | } 63 | 64 | // Generate Short Token ID for given index n 65 | shortID, err := helper.GenerateToken(token, n) 66 | if err != nil { 67 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID104)}) 68 | } 69 | 70 | // json for storage 71 | jstore, err := json.Marshal(fiber.Map{"createdAt": msgTime, "shortURL": helper.Domain + shortID, "longURL": post.URL}) 72 | if err != nil { 73 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID104)}) 74 | } 75 | /** 76 | * Now Create 2 KEY "API+md of URL" and "shortID" 77 | * One key will be used to populate list of all URL done by API Holder 78 | * Second key will be for redirect 79 | */ 80 | // Stored in Pogreb Database 81 | err = helper.PutDB([]byte(shortID), []byte(post.URL), nil, db) 82 | if err != nil { 83 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID104)}) 84 | } 85 | 86 | // Stored in Badger Database 87 | err = helper.PutBDB([]byte(token+"-"+md5URL), jstore, nil, bdb) 88 | if err != nil { 89 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID104)}) 90 | } 91 | // Increment the last ID and update the database 92 | err = helper.PutBDB([]byte("lastID"), []byte(strconv.Itoa(n)), []byte(token+"-"+md5URL), bdb) 93 | if err != nil { 94 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID104)}) 95 | } 96 | n++ 97 | return c.JSON(fiber.Map{"error": "false", "message": string(jstore)}) 98 | } 99 | } 100 | 101 | func FetchAllHandler(bdb *badger.DB, db *pogreb.DB) fiber.Handler { 102 | return func(c *fiber.Ctx) error { 103 | token := helper.ParseToken(c.Get("Authorization")) 104 | // Basic Checking without Real time API Token checking 105 | if len(token) != helper.APITokenLength { 106 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ID103}) 107 | } 108 | skey := make([]string, 0) 109 | bdb.View(func(txn *badger.Txn) error { 110 | it := txn.NewIterator(badger.DefaultIteratorOptions) 111 | defer it.Close() 112 | prefix := []byte(token + "-") 113 | for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() { 114 | item := it.Item() 115 | // k := item.Key() 116 | err := item.Value(func(v []byte) error { 117 | skey = append(skey, string(v)) 118 | return nil 119 | }) 120 | if err != nil { 121 | return err 122 | } 123 | } 124 | return nil 125 | }) 126 | jstore, _ := json.Marshal(skey) 127 | return c.JSON(fiber.Map{"error": "false", "message": string(jstore)}) 128 | } 129 | } 130 | 131 | func UpdateHandler(bdb *badger.DB, db *pogreb.DB) fiber.Handler { 132 | return func(c *fiber.Ctx) error { 133 | post := new(helper.UpdateURL) 134 | if err := c.BodyParser(&post); err != nil { 135 | return c.Status(400).Send([]byte(err.Error())) 136 | } 137 | if !helper.ValidateURL(post.NURL) { 138 | return c.JSON(fiber.Map{"error": "true", "message": "Invalid New URL"}) 139 | } 140 | // Get the API Token from Authorization Bearer Header 141 | token := helper.ParseToken(c.Get("Authorization")) 142 | 143 | // Basic Checking without Real time API Token checking 144 | if len(token) != helper.APITokenLength { 145 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(helper.ID103, helper.ID103)}) 146 | } 147 | md5URL := helper.CreateMD5Hash(post.OURL) 148 | // Check if user already has the URL 149 | oldVal, err := helper.FindBDB([]byte(token+"-"+md5URL), bdb) 150 | if err != nil { 151 | return c.JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID108)}) 152 | } 153 | 154 | val, err := helper.FindDB([]byte(post.ShortID), db) 155 | if err != nil { 156 | return c.JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID105)}) 157 | } 158 | if val != post.OURL { 159 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(helper.ID103, helper.ID108)}) 160 | } 161 | 162 | Newmd5URL := helper.CreateMD5Hash(post.NURL) 163 | // Check if user already has the URL 164 | _, err = helper.FindBDB([]byte(token+"-"+Newmd5URL), bdb) 165 | if err == nil { 166 | return c.JSON(fiber.Map{"error": "true", "message": helper.ID109}) 167 | } 168 | // Update OLD Database 169 | err = helper.PutBDB([]byte(token+"-"+Newmd5URL), []byte(oldVal), []byte(token+"-"+md5URL), bdb) 170 | if err != nil { 171 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID104)}) 172 | } 173 | // Update New Redirect 174 | err = helper.PutDB([]byte(post.ShortID), []byte(post.NURL), nil, db) 175 | if err != nil { 176 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID104)}) 177 | } 178 | 179 | return c.JSON(fiber.Map{"error": "false", "message": "Successfully Updated"}) 180 | } 181 | } 182 | 183 | func FetchSingleHandler(bdb *badger.DB, db *pogreb.DB) fiber.Handler { 184 | return func(c *fiber.Ctx) error { 185 | code := c.Params("code") 186 | code = strings.Replace(code, "%7C", "|", -1) 187 | if len(code) > helper.ShortIDToken { 188 | token := helper.ParseToken(c.Get("Authorization")) 189 | // Basic Checking without Real time API Token checking 190 | if len(token) != helper.APITokenLength { 191 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ID103}) 192 | } 193 | val, err := helper.FindDB([]byte(code), db) 194 | if err != nil { 195 | return c.JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID105)}) 196 | } 197 | traffic := make(map[string]int) 198 | table := make(map[string]string) 199 | bdb.View(func(txn *badger.Txn) error { 200 | opts := badger.DefaultIteratorOptions 201 | opts.PrefetchValues = false 202 | it := txn.NewIterator(opts) 203 | defer it.Close() 204 | prefix := []byte(code + "-") 205 | for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() { 206 | item := it.Item() 207 | k := item.Key() 208 | split := strings.Split(string(k), "-|-") 209 | if len(split) == 3 { 210 | if _, present := traffic["total"]; !present { 211 | traffic["total"] = 1 212 | } else { 213 | traffic["total"] += 1 214 | } 215 | timer := strings.Split(split[2], "-") 216 | if _, present := table[split[1]]; !present { 217 | table[split[1]] = "done" 218 | traffic["unique"] += 1 219 | if _, present := traffic["unique-"+timer[0]+"-"+timer[1]+"-"+timer[2]]; !present { 220 | traffic["unique-"+timer[0]+"-"+timer[1]+"-"+timer[2]] = 1 221 | } else { 222 | traffic["unique-"+timer[0]+"-"+timer[1]+"-"+timer[2]] += 1 223 | } 224 | } 225 | if _, present := traffic[timer[0]+"-"+timer[1]+"-"+timer[2]]; !present { 226 | traffic[timer[0]+"-"+timer[1]+"-"+timer[2]] = 1 227 | } else { 228 | traffic[timer[0]+"-"+timer[1]+"-"+timer[2]] += 1 229 | } 230 | } 231 | } 232 | return nil 233 | }) 234 | return c.JSON(fiber.Map{"error": "false", "message": traffic, "longurl": val}) 235 | } 236 | return c.JSON(fiber.Map{"error": "true", "message": "Invalid Short Code Length"}) 237 | } 238 | } 239 | 240 | func DeleteHandler(bdb *badger.DB, db *pogreb.DB) fiber.Handler { 241 | return func(c *fiber.Ctx) error { 242 | post := new(helper.DeleteURL) 243 | if err := c.BodyParser(&post); err != nil { 244 | return c.Status(400).Send([]byte(err.Error())) 245 | } 246 | if !helper.ValidateURL(post.URL) { 247 | return c.JSON(fiber.Map{"error": "true", "message": "Invalid URL"}) 248 | } 249 | // Get the API Token from Authorization Bearer Header 250 | token := helper.ParseToken(c.Get("Authorization")) 251 | 252 | // Basic Checking without Real time API Token checking 253 | if len(token) != helper.APITokenLength { 254 | return c.Status(400).JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(helper.ID103, helper.ID103)}) 255 | } 256 | md5URL := helper.CreateMD5Hash(post.URL) 257 | // Check if user already has the URL 258 | _, err := helper.FindBDB([]byte(token+"-"+md5URL), bdb) 259 | if err != nil { 260 | return c.JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID107)}) 261 | } 262 | // check the shortID, avoid delete wrong link. 263 | if len(post.ShortID) > helper.ShortIDToken { 264 | code := strings.Replace(post.ShortID, "%7C", "|", -1) 265 | val, err := helper.FindDB([]byte(code), db) 266 | if err != nil { 267 | return c.JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID107)}) 268 | } 269 | if val != post.URL { 270 | return c.JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(helper.ID107, helper.ID107)}) 271 | } 272 | } 273 | // delete short URL 274 | err = db.Delete([]byte(post.ShortID)) 275 | if err != nil { 276 | return c.JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID107)}) 277 | } 278 | // delete "token-md5url" + "analytics" data 279 | err = bdb.Update(func(txn *badger.Txn) error { 280 | err = txn.Delete([]byte(token + "-" + md5URL)) 281 | it := txn.NewIterator(badger.DefaultIteratorOptions) 282 | defer it.Close() 283 | prefix := []byte(post.ShortID + "-") 284 | for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() { 285 | item := it.Item() 286 | k := item.Key() 287 | txn.Delete(k) 288 | } 289 | return err 290 | }) 291 | if err != nil { 292 | return c.JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID107)}) 293 | } 294 | return c.JSON(fiber.Map{"error": "false", "message": "Successfully deleted"}) 295 | } 296 | } 297 | 298 | func RedirectToMeWebsite(db *pogreb.DB, bdb *badger.DB) fiber.Handler { 299 | return func(c *fiber.Ctx) error { 300 | msgTime := time.Now().Format("2006-01-02-15:04:05") 301 | code := c.Params("code") 302 | if len(code) > helper.ShortIDToken { 303 | code = strings.Replace(code, "%7C", "|", -1) 304 | val, err := helper.FindDB([]byte(code), db) 305 | if err != nil { 306 | return c.JSON(fiber.Map{"error": "true", "message": helper.ErrorPrint(err.Error(), helper.ID105)}) 307 | } 308 | if !helper.ValidateURL(val) { 309 | return c.JSON(fiber.Map{"error": "true", "message": helper.ID106}) 310 | } 311 | // Do Cookie Way + IP Way 312 | getCookie := c.Cookies(helper.CookieName, "no") 313 | if len(getCookie) > 2 { 314 | checkIfExist := helper.CheckBDB([]byte(code+"-|-"+getCookie+"-|-"), bdb) 315 | if !checkIfExist { 316 | guid := xid.New() 317 | getCookie = guid.String() 318 | c.Cookie(&fiber.Cookie{ 319 | Name: helper.CookieName, 320 | Value: getCookie, 321 | Expires: time.Now().Add(365 * 24 * time.Hour), 322 | HTTPOnly: true, 323 | SameSite: "strict", 324 | }) 325 | } 326 | go helper.PutBDB([]byte(code+"-|-"+getCookie+"-|-"+msgTime), nil, nil, bdb) 327 | } else { 328 | guid := xid.New() 329 | cookieToken := guid.String() 330 | c.Cookie(&fiber.Cookie{ 331 | Name: helper.CookieName, 332 | Value: cookieToken, 333 | Expires: time.Now().Add(365 * 24 * time.Hour), 334 | HTTPOnly: true, 335 | SameSite: "strict", 336 | }) 337 | // when we will be spliting the string with "|", we will have node,code,ip,date 338 | go helper.PutBDB([]byte(code+"-|-"+cookieToken+"-|-"+msgTime), nil, nil, bdb) 339 | } 340 | return c.Redirect(val) 341 | } 342 | return c.JSON(fiber.Map{"error": "true", "message": helper.ID106}) 343 | } 344 | } 345 | -------------------------------------------------------------------------------- /helper/constant.go: -------------------------------------------------------------------------------- 1 | package helper 2 | 3 | const ( 4 | Production = 2 // Please set to 1 if in production. 5 | Domain = "https://lin.ks/" 6 | CookieName = "lin.ks" 7 | NodeID = "N1|" // Increase per node by value as "N2|", "N3|"... for multiple node 8 | DBFolder = "/home/ubuntu/go/src/shortlink/db" // Without trailing slash at the end. 9 | AddFromToken = 3 // firt N character to get from token and use it in ShortID 10 | ShortIDToken = 7 // Further added from 1st N char of AddFromToken+NodeID: total=12 11 | APITokenLength = 32 12 | BypassLockGuard = false // set to true if DB is read from multiple instance. 13 | DB101 = "Failed to Load Read-Heavy database, Please try again!" 14 | DB102 = "Failed to Load Write-Heavy database, Please try again!" 15 | CO101 = "Error CO101: Something went wrong! Please try again!" 16 | ID101 = "Error ID101: Can not create" 17 | ID102 = "Error ID102: Can not create due to Numbher issue" 18 | ID103 = "Error ID103: Authorization Token Missing" 19 | ID104 = "Error ID104: Failed to generate Short ID, Please try again!" 20 | ID105 = "Error ID105: Can't fetch Short URL" 21 | ID106 = "Error ID106: Invalid Short URL" 22 | ID107 = "Error ID107: Failed to Delete, Please try again" 23 | ID108 = "Error ID108: Invalid Old Long URL" 24 | ID109 = "Error ID109: Provided Long URL already exists in your Account" 25 | ) 26 | -------------------------------------------------------------------------------- /helper/functions.go: -------------------------------------------------------------------------------- 1 | package helper 2 | 3 | import ( 4 | "crypto/md5" 5 | "encoding/hex" 6 | vurl "net/url" 7 | "strings" 8 | 9 | "github.com/akrylysov/pogreb" 10 | "github.com/dgraph-io/badger/v3" 11 | "github.com/speps/go-hashids/v2" 12 | ) 13 | 14 | // CreateURL 15 | type CreateURL struct { 16 | URL string `json:"url"` 17 | } 18 | 19 | // DeleteURL - Delete 20 | type DeleteURL struct { 21 | URL string `json:"long"` 22 | ShortID string `json:"short"` 23 | } 24 | 25 | // UpdateURL - Delete 26 | type UpdateURL struct { 27 | OURL string `json:"old"` 28 | NURL string `json:"new"` 29 | ShortID string `json:"short"` 30 | } 31 | 32 | // Ips 33 | func IPs(ips []string) string { 34 | return strings.Join(ips, "-") 35 | } 36 | 37 | // validateURL is basic and not 100% correct validate URL, but this can be starting place... 38 | func ValidateURL(url string) bool { 39 | if len(url) < 5 { 40 | return false 41 | } 42 | if _, err := vurl.ParseRequestURI(url); err != nil { 43 | return false 44 | } 45 | return true 46 | } 47 | 48 | // createMD5Hash md5 hash the URL 49 | func CreateMD5Hash(text string) string { 50 | hash := md5.Sum([]byte(text)) 51 | return hex.EncodeToString(hash[:]) 52 | } 53 | 54 | // generateToken generates uniqiue Token 55 | func GenerateToken(salt string, num int) (string, error) { 56 | hd := hashids.NewData() 57 | hd.Salt = salt 58 | hd.Alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" 59 | hd.MinLength = ShortIDToken 60 | h, err := hashids.NewWithData(hd) 61 | if err != nil { 62 | return "", err 63 | } 64 | e, err := h.Encode([]int{num}) 65 | if err != nil { 66 | return "", err 67 | } 68 | shortID := NodeID + string(salt[0:AddFromToken]) + e 69 | return shortID, nil 70 | } 71 | 72 | // errorPrint based on development or production server 73 | func ErrorPrint(devError string, prodError string) string { 74 | if Production == 1 { 75 | return prodError 76 | } 77 | return devError 78 | } 79 | 80 | // findDB is to get the key & value from badger database 81 | func FindDB(key []byte, db *pogreb.DB) (string, error) { 82 | // get the value from pogreb database 83 | val, err := db.Get(key) 84 | if err != nil { 85 | return "", err 86 | } 87 | return string(val), nil 88 | } 89 | 90 | // putDB is to store key & value in badger database 91 | func PutDB(key []byte, value []byte, ifDelete []byte, db *pogreb.DB) error { 92 | // save the value to pogreb database 93 | err := db.Put(key, value) 94 | if err != nil { 95 | if ifDelete != nil { 96 | // delete It if there is an error in saving record 97 | db.Delete(ifDelete) 98 | } 99 | return err 100 | } 101 | return nil 102 | } 103 | 104 | // findBDB is to get the key & value from badger database 105 | func FindBDB(key []byte, bdb *badger.DB) (string, error) { 106 | var valCopy []byte 107 | err := bdb.View(func(txn *badger.Txn) error { 108 | item, err := txn.Get(key) 109 | if err != nil { 110 | return err 111 | } else { 112 | valCopy, err = item.ValueCopy(nil) 113 | if err != nil { 114 | return err 115 | } 116 | } 117 | return nil 118 | }) 119 | if err != nil { 120 | return "", err 121 | } 122 | return string(valCopy), nil 123 | } 124 | 125 | // CheckBDB is to get the key & value from badger database 126 | func CheckBDB(key []byte, bdb *badger.DB) bool { 127 | skey := make([]string, 0) 128 | err := bdb.View(func(txn *badger.Txn) error { 129 | opts := badger.DefaultIteratorOptions 130 | opts.PrefetchValues = false 131 | it := txn.NewIterator(opts) 132 | defer it.Close() 133 | prefix := key 134 | for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() { 135 | item := it.Item() 136 | k := item.Key() 137 | skey = append(skey, string(k)) 138 | if len(skey) >= 1 { 139 | break 140 | } 141 | } 142 | return nil 143 | }) 144 | if err != nil { 145 | return false 146 | } 147 | if len(skey) >= 1 { 148 | return true 149 | } else { 150 | return false 151 | } 152 | } 153 | 154 | // putBDB is to store key & value in badger database 155 | func PutBDB(key []byte, value []byte, ifDelete []byte, bdb *badger.DB) error { 156 | err := bdb.Update(func(txn *badger.Txn) error { 157 | err := txn.Set(key, value) 158 | if err != nil { 159 | if ifDelete != nil { 160 | txn.Delete(ifDelete) 161 | } 162 | } 163 | return err 164 | }) 165 | if err != nil { 166 | return err 167 | } 168 | return nil 169 | } 170 | 171 | // parseToken - Parse Token from Authorization Header and get the Token 172 | func ParseToken(authToken string) string { 173 | if authToken == "" { 174 | return "" 175 | } 176 | bearer := strings.Split(authToken, "Bearer ") 177 | if len(bearer) != 2 { 178 | return "" 179 | } 180 | token := strings.TrimSpace(bearer[1]) 181 | if len(token) < 1 { 182 | return "" 183 | } 184 | return token 185 | } 186 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | "os/signal" 7 | "runtime" 8 | "time" 9 | 10 | "github.com/go-awesome/shortlink/handler" 11 | "github.com/go-awesome/shortlink/helper" 12 | 13 | "github.com/akrylysov/pogreb" 14 | badger "github.com/dgraph-io/badger/v3" 15 | "github.com/gofiber/fiber/v2" 16 | "github.com/gofiber/fiber/v2/middleware/favicon" 17 | "github.com/gofiber/fiber/v2/middleware/recover" 18 | ) 19 | 20 | func main() { 21 | runtime.GOMAXPROCS(runtime.NumCPU()) 22 | 23 | db, err := pogreb.Open(helper.DBFolder+"/pogreb/url.db", nil) 24 | if err != nil { 25 | log.Fatal(helper.ErrorPrint(err.Error(), helper.DB101)) 26 | return 27 | } 28 | defer db.Close() 29 | opts := badger.DefaultOptions(helper.DBFolder + "/badger") 30 | opts.NumVersionsToKeep = 1 31 | opts.ReadOnly = false 32 | 33 | if helper.BypassLockGuard { 34 | // When uisng centralized DB server, it will be read from many and write to 1 node. 35 | opts.BypassLockGuard = true 36 | } 37 | 38 | // Open badger Database 39 | bdb, err := badger.Open(opts) 40 | if err != nil { 41 | log.Fatal(helper.ErrorPrint(err.Error(), helper.DB102)) 42 | } 43 | defer bdb.Close() 44 | 45 | var n int = 1 46 | 47 | // Start the Gofiber APP 48 | app := fiber.New(fiber.Config{ 49 | ReadTimeout: 10 * time.Minute, 50 | WriteTimeout: 5 * time.Minute, 51 | Prefork: false, 52 | CaseSensitive: false, 53 | StrictRouting: true, 54 | DisableStartupMessage: true, 55 | ErrorHandler: func(c *fiber.Ctx, err error) error { 56 | code := fiber.StatusInternalServerError 57 | if e, ok := err.(*fiber.Error); ok { 58 | code = e.Code 59 | } 60 | return c.Status(code).JSON(fiber.Map{"error": "true", "Message": helper.ErrorPrint(err.Error(), helper.CO101)}) 61 | }, 62 | }) 63 | app.Use(recover.New(), favicon.New(favicon.Config{ 64 | File: "./favicon.ico", 65 | })) 66 | 67 | // Close the server and database on interruptor 68 | go func() { 69 | interruptor := make(chan os.Signal, 1) 70 | signal.Notify(interruptor, os.Interrupt) 71 | for range interruptor { 72 | app.Shutdown() 73 | db.Close() 74 | bdb.Close() 75 | os.Exit(1) 76 | } 77 | }() 78 | 79 | /** 80 | * fetch a short ID and redirect 81 | * @param `shortURL` from URL 82 | * @action also store analytics of the URL 83 | * @return redirect 84 | **/ 85 | 86 | api := app.Group("/api") 87 | 88 | // IndexHandler 89 | api.Get("/", handler.IndexHandler) 90 | 91 | /** 92 | * Store a new Long URL in storage. 93 | * @param `url` from JSON 94 | * @action generate unique short ID, increment n, store short id. 95 | * @return json response with error and successful message 96 | **/ 97 | 98 | api.Post("/create", handler.CreateHandler(n, bdb, db)) 99 | 100 | /** 101 | * Update a Long URL in storage. 102 | * @param `url` from JSON & Authorization token from Header 103 | * @action update database for long URL 104 | * @return json response with error and successful message 105 | */ 106 | 107 | api.Post("/update", handler.UpdateHandler(bdb, db)) 108 | 109 | /** 110 | * Fetch a list of Long URL in storage for the API Holder. 111 | * @param `Authorization Token` from Header 112 | * @action fetch entry list from database 113 | * @return json response with error and successful message with list 114 | */ 115 | 116 | api.Get("/fetch", handler.FetchAllHandler(bdb, db)) 117 | 118 | /** 119 | * Fetch detail analytics in storage by the API holder 120 | * @param `ShortID` from URL 121 | * @action fetch all entry from database 122 | * @return json response with error and successful message 123 | **/ 124 | 125 | api.Get("/fetch/:code", handler.FetchSingleHandler(bdb, db)) 126 | 127 | /** 128 | * Delete a Long URL in storage by the API holder 129 | * @param `helper.DeleteURL struct` from JSON 130 | * @action delete entry from database 131 | * @return json response with error and successful message 132 | **/ 133 | api.Post("/delete", handler.DeleteHandler(bdb, db)) 134 | 135 | app.Get("/:code", handler.RedirectToMeWebsite(db, bdb)) 136 | 137 | // run the server... 138 | app.Listen(":8080") 139 | } 140 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## About The Project 2 | 3 |
4 | 5 | Shortlink App in Golang 6 | 7 | * Multiple Node based Architecture to create and scale at ease 8 | * Highly performant key-value storage system 9 | * Centralized Storage option when multiple node created - requires tweaking. 10 | * **API auth system not built**. Left for using it for your own use case like `JWT` or `paseto`. Self Implement. 11 | 12 | Please see the `architecture` file in the repository on option you can use the app. For some minor tweaking may be required. 13 | 14 | ### Built With 15 | 16 | List of Library and Framework used in building the app: 17 | 18 | * [Gofiber](https://gofiber.io) 19 | * [BadgerDB](https://github.com/dgraph-io/badger) 20 | * [PogrebDB](https://github.com/akrylysov/pogreb) 21 | * [hashid](https://github.com/go-awesome/shortlink/blob/main/helper/functions.go#L11) 22 | * [xid](https://github.com/go-awesome/shortlink/blob/main/handler/handler.go#L13) 23 | 24 | 25 | 26 | ## Getting Started 27 | 28 | Just download and run `go run main.go` and you are ready to go. 29 | To deploy to server, prefer [DigitalOcean](https://m.do.co/c/5b28c38da55b) 30 | 31 | ### Steps 32 | 33 | Common Steps to Launch: 34 | 35 | ```sh 36 | go mod tidy 37 | go mod vendor 38 | go run main.go OR go build -ldflags "-s -w" main.go && ./main 39 | ``` 40 | 41 | ### Must Changeable Variables in `constant.go`: 42 | 43 | ``` 44 | Production = 2 // Please set to 1 if in production. 45 | Domain = "https://lin.ks/" 46 | CookieName = "lin.ks" 47 | NodeID = "N1|" // Increase per node by value as "N2|", "N3|"... for multiple node 48 | DBFolder = "/home/ubuntu/go/src/shortlink/db/" 49 | AddFromToken = 3 // firt N character to get from token and use it in ShortID 50 | ShortIDToken = 7 // Further added from 1st N char of AddFromToken+NodeID: total=12 51 | APITokenLength = 32 52 | ``` 53 | 54 |