├── .circleci └── config.yml ├── .github ├── README.adoc └── jetbrains-variant-4.png ├── .gitignore ├── .idea ├── .gitignore ├── dictionaries │ └── wincer.xml ├── diem.iml ├── misc.xml ├── modules.xml ├── sqldialects.xml ├── vcs.xml └── watcherTasks.xml ├── Dockerfile ├── LICENSE ├── config └── config.go ├── diem.toml ├── go.mod ├── go.sum ├── main.go ├── middleware ├── interface.go ├── limiting │ └── limiting.go ├── logger │ └── logger.go └── recovery │ └── recovery.go ├── models ├── bbolt.go ├── blogs │ └── model.go ├── ga.go ├── googleanalytics │ └── model.go ├── hitokoto │ ├── migrate_hitokoto.go │ └── model.go └── interface.go ├── rpcserver ├── client.go ├── client_test.go ├── conn.go ├── decode.go ├── decode_test.go ├── interface.go ├── io.go └── pool_v2.go ├── tools ├── dnslookup │ ├── dns.go │ └── dns_test.go ├── filefactory │ ├── file.go │ └── file_test.go ├── logfactory │ ├── levels.go │ ├── logrotate.go │ └── logrotate_test.go ├── tomlparser │ └── parser.go ├── tools.go └── tools_test.go └── views ├── blogsearch.go ├── gaviews.go ├── hitokoto.go └── interface.go /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | defaults: &defaults 2 | docker: 3 | - image: circleci/golang:1.14.2 4 | 5 | version: 2 6 | 7 | jobs: 8 | build: 9 | <<: *defaults 10 | steps: 11 | - checkout 12 | - run: 13 | name: Build 14 | command: | 15 | go build -o server 16 | - save_cache: 17 | key: hitokoto-v1-{{ checksum "config_sample.yaml" }} 18 | paths: 19 | - server 20 | 21 | workflows: 22 | version: 2 23 | 24 | Hito-CI: 25 | jobs: 26 | - build 27 | -------------------------------------------------------------------------------- /.github/README.adoc: -------------------------------------------------------------------------------- 1 | = DIEM API 2 | WincerChan 3 | 4 | image:https://img.shields.io/circleci/project/github/WincerChan/Meme-generator.svg?style=flat-square[CircleCI, link=https://circleci.com/gh/WincerChan/Hitokoto/tree/master] 5 | image:https://img.shields.io/badge/License-GPL%20v3-blue.svg?style=flat-square[License: GPL v3, https://www.gnu.org/licenses/gpl-3.0] 6 | image:https://img.shields.io/github/languages/code-size/WincerChan/Hitokoto.svg?style=flat-square[GitHub code size in bytes] 7 | 8 | 9 | 目前系统有如下 API: 10 | 11 | . 一言(Hitokoto) 12 | . 网易云音乐(Cloudmusic) 13 | . Google 分析(Google Analytics)(这条并没有什么卵用 14 | . 博客搜索的 API(仅作流量转发和字段验证) 15 | 16 | 有关这些 API 的具体信息请移步至 https://api.itswincer.com[API 的文档](文档包含展示及测试模块,GitHub 无法展示)。 17 | 18 | == 部署 19 | 20 | 以下依赖都是编译时依赖,并非是运行时的依赖: 21 | 22 | . Elixir (限流模块使用,二者使用 Unix Domain Socket 通信,可以通过配置文件 [rate-limit] 的 enable 字段禁用掉) 23 | . Rust(博客的搜索 API,基于 Tantivy) 24 | 25 | 如果只想部署 Hitokoto 这一特定 API,则这两个依赖都不需要。 26 | 27 | === 运行参数 28 | 29 | 支持仅运行某一个 API 服务。比如想只运行 Hitokoto 服务: 30 | 31 | [source,sh] 32 | ---- 33 | ./DIEM-API -view=hitokoto 34 | ---- 35 | 36 | 在运行 Hitokoto 服务之前,记得使用如下参数来初始化数据库: 37 | 38 | [source,sh] 39 | ---- 40 | ./DIEM-API -migrate 41 | ---- 42 | 43 | 数据库的配置路径以及 Hitokoto 源文件的配置路径在配置文件的 [hitokoto] 字段。其中源文件的每一行格式是包含以下元素的五元组(不同列之间采用制表符分割): 44 | 45 | |=== 46 | | | id | origin | length | source | hitokoto 47 | 48 | | 释义 49 | |一言的主键,整数 50 | | 可随意填写 51 | |一言主体长度,整数 52 | | 一言的出处 53 | | 一言主体 54 | 55 | | 举例 56 | | 1271737837318521026 57 | | ohx 58 | | 16 59 | | 小王子 60 | | 我太年轻了,甚至不懂怎么去爱她。 61 | |=== 62 | 63 | 避免侵权,我不会将接口目前使用的一言数据库源文件公开,如果有需要,可以使用 https://github.com/hitokoto-osc/sentences-bundle[hitokoto.cn 开源的句子],或者使用我之前写的 https://github.com/WincerChan/Hitokoto-Spider[一言爬虫] 来爬取,聪明如你肯定知道如何把 JSON 格式转化成对应的五元组~ 64 | 65 | === 普通部署 66 | 67 | clone 本仓库并在 `diem.toml` 修改相关的信息。 68 | 69 | [source,sh] 70 | ---- 71 | go build -o server # <1> 72 | ./server -config diem.toml -migrate # <2> 73 | env GIN_MODE=release ./server -config diem.toml # <3> 74 | ---- 75 | <1> 编译成二进制 76 | <2> 创建 Hitokoto 的数据库(基于 Bolt) 77 | <3> 生产环境(即设置环境变量 GIN_MODE 为 release) 78 | 79 | 其中生产环境不会在终端打印日志,而是会把日志都记录在 _log 文件夹内。 80 | 81 | === 容器版部署(推荐) 82 | 83 | 由于默认采用 Unix Domain Socket 作为通信方式,如果想在 K8s 或者 Docker 或者跨机器部署的话,需要将配置文件里面的 network 的字段值改为 tcp。 84 | 85 | == 致谢 86 | 87 | image:jetbrains-variant-4.png[jetbrains, link=https://www.jetbrains.com/?from=DIEM-API,width=160,height=90] 88 | -------------------------------------------------------------------------------- /.github/jetbrains-variant-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WincerChan/DIEM-API/52e56153e231a259bf4cfedd2c676622b8029b79/.github/jetbrains-variant-4.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.dll 4 | *.so 5 | *.dylib 6 | 7 | # Test binary, build with `go test -c` 8 | *.test 9 | 10 | # Output of the go coverage tool, specifically when used with LiteIDE 11 | *.out 12 | 13 | # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 14 | .glide/ 15 | 16 | *.db 17 | *.log 18 | deploy 19 | server 20 | id_rsa 21 | .directory 22 | _log/ 23 | credential.json 24 | .DS_Store 25 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /workspace.xml 3 | -------------------------------------------------------------------------------- /.idea/dictionaries/wincer.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | hito 5 | hitokoto 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/diem.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/sqldialects.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/watcherTasks.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 16 | 28 | 29 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:alpine 2 | 3 | RUN apk update 4 | RUN apk add --no-cache git 5 | 6 | WORKDIR /hitokoto 7 | 8 | # proxy for speed up git clone and go get 9 | # RUN git clone https://github.com/WincerChan/DIEM-API.git /hitokoto 10 | ADD . /hitokoto 11 | 12 | RUN CGO_ENABLED=0 go build -o /go/bin/server 13 | COPY ./config.yaml /etc/config.yaml 14 | ENTRYPOINT ["/go/bin/server", "/etc/config.yaml"] 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | D "DIEM-API/models" 5 | L "DIEM-API/tools/logfactory" 6 | C "DIEM-API/tools/tomlparser" 7 | V "DIEM-API/views" 8 | 9 | R "DIEM-API/middleware/limiting" 10 | "strings" 11 | 12 | "github.com/gin-gonic/gin" 13 | ) 14 | 15 | func initLogService() { 16 | L.InitLog() 17 | } 18 | 19 | // load config file(`config.yaml`) from disk. 20 | func loadCredential() { 21 | id := C.GetString("credential.analytics-id") 22 | credentialPath := C.ConfigAbsPath("credential.filename") 23 | D.InitGoogleAnalytics(id, credentialPath) 24 | } 25 | 26 | func loadAddrFromConfig(component string) (net, addr string) { 27 | networkType := component + ".network" 28 | addrPath := component + ".addr" 29 | net = C.GetString(networkType) 30 | if net == "uds" { 31 | addr = C.ConfigAbsPath(addrPath) 32 | } else { 33 | addr = C.GetString(addrPath) 34 | } 35 | return 36 | } 37 | 38 | func initDatabase() { 39 | path := C.ConfigAbsPath("hitokoto.dbpath") 40 | D.InitBoltConn(path) 41 | } 42 | 43 | // init rpc server Connection-Pool 44 | func initRPCServer() { 45 | net, addr := loadAddrFromConfig("rate-limit") 46 | R.InitRalPool(net, addr, C.GetInt("rate-limit.poolsize")) 47 | } 48 | 49 | func initSearchAPI() { 50 | net, addr := loadAddrFromConfig("search") 51 | V.InitSearchPool(net, addr, C.GetInt("search.poolsize")) 52 | } 53 | 54 | // InitConfig init all config 55 | func InitConfig(conf string) { 56 | C.LoadTOML(conf) 57 | initCommonService() 58 | } 59 | 60 | func initCommonService() { 61 | initRPCServer() 62 | initLogService() 63 | } 64 | 65 | func InitService(r *gin.Engine, service string) { 66 | if strings.HasPrefix("hitokoto", service) { 67 | initDatabase() 68 | } 69 | if strings.HasPrefix("analytics", service) { 70 | loadCredential() 71 | } 72 | if strings.HasPrefix("search", service) { 73 | initSearchAPI() 74 | } 75 | V.Register(r, service) 76 | } 77 | -------------------------------------------------------------------------------- /diem.toml: -------------------------------------------------------------------------------- 1 | config_dir = "/Users/loerfy/opts" 2 | 3 | log-path = "_logs" 4 | 5 | [hitokoto] 6 | dbpath = "bbolt" 7 | source = "hito" 8 | 9 | [rate-limit] 10 | network = "uds" # or uds 11 | poolsize = 5 12 | addr = "ral.sock" # if "tcp", this field should be host:addr 13 | enable = false 14 | 15 | [search] 16 | network = "tcp" # or uds 17 | poolsize = 1 18 | addr = "127.0.0.1:8834" 19 | 20 | [credential] 21 | analytics-id = "153425181" 22 | filename = "credential.json" 23 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module DIEM-API 2 | 3 | go 1.12 4 | 5 | require ( 6 | github.com/gin-gonic/gin v1.7.7 7 | github.com/go-playground/validator/v10 v10.9.0 // indirect 8 | github.com/golang/protobuf v1.5.2 // indirect 9 | github.com/json-iterator/go v1.1.12 // indirect 10 | github.com/mattn/go-isatty v0.0.14 // indirect 11 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 12 | github.com/pelletier/go-toml v1.6.0 13 | github.com/rs/zerolog v1.17.2 14 | github.com/ugorji/go v1.2.6 // indirect 15 | go.etcd.io/bbolt v1.3.5 16 | golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect 17 | golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect 18 | golang.org/x/text v0.3.7 // indirect 19 | google.golang.org/api v0.22.0 20 | google.golang.org/protobuf v1.27.1 // indirect 21 | gopkg.in/yaml.v2 v2.4.0 // indirect 22 | ) 23 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 3 | cloud.google.com/go v0.38.0 h1:ROfEUZz+Gh5pa62DJWXSaonyu3StP6EA6lPEXPI6mCo= 4 | cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= 5 | github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= 6 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 7 | github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= 8 | github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= 9 | github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= 10 | github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 11 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 12 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 13 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 14 | github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 15 | github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= 16 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= 17 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 18 | github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs= 19 | github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U= 20 | github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= 21 | github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 22 | github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= 23 | github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU= 24 | github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs= 25 | github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= 26 | github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= 27 | github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= 28 | github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= 29 | github.com/go-playground/validator/v10 v10.9.0 h1:NgTtmN58D0m8+UuxtYmGztBJB7VnPgjj221I1QHci2A= 30 | github.com/go-playground/validator/v10 v10.9.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos= 31 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= 32 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= 33 | github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= 34 | github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= 35 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 36 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 37 | github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= 38 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= 39 | github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= 40 | github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= 41 | github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= 42 | github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= 43 | github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 44 | github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= 45 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 46 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 47 | github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= 48 | github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= 49 | github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= 50 | github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM= 51 | github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= 52 | github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= 53 | github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU= 54 | github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= 55 | github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 56 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= 57 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= 58 | github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= 59 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 60 | github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= 61 | github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= 62 | github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= 63 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 64 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 65 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 66 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 67 | github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= 68 | github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= 69 | github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= 70 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= 71 | github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= 72 | github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= 73 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 74 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= 75 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 76 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 77 | github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= 78 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= 79 | github.com/pelletier/go-toml v1.6.0 h1:aetoXYr0Tv7xRU/V4B4IZJ2QcbtMUFoNb3ORp7TzIK4= 80 | github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys= 81 | github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= 82 | github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 83 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 84 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 85 | github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 86 | github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= 87 | github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= 88 | github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= 89 | github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= 90 | github.com/rs/zerolog v1.17.2 h1:RMRHFw2+wF7LO0QqtELQwo8hqSmqISyCJeFeAAuWcRo= 91 | github.com/rs/zerolog v1.17.2/go.mod h1:9nvC1axdVrAHcu/s9taAVfBuIdTZLVQmKQyvrUjF5+I= 92 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 93 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 94 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 95 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 96 | github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= 97 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 98 | github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= 99 | github.com/ugorji/go v1.2.6 h1:tGiWC9HENWE2tqYycIqFTNorMmFRVhNwCpDOpWqnk8E= 100 | github.com/ugorji/go v1.2.6/go.mod h1:anCg0y61KIhDlPZmnH+so+RQbysYVyDko0IMgJv0Nn0= 101 | github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= 102 | github.com/ugorji/go/codec v1.2.6 h1:7kbGefxLoDBuYXOms4yD7223OpNMMPNPZxXk5TvFcyQ= 103 | github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxWFFpvxTw= 104 | github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= 105 | go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0= 106 | go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= 107 | go.opencensus.io v0.21.0 h1:mU6zScU4U1YAFPHEHYk+3JC4SY7JxgkqS10ZOSyksNg= 108 | go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= 109 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 110 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 111 | golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 112 | golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M= 113 | golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= 114 | golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= 115 | golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 116 | golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= 117 | golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 118 | golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 119 | golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 120 | golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 121 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 122 | golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 123 | golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 124 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 125 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 126 | golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 127 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 128 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 129 | golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= 130 | golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= 131 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 132 | golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= 133 | golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0= 134 | golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= 135 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 136 | golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 137 | golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 138 | golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 139 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 140 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 141 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 142 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 143 | golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 144 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 145 | golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 146 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 147 | golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 148 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 149 | golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 150 | golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 151 | golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= 152 | golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 153 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 154 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 155 | golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 156 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 157 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 158 | golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 159 | golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= 160 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 161 | golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= 162 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 163 | golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 164 | golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= 165 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 166 | golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 167 | golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= 168 | golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 169 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 170 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 171 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 172 | google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= 173 | google.golang.org/api v0.22.0 h1:J1Pl9P2lnmYFSJvgs70DKELqHNh8CNWXPbud4njEE2s= 174 | google.golang.org/api v0.22.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= 175 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 176 | google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= 177 | google.golang.org/appengine v1.5.0 h1:KxkO13IPW4Lslp2bz+KHP2E3gtFlrIGNThxkZQ3g+4c= 178 | google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= 179 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 180 | google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= 181 | google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= 182 | google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= 183 | google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= 184 | google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= 185 | google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= 186 | google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= 187 | google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= 188 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= 189 | google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= 190 | google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= 191 | google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= 192 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 193 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 194 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 195 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 196 | gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= 197 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 198 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 199 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 200 | gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= 201 | gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 202 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 203 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= 204 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 205 | honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 206 | honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 207 | honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 208 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | M "DIEM-API/middleware" 5 | "flag" 6 | "fmt" 7 | "os" 8 | 9 | H "DIEM-API/models/hitokoto" 10 | 11 | F "DIEM-API/config" 12 | 13 | "github.com/gin-gonic/gin" 14 | ) 15 | 16 | func getServerFromArgs() string { 17 | isMigrate := false 18 | service := "" 19 | config := "" 20 | flag.BoolVar(&isMigrate, "migrate", false, "shuld migrate?") 21 | flag.BoolVar(&isMigrate, "m", false, "shuld migrate?") 22 | flag.StringVar(&service, "service", "", "running service") 23 | flag.StringVar(&service, "s", "", "running service") 24 | flag.StringVar(&config, "config", "", "config file") 25 | flag.StringVar(&config, "c", "", "config file") 26 | flag.Parse() 27 | // config not provided 28 | if config == "" { 29 | fmt.Printf("Usage of %s:\n", os.Args[0]) 30 | flag.PrintDefaults() 31 | os.Exit(1) 32 | } 33 | F.InitConfig(config) 34 | if isMigrate { 35 | H.MigrateBolt() 36 | os.Exit(0) 37 | } 38 | return service 39 | } 40 | 41 | func main() { 42 | service := getServerFromArgs() 43 | r := gin.New() 44 | // register for middlewares 45 | M.Register(r) 46 | // register for views 47 | F.InitService(r, service) 48 | r.Run() 49 | } 50 | -------------------------------------------------------------------------------- /middleware/interface.go: -------------------------------------------------------------------------------- 1 | package middleware 2 | 3 | import ( 4 | C "DIEM-API/middleware/limiting" 5 | L "DIEM-API/middleware/logger" 6 | R "DIEM-API/middleware/recovery" 7 | 8 | T "DIEM-API/tools/tomlparser" 9 | 10 | "github.com/gin-gonic/gin" 11 | ) 12 | 13 | func Register(r *gin.Engine) { 14 | if T.GetBool("rate-limit.enable") { 15 | r.Use(C.Limiting) 16 | } 17 | r.Use(L.Log, R.Recover) 18 | } 19 | -------------------------------------------------------------------------------- /middleware/limiting/limiting.go: -------------------------------------------------------------------------------- 1 | package limiting 2 | 3 | import ( 4 | I "DIEM-API/rpcserver" 5 | T "DIEM-API/tools" 6 | 7 | "github.com/gin-gonic/gin" 8 | ) 9 | 10 | var RalPool *I.Pool 11 | 12 | func InitRalPool(type_ string, addr string, poolSize int) { 13 | if type_ == "uds" { 14 | RalPool = I.NewPool(poolSize, addr, I.DialUDS) 15 | } else { 16 | RalPool = I.NewPool(poolSize, addr, I.DialTCP) 17 | } 18 | } 19 | 20 | // request redis's throttle module for limit-rating info. 21 | func check(xff string) []interface{} { 22 | return I.Choke(xff, 10, 0.1, RalPool) 23 | } 24 | 25 | // check if current request is valid 26 | func Limiting(c *gin.Context) { 27 | xff := c.GetHeader("X-Forwarded-For") 28 | ret := check(xff) 29 | c.Header("X-RateLimit-Limit", T.Str(ret[1])) 30 | c.Header("X-RateLimit-Remaining", T.Str(ret[2])) 31 | c.Header("X-RateLimit-Next", T.Str(ret[3])) 32 | // `0`: current request check passed. 33 | if T.Str(ret[0]) != "1" { 34 | c.String(200, "Sorry, Your IP requests is too frequently.") 35 | c.Abort() 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /middleware/logger/logger.go: -------------------------------------------------------------------------------- 1 | package logger 2 | 3 | import ( 4 | Logf "DIEM-API/tools/logfactory" 5 | 6 | "github.com/gin-gonic/gin" 7 | ) 8 | 9 | // log every request 10 | func Log(c *gin.Context) { 11 | c.Next() 12 | Logf.Access.Debug(). 13 | Str("Error", c.Errors.String()). 14 | Int("Status", c.Writer.Status()). 15 | Str("Path", c.Request.URL.String()). 16 | Str("XFF", c.ClientIP()). 17 | Msg("") 18 | } 19 | -------------------------------------------------------------------------------- /middleware/recovery/recovery.go: -------------------------------------------------------------------------------- 1 | package recovery 2 | 3 | import ( 4 | Logf "DIEM-API/tools/logfactory" 5 | "errors" 6 | "runtime/debug" 7 | 8 | "github.com/gin-gonic/gin" 9 | ) 10 | 11 | // log error message 12 | func storeError(e error) { 13 | Logf.Error.Error().Msg(e.Error()) 14 | Logf.Error.Error().Msg(string(debug.Stack())) 15 | } 16 | 17 | // recover from error, and save stack message to context. 18 | func Recover(c *gin.Context) { 19 | defer func() { 20 | r := recover() 21 | if r == nil { 22 | return 23 | } 24 | e := r.(error) 25 | storeError(e) 26 | c.Error(errors.New(e.Error())) 27 | c.String(500, "Sorry, server occurs a problem.") 28 | 29 | }() 30 | c.Next() 31 | } 32 | -------------------------------------------------------------------------------- /models/bbolt.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | T "DIEM-API/tools" 5 | 6 | bolt "go.etcd.io/bbolt" 7 | ) 8 | 9 | type BoltConn struct{ bolt.DB } 10 | 11 | var BoltDB *BoltConn 12 | 13 | func (bc *BoltConn) Read(fn func(tx *bolt.Tx) error) { 14 | bc.View(fn) 15 | } 16 | 17 | func (bc *BoltConn) Write(fn func(tx *bolt.Tx) error) { 18 | bc.Update(fn) 19 | } 20 | 21 | func InitBoltConn(path string) { 22 | db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true}) 23 | T.CheckFatalError(err, false) 24 | BoltDB = &BoltConn{*db} 25 | BoltDB.Read(InitHitokoto) 26 | } 27 | -------------------------------------------------------------------------------- /models/blogs/model.go: -------------------------------------------------------------------------------- 1 | package blogs 2 | 3 | import ( 4 | "errors" 5 | "net/url" 6 | "reflect" 7 | "strconv" 8 | "strings" 9 | "time" 10 | ) 11 | 12 | const INDEXUID = "blogs" 13 | const MAXKEYWORDLENGTH = 37 14 | 15 | type Params struct { 16 | Paginate []int `form:"pages" json:"pages" deserialize:"BindPage"` 17 | Terms []string `form:"terms" json:"terms" deserialize:"BindTerms"` 18 | Query []string `form:"q" json:"q" deserialize:"BindQ"` 19 | DateRange []int `form:"range" json:"range" deserialize:"BindRange"` 20 | } 21 | 22 | func (p *Params) BindPage(str string) string { 23 | pages := strings.Split(str, "-") 24 | p.Paginate = make([]int, 2) 25 | if len(pages) != 2 { 26 | return "invalid pages format, expected likes: 1-10" 27 | } else if len(pages[1]) > 1 { 28 | return "invalid pages format, page cannot greate than 10" 29 | } 30 | for i, pag := range pages { 31 | n, err := strconv.ParseUint(pag, 10, 32) 32 | if err != nil { 33 | return err.Error() 34 | } 35 | p.Paginate[i] = int(n) 36 | } 37 | return "" 38 | } 39 | 40 | func (p *Params) BindRange(str string) string { 41 | ranges := strings.Split(str, "~") 42 | if len(ranges) == 1 { 43 | return "" 44 | } 45 | p.DateRange = []int{0, int(time.Now().Unix())} 46 | for i, r := range ranges { 47 | if r != "" { 48 | t, err := time.Parse("2006-01-02", r) 49 | if err != nil { 50 | return err.Error() 51 | } 52 | p.DateRange[i] = int(t.Unix()) 53 | } 54 | } 55 | return "" 56 | } 57 | 58 | func (p *Params) BindTerms(str string) string { 59 | terms := strings.Split(str, " ") 60 | p.Terms = make([]string, 0, 4) 61 | truncated := make([]string, 0, 4) 62 | if terms[0] == "" { 63 | return "" 64 | } else if len(terms) > 4 { 65 | truncated = terms[:4] 66 | } else { 67 | truncated = terms 68 | } 69 | for _, term := range truncated { 70 | if !strings.HasPrefix(term, "tags:") && !strings.HasPrefix(term, "category:") { 71 | return "invalid terms, expects category or tags" 72 | } 73 | p.Terms = append(p.Terms, term) 74 | } 75 | return "" 76 | } 77 | 78 | func (p *Params) BindQ(str string) string { 79 | q := []rune(str) 80 | if len(q) >= MAXKEYWORDLENGTH { 81 | p.Query = strings.Split(string(q[:MAXKEYWORDLENGTH]), ",") 82 | } else { 83 | p.Query = strings.Split(str, ",") 84 | } 85 | return "" 86 | } 87 | 88 | func (p *Params) Serialize() string { 89 | paramsValue := reflect.ValueOf(*p) 90 | lens := paramsValue.NumField() 91 | results := make([]string, lens, lens) 92 | for i := 0; i < lens; i++ { 93 | results[i] = paramsValue.Field(i).String() 94 | } 95 | return strings.Join(results, "\x00") 96 | } 97 | 98 | func BindStruct(m url.Values, p *Params) error { 99 | paramKey := reflect.TypeOf(p).Elem() 100 | paramMethod := reflect.ValueOf(p) 101 | paramValue := paramMethod.Elem() 102 | for i := 0; i < paramValue.NumField(); i++ { 103 | field := paramKey.Field(i) 104 | key := field.Tag.Get("form") 105 | method := paramMethod.MethodByName(field.Tag.Get("deserialize")) 106 | err := method.Call([]reflect.Value{reflect.ValueOf(m.Get(key))}) 107 | if !err[0].IsZero() { 108 | return errors.New(err[0].String()) 109 | } 110 | } 111 | return nil 112 | } 113 | -------------------------------------------------------------------------------- /models/ga.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | G "DIEM-API/models/googleanalytics" 5 | T "DIEM-API/tools" 6 | "context" 7 | 8 | gar "google.golang.org/api/analyticsreporting/v4" 9 | "google.golang.org/api/option" 10 | ) 11 | 12 | var ( 13 | AnalyticsReportingService *gar.Service 14 | ) 15 | 16 | func InitGACredential(path string) { 17 | ctx := context.Background() 18 | json := T.LoadJSON(path) 19 | ars, err := gar.NewService(ctx, option.WithCredentialsJSON(json)) 20 | AnalyticsReportingService = ars 21 | T.CheckFatalError(err, false) 22 | } 23 | 24 | func GetReport(p *G.Params) G.ReportResponse { 25 | report := G.ConstructReport(p) 26 | req := &gar.GetReportsRequest{ 27 | ReportRequests: []*gar.ReportRequest{ 28 | report, 29 | }, 30 | } 31 | resp, err := AnalyticsReportingService.Reports.BatchGet(req).Do() 32 | T.CheckException(err, "Failed to get analytics report.") 33 | return G.SimplifiedResponse(resp) 34 | } 35 | -------------------------------------------------------------------------------- /models/googleanalytics/model.go: -------------------------------------------------------------------------------- 1 | package googleanalytics 2 | 3 | import ( 4 | T "DIEM-API/tools" 5 | 6 | gar "google.golang.org/api/analyticsreporting/v4" 7 | ) 8 | 9 | var ( 10 | GAViewID string 11 | ) 12 | 13 | type Params struct { 14 | Prefix string `form:"prefix"` 15 | } 16 | 17 | type ReportResponse struct { 18 | Details []accessInfo `json:"details"` 19 | Total int `json:"total"` 20 | } 21 | 22 | type accessInfo struct { 23 | Path string `json:"path"` 24 | Count int `json:"count"` 25 | } 26 | 27 | func ConstructReport(p *Params) *gar.ReportRequest { 28 | reportParams := new(gar.ReportRequest) 29 | reportParams.ViewId = GAViewID 30 | reportParams.DateRanges = []*gar.DateRange{&gar.DateRange{StartDate: "2017-06-18", EndDate: "today"}} 31 | reportParams.Metrics = []*gar.Metric{&gar.Metric{Expression: "ga:pageviews"}} 32 | reportParams.Dimensions = []*gar.Dimension{&gar.Dimension{Name: "ga:pagePath"}} 33 | reportParams.DimensionFilterClauses = []*gar.DimensionFilterClause{ 34 | { 35 | Filters: []*gar.DimensionFilter{&gar.DimensionFilter{ 36 | DimensionName: "ga:pagePath", 37 | Operator: "BEGINS_WITH", 38 | Expressions: []string{p.Prefix}, 39 | }, 40 | }, 41 | }, 42 | } 43 | return reportParams 44 | } 45 | 46 | func SimplifiedResponse(response *gar.GetReportsResponse) (rr ReportResponse) { 47 | rr = *new(ReportResponse) 48 | for _, report := range response.Reports { 49 | rr.Total = T.Int(report.Data.Totals[0].Values[0]) 50 | rr.Details = make([]accessInfo, report.Data.RowCount) 51 | for i, row := range report.Data.Rows { 52 | rr.Details[i] = accessInfo{Path: row.Dimensions[0], Count: T.Int(row.Metrics[0].Values[0])} 53 | } 54 | } 55 | return 56 | } 57 | -------------------------------------------------------------------------------- /models/hitokoto/migrate_hitokoto.go: -------------------------------------------------------------------------------- 1 | package hitokoto 2 | 3 | import ( 4 | T "DIEM-API/tools" 5 | L "DIEM-API/tools/logfactory" 6 | C "DIEM-API/tools/tomlparser" 7 | "bufio" 8 | "bytes" 9 | "encoding/binary" 10 | "encoding/gob" 11 | "log" 12 | "os" 13 | "sort" 14 | "strconv" 15 | "strings" 16 | 17 | bolt "go.etcd.io/bbolt" 18 | ) 19 | 20 | type HitoInfo struct { 21 | Source string `json:"source"` 22 | Hito string `json:"hitokoto"` 23 | } 24 | 25 | type Record struct { 26 | Xxhash int64 27 | Length int 28 | Origin string 29 | Hitokoto HitoInfo 30 | } 31 | 32 | type SortBy []Record 33 | 34 | func (a SortBy) Len() int { return len(a) } 35 | func (a SortBy) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 36 | func (a SortBy) Less(i, j int) bool { return a[i].Length < a[j].Length } 37 | 38 | func (r *Record) insert(db *bolt.DB, id uint32) { 39 | key := make([]byte, 4) 40 | binary.BigEndian.PutUint32(key, id) 41 | value := new(bytes.Buffer) 42 | err := gob.NewEncoder(value).Encode(r) 43 | if err != nil { 44 | log.Println(err) 45 | } 46 | db.Update(func(tx *bolt.Tx) error { 47 | b, err := tx.CreateBucketIfNotExists([]byte("hitokoto")) 48 | if err != nil { 49 | return err 50 | } 51 | return b.Put(key, value.Bytes()) 52 | }) 53 | } 54 | 55 | func formatAsRecord(data []string) []Record { 56 | records := make([]Record, len(data), len(data)) 57 | for i, text := range data { 58 | words := strings.Split(text, "\t") 59 | xxhash, err := strconv.Atoi(words[0]) 60 | T.CheckFatalError(err, false) 61 | length, err := strconv.Atoi(words[2]) 62 | T.CheckFatalError(err, false) 63 | r := &Record{ 64 | Xxhash: int64(xxhash), 65 | Length: length, 66 | Origin: words[1], 67 | } 68 | r.Hitokoto.Source = words[3] 69 | r.Hitokoto.Hito = words[4] 70 | records[i] = *r 71 | } 72 | return records 73 | } 74 | 75 | func bulkInsert(db *bolt.DB, data []string) { 76 | records := formatAsRecord(data) 77 | sort.Sort(SortBy(records)) 78 | db.Update(func(tx *bolt.Tx) error { 79 | b, err := tx.CreateBucketIfNotExists([]byte("hitokoto")) 80 | T.CheckFatalError(err, false) 81 | for i, r := range records { 82 | key := make([]byte, 4) 83 | binary.BigEndian.PutUint32(key, uint32(i)) 84 | value := new(bytes.Buffer) 85 | err := gob.NewEncoder(value).Encode(r) 86 | T.CheckFatalError(err, false) 87 | b.Put(key, value.Bytes()) 88 | } 89 | return nil 90 | }) 91 | } 92 | 93 | func migrateHitokoto(source, path string) { 94 | db, _ := bolt.Open(path, 0666, nil) 95 | file, err := os.Open(source) 96 | if err != nil { 97 | log.Fatal(err) 98 | } 99 | defer file.Close() 100 | defer db.Close() 101 | hitokotos := make([]string, 0) 102 | scanner := bufio.NewScanner(file) 103 | 104 | for i := 0; scanner.Scan(); i++ { 105 | hitokotos = append(hitokotos, scanner.Text()) 106 | } 107 | bulkInsert(db, hitokotos) 108 | } 109 | 110 | func MigrateBolt() { 111 | path := C.ConfigAbsPath("hitokoto.dbpath") 112 | source := C.ConfigAbsPath("hitokoto.source") 113 | os.Remove(path) 114 | L.Error.Debug().Msg("Trying to migrate database") 115 | migrateHitokoto(source, path) 116 | L.Error.Debug().Msg("Succeed.") 117 | } 118 | -------------------------------------------------------------------------------- /models/hitokoto/model.go: -------------------------------------------------------------------------------- 1 | package hitokoto 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "encoding/gob" 7 | 8 | bolt "go.etcd.io/bbolt" 9 | ) 10 | 11 | var ( 12 | counts int 13 | HitokotoMapping map[int]int 14 | HitoBucket = []byte("hitokoto") 15 | ) 16 | 17 | type Params struct { 18 | Length int `form:"length"` 19 | Callback string `form:"callback"` 20 | Encode string `form:"encode"` 21 | } 22 | 23 | func LoadRecordFromBytes(value []byte) Record { 24 | buf, r := new(bytes.Buffer), new(Record) 25 | buf.Write(value) 26 | gob.NewDecoder(buf).Decode(r) 27 | return *r 28 | } 29 | 30 | func IndexOf(length int) int { 31 | if val, ok := HitokotoMapping[length]; ok { 32 | return val 33 | } 34 | return counts 35 | } 36 | 37 | func ScanRecordLength(tx *bolt.Tx) error { 38 | b := tx.Bucket(HitoBucket) 39 | preLength := 0 40 | b.ForEach(func(k, v []byte) error { 41 | counts++ 42 | id := binary.BigEndian.Uint32(k) 43 | record := LoadRecordFromBytes(v) 44 | if record.Length != preLength { 45 | HitokotoMapping[record.Length] = int(id) 46 | } 47 | preLength = record.Length 48 | return nil 49 | }) 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /models/interface.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | G "DIEM-API/models/googleanalytics" 5 | H "DIEM-API/models/hitokoto" 6 | 7 | bolt "go.etcd.io/bbolt" 8 | ) 9 | 10 | func InitHitokoto(tx *bolt.Tx) error { 11 | H.HitokotoMapping = make(map[int]int) 12 | return H.ScanRecordLength(tx) 13 | } 14 | 15 | func InitGoogleAnalytics(viewID, filepath string) { 16 | InitGACredential(filepath) 17 | G.GAViewID = viewID 18 | } 19 | -------------------------------------------------------------------------------- /rpcserver/client.go: -------------------------------------------------------------------------------- 1 | package rpcserver 2 | 3 | import ( 4 | "bufio" 5 | "bytes" 6 | "encoding/binary" 7 | "math" 8 | "net" 9 | "sync" 10 | ) 11 | 12 | const ( 13 | // Basic Type 14 | String = 0 15 | Atom = 1 16 | Integer = 2 17 | Float = 3 18 | 19 | // Compound Type 20 | List = 16 21 | ) 22 | 23 | var once sync.Once 24 | 25 | var rpcConn *RPCConn 26 | var wg sync.WaitGroup 27 | 28 | type RPCConn struct { 29 | conn net.Conn 30 | signal chan error 31 | addr *net.TCPAddr 32 | reader *bufio.Reader 33 | } 34 | 35 | func integerToBytes(integer, bytes int) []byte { 36 | data := make([]byte, bytes, bytes) 37 | switch bytes { 38 | case 4: 39 | binary.BigEndian.PutUint32(data, uint32(integer)) 40 | case 8: 41 | binary.BigEndian.PutUint64(data, uint64(integer)) 42 | } 43 | return data 44 | } 45 | 46 | func encodeStringList(bf *bytes.Buffer, values []string) { 47 | bf.WriteByte(List) 48 | subBf := new(bytes.Buffer) 49 | for _, value := range values { 50 | encodeString(subBf, value) 51 | } 52 | subBytes := subBf.Bytes() 53 | bf.Write(integerToBytes(len(subBytes), SizeBytes)) 54 | bf.Write(subBytes) 55 | } 56 | 57 | func encodeIntegerList(bf *bytes.Buffer, values []int) { 58 | bf.WriteByte(List) 59 | subBf := new(bytes.Buffer) 60 | for _, value := range values { 61 | encodeInteger(subBf, value) 62 | } 63 | subBytes := subBf.Bytes() 64 | bf.Write(integerToBytes(len(subBytes), SizeBytes)) 65 | bf.Write(subBytes) 66 | } 67 | 68 | func encodeString(bf *bytes.Buffer, value string) { 69 | bf.WriteByte(String) 70 | bf.Write(integerToBytes(len(value), SizeBytes)) 71 | bf.Write([]byte(value)) 72 | } 73 | 74 | func encodeAtom(bf *bytes.Buffer, value string) { 75 | bf.WriteByte(Atom) 76 | bf.Write(integerToBytes(len(value), SizeBytes)) 77 | bf.Write([]byte(value)) 78 | } 79 | 80 | func encodeInteger(bf *bytes.Buffer, value int) { 81 | bf.WriteByte(Integer) 82 | bf.Write(integerToBytes(IntegerBytes, SizeBytes)) 83 | bf.Write(integerToBytes(value, IntegerBytes)) 84 | } 85 | 86 | func encodeFloat(bf *bytes.Buffer, value float64) { 87 | bf.WriteByte(Float) 88 | bits := math.Float64bits(value) 89 | bf.Write(integerToBytes(IntegerBytes, SizeBytes)) 90 | bf.Write(integerToBytes(int(bits), IntegerBytes)) 91 | } 92 | 93 | func execute(bf *bytes.Buffer, conn *Conn) []interface{} { 94 | conn.WriteOnce(bf.Bytes()) 95 | body := conn.ReadOnce() 96 | return extract(&body) 97 | } 98 | -------------------------------------------------------------------------------- /rpcserver/client_test.go: -------------------------------------------------------------------------------- 1 | package rpcserver 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "strconv" 7 | "testing" 8 | ) 9 | 10 | func BenchmarkToString(b *testing.B) { 11 | var bs []byte 12 | for i := 0; i < b.N; i++ { 13 | s := strconv.Itoa(37) 14 | bs = append(bs, []byte(s)...) 15 | f := fmt.Sprintf("%f", 0.3497) 16 | bs = append(bs, []byte(f)...) 17 | i := strconv.Itoa(37) 18 | bs = append(bs, []byte(i)...) 19 | bs = []byte{} 20 | } 21 | } 22 | 23 | func BenchmarkTLVEncode(b *testing.B) { 24 | for n := 0; n < b.N; n++ { 25 | bf := new(bytes.Buffer) 26 | encodeString(bf, "choke") 27 | encodeInteger(bf, 37) 28 | encodeFloat(bf, 0.3498) 29 | bf.Bytes() 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /rpcserver/conn.go: -------------------------------------------------------------------------------- 1 | package rpcserver 2 | 3 | import ( 4 | "bufio" 5 | "net" 6 | ) 7 | 8 | type CompoundConn struct { 9 | TcpConn *net.TCPConn 10 | UDSConn *net.UnixConn 11 | } 12 | 13 | func (c *CompoundConn) Reader() *bufio.Reader { 14 | if c.TcpConn != nil { 15 | return bufio.NewReader(c.TcpConn) 16 | } 17 | return bufio.NewReader(c.UDSConn) 18 | } 19 | 20 | func (c *CompoundConn) Writer() *bufio.Writer { 21 | if c.TcpConn != nil { 22 | return bufio.NewWriter(c.TcpConn) 23 | } 24 | return bufio.NewWriter(c.UDSConn) 25 | } 26 | 27 | func (c *CompoundConn) Close() { 28 | if c.TcpConn != nil { 29 | c.TcpConn.Close() 30 | return 31 | } 32 | c.UDSConn.Close() 33 | } 34 | -------------------------------------------------------------------------------- /rpcserver/decode.go: -------------------------------------------------------------------------------- 1 | package rpcserver 2 | 3 | import ( 4 | "encoding/binary" 5 | "math" 6 | ) 7 | 8 | const ( 9 | SizeBytes = 4 10 | IntegerBytes = 8 11 | TypeByte = 1 12 | ) 13 | 14 | func decodeFloat(data *[]byte) (value float64) { 15 | *data = (*data)[SizeBytes:] 16 | valuePart := (*data)[:IntegerBytes] 17 | bits := binary.BigEndian.Uint64(valuePart) 18 | value = math.Float64frombits(bits) 19 | *data = (*data)[IntegerBytes:] 20 | return 21 | } 22 | 23 | func bytesToInteger(value *[]byte, bytes int) (length int) { 24 | lengthPart := (*value)[:bytes] 25 | switch bytes { 26 | case 4: 27 | length = int(binary.BigEndian.Uint32(lengthPart)) 28 | case 8: 29 | length = int(binary.BigEndian.Uint64(lengthPart)) 30 | } 31 | *value = (*value)[bytes:] 32 | return 33 | } 34 | 35 | func decodeString(data *[]byte) (value string) { 36 | length := bytesToInteger(data, SizeBytes) 37 | valuePart := (*data)[:length] 38 | value = string(valuePart) 39 | *data = (*data)[length:] 40 | return value 41 | } 42 | 43 | func decodeInteger(data *[]byte) (value int) { 44 | *data = (*data)[SizeBytes:] 45 | value = bytesToInteger(data, IntegerBytes) 46 | return 47 | } 48 | 49 | func extract(data *[]byte) []interface{} { 50 | results := make([]interface{}, 0) 51 | outside: 52 | for len(*data) != 0 { 53 | eleType := (*data)[0] 54 | *data = (*data)[1:] 55 | switch eleType { 56 | case 0: 57 | results = append(results, decodeString(data)) 58 | case 2: 59 | results = append(results, decodeInteger(data)) 60 | case 3: 61 | results = append(results, decodeFloat(data)) 62 | default: 63 | break outside 64 | } 65 | } 66 | return results 67 | } 68 | -------------------------------------------------------------------------------- /rpcserver/decode_test.go: -------------------------------------------------------------------------------- 1 | package rpcserver 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | "testing" 7 | ) 8 | 9 | func BenchmarkStringTo(b *testing.B) { 10 | for i := 0; i < b.N; i++ { 11 | a := "choke$37$0.3498" 12 | strs := strings.Split(a, "$") 13 | _ = strs[0] 14 | strconv.Atoi(strs[1]) 15 | strconv.ParseFloat(strs[2], 64) 16 | } 17 | } 18 | 19 | func BenchmarkTLVDecode(b *testing.B) { 20 | for n := 0; n < b.N; n++ { 21 | bs := []byte{0, 0, 0, 0, 5, 99, 104, 111, 107, 101, 2, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 37, 3, 0, 0, 0, 8, 63, 214, 99, 31, 138, 9, 2, 222} 22 | decodeString(&bs) 23 | decodeInteger(&bs) 24 | decodeFloat(&bs) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /rpcserver/interface.go: -------------------------------------------------------------------------------- 1 | package rpcserver 2 | 3 | import "bytes" 4 | 5 | func Choke(key string, total int, speed float64, p *Pool) []interface{} { 6 | bf := new(bytes.Buffer) 7 | encodeAtom(bf, "choke") 8 | encodeString(bf, key) 9 | encodeInteger(bf, total) 10 | encodeFloat(bf, speed) 11 | conn := p.Get() 12 | defer p.Put(conn) 13 | return execute(bf, conn) 14 | } 15 | 16 | func Search(pages, ranges []int, terms, q []string, p *Pool) []interface{} { 17 | bf := new(bytes.Buffer) 18 | encodeIntegerList(bf, pages) 19 | encodeIntegerList(bf, ranges) 20 | encodeStringList(bf, terms) 21 | encodeStringList(bf, q) 22 | conn := p.Get() 23 | defer p.Put(conn) 24 | return execute(bf, conn) 25 | } 26 | -------------------------------------------------------------------------------- /rpcserver/io.go: -------------------------------------------------------------------------------- 1 | package rpcserver 2 | 3 | import ( 4 | "bufio" 5 | "io" 6 | ) 7 | 8 | type Reader struct { 9 | rd *bufio.Reader 10 | _buf []byte 11 | } 12 | 13 | func NewReader(reader io.Reader) *Reader { 14 | return &Reader{ 15 | rd: bufio.NewReader(reader), 16 | _buf: make([]byte, 64), 17 | } 18 | } 19 | 20 | func (r *Reader) Buffered() int { 21 | return r.rd.Buffered() 22 | } 23 | 24 | func (r *Reader) Peek(n int) ([]byte, error) { 25 | return r.rd.Peek(n) 26 | } 27 | 28 | func (r *Reader) Reset(rd io.Reader) { 29 | r.rd.Reset(rd) 30 | } 31 | 32 | func (r *Reader) ReadOnce() ([]byte, error) { 33 | line, err := r.readLine() 34 | if err != nil { 35 | return nil, err 36 | } 37 | return line, nil 38 | } 39 | 40 | func (r *Reader) readLine() ([]byte, error) { 41 | b, err := r.rd.ReadSlice('\n') 42 | if err != nil { 43 | return nil, err 44 | } 45 | return b, nil 46 | } 47 | 48 | type Writer struct { 49 | writer 50 | 51 | lenBuf []byte 52 | numBuf []byte 53 | } 54 | type writer interface { 55 | io.Writer 56 | io.ByteWriter 57 | // io.StringWriter 58 | WriteString(s string) (n int, err error) 59 | } 60 | 61 | func NewWriter(wr writer) *Writer { 62 | return &Writer{ 63 | writer: wr, 64 | 65 | lenBuf: make([]byte, 64), 66 | numBuf: make([]byte, 64), 67 | } 68 | } 69 | 70 | func (w *Writer) WriteOnce(line []byte) error { 71 | _, err := w.Write(line) 72 | return err 73 | } 74 | -------------------------------------------------------------------------------- /rpcserver/pool_v2.go: -------------------------------------------------------------------------------- 1 | package rpcserver 2 | 3 | import ( 4 | T "DIEM-API/tools" 5 | "bufio" 6 | "encoding/binary" 7 | "io" 8 | "log" 9 | "net" 10 | "sync" 11 | "time" 12 | ) 13 | 14 | type Options struct { 15 | Dial func(string) (*CompoundConn, error) 16 | PoolSize int 17 | Addr string 18 | } 19 | 20 | type Pool struct { 21 | ops *Options 22 | mutex sync.Mutex 23 | queue chan struct{} 24 | // idle connections in pool 25 | idleConns []*Conn 26 | // all connections in pool 27 | usedConns []*Conn 28 | } 29 | 30 | type Conn struct { 31 | netConn *CompoundConn 32 | reader *bufio.Reader 33 | writer *bufio.Writer 34 | closed bool 35 | } 36 | 37 | func NewPool(poolSize int, Addr string, dial func(string) (*CompoundConn, error)) *Pool { 38 | p := &Pool{ 39 | ops: &Options{ 40 | Dial: dial, 41 | PoolSize: poolSize, 42 | Addr: Addr, 43 | }, 44 | queue: make(chan struct{}, poolSize), 45 | usedConns: make([]*Conn, 0, poolSize), 46 | idleConns: make([]*Conn, 0, poolSize), 47 | } 48 | return p 49 | } 50 | 51 | func DialUDS(addr string) (*CompoundConn, error) { 52 | sockFile, err := net.ResolveUnixAddr("unix", addr) 53 | if err != nil { 54 | T.CheckException(err, "resolve unix addr failed.") 55 | } 56 | conn, err := net.DialUnix("unix", nil, sockFile) 57 | if err != nil { 58 | T.CheckException(err, "dial unix addr failed.") 59 | } 60 | return &CompoundConn{UDSConn: conn}, nil 61 | } 62 | 63 | func DialTCP(addr string) (*CompoundConn, error) { 64 | sockConn, err := net.ResolveTCPAddr("tcp", addr) 65 | if err != nil { 66 | T.CheckException(err, "resolve tcp addr failed.") 67 | } 68 | conn, err := net.DialTCP("tcp", nil, sockConn) 69 | if err != nil { 70 | T.CheckException(err, "dial tcp addr failed.") 71 | } 72 | return &CompoundConn{TcpConn: conn}, nil 73 | } 74 | 75 | func (c *Conn) WriteLine(line []byte) { 76 | line = append(line, 10) 77 | _, err := c.writer.Write(line) 78 | if err != nil { 79 | c.closed = true 80 | log.Println(err) 81 | } 82 | c.writer.Flush() 83 | } 84 | 85 | func (c *Conn) ReadLine() []byte { 86 | k, err := c.reader.ReadBytes(10) 87 | if err != nil { 88 | c.closed = true 89 | log.Println(err) 90 | } 91 | return k 92 | } 93 | 94 | func (c *Conn) WriteOnce(line []byte) { 95 | c.writer.Write(integerToBytes(len(line), 4)) 96 | _, err := c.writer.Write(line) 97 | if err != nil { 98 | c.closed = true 99 | log.Println(err) 100 | } 101 | c.writer.Flush() 102 | } 103 | 104 | func (c *Conn) ReadOnce() []byte { 105 | prefix, err := c.reader.Peek(SizeBytes) 106 | if err != nil { 107 | c.closed = true 108 | log.Println(err) 109 | return nil 110 | } 111 | size := binary.BigEndian.Uint32(prefix) 112 | data := make([]byte, size+SizeBytes) 113 | _, err = io.ReadFull(c.reader, data) 114 | if err != nil { 115 | log.Println(err) 116 | } 117 | return data[SizeBytes:] 118 | } 119 | 120 | func (p *Pool) Get() *Conn { 121 | // log.Println("get a conn") 122 | if c := p.fillToPool(); c != nil { 123 | return c 124 | } 125 | return p.popIdle() 126 | } 127 | 128 | func (p *Pool) fillToPool() *Conn { 129 | p.mutex.Lock() 130 | defer p.mutex.Unlock() 131 | if len(p.usedConns) < cap(p.usedConns) && len(p.idleConns) == 0 { 132 | c := p.newConn() 133 | p.usedConns = append(p.usedConns, c) 134 | return c 135 | } 136 | return nil 137 | } 138 | 139 | func (p *Pool) Put(c *Conn) { 140 | p.mutex.Lock() 141 | closed := c.closed 142 | p.mutex.Unlock() 143 | if closed { 144 | log.Println("error: closed connection") 145 | c = p.newConn() 146 | } 147 | p.pushIdle(c) 148 | } 149 | 150 | func (p *Pool) pushIdle(c *Conn) { 151 | p.mutex.Lock() 152 | p.idleConns = append(p.idleConns, c) 153 | p.mutex.Unlock() 154 | p.queue <- struct{}{} 155 | } 156 | 157 | func (p *Pool) popIdle() *Conn { 158 | select { 159 | case <-p.queue: 160 | p.mutex.Lock() 161 | c := p.idleConns[0] 162 | p.idleConns = p.idleConns[1:] 163 | p.mutex.Unlock() 164 | return c 165 | // All Conns in the pool has been timeout 166 | // Create a new one and fill it in the pool 167 | case <-time.After(time.Second * 30): 168 | log.Println("All Conn Timeout. Creating a new connection") 169 | p.mutex.Lock() 170 | c := p.newConn() 171 | drop := p.usedConns[0] 172 | p.usedConns = append(p.usedConns[1:], c) 173 | p.mutex.Unlock() 174 | drop.closed = true 175 | drop.netConn.Close() 176 | return c 177 | } 178 | } 179 | 180 | func (p *Pool) newConn() *Conn { 181 | // log.Println("here new") 182 | netConn, err := p.ops.Dial(p.ops.Addr) 183 | if err != nil { 184 | log.Panicln(err) 185 | } 186 | conn := &Conn{ 187 | netConn: netConn, 188 | reader: netConn.Reader(), 189 | writer: netConn.Writer(), 190 | } 191 | return conn 192 | } 193 | -------------------------------------------------------------------------------- /tools/dnslookup/dns.go: -------------------------------------------------------------------------------- 1 | package dnslookup 2 | 3 | import ( 4 | T "DIEM-API/tools" 5 | "net" 6 | "strings" 7 | ) 8 | 9 | // Resolve host use default dns lookup, 10 | // return the first address. 11 | func ResolveOne(hostname string) string { 12 | addresses, err := net.LookupHost(hostname) 13 | T.CheckFatalError(err, false) 14 | return addresses[0] 15 | } 16 | 17 | // Resolve host:port pair, then modify as 18 | // ip_address:port. 19 | func ResolveAddr(hostAndPort string) string { 20 | addresses := strings.Split(hostAndPort, ":") 21 | addresses[0] = ResolveOne(addresses[0]) 22 | return strings.Join(addresses, ":") 23 | } 24 | -------------------------------------------------------------------------------- /tools/dnslookup/dns_test.go: -------------------------------------------------------------------------------- 1 | package dnslookup 2 | 3 | import ( 4 | "net" 5 | "testing" 6 | ) 7 | 8 | func TestResolveAddr(t *testing.T) { 9 | // normal 10 | var ( 11 | in = "itswincer.com:80" 12 | expected = "104.24.125.13:80" 13 | ) 14 | out := ResolveAddr(in) 15 | if out != expected { 16 | t.Errorf("ResolveAddr(%s) = %s; expected %s", in, out, expected) 17 | } 18 | } 19 | 20 | func TestResolveOne2(t *testing.T) { 21 | var ( 22 | in = "www.baidu.com" 23 | ) 24 | out := ResolveOne(in) 25 | if net.ParseIP(out) == nil { 26 | t.Errorf("ResolveOne(%s) = %s; not a valid ip address", in, out) 27 | } 28 | } 29 | 30 | func TestResolveOne(t *testing.T) { 31 | var ( 32 | in = "itswincer.com" 33 | expected = "104.24.125.13" 34 | ) 35 | out := ResolveOne(in) 36 | if out != expected { 37 | t.Errorf("ResolveAddr(%s) = %s; expected %s", in, out, expected) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tools/filefactory/file.go: -------------------------------------------------------------------------------- 1 | package filefactory 2 | 3 | import ( 4 | "io" 5 | "os" 6 | "path/filepath" 7 | ) 8 | 9 | // Copy file from src to dest 10 | func CopyFile(srcFile, destFile string) { 11 | file, err := os.Open(srcFile) 12 | if err != nil { 13 | panic(err) 14 | } 15 | defer file.Close() 16 | dest, err := os.Create(destFile) 17 | if err != nil { 18 | panic(err) 19 | } 20 | defer dest.Close() 21 | _, err = io.Copy(dest, file) 22 | if err != nil { 23 | panic(err) 24 | } 25 | } 26 | 27 | // create the directory of filename. 28 | func createDirectory(filename string) { 29 | dir := filepath.Dir(filename) 30 | err := os.MkdirAll(dir, os.ModePerm) 31 | if err != nil { 32 | panic(err) 33 | } 34 | } 35 | 36 | // create logfile and open 37 | func NewFile(filename string) *os.File { 38 | createDirectory(filename) 39 | 40 | newFile, err := os.OpenFile(filename, 41 | os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) 42 | if err != nil { 43 | panic(err) 44 | } 45 | return newFile 46 | } 47 | -------------------------------------------------------------------------------- /tools/filefactory/file_test.go: -------------------------------------------------------------------------------- 1 | package filefactory 2 | 3 | import ( 4 | "os" 5 | "path/filepath" 6 | "testing" 7 | ) 8 | 9 | func TestCopyFile(t *testing.T) { 10 | var ( 11 | in = "file.go" 12 | out = "file_cp.go" 13 | ) 14 | CopyFile(in, out) 15 | inInfo, _ := os.Stat(in) 16 | outInfo, _ := os.Stat(out) 17 | if inInfo.Mode() != outInfo.Mode() { 18 | t.Errorf("Input file mode is %s, output file mode is %s", inInfo.Mode(), outInfo.Mode()) 19 | } 20 | if inInfo.Size() != outInfo.Size() { 21 | t.Errorf("Input file size is %d, output file mode is %d", inInfo.Size(), outInfo.Size()) 22 | } 23 | os.Remove(out) 24 | } 25 | 26 | func TestNewFile(t *testing.T) { 27 | var ( 28 | in = "fh/df.txt" 29 | ) 30 | _ = NewFile(in) 31 | _, err := os.Stat(in) 32 | if err != nil { 33 | t.Errorf("New file error %v", err) 34 | } 35 | os.Remove(in) 36 | os.Remove(filepath.Dir(in)) 37 | } 38 | -------------------------------------------------------------------------------- /tools/logfactory/levels.go: -------------------------------------------------------------------------------- 1 | package logfactory 2 | 3 | import ( 4 | "DIEM-API/tools/filefactory" 5 | "os" 6 | "path" 7 | 8 | "github.com/rs/zerolog" 9 | ) 10 | 11 | var zlogE zerolog.Logger 12 | var zlogA zerolog.Logger 13 | 14 | type access struct{} 15 | type error struct{} 16 | type stdErr struct{} 17 | 18 | var Access = access{} 19 | var Error = error{} 20 | var StdErr = stdErr{} 21 | 22 | func InitLog() { 23 | 24 | zlogStderr := zerolog.ConsoleWriter{Out: os.Stderr} 25 | 26 | zlogE = zerolog.New(zlogStderr).With().Timestamp().Logger() 27 | zlogA = zerolog.New(zlogStderr).With().Timestamp().Logger() // comment is to disable access log 28 | } 29 | 30 | func newFactory(level, logPath string) *factory { 31 | aLogger := new(factory) 32 | aLogger.level = level 33 | aLogger.fullName = path.Join(logPath, level, level+".log") 34 | aLogger.Writer = filefactory.NewFile(aLogger.fullName) 35 | 36 | return aLogger 37 | } 38 | 39 | func (e *error) Debug() *zerolog.Event { 40 | return zlogE.Debug() 41 | } 42 | 43 | func (e *error) Error() *zerolog.Event { 44 | return zlogE.Error() 45 | } 46 | 47 | func (l *access) Debug() *zerolog.Event { 48 | return zlogA.Debug() 49 | } 50 | 51 | func (l *access) Error() *zerolog.Event { 52 | return zlogA.Error() 53 | } 54 | -------------------------------------------------------------------------------- /tools/logfactory/logrotate.go: -------------------------------------------------------------------------------- 1 | package logfactory 2 | 3 | import ( 4 | "DIEM-API/tools/filefactory" 5 | "io" 6 | "time" 7 | ) 8 | 9 | const ( 10 | dayHour = 23 11 | dayMinute = 59 12 | daySecond = 60 13 | ) 14 | 15 | type factory struct { 16 | Writer io.Writer 17 | level string 18 | fullName string 19 | } 20 | 21 | // rollover logfile everyday. 22 | func (l *factory) doRollover(now time.Time) { 23 | filefactory.CopyFile(l.fullName, l.fullName+now.Format("2006-01-02")) 24 | l.Writer = filefactory.NewFile(l.fullName) 25 | } 26 | 27 | // run rotate at 00:00:00 28 | func (l *factory) rotate() { 29 | for { 30 | now := time.Now() 31 | restHour := time.Hour * time.Duration(dayHour-now.Hour()) 32 | restMinute := time.Minute * time.Duration(dayMinute-now.Minute()) 33 | restSecond := time.Second * time.Duration(daySecond-now.Second()) 34 | t := time.NewTimer(restHour + restMinute + restSecond) 35 | <-t.C 36 | l.doRollover(now) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tools/logfactory/logrotate_test.go: -------------------------------------------------------------------------------- 1 | package logfactory 2 | 3 | import "testing" 4 | 5 | func TestNewLogger(t *testing.T) { 6 | //t.Log(newFactory("error")) 7 | } 8 | -------------------------------------------------------------------------------- /tools/tomlparser/parser.go: -------------------------------------------------------------------------------- 1 | package tomlparser 2 | 3 | import ( 4 | T "DIEM-API/tools" 5 | "path/filepath" 6 | 7 | "github.com/pelletier/go-toml" 8 | ) 9 | 10 | var Config *toml.Tree 11 | 12 | func LoadTOML(path string) { 13 | var err error 14 | Config, err = toml.LoadFile(path) 15 | T.CheckFatalError(err, false) 16 | } 17 | 18 | func GetString(key string) string { 19 | value := Config.Get(key) 20 | return T.Str(value) 21 | } 22 | 23 | func GetInt(key string) int { 24 | value := Config.Get(key) 25 | return T.Int(value) 26 | } 27 | 28 | func GetBool(key string) bool { 29 | value := Config.Get(key) 30 | return value.(bool) 31 | } 32 | 33 | func ConfigAbsPath(key string) string { 34 | base := GetString("config_dir") 35 | file := GetString(key) 36 | if filepath.IsAbs(file) { 37 | return file 38 | } 39 | return filepath.Join(base, file) 40 | } 41 | -------------------------------------------------------------------------------- /tools/tools.go: -------------------------------------------------------------------------------- 1 | package tools 2 | 3 | import ( 4 | Logf "DIEM-API/tools/logfactory" 5 | "encoding/binary" 6 | "fmt" 7 | "io/ioutil" 8 | "os" 9 | "strconv" 10 | ) 11 | 12 | // check exception, just log. can't crash the service 13 | func CheckException(err error, message string) { 14 | if err != nil { 15 | Logf.Error.Error().Msg(message) 16 | } 17 | } 18 | 19 | // check error, if not mute, crash the service 20 | func CheckFatalError(err error, mute bool) { 21 | if err != nil && !mute { 22 | panic(err) 23 | } 24 | } 25 | 26 | // cast some type to string 27 | func Str(arg interface{}) (ret string) { 28 | switch arg.(type) { 29 | case int64: 30 | ret = strconv.FormatInt(arg.(int64), 10) 31 | case int: 32 | ret = strconv.Itoa(arg.(int)) 33 | case float64: 34 | ret = fmt.Sprintf("%.1f", arg.(float64)) 35 | case uint32: 36 | ret = strconv.Itoa(int(arg.(uint32))) 37 | case uint64: 38 | ret = strconv.Itoa(int(arg.(uint64))) 39 | case string: 40 | ret = arg.(string) 41 | } 42 | return 43 | } 44 | 45 | // cast some type to int 46 | func Int(arg interface{}) (ret int) { 47 | switch arg.(type) { 48 | case string: 49 | ret, _ = strconv.Atoi(arg.(string)) 50 | case int64: 51 | ret = int(arg.(int64)) 52 | } 53 | return 54 | } 55 | 56 | // load json file 57 | func LoadJSON(JSONPath string) []byte { 58 | jsonFile, err := os.Open(JSONPath) 59 | CheckFatalError(err, false) 60 | 61 | byteValue, err := ioutil.ReadAll(jsonFile) 62 | CheckFatalError(err, false) 63 | 64 | return byteValue 65 | } 66 | 67 | func Int32ToBytes(num int) []byte { 68 | key := make([]byte, 4) 69 | binary.BigEndian.PutUint32(key, uint32(num)) 70 | return key 71 | } 72 | 73 | func Min(num1, num2 int) int { 74 | if num1 < num2 { 75 | return num1 76 | } 77 | return num2 78 | } 79 | 80 | func Max(num1, num2 int) int { 81 | if num1 < num2 { 82 | return num2 83 | } 84 | return num1 85 | } 86 | -------------------------------------------------------------------------------- /tools/tools_test.go: -------------------------------------------------------------------------------- 1 | package tools 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestStr(t *testing.T) { 8 | var ( 9 | in = 43 10 | expected = "43" 11 | ) 12 | out := Str(in) 13 | if out != expected { 14 | t.Errorf("Str(%d) = %s; expected %s", in, out, expected) 15 | } 16 | } 17 | 18 | func TestStr2(t *testing.T) { 19 | var ( 20 | in = 0.2143 21 | expected = "0.214300" 22 | ) 23 | out := Str(in) 24 | if out != expected { 25 | t.Errorf("Str(%f) = %s; expected %s", in, out, expected) 26 | } 27 | } 28 | 29 | func TestInt(t *testing.T) { 30 | var ( 31 | in = "325" 32 | expected = 325 33 | ) 34 | out := Int(in) 35 | if out != expected { 36 | t.Errorf("Int(%s) = %d; expected %d", in, out, expected) 37 | } 38 | } 39 | 40 | func TestLoadJSON(t *testing.T) { 41 | var ( 42 | in = "/home/web/server/api/diem-api/credential.json" 43 | ) 44 | byteValue := LoadJSON(in) 45 | if byteValue[0] != 123 { 46 | t.Errorf("Error load json, expected first char is `{`, got %c ", byteValue[0]) 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /views/blogsearch.go: -------------------------------------------------------------------------------- 1 | package views 2 | 3 | import ( 4 | B "DIEM-API/models/blogs" 5 | I "DIEM-API/rpcserver" 6 | T "DIEM-API/tools" 7 | "fmt" 8 | "time" 9 | 10 | "github.com/gin-gonic/gin" 11 | ) 12 | 13 | var SearchPool *I.Pool 14 | 15 | func InitSearchPool(type_ string, addr string, poolSize int) { 16 | if type_ == "uds" { 17 | SearchPool = I.NewPool(poolSize, addr, I.DialUDS) 18 | } else { 19 | SearchPool = I.NewPool(poolSize, addr, I.DialTCP) 20 | } 21 | } 22 | 23 | func checkSearchParams(ctx *gin.Context, p *B.Params) { 24 | err := ctx.Bind(p) 25 | if err != nil { 26 | ctx.JSON(400, gin.H{ 27 | "error": err.Error(), 28 | }) 29 | ctx.Abort() 30 | } 31 | } 32 | 33 | func validDateRange(r string) string { 34 | if r == "" { 35 | return fmt.Sprintf("%d~%d", 0, time.Now().Unix()) 36 | } 37 | var begin, end int64 38 | f := func(s *int64, t time.Time, err error) { 39 | if err == nil { 40 | *s = t.Unix() 41 | } 42 | } 43 | lenOfTime := 10 44 | if len(r) <= lenOfTime { 45 | return "" 46 | } 47 | if r[lenOfTime] == '~' { 48 | beginTime, err := time.Parse("2006-01-02", r[:lenOfTime]) 49 | f(&begin, beginTime, err) 50 | r = r[lenOfTime:] 51 | } 52 | if r[0] == '~' { 53 | endTime, err := time.Parse("2006-01-02", r[1:]) 54 | f(&end, endTime, err) 55 | } 56 | return fmt.Sprintf("%d~%d", begin, end) 57 | } 58 | 59 | func execute(ctx *gin.Context) []byte { 60 | p := B.Params{} 61 | // checkSearchParams(ctx, p) 62 | err := B.BindStruct(ctx.Request.URL.Query(), &p) 63 | if err != nil { 64 | ctx.JSON(200, gin.H{ 65 | "error": err.Error(), 66 | }) 67 | ctx.Abort() 68 | return []byte{} 69 | } 70 | // v, err := json.Marshal(p) 71 | // log.Println(string(v)) 72 | T.CheckException(err, "decode json error") 73 | ret := I.Search(p.Paginate, p.DateRange, p.Terms, p.Query, SearchPool) 74 | if len(ret) == 0 { 75 | return []byte("{\"msg\": \"error, try again later.\"}") 76 | } 77 | return []byte(T.Str(ret[0])) 78 | } 79 | 80 | func BlogSearchViews(ctx *gin.Context) { 81 | ret := execute(ctx) 82 | ctx.Header("Content-Type", "application/json") 83 | ctx.Writer.Write(ret) 84 | } 85 | -------------------------------------------------------------------------------- /views/gaviews.go: -------------------------------------------------------------------------------- 1 | package views 2 | 3 | import ( 4 | M "DIEM-API/models" 5 | G "DIEM-API/models/googleanalytics" 6 | 7 | "github.com/gin-gonic/gin" 8 | ) 9 | 10 | func checkGAParams(ctx *gin.Context, p *G.Params) { 11 | err := ctx.Bind(p) 12 | 13 | if err != nil { 14 | ctx.JSON(400, gin.H{ 15 | "error": err.Error(), 16 | }) 17 | ctx.Abort() 18 | } 19 | } 20 | 21 | func GAViews(ctx *gin.Context) { 22 | p := new(G.Params) 23 | checkGAParams(ctx, p) 24 | pageView := M.GetReport(p) 25 | ctx.JSON(200, pageView) 26 | } 27 | -------------------------------------------------------------------------------- /views/hitokoto.go: -------------------------------------------------------------------------------- 1 | package views 2 | 3 | import ( 4 | M "DIEM-API/models" 5 | H "DIEM-API/models/hitokoto" 6 | T "DIEM-API/tools" 7 | "bytes" 8 | "math/rand" 9 | "time" 10 | 11 | "github.com/gin-gonic/gin" 12 | bolt "go.etcd.io/bbolt" 13 | ) 14 | 15 | var r = rand.New(rand.NewSource(time.Now().UnixNano())) 16 | 17 | func JSONFormat(ctx *gin.Context, info H.HitoInfo) { 18 | ctx.JSON(200, info) 19 | } 20 | 21 | func PlainFormat(ctx *gin.Context, info H.HitoInfo) { 22 | ctx.String(200, info.Hito+"——「"+info.Source+"」") 23 | } 24 | 25 | func JSONP(ctx *gin.Context, info H.HitoInfo) { 26 | ctx.JSONP(200, info) 27 | } 28 | 29 | func JSFormat(ctx *gin.Context, info H.HitoInfo) { 30 | var buf bytes.Buffer 31 | buf.WriteString("var hitokoto=\"") 32 | buf.WriteString(info.Hito) 33 | buf.WriteString("——「") 34 | buf.WriteString(info.Source) 35 | buf.WriteString("」\";var dom=document.querySelector('.hitokoto');") 36 | buf.WriteString("Array.isArray(dom)?dom[0].innerText=hitokoto:dom.innerText=hitokoto;") 37 | ctx.Data(200, "text/javascript; charset=utf-8", buf.Bytes()) 38 | } 39 | 40 | // attempt to bind url params 41 | func checkParams(ctx *gin.Context, p *H.Params) { 42 | err := ctx.Bind(p) 43 | 44 | if err != nil { 45 | ctx.JSON(400, gin.H{ 46 | "error": err.Error(), 47 | }) 48 | ctx.Abort() 49 | } 50 | } 51 | 52 | func fetchHitokoto(length int) (record H.HitoInfo) { 53 | randomNumber := r.Intn(H.IndexOf(length)) 54 | key := T.Int32ToBytes(randomNumber) 55 | M.BoltDB.Read(func(tx *bolt.Tx) error { 56 | b := tx.Bucket(H.HitoBucket) 57 | record = H.LoadRecordFromBytes(b.Get(key)).Hitokoto 58 | return nil 59 | }) 60 | return record 61 | } 62 | 63 | func Hitokoto(ctx *gin.Context) { 64 | p := new(H.Params) 65 | 66 | checkParams(ctx, p) 67 | info := fetchHitokoto(p.Length) 68 | if p.Callback != "" { 69 | JSONP(ctx, info) 70 | } else if p.Encode == "js" { 71 | JSFormat(ctx, info) 72 | } else if p.Encode == "json" { 73 | JSONFormat(ctx, info) 74 | } else { 75 | PlainFormat(ctx, info) 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /views/interface.go: -------------------------------------------------------------------------------- 1 | package views 2 | 3 | import ( 4 | "strings" 5 | 6 | "github.com/gin-gonic/gin" 7 | ) 8 | 9 | func Register(r *gin.Engine, service string) { 10 | if strings.HasPrefix("hitokoto", service) { 11 | r.GET("/hitokoto/v2/", Hitokoto) 12 | } 13 | if strings.HasPrefix("analytics", service) { 14 | r.GET("/gaviews/v1/", GAViews) 15 | } 16 | if strings.HasPrefix("search", service) { 17 | r.GET("/blog-search/v1/", BlogSearchViews) 18 | } 19 | } 20 | --------------------------------------------------------------------------------