You've already forked golang-saas-starter-kit
mirror of
https://github.com/raseels-repos/golang-saas-starter-kit.git
synced 2025-06-15 00:15:15 +02:00
Imported github.com/ardanlabs/service as base example project
This commit is contained in:
33
example-project/CONTRIBUTORS
Normal file
33
example-project/CONTRIBUTORS
Normal file
@ -0,0 +1,33 @@
|
||||
# This is the official list of people who can contribute
|
||||
# (and typically have contributed) code to the gotraining repository.
|
||||
#
|
||||
# Names should be added to this file only after verifying that
|
||||
# the individual or the individual's organization has agreed to
|
||||
# the appropriate Contributor License Agreement, found here:
|
||||
#
|
||||
# http://code.google.com/legal/individual-cla-v1.0.html
|
||||
# http://code.google.com/legal/corporate-cla-v1.0.html
|
||||
#
|
||||
# The agreement for individuals can be filled out on the web.
|
||||
|
||||
# Names should be added to this file like so:
|
||||
# Name <email address>
|
||||
#
|
||||
# An entry with two email addresses specifies that the
|
||||
# first address should be used in the submit logs and
|
||||
# that the second address should be recognized as the
|
||||
# same person when interacting with Rietveld.
|
||||
|
||||
# Please keep the list sorted.
|
||||
|
||||
Arash Bina <arash@arash.io>
|
||||
Askar Sagyndyk <superwhykz@gmail.com>
|
||||
Bob Cao <3308031+bobintornado@users.noreply.github.com>
|
||||
Ed Gonzo <Ed@ardanstudios.com>
|
||||
Farrukh Kurbanov <farrukhkurbanov@Administrators-MacBook-Pro.local>
|
||||
Jacob Walker <jacob@ardanlabs.com>
|
||||
Jeremy Stone <slycrel@gmail.com>
|
||||
Nick Stogner <nstogner@users.noreply.github.com>
|
||||
William Kennedy <bill@ardanlabs.com>
|
||||
Wyatt Johnson <wyattjoh@gmail.com>
|
||||
Zachary Johnson <zachjohnsondev@gmail.com>
|
201
example-project/LICENSE
Normal file
201
example-project/LICENSE
Normal file
@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
148
example-project/README.md
Normal file
148
example-project/README.md
Normal file
@ -0,0 +1,148 @@
|
||||
# Ultimate Service
|
||||
|
||||
Copyright 2018, Ardan Labs
|
||||
info@ardanlabs.com
|
||||
|
||||
## Licensing
|
||||
|
||||
```
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
```
|
||||
|
||||
## Description
|
||||
|
||||
Service is a project that provides a starter-kit for a REST based web service. It provides best practices around Go web services using POD architecture and design. It contains the following features:
|
||||
|
||||
* Minimal application web framework.
|
||||
* Middleware integration.
|
||||
* Database support using MongoDB.
|
||||
* CRUD based pattern.
|
||||
* Distributed logging and tracing.
|
||||
* Testing patterns.
|
||||
* User authentication.
|
||||
* POD architecture with sidecars for metrics and tracing.
|
||||
* Use of Docker, Docker Compose, and Makefiles.
|
||||
* Vendoring dependencies with Modules, requires Go 1.11 or higher.
|
||||
|
||||
## Local Installation
|
||||
|
||||
This project contains three services and uses 3rd party services such as MongoDB and Zipkin. Docker is required to run this software on your local machine.
|
||||
|
||||
### Getting the project
|
||||
|
||||
You can use the traditional `go get` command to download this project into your configured GOPATH.
|
||||
|
||||
```
|
||||
$ go get -u geeks-accelerator/oss/saas-starter-kit/example-project
|
||||
```
|
||||
|
||||
### Go Modules
|
||||
|
||||
This project is using Go Module support for vendoring dependencies. We are using the `tidy` and `vendor` commands to maintain the dependencies and make sure the project can create reproducible builds. This project assumes the source code will be inside your GOPATH within the traditional location.
|
||||
|
||||
```
|
||||
cd $GOPATH/src/geeks-accelerator/oss/saas-starter-kit/example-project
|
||||
GO111MODULE=on go mod tidy
|
||||
GO111MODULE=on go mod vendor
|
||||
```
|
||||
|
||||
### Installing Docker
|
||||
|
||||
Docker is a critical component to managing and running this project. It kills me to just send you to the Docker installation page but it's all I got for now.
|
||||
|
||||
https://docs.docker.com/install/
|
||||
|
||||
If you are having problems installing docker reach out or jump on [Gopher Slack](http://invite.slack.golangbridge.org/) for help.
|
||||
|
||||
## Running The Project
|
||||
|
||||
All the source code, including any dependencies, have been vendored into the project. There is a single `dockerfile`and a `docker-compose` file that knows how to build and run all the services.
|
||||
|
||||
A `makefile` has also been provide to make building, running and testing the software easier.
|
||||
|
||||
### Building the project
|
||||
|
||||
Navigate to the root of the project and use the `makefile` to build all of the services.
|
||||
|
||||
```
|
||||
$ cd $GOPATH/src/geeks-accelerator/oss/saas-starter-kit/example-project
|
||||
$ make all
|
||||
```
|
||||
|
||||
### Running the project
|
||||
|
||||
Navigate to the root of the project and use the `makefile` to run all of the services.
|
||||
|
||||
```
|
||||
$ cd $GOPATH/src/geeks-accelerator/oss/saas-starter-kit/example-project
|
||||
$ make up
|
||||
```
|
||||
|
||||
The `make up` command will leverage Docker Compose to run all the services, including the 3rd party services. The first time to run this command, Docker will download the required images for the 3rd party services.
|
||||
|
||||
Default configuration is set which should be valid for most systems. Use the `docker-compose.yaml` file to configure the services differently is necessary. Email me if you have issues or questions.
|
||||
|
||||
### Stopping the project
|
||||
|
||||
You can hit <ctrl>C in the terminal window running `make up`. Once that shutdown sequence is complete, it is important to run the `make down` command.
|
||||
|
||||
```
|
||||
$ <ctrl>C
|
||||
$ make down
|
||||
```
|
||||
|
||||
Running `make down` will properly stop and terminate the Docker Compose session.
|
||||
|
||||
## About The Project
|
||||
|
||||
The service provides record keeping for someone running a multi-family garage sale. Authenticated users can maintain a list of products for sale.
|
||||
|
||||
<!--The service uses the following models:-->
|
||||
|
||||
<!--<img src="https://raw.githubusercontent.com/ardanlabs/service/master/models.jpg" alt="Garage Sale Service Models" title="Garage Sale Service Models" />-->
|
||||
|
||||
<!--(Diagram generated with draw.io using `models.xml` file)-->
|
||||
|
||||
### Making Requests
|
||||
|
||||
#### Initial User
|
||||
|
||||
To make a request to the service you must have an authenticated user. Users can be created with the API but an initial admin user must first be created. While the service is running you can create the initial user with the command `make admin`
|
||||
|
||||
```
|
||||
$ make admin
|
||||
```
|
||||
|
||||
This will create a user with email `admin@example.com` and password `gophers`.
|
||||
|
||||
#### Authenticating
|
||||
|
||||
Before any authenticated requests can be sent you must acquire an auth token. Make a request using HTTP Basic auth with your email and password to get the token.
|
||||
|
||||
```
|
||||
$ curl --user "admin@example.com:gophers" http://localhost:3000/v1/users/token
|
||||
```
|
||||
|
||||
I suggest putting the resulting token in an environment variable like `$TOKEN`.
|
||||
|
||||
#### Authenticated Requests
|
||||
|
||||
To make authenticated requests put the token in the `Authorization` header with the `Bearer ` prefix.
|
||||
|
||||
```
|
||||
$ curl -H "Authorization: Bearer ${TOKEN}" http://localhost:3000/v1/users
|
||||
```
|
||||
|
||||
## What's Next
|
||||
|
||||
We are in the process of writing more documentation about this code. Classes are being finalized as part of the Ultimate series.
|
137
example-project/cmd/sales-admin/main.go
Normal file
137
example-project/cmd/sales-admin/main.go
Normal file
@ -0,0 +1,137 @@
|
||||
// This program performs administrative tasks for the garage sale service.
|
||||
//
|
||||
// Run it with --cmd keygen or --cmd useradd
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/db"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/flag"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/user"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// =========================================================================
|
||||
// Logging
|
||||
|
||||
log := log.New(os.Stdout, "sales-admin : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
|
||||
|
||||
// =========================================================================
|
||||
// Configuration
|
||||
|
||||
var cfg struct {
|
||||
CMD string `envconfig:"CMD"`
|
||||
DB struct {
|
||||
DialTimeout time.Duration `default:"5s" envconfig:"DIAL_TIMEOUT"`
|
||||
Host string `default:"localhost:27017/gotraining" envconfig:"HOST"`
|
||||
}
|
||||
Auth struct {
|
||||
PrivateKeyFile string `default:"private.pem" envconfig:"PRIVATE_KEY_FILE"`
|
||||
}
|
||||
User struct {
|
||||
Email string
|
||||
Password string
|
||||
}
|
||||
}
|
||||
|
||||
if err := envconfig.Process("SALES", &cfg); err != nil {
|
||||
log.Fatalf("main : Parsing Config : %v", err)
|
||||
}
|
||||
|
||||
if err := flag.Process(&cfg); err != nil {
|
||||
if err != flag.ErrHelp {
|
||||
log.Fatalf("main : Parsing Command Line : %v", err)
|
||||
}
|
||||
return // We displayed help.
|
||||
}
|
||||
|
||||
var err error
|
||||
switch cfg.CMD {
|
||||
case "keygen":
|
||||
err = keygen(cfg.Auth.PrivateKeyFile)
|
||||
case "useradd":
|
||||
err = useradd(cfg.DB.Host, cfg.DB.DialTimeout, cfg.User.Email, cfg.User.Password)
|
||||
default:
|
||||
err = errors.New("Must provide --cmd keygen or --cmd useradd")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// keygen creates an x509 private key for signing auth tokens.
|
||||
func keygen(path string) error {
|
||||
|
||||
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "generating keys")
|
||||
}
|
||||
|
||||
file, err := os.Create(path)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "creating private file")
|
||||
}
|
||||
|
||||
block := pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: x509.MarshalPKCS1PrivateKey(key),
|
||||
}
|
||||
|
||||
if err := pem.Encode(file, &block); err != nil {
|
||||
return errors.Wrap(err, "encoding to private file")
|
||||
}
|
||||
|
||||
if err := file.Close(); err != nil {
|
||||
return errors.Wrap(err, "closing private file")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func useradd(dbHost string, dbTimeout time.Duration, email, pass string) error {
|
||||
|
||||
dbConn, err := db.New(dbHost, dbTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dbConn.Close()
|
||||
|
||||
if email == "" {
|
||||
return errors.New("Must provide --user_email")
|
||||
}
|
||||
if pass == "" {
|
||||
return errors.New("Must provide --user_password or set the env var SALES_USER_PASSWORD")
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
newU := user.NewUser{
|
||||
Email: email,
|
||||
Password: pass,
|
||||
PasswordConfirm: pass,
|
||||
Roles: []string{auth.RoleAdmin, auth.RoleUser},
|
||||
}
|
||||
|
||||
usr, err := user.Create(ctx, dbConn, &newU, time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("User created with id: %v\n", usr.ID.Hex())
|
||||
return nil
|
||||
}
|
38
example-project/cmd/sales-api/handlers/check.go
Normal file
38
example-project/cmd/sales-api/handlers/check.go
Normal file
@ -0,0 +1,38 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/db"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// Check provides support for orchestration health checks.
|
||||
type Check struct {
|
||||
MasterDB *db.DB
|
||||
|
||||
// ADD OTHER STATE LIKE THE LOGGER IF NEEDED.
|
||||
}
|
||||
|
||||
// Health validates the service is healthy and ready to accept requests.
|
||||
func (c *Check) Health(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "handlers.Check.Health")
|
||||
defer span.End()
|
||||
|
||||
dbConn := c.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
if err := dbConn.StatusCheck(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
status := struct {
|
||||
Status string `json:"status"`
|
||||
}{
|
||||
Status: "ok",
|
||||
}
|
||||
|
||||
return web.Respond(ctx, w, status, http.StatusOK)
|
||||
}
|
140
example-project/cmd/sales-api/handlers/product.go
Normal file
140
example-project/cmd/sales-api/handlers/product.go
Normal file
@ -0,0 +1,140 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/db"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/product"
|
||||
"github.com/pkg/errors"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// Product represents the Product API method handler set.
|
||||
type Product struct {
|
||||
MasterDB *db.DB
|
||||
|
||||
// ADD OTHER STATE LIKE THE LOGGER IF NEEDED.
|
||||
}
|
||||
|
||||
// List returns all the existing products in the system.
|
||||
func (p *Product) List(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "handlers.Product.List")
|
||||
defer span.End()
|
||||
|
||||
dbConn := p.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
products, err := product.List(ctx, dbConn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return web.Respond(ctx, w, products, http.StatusOK)
|
||||
}
|
||||
|
||||
// Retrieve returns the specified product from the system.
|
||||
func (p *Product) Retrieve(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "handlers.Product.Retrieve")
|
||||
defer span.End()
|
||||
|
||||
dbConn := p.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
prod, err := product.Retrieve(ctx, dbConn, params["id"])
|
||||
if err != nil {
|
||||
switch err {
|
||||
case product.ErrInvalidID:
|
||||
return web.NewRequestError(err, http.StatusBadRequest)
|
||||
case product.ErrNotFound:
|
||||
return web.NewRequestError(err, http.StatusNotFound)
|
||||
default:
|
||||
return errors.Wrapf(err, "ID: %s", params["id"])
|
||||
}
|
||||
}
|
||||
|
||||
return web.Respond(ctx, w, prod, http.StatusOK)
|
||||
}
|
||||
|
||||
// Create inserts a new product into the system.
|
||||
func (p *Product) Create(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "handlers.Product.Create")
|
||||
defer span.End()
|
||||
|
||||
dbConn := p.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
v, ok := ctx.Value(web.KeyValues).(*web.Values)
|
||||
if !ok {
|
||||
return web.NewShutdownError("web value missing from context")
|
||||
}
|
||||
|
||||
var np product.NewProduct
|
||||
if err := web.Decode(r, &np); err != nil {
|
||||
return errors.Wrap(err, "")
|
||||
}
|
||||
|
||||
nUsr, err := product.Create(ctx, dbConn, &np, v.Now)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Product: %+v", &np)
|
||||
}
|
||||
|
||||
return web.Respond(ctx, w, nUsr, http.StatusCreated)
|
||||
}
|
||||
|
||||
// Update updates the specified product in the system.
|
||||
func (p *Product) Update(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "handlers.Product.Update")
|
||||
defer span.End()
|
||||
|
||||
dbConn := p.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
v, ok := ctx.Value(web.KeyValues).(*web.Values)
|
||||
if !ok {
|
||||
return web.NewShutdownError("web value missing from context")
|
||||
}
|
||||
|
||||
var up product.UpdateProduct
|
||||
if err := web.Decode(r, &up); err != nil {
|
||||
return errors.Wrap(err, "")
|
||||
}
|
||||
|
||||
err := product.Update(ctx, dbConn, params["id"], up, v.Now)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case product.ErrInvalidID:
|
||||
return web.NewRequestError(err, http.StatusBadRequest)
|
||||
case product.ErrNotFound:
|
||||
return web.NewRequestError(err, http.StatusNotFound)
|
||||
default:
|
||||
return errors.Wrapf(err, "ID: %s Update: %+v", params["id"], up)
|
||||
}
|
||||
}
|
||||
|
||||
return web.Respond(ctx, w, nil, http.StatusNoContent)
|
||||
}
|
||||
|
||||
// Delete removes the specified product from the system.
|
||||
func (p *Product) Delete(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "handlers.Product.Delete")
|
||||
defer span.End()
|
||||
|
||||
dbConn := p.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
err := product.Delete(ctx, dbConn, params["id"])
|
||||
if err != nil {
|
||||
switch err {
|
||||
case product.ErrInvalidID:
|
||||
return web.NewRequestError(err, http.StatusBadRequest)
|
||||
case product.ErrNotFound:
|
||||
return web.NewRequestError(err, http.StatusNotFound)
|
||||
default:
|
||||
return errors.Wrapf(err, "Id: %s", params["id"])
|
||||
}
|
||||
}
|
||||
|
||||
return web.Respond(ctx, w, nil, http.StatusNoContent)
|
||||
}
|
51
example-project/cmd/sales-api/handlers/routes.go
Normal file
51
example-project/cmd/sales-api/handlers/routes.go
Normal file
@ -0,0 +1,51 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/mid"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/db"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
)
|
||||
|
||||
// API returns a handler for a set of routes.
|
||||
func API(shutdown chan os.Signal, log *log.Logger, masterDB *db.DB, authenticator *auth.Authenticator) http.Handler {
|
||||
|
||||
// Construct the web.App which holds all routes as well as common Middleware.
|
||||
app := web.NewApp(shutdown, log, mid.Logger(log), mid.Errors(log), mid.Metrics(), mid.Panics())
|
||||
|
||||
// Register health check endpoint. This route is not authenticated.
|
||||
check := Check{
|
||||
MasterDB: masterDB,
|
||||
}
|
||||
app.Handle("GET", "/v1/health", check.Health)
|
||||
|
||||
// Register user management and authentication endpoints.
|
||||
u := User{
|
||||
MasterDB: masterDB,
|
||||
TokenGenerator: authenticator,
|
||||
}
|
||||
app.Handle("GET", "/v1/users", u.List, mid.Authenticate(authenticator), mid.HasRole(auth.RoleAdmin))
|
||||
app.Handle("POST", "/v1/users", u.Create, mid.Authenticate(authenticator), mid.HasRole(auth.RoleAdmin))
|
||||
app.Handle("GET", "/v1/users/:id", u.Retrieve, mid.Authenticate(authenticator))
|
||||
app.Handle("PUT", "/v1/users/:id", u.Update, mid.Authenticate(authenticator), mid.HasRole(auth.RoleAdmin))
|
||||
app.Handle("DELETE", "/v1/users/:id", u.Delete, mid.Authenticate(authenticator), mid.HasRole(auth.RoleAdmin))
|
||||
|
||||
// This route is not authenticated
|
||||
app.Handle("GET", "/v1/users/token", u.Token)
|
||||
|
||||
// Register product and sale endpoints.
|
||||
p := Product{
|
||||
MasterDB: masterDB,
|
||||
}
|
||||
app.Handle("GET", "/v1/products", p.List, mid.Authenticate(authenticator))
|
||||
app.Handle("POST", "/v1/products", p.Create, mid.Authenticate(authenticator))
|
||||
app.Handle("GET", "/v1/products/:id", p.Retrieve, mid.Authenticate(authenticator))
|
||||
app.Handle("PUT", "/v1/products/:id", p.Update, mid.Authenticate(authenticator))
|
||||
app.Handle("DELETE", "/v1/products/:id", p.Delete, mid.Authenticate(authenticator))
|
||||
|
||||
return app
|
||||
}
|
186
example-project/cmd/sales-api/handlers/user.go
Normal file
186
example-project/cmd/sales-api/handlers/user.go
Normal file
@ -0,0 +1,186 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/db"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/user"
|
||||
"github.com/pkg/errors"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// User represents the User API method handler set.
|
||||
type User struct {
|
||||
MasterDB *db.DB
|
||||
TokenGenerator user.TokenGenerator
|
||||
|
||||
// ADD OTHER STATE LIKE THE LOGGER AND CONFIG HERE.
|
||||
}
|
||||
|
||||
// List returns all the existing users in the system.
|
||||
func (u *User) List(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "handlers.User.List")
|
||||
defer span.End()
|
||||
|
||||
dbConn := u.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
usrs, err := user.List(ctx, dbConn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return web.Respond(ctx, w, usrs, http.StatusOK)
|
||||
}
|
||||
|
||||
// Retrieve returns the specified user from the system.
|
||||
func (u *User) Retrieve(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "handlers.User.Retrieve")
|
||||
defer span.End()
|
||||
|
||||
dbConn := u.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
claims, ok := ctx.Value(auth.Key).(auth.Claims)
|
||||
if !ok {
|
||||
return errors.New("claims missing from context")
|
||||
}
|
||||
|
||||
usr, err := user.Retrieve(ctx, claims, dbConn, params["id"])
|
||||
if err != nil {
|
||||
switch err {
|
||||
case user.ErrInvalidID:
|
||||
return web.NewRequestError(err, http.StatusBadRequest)
|
||||
case user.ErrNotFound:
|
||||
return web.NewRequestError(err, http.StatusNotFound)
|
||||
case user.ErrForbidden:
|
||||
return web.NewRequestError(err, http.StatusForbidden)
|
||||
default:
|
||||
return errors.Wrapf(err, "Id: %s", params["id"])
|
||||
}
|
||||
}
|
||||
|
||||
return web.Respond(ctx, w, usr, http.StatusOK)
|
||||
}
|
||||
|
||||
// Create inserts a new user into the system.
|
||||
func (u *User) Create(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "handlers.User.Create")
|
||||
defer span.End()
|
||||
|
||||
dbConn := u.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
v, ok := ctx.Value(web.KeyValues).(*web.Values)
|
||||
if !ok {
|
||||
return web.NewShutdownError("web value missing from context")
|
||||
}
|
||||
|
||||
var newU user.NewUser
|
||||
if err := web.Decode(r, &newU); err != nil {
|
||||
return errors.Wrap(err, "")
|
||||
}
|
||||
|
||||
usr, err := user.Create(ctx, dbConn, &newU, v.Now)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "User: %+v", &usr)
|
||||
}
|
||||
|
||||
return web.Respond(ctx, w, usr, http.StatusCreated)
|
||||
}
|
||||
|
||||
// Update updates the specified user in the system.
|
||||
func (u *User) Update(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "handlers.User.Update")
|
||||
defer span.End()
|
||||
|
||||
dbConn := u.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
v, ok := ctx.Value(web.KeyValues).(*web.Values)
|
||||
if !ok {
|
||||
return web.NewShutdownError("web value missing from context")
|
||||
}
|
||||
|
||||
var upd user.UpdateUser
|
||||
if err := web.Decode(r, &upd); err != nil {
|
||||
return errors.Wrap(err, "")
|
||||
}
|
||||
|
||||
err := user.Update(ctx, dbConn, params["id"], &upd, v.Now)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case user.ErrInvalidID:
|
||||
return web.NewRequestError(err, http.StatusBadRequest)
|
||||
case user.ErrNotFound:
|
||||
return web.NewRequestError(err, http.StatusNotFound)
|
||||
case user.ErrForbidden:
|
||||
return web.NewRequestError(err, http.StatusForbidden)
|
||||
default:
|
||||
return errors.Wrapf(err, "Id: %s User: %+v", params["id"], &upd)
|
||||
}
|
||||
}
|
||||
|
||||
return web.Respond(ctx, w, nil, http.StatusNoContent)
|
||||
}
|
||||
|
||||
// Delete removes the specified user from the system.
|
||||
func (u *User) Delete(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "handlers.User.Delete")
|
||||
defer span.End()
|
||||
|
||||
dbConn := u.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
err := user.Delete(ctx, dbConn, params["id"])
|
||||
if err != nil {
|
||||
switch err {
|
||||
case user.ErrInvalidID:
|
||||
return web.NewRequestError(err, http.StatusBadRequest)
|
||||
case user.ErrNotFound:
|
||||
return web.NewRequestError(err, http.StatusNotFound)
|
||||
case user.ErrForbidden:
|
||||
return web.NewRequestError(err, http.StatusForbidden)
|
||||
default:
|
||||
return errors.Wrapf(err, "Id: %s", params["id"])
|
||||
}
|
||||
}
|
||||
|
||||
return web.Respond(ctx, w, nil, http.StatusNoContent)
|
||||
}
|
||||
|
||||
// Token handles a request to authenticate a user. It expects a request using
|
||||
// Basic Auth with a user's email and password. It responds with a JWT.
|
||||
func (u *User) Token(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "handlers.User.Token")
|
||||
defer span.End()
|
||||
|
||||
dbConn := u.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
v, ok := ctx.Value(web.KeyValues).(*web.Values)
|
||||
if !ok {
|
||||
return web.NewShutdownError("web value missing from context")
|
||||
}
|
||||
|
||||
email, pass, ok := r.BasicAuth()
|
||||
if !ok {
|
||||
err := errors.New("must provide email and password in Basic auth")
|
||||
return web.NewRequestError(err, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
tkn, err := user.Authenticate(ctx, dbConn, u.TokenGenerator, v.Now, email, pass)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case user.ErrAuthenticationFailure:
|
||||
return web.NewRequestError(err, http.StatusUnauthorized)
|
||||
default:
|
||||
return errors.Wrap(err, "authenticating")
|
||||
}
|
||||
}
|
||||
|
||||
return web.Respond(ctx, w, tkn, http.StatusOK)
|
||||
}
|
228
example-project/cmd/sales-api/main.go
Normal file
228
example-project/cmd/sales-api/main.go
Normal file
@ -0,0 +1,228 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rsa"
|
||||
"encoding/json"
|
||||
"expvar"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/cmd/sales-api/handlers"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/db"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/flag"
|
||||
itrace "geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/trace"
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
/*
|
||||
ZipKin: http://localhost:9411
|
||||
AddLoad: hey -m GET -c 10 -n 10000 "http://localhost:3000/v1/users"
|
||||
expvarmon -ports=":3001" -endpoint="/metrics" -vars="requests,goroutines,errors,mem:memstats.Alloc"
|
||||
*/
|
||||
|
||||
/*
|
||||
Need to figure out timeouts for http service.
|
||||
You might want to reset your DB_HOST env var during test tear down.
|
||||
Service should start even without a DB running yet.
|
||||
symbols in profiles: https://github.com/golang/go/issues/23376 / https://github.com/google/pprof/pull/366
|
||||
*/
|
||||
|
||||
// build is the git version of this program. It is set using build flags in the makefile.
|
||||
var build = "develop"
|
||||
|
||||
func main() {
|
||||
|
||||
// =========================================================================
|
||||
// Logging
|
||||
|
||||
log := log.New(os.Stdout, "SALES : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
|
||||
|
||||
// =========================================================================
|
||||
// Configuration
|
||||
|
||||
var cfg struct {
|
||||
Web struct {
|
||||
APIHost string `default:"0.0.0.0:3000" envconfig:"API_HOST"`
|
||||
DebugHost string `default:"0.0.0.0:4000" envconfig:"DEBUG_HOST"`
|
||||
ReadTimeout time.Duration `default:"5s" envconfig:"READ_TIMEOUT"`
|
||||
WriteTimeout time.Duration `default:"5s" envconfig:"WRITE_TIMEOUT"`
|
||||
ShutdownTimeout time.Duration `default:"5s" envconfig:"SHUTDOWN_TIMEOUT"`
|
||||
}
|
||||
DB struct {
|
||||
DialTimeout time.Duration `default:"5s" envconfig:"DIAL_TIMEOUT"`
|
||||
Host string `default:"mongo:27017/gotraining" envconfig:"HOST"`
|
||||
}
|
||||
Trace struct {
|
||||
Host string `default:"http://tracer:3002/v1/publish" envconfig:"HOST"`
|
||||
BatchSize int `default:"1000" envconfig:"BATCH_SIZE"`
|
||||
SendInterval time.Duration `default:"15s" envconfig:"SEND_INTERVAL"`
|
||||
SendTimeout time.Duration `default:"500ms" envconfig:"SEND_TIMEOUT"`
|
||||
}
|
||||
Auth struct {
|
||||
KeyID string `envconfig:"KEY_ID"`
|
||||
PrivateKeyFile string `default:"/app/private.pem" envconfig:"PRIVATE_KEY_FILE"`
|
||||
Algorithm string `default:"RS256" envconfig:"ALGORITHM"`
|
||||
}
|
||||
}
|
||||
|
||||
if err := envconfig.Process("SALES", &cfg); err != nil {
|
||||
log.Fatalf("main : Parsing Config : %v", err)
|
||||
}
|
||||
|
||||
if err := flag.Process(&cfg); err != nil {
|
||||
if err != flag.ErrHelp {
|
||||
log.Fatalf("main : Parsing Command Line : %v", err)
|
||||
}
|
||||
return // We displayed help.
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// App Starting
|
||||
|
||||
// Print the build version for our logs. Also expose it under /debug/vars.
|
||||
expvar.NewString("build").Set(build)
|
||||
log.Printf("main : Started : Application Initializing version %q", build)
|
||||
defer log.Println("main : Completed")
|
||||
|
||||
cfgJSON, err := json.MarshalIndent(cfg, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("main : Marshalling Config to JSON : %v", err)
|
||||
}
|
||||
|
||||
// TODO: Validate what is being written to the logs. We don't
|
||||
// want to leak credentials or anything that can be a security risk.
|
||||
log.Printf("main : Config : %v\n", string(cfgJSON))
|
||||
|
||||
// =========================================================================
|
||||
// Find auth keys
|
||||
|
||||
keyContents, err := ioutil.ReadFile(cfg.Auth.PrivateKeyFile)
|
||||
if err != nil {
|
||||
log.Fatalf("main : Reading auth private key : %v", err)
|
||||
}
|
||||
|
||||
key, err := jwt.ParseRSAPrivateKeyFromPEM(keyContents)
|
||||
if err != nil {
|
||||
log.Fatalf("main : Parsing auth private key : %v", err)
|
||||
}
|
||||
|
||||
publicKeyLookup := auth.NewSingleKeyFunc(cfg.Auth.KeyID, key.Public().(*rsa.PublicKey))
|
||||
|
||||
authenticator, err := auth.NewAuthenticator(key, cfg.Auth.KeyID, cfg.Auth.Algorithm, publicKeyLookup)
|
||||
if err != nil {
|
||||
log.Fatalf("main : Constructing authenticator : %v", err)
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Start Mongo
|
||||
|
||||
log.Println("main : Started : Initialize Mongo")
|
||||
masterDB, err := db.New(cfg.DB.Host, cfg.DB.DialTimeout)
|
||||
if err != nil {
|
||||
log.Fatalf("main : Register DB : %v", err)
|
||||
}
|
||||
defer masterDB.Close()
|
||||
|
||||
// =========================================================================
|
||||
// Start Tracing Support
|
||||
|
||||
logger := func(format string, v ...interface{}) {
|
||||
log.Printf(format, v...)
|
||||
}
|
||||
|
||||
log.Printf("main : Tracing Started : %s", cfg.Trace.Host)
|
||||
exporter, err := itrace.NewExporter(logger, cfg.Trace.Host, cfg.Trace.BatchSize, cfg.Trace.SendInterval, cfg.Trace.SendTimeout)
|
||||
if err != nil {
|
||||
log.Fatalf("main : RegiTracingster : ERROR : %v", err)
|
||||
}
|
||||
defer func() {
|
||||
log.Printf("main : Tracing Stopping : %s", cfg.Trace.Host)
|
||||
batch, err := exporter.Close()
|
||||
if err != nil {
|
||||
log.Printf("main : Tracing Stopped : ERROR : Batch[%d] : %v", batch, err)
|
||||
} else {
|
||||
log.Printf("main : Tracing Stopped : Flushed Batch[%d]", batch)
|
||||
}
|
||||
}()
|
||||
|
||||
trace.RegisterExporter(exporter)
|
||||
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
|
||||
|
||||
// =========================================================================
|
||||
// Start Debug Service. Not concerned with shutting this down when the
|
||||
// application is being shutdown.
|
||||
//
|
||||
// /debug/vars - Added to the default mux by the expvars package.
|
||||
// /debug/pprof - Added to the default mux by the net/http/pprof package.
|
||||
go func() {
|
||||
log.Printf("main : Debug Listening %s", cfg.Web.DebugHost)
|
||||
log.Printf("main : Debug Listener closed : %v", http.ListenAndServe(cfg.Web.DebugHost, http.DefaultServeMux))
|
||||
}()
|
||||
|
||||
// =========================================================================
|
||||
// Start API Service
|
||||
|
||||
// Make a channel to listen for an interrupt or terminate signal from the OS.
|
||||
// Use a buffered channel because the signal package requires it.
|
||||
shutdown := make(chan os.Signal, 1)
|
||||
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
api := http.Server{
|
||||
Addr: cfg.Web.APIHost,
|
||||
Handler: handlers.API(shutdown, log, masterDB, authenticator),
|
||||
ReadTimeout: cfg.Web.ReadTimeout,
|
||||
WriteTimeout: cfg.Web.WriteTimeout,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
}
|
||||
|
||||
// Make a channel to listen for errors coming from the listener. Use a
|
||||
// buffered channel so the goroutine can exit if we don't collect this error.
|
||||
serverErrors := make(chan error, 1)
|
||||
|
||||
// Start the service listening for requests.
|
||||
go func() {
|
||||
log.Printf("main : API Listening %s", cfg.Web.APIHost)
|
||||
serverErrors <- api.ListenAndServe()
|
||||
}()
|
||||
|
||||
// =========================================================================
|
||||
// Shutdown
|
||||
|
||||
// Blocking main and waiting for shutdown.
|
||||
select {
|
||||
case err := <-serverErrors:
|
||||
log.Fatalf("main : Error starting server: %v", err)
|
||||
|
||||
case sig := <-shutdown:
|
||||
log.Printf("main : %v : Start shutdown..", sig)
|
||||
|
||||
// Create context for Shutdown call.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), cfg.Web.ShutdownTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Asking listener to shutdown and load shed.
|
||||
err := api.Shutdown(ctx)
|
||||
if err != nil {
|
||||
log.Printf("main : Graceful shutdown did not complete in %v : %v", cfg.Web.ShutdownTimeout, err)
|
||||
err = api.Close()
|
||||
}
|
||||
|
||||
// Log the status of this shutdown.
|
||||
switch {
|
||||
case sig == syscall.SIGSTOP:
|
||||
log.Fatal("main : Integrity issue caused shutdown")
|
||||
case err != nil:
|
||||
log.Fatalf("main : Could not stop server gracefully : %v", err)
|
||||
}
|
||||
}
|
||||
}
|
447
example-project/cmd/sales-api/tests/product_test.go
Normal file
447
example-project/cmd/sales-api/tests/product_test.go
Normal file
@ -0,0 +1,447 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/tests"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/product"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
// TestProducts is the entry point for the products
|
||||
func TestProducts(t *testing.T) {
|
||||
defer tests.Recover(t)
|
||||
|
||||
t.Run("getProducts200Empty", getProducts200Empty)
|
||||
t.Run("postProduct400", postProduct400)
|
||||
t.Run("postProduct401", postProduct401)
|
||||
t.Run("getProduct404", getProduct404)
|
||||
t.Run("getProduct400", getProduct400)
|
||||
t.Run("deleteProduct404", deleteProduct404)
|
||||
t.Run("putProduct404", putProduct404)
|
||||
t.Run("crudProducts", crudProduct)
|
||||
}
|
||||
|
||||
// getProducts200Empty validates an empty products list can be retrieved with the endpoint.
|
||||
func getProducts200Empty(t *testing.T) {
|
||||
r := httptest.NewRequest("GET", "/v1/products", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to fetch an empty list of products with the products endpoint.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen fetching an empty product list.")
|
||||
{
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 200 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 200 for the response.", tests.Success)
|
||||
|
||||
recv := w.Body.String()
|
||||
resp := `[]`
|
||||
if resp != recv {
|
||||
t.Log("Got :", recv)
|
||||
t.Log("Want:", resp)
|
||||
t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// postProduct400 validates a product can't be created with the endpoint
|
||||
// unless a valid product document is submitted.
|
||||
func postProduct400(t *testing.T) {
|
||||
r := httptest.NewRequest("POST", "/v1/products", strings.NewReader(`{}`))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate a new product can't be created with an invalid document.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen using an incomplete product value.")
|
||||
{
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 400 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 400 for the response.", tests.Success)
|
||||
|
||||
// Inspect the response.
|
||||
var got web.ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&got); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to unmarshal the response to an error type : %v", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to unmarshal the response to an error type.", tests.Success)
|
||||
|
||||
// Define what we want to see.
|
||||
want := web.ErrorResponse{
|
||||
Error: "field validation error",
|
||||
Fields: []web.FieldError{
|
||||
{Field: "name", Error: "name is a required field"},
|
||||
{Field: "cost", Error: "cost is a required field"},
|
||||
{Field: "quantity", Error: "quantity is a required field"},
|
||||
},
|
||||
}
|
||||
|
||||
// We can't rely on the order of the field errors so they have to be
|
||||
// sorted. Tell the cmp package how to sort them.
|
||||
sorter := cmpopts.SortSlices(func(a, b web.FieldError) bool {
|
||||
return a.Field < b.Field
|
||||
})
|
||||
|
||||
if diff := cmp.Diff(want, got, sorter); diff != "" {
|
||||
t.Fatalf("\t%s\tShould get the expected result. Diff:\n%s", tests.Failed, diff)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// postProduct401 validates a product can't be created with the endpoint
|
||||
// unless the user is authenticated
|
||||
func postProduct401(t *testing.T) {
|
||||
np := product.NewProduct{
|
||||
Name: "Comic Books",
|
||||
Cost: 25,
|
||||
Quantity: 60,
|
||||
}
|
||||
|
||||
body, err := json.Marshal(&np)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := httptest.NewRequest("POST", "/v1/products", bytes.NewBuffer(body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
// Not setting an authorization header
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate a new product can't be created with an invalid document.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen using an incomplete product value.")
|
||||
{
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 401 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 401 for the response.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getProduct400 validates a product request for a malformed id.
|
||||
func getProduct400(t *testing.T) {
|
||||
id := "12345"
|
||||
|
||||
r := httptest.NewRequest("GET", "/v1/products/"+id, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate getting a product with a malformed id.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen using the new product %s.", id)
|
||||
{
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 400 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 400 for the response.", tests.Success)
|
||||
|
||||
recv := w.Body.String()
|
||||
resp := `{"error":"ID is not in its proper form"}`
|
||||
if resp != recv {
|
||||
t.Log("Got :", recv)
|
||||
t.Log("Want:", resp)
|
||||
t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getProduct404 validates a product request for a product that does not exist with the endpoint.
|
||||
func getProduct404(t *testing.T) {
|
||||
id := bson.NewObjectId().Hex()
|
||||
|
||||
r := httptest.NewRequest("GET", "/v1/products/"+id, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate getting a product with an unknown id.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen using the new product %s.", id)
|
||||
{
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 404 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 404 for the response.", tests.Success)
|
||||
|
||||
recv := w.Body.String()
|
||||
resp := "Entity not found"
|
||||
if !strings.Contains(recv, resp) {
|
||||
t.Log("Got :", recv)
|
||||
t.Log("Want:", resp)
|
||||
t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// deleteProduct404 validates deleting a product that does not exist.
|
||||
func deleteProduct404(t *testing.T) {
|
||||
id := bson.NewObjectId().Hex()
|
||||
|
||||
r := httptest.NewRequest("DELETE", "/v1/products/"+id, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate deleting a product that does not exist.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen using the new product %s.", id)
|
||||
{
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 404 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 404 for the response.", tests.Success)
|
||||
|
||||
recv := w.Body.String()
|
||||
resp := "Entity not found"
|
||||
if !strings.Contains(recv, resp) {
|
||||
t.Log("Got :", recv)
|
||||
t.Log("Want:", resp)
|
||||
t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// putProduct404 validates updating a product that does not exist.
|
||||
func putProduct404(t *testing.T) {
|
||||
up := product.UpdateProduct{
|
||||
Name: tests.StringPointer("Nonexistent"),
|
||||
}
|
||||
|
||||
id := bson.NewObjectId().Hex()
|
||||
|
||||
body, err := json.Marshal(&up)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := httptest.NewRequest("PUT", "/v1/products/"+id, bytes.NewBuffer(body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate updating a product that does not exist.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen using the new product %s.", id)
|
||||
{
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 404 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 404 for the response.", tests.Success)
|
||||
|
||||
recv := w.Body.String()
|
||||
resp := "Entity not found"
|
||||
if !strings.Contains(recv, resp) {
|
||||
t.Log("Got :", recv)
|
||||
t.Log("Want:", resp)
|
||||
t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// crudProduct performs a complete test of CRUD against the api.
|
||||
func crudProduct(t *testing.T) {
|
||||
p := postProduct201(t)
|
||||
defer deleteProduct204(t, p.ID.Hex())
|
||||
|
||||
getProduct200(t, p.ID.Hex())
|
||||
putProduct204(t, p.ID.Hex())
|
||||
}
|
||||
|
||||
// postProduct201 validates a product can be created with the endpoint.
|
||||
func postProduct201(t *testing.T) product.Product {
|
||||
np := product.NewProduct{
|
||||
Name: "Comic Books",
|
||||
Cost: 25,
|
||||
Quantity: 60,
|
||||
}
|
||||
|
||||
body, err := json.Marshal(&np)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := httptest.NewRequest("POST", "/v1/products", bytes.NewBuffer(body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
// p is the value we will return.
|
||||
var p product.Product
|
||||
|
||||
t.Log("Given the need to create a new product with the products endpoint.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen using the declared product value.")
|
||||
{
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 201 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 201 for the response.", tests.Success)
|
||||
|
||||
if err := json.NewDecoder(w.Body).Decode(&p); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to unmarshal the response : %v", tests.Failed, err)
|
||||
}
|
||||
|
||||
// Define what we wanted to receive. We will just trust the generated
|
||||
// fields like ID and Dates so we copy p.
|
||||
want := p
|
||||
want.Name = "Comic Books"
|
||||
want.Cost = 25
|
||||
want.Quantity = 60
|
||||
|
||||
if diff := cmp.Diff(want, p); diff != "" {
|
||||
t.Fatalf("\t%s\tShould get the expected result. Diff:\n%s", tests.Failed, diff)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// deleteProduct200 validates deleting a product that does exist.
|
||||
func deleteProduct204(t *testing.T, id string) {
|
||||
r := httptest.NewRequest("DELETE", "/v1/products/"+id, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate deleting a product that does exist.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen using the new product %s.", id)
|
||||
{
|
||||
if w.Code != http.StatusNoContent {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 204 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 204 for the response.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getProduct200 validates a product request for an existing id.
|
||||
func getProduct200(t *testing.T, id string) {
|
||||
r := httptest.NewRequest("GET", "/v1/products/"+id, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate getting a product that exists.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen using the new product %s.", id)
|
||||
{
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 200 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 200 for the response.", tests.Success)
|
||||
|
||||
var p product.Product
|
||||
if err := json.NewDecoder(w.Body).Decode(&p); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to unmarshal the response : %v", tests.Failed, err)
|
||||
}
|
||||
|
||||
// Define what we wanted to receive. We will just trust the generated
|
||||
// fields like Dates so we copy p.
|
||||
want := p
|
||||
want.ID = bson.ObjectIdHex(id)
|
||||
want.Name = "Comic Books"
|
||||
want.Cost = 25
|
||||
want.Quantity = 60
|
||||
|
||||
if diff := cmp.Diff(want, p); diff != "" {
|
||||
t.Fatalf("\t%s\tShould get the expected result. Diff:\n%s", tests.Failed, diff)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// putProduct204 validates updating a product that does exist.
|
||||
func putProduct204(t *testing.T, id string) {
|
||||
body := `{"name": "Graphic Novels", "cost": 100}`
|
||||
r := httptest.NewRequest("PUT", "/v1/products/"+id, strings.NewReader(body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to update a product with the products endpoint.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen using the modified product value.")
|
||||
{
|
||||
if w.Code != http.StatusNoContent {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 204 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 204 for the response.", tests.Success)
|
||||
|
||||
r = httptest.NewRequest("GET", "/v1/products/"+id, nil)
|
||||
w = httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 200 for the retrieve : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 200 for the retrieve.", tests.Success)
|
||||
|
||||
var ru product.Product
|
||||
if err := json.NewDecoder(w.Body).Decode(&ru); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to unmarshal the response : %v", tests.Failed, err)
|
||||
}
|
||||
|
||||
if ru.Name != "Graphic Novels" {
|
||||
t.Fatalf("\t%s\tShould see an updated Name : got %q want %q", tests.Failed, ru.Name, "Graphic Novels")
|
||||
}
|
||||
t.Logf("\t%s\tShould see an updated Name.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
97
example-project/cmd/sales-api/tests/tests_test.go
Normal file
97
example-project/cmd/sales-api/tests/tests_test.go
Normal file
@ -0,0 +1,97 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/cmd/sales-api/handlers"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/tests"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/user"
|
||||
)
|
||||
|
||||
var a http.Handler
|
||||
var test *tests.Test
|
||||
|
||||
// Information about the users we have created for testing.
|
||||
var adminAuthorization string
|
||||
var adminID string
|
||||
var userAuthorization string
|
||||
var userID string
|
||||
|
||||
// TestMain is the entry point for testing.
|
||||
func TestMain(m *testing.M) {
|
||||
os.Exit(testMain(m))
|
||||
}
|
||||
|
||||
func testMain(m *testing.M) int {
|
||||
test = tests.New()
|
||||
defer test.TearDown()
|
||||
|
||||
// Create RSA keys to enable authentication in our service.
|
||||
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
kid := "4754d86b-7a6d-4df5-9c65-224741361492"
|
||||
kf := auth.NewSingleKeyFunc(kid, key.Public().(*rsa.PublicKey))
|
||||
authenticator, err := auth.NewAuthenticator(key, kid, "RS256", kf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
shutdown := make(chan os.Signal, 1)
|
||||
a = handlers.API(shutdown, test.Log, test.MasterDB, authenticator)
|
||||
|
||||
// Create an admin user directly with our business logic. This creates an
|
||||
// initial user that we will use for admin validated endpoints.
|
||||
nu := user.NewUser{
|
||||
Email: "admin@ardanlabs.com",
|
||||
Name: "Admin User",
|
||||
Roles: []string{auth.RoleAdmin, auth.RoleUser},
|
||||
Password: "gophers",
|
||||
PasswordConfirm: "gophers",
|
||||
}
|
||||
|
||||
admin, err := user.Create(tests.Context(), test.MasterDB, &nu, time.Now())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
adminID = admin.ID.Hex()
|
||||
|
||||
tkn, err := user.Authenticate(tests.Context(), test.MasterDB, authenticator, time.Now(), nu.Email, nu.Password)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
adminAuthorization = "Bearer " + tkn.Token
|
||||
|
||||
// Create a regular user to use when calling regular validated endpoints.
|
||||
nu = user.NewUser{
|
||||
Email: "user@ardanlabs.com",
|
||||
Name: "Regular User",
|
||||
Roles: []string{auth.RoleUser},
|
||||
Password: "concurrency",
|
||||
PasswordConfirm: "concurrency",
|
||||
}
|
||||
|
||||
usr, err := user.Create(tests.Context(), test.MasterDB, &nu, time.Now())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
userID = usr.ID.Hex()
|
||||
|
||||
tkn, err = user.Authenticate(tests.Context(), test.MasterDB, authenticator, time.Now(), nu.Email, nu.Password)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
userAuthorization = "Bearer " + tkn.Token
|
||||
|
||||
return m.Run()
|
||||
}
|
576
example-project/cmd/sales-api/tests/user_test.go
Normal file
576
example-project/cmd/sales-api/tests/user_test.go
Normal file
@ -0,0 +1,576 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/tests"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/user"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
// TestUsers is the entry point for testing user management functions.
|
||||
func TestUsers(t *testing.T) {
|
||||
defer tests.Recover(t)
|
||||
|
||||
t.Run("getToken401", getToken401)
|
||||
t.Run("getToken200", getToken200)
|
||||
t.Run("postUser400", postUser400)
|
||||
t.Run("postUser401", postUser401)
|
||||
t.Run("postUser403", postUser403)
|
||||
t.Run("getUser400", getUser400)
|
||||
t.Run("getUser403", getUser403)
|
||||
t.Run("getUser404", getUser404)
|
||||
t.Run("deleteUser404", deleteUser404)
|
||||
t.Run("putUser404", putUser404)
|
||||
t.Run("crudUsers", crudUser)
|
||||
}
|
||||
|
||||
// getToken401 ensures an unknown user can't generate a token.
|
||||
func getToken401(t *testing.T) {
|
||||
r := httptest.NewRequest("GET", "/v1/users/token", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.SetBasicAuth("unknown@example.com", "some-password")
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to deny tokens to unknown users.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen fetching a token with an unrecognized email.")
|
||||
{
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 401 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 401 for the response.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getToken200
|
||||
func getToken200(t *testing.T) {
|
||||
|
||||
r := httptest.NewRequest("GET", "/v1/users/token", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.SetBasicAuth("admin@ardanlabs.com", "gophers")
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to issues tokens to known users.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen fetching a token with valid credentials.")
|
||||
{
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 200 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 200 for the response.", tests.Success)
|
||||
|
||||
var got user.Token
|
||||
if err := json.NewDecoder(w.Body).Decode(&got); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to unmarshal the response : %v", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to unmarshal the response.", tests.Success)
|
||||
|
||||
// TODO(jlw) Should we ensure the token is valid?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// postUser400 validates a user can't be created with the endpoint
|
||||
// unless a valid user document is submitted.
|
||||
func postUser400(t *testing.T) {
|
||||
body, err := json.Marshal(&user.NewUser{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := httptest.NewRequest("POST", "/v1/users", bytes.NewBuffer(body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", adminAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate a new user can't be created with an invalid document.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen using an incomplete user value.")
|
||||
{
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 400 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 400 for the response.", tests.Success)
|
||||
|
||||
// Inspect the response.
|
||||
var got web.ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&got); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to unmarshal the response to an error type : %v", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to unmarshal the response to an error type.", tests.Success)
|
||||
|
||||
// Define what we want to see.
|
||||
want := web.ErrorResponse{
|
||||
Error: "field validation error",
|
||||
Fields: []web.FieldError{
|
||||
{Field: "name", Error: "name is a required field"},
|
||||
{Field: "email", Error: "email is a required field"},
|
||||
{Field: "roles", Error: "roles is a required field"},
|
||||
{Field: "password", Error: "password is a required field"},
|
||||
},
|
||||
}
|
||||
|
||||
// We can't rely on the order of the field errors so they have to be
|
||||
// sorted. Tell the cmp package how to sort them.
|
||||
sorter := cmpopts.SortSlices(func(a, b web.FieldError) bool {
|
||||
return a.Field < b.Field
|
||||
})
|
||||
|
||||
if diff := cmp.Diff(want, got, sorter); diff != "" {
|
||||
t.Fatalf("\t%s\tShould get the expected result. Diff:\n%s", tests.Failed, diff)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// postUser401 validates a user can't be created unless the calling user is
|
||||
// authenticated.
|
||||
func postUser401(t *testing.T) {
|
||||
body, err := json.Marshal(&user.User{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := httptest.NewRequest("POST", "/v1/users", bytes.NewBuffer(body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate a new user can't be created with an invalid document.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen using an incomplete user value.")
|
||||
{
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 403 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 403 for the response.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// postUser403 validates a user can't be created unless the calling user is
|
||||
// an admin user. Regular users can't do this.
|
||||
func postUser403(t *testing.T) {
|
||||
body, err := json.Marshal(&user.User{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := httptest.NewRequest("POST", "/v1/users", bytes.NewBuffer(body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
// Not setting the Authorization header
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate a new user can't be created with an invalid document.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen using an incomplete user value.")
|
||||
{
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 401 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 401 for the response.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getUser400 validates a user request for a malformed userid.
|
||||
func getUser400(t *testing.T) {
|
||||
id := "12345"
|
||||
|
||||
r := httptest.NewRequest("GET", "/v1/users/"+id, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", adminAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate getting a user with a malformed userid.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen using the new user %s.", id)
|
||||
{
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 400 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 400 for the response.", tests.Success)
|
||||
|
||||
recv := w.Body.String()
|
||||
resp := `{"error":"ID is not in its proper form"}`
|
||||
if resp != recv {
|
||||
t.Log("Got :", recv)
|
||||
t.Log("Want:", resp)
|
||||
t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getUser403 validates a regular user can't fetch anyone but themselves
|
||||
func getUser403(t *testing.T) {
|
||||
t.Log("Given the need to validate regular users can't fetch other users.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen fetching the admin user as a regular user.")
|
||||
{
|
||||
r := httptest.NewRequest("GET", "/v1/users/"+adminID, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 403 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 403 for the response.", tests.Success)
|
||||
|
||||
recv := w.Body.String()
|
||||
resp := `{"error":"Attempted action is not allowed"}`
|
||||
if resp != recv {
|
||||
t.Log("Got :", recv)
|
||||
t.Log("Want:", resp)
|
||||
t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
|
||||
t.Logf("\tTest 1:\tWhen fetching the user as a themselves.")
|
||||
{
|
||||
|
||||
r := httptest.NewRequest("GET", "/v1/users/"+userID, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 200 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 200 for the response.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getUser404 validates a user request for a user that does not exist with the endpoint.
|
||||
func getUser404(t *testing.T) {
|
||||
id := bson.NewObjectId().Hex()
|
||||
|
||||
r := httptest.NewRequest("GET", "/v1/users/"+id, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", adminAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate getting a user with an unknown id.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen using the new user %s.", id)
|
||||
{
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 404 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 404 for the response.", tests.Success)
|
||||
|
||||
recv := w.Body.String()
|
||||
resp := "Entity not found"
|
||||
if !strings.Contains(recv, resp) {
|
||||
t.Log("Got :", recv)
|
||||
t.Log("Want:", resp)
|
||||
t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// deleteUser404 validates deleting a user that does not exist.
|
||||
func deleteUser404(t *testing.T) {
|
||||
id := bson.NewObjectId().Hex()
|
||||
|
||||
r := httptest.NewRequest("DELETE", "/v1/users/"+id, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", adminAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate deleting a user that does not exist.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen using the new user %s.", id)
|
||||
{
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 404 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 404 for the response.", tests.Success)
|
||||
|
||||
recv := w.Body.String()
|
||||
resp := "Entity not found"
|
||||
if !strings.Contains(recv, resp) {
|
||||
t.Log("Got :", recv)
|
||||
t.Log("Want:", resp)
|
||||
t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// putUser404 validates updating a user that does not exist.
|
||||
func putUser404(t *testing.T) {
|
||||
u := user.UpdateUser{
|
||||
Name: tests.StringPointer("Doesn't Exist"),
|
||||
}
|
||||
|
||||
id := bson.NewObjectId().Hex()
|
||||
|
||||
body, err := json.Marshal(&u)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := httptest.NewRequest("PUT", "/v1/users/"+id, bytes.NewBuffer(body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", adminAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate updating a user that does not exist.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen using the new user %s.", id)
|
||||
{
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 404 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 404 for the response.", tests.Success)
|
||||
|
||||
recv := w.Body.String()
|
||||
resp := "Entity not found"
|
||||
if !strings.Contains(recv, resp) {
|
||||
t.Log("Got :", recv)
|
||||
t.Log("Want:", resp)
|
||||
t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// crudUser performs a complete test of CRUD against the api.
|
||||
func crudUser(t *testing.T) {
|
||||
nu := postUser201(t)
|
||||
defer deleteUser204(t, nu.ID.Hex())
|
||||
|
||||
getUser200(t, nu.ID.Hex())
|
||||
putUser204(t, nu.ID.Hex())
|
||||
putUser403(t, nu.ID.Hex())
|
||||
}
|
||||
|
||||
// postUser201 validates a user can be created with the endpoint.
|
||||
func postUser201(t *testing.T) user.User {
|
||||
nu := user.NewUser{
|
||||
Name: "Bill Kennedy",
|
||||
Email: "bill@ardanlabs.com",
|
||||
Roles: []string{auth.RoleAdmin},
|
||||
Password: "gophers",
|
||||
PasswordConfirm: "gophers",
|
||||
}
|
||||
|
||||
body, err := json.Marshal(&nu)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := httptest.NewRequest("POST", "/v1/users", bytes.NewBuffer(body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", adminAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
// u is the value we will return.
|
||||
var u user.User
|
||||
|
||||
t.Log("Given the need to create a new user with the users endpoint.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen using the declared user value.")
|
||||
{
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 201 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 201 for the response.", tests.Success)
|
||||
|
||||
if err := json.NewDecoder(w.Body).Decode(&u); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to unmarshal the response : %v", tests.Failed, err)
|
||||
}
|
||||
|
||||
// Define what we wanted to receive. We will just trust the generated
|
||||
// fields like ID and Dates so we copy u.
|
||||
want := u
|
||||
want.Name = "Bill Kennedy"
|
||||
want.Email = "bill@ardanlabs.com"
|
||||
want.Roles = []string{auth.RoleAdmin}
|
||||
|
||||
if diff := cmp.Diff(want, u); diff != "" {
|
||||
t.Fatalf("\t%s\tShould get the expected result. Diff:\n%s", tests.Failed, diff)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
|
||||
return u
|
||||
}
|
||||
|
||||
// deleteUser200 validates deleting a user that does exist.
|
||||
func deleteUser204(t *testing.T, id string) {
|
||||
r := httptest.NewRequest("DELETE", "/v1/users/"+id, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", adminAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate deleting a user that does exist.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen using the new user %s.", id)
|
||||
{
|
||||
if w.Code != http.StatusNoContent {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 204 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 204 for the response.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getUser200 validates a user request for an existing userid.
|
||||
func getUser200(t *testing.T, id string) {
|
||||
r := httptest.NewRequest("GET", "/v1/users/"+id, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", adminAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to validate getting a user that exsits.")
|
||||
{
|
||||
t.Logf("\tTest 0:\tWhen using the new user %s.", id)
|
||||
{
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 200 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 200 for the response.", tests.Success)
|
||||
|
||||
var u user.User
|
||||
if err := json.NewDecoder(w.Body).Decode(&u); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to unmarshal the response : %v", tests.Failed, err)
|
||||
}
|
||||
|
||||
// Define what we wanted to receive. We will just trust the generated
|
||||
// fields like Dates so we copy p.
|
||||
want := u
|
||||
want.ID = bson.ObjectIdHex(id)
|
||||
want.Name = "Bill Kennedy"
|
||||
want.Email = "bill@ardanlabs.com"
|
||||
want.Roles = []string{auth.RoleAdmin}
|
||||
|
||||
if diff := cmp.Diff(want, u); diff != "" {
|
||||
t.Fatalf("\t%s\tShould get the expected result. Diff:\n%s", tests.Failed, diff)
|
||||
}
|
||||
t.Logf("\t%s\tShould get the expected result.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// putUser204 validates updating a user that does exist.
|
||||
func putUser204(t *testing.T, id string) {
|
||||
body := `{"name": "Jacob Walker"}`
|
||||
|
||||
r := httptest.NewRequest("PUT", "/v1/users/"+id, strings.NewReader(body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", adminAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to update a user with the users endpoint.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen using the modified user value.")
|
||||
{
|
||||
if w.Code != http.StatusNoContent {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 204 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 204 for the response.", tests.Success)
|
||||
|
||||
r = httptest.NewRequest("GET", "/v1/users/"+id, nil)
|
||||
w = httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", adminAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 200 for the retrieve : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 200 for the retrieve.", tests.Success)
|
||||
|
||||
var ru user.User
|
||||
if err := json.NewDecoder(w.Body).Decode(&ru); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to unmarshal the response : %v", tests.Failed, err)
|
||||
}
|
||||
|
||||
if ru.Name != "Jacob Walker" {
|
||||
t.Fatalf("\t%s\tShould see an updated Name : got %q want %q", tests.Failed, ru.Name, "Jacob Walker")
|
||||
}
|
||||
t.Logf("\t%s\tShould see an updated Name.", tests.Success)
|
||||
|
||||
if ru.Email != "bill@ardanlabs.com" {
|
||||
t.Fatalf("\t%s\tShould not affect other fields like Email : got %q want %q", tests.Failed, ru.Email, "bill@ardanlabs.com")
|
||||
}
|
||||
t.Logf("\t%s\tShould not affect other fields like Email.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// putUser403 validates that a user can't modify users unless they are an admin.
|
||||
func putUser403(t *testing.T, id string) {
|
||||
body := `{"name": "Anna Walker"}`
|
||||
|
||||
r := httptest.NewRequest("PUT", "/v1/users/"+id, strings.NewReader(body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.Header.Set("Authorization", userAuthorization)
|
||||
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
t.Log("Given the need to update a user with the users endpoint.")
|
||||
{
|
||||
t.Log("\tTest 0:\tWhen a non-admin user makes a request")
|
||||
{
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Fatalf("\t%s\tShould receive a status code of 403 for the response : %v", tests.Failed, w.Code)
|
||||
}
|
||||
t.Logf("\t%s\tShould receive a status code of 403 for the response.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
73
example-project/cmd/sidecar/metrics/collector/expvar.go
Normal file
73
example-project/cmd/sidecar/metrics/collector/expvar.go
Normal file
@ -0,0 +1,73 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Expvar provides the ability to receive metrics
|
||||
// from internal services using expvar.
|
||||
type Expvar struct {
|
||||
host string
|
||||
tr *http.Transport
|
||||
client http.Client
|
||||
}
|
||||
|
||||
// New creates a Expvar for collection metrics.
|
||||
func New(host string) (*Expvar, error) {
|
||||
tr := http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
DualStack: true,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 2,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
|
||||
exp := Expvar{
|
||||
host: host,
|
||||
tr: &tr,
|
||||
client: http.Client{
|
||||
Transport: &tr,
|
||||
Timeout: 1 * time.Second,
|
||||
},
|
||||
}
|
||||
|
||||
return &exp, nil
|
||||
}
|
||||
|
||||
func (exp *Expvar) Collect() (map[string]interface{}, error) {
|
||||
req, err := http.NewRequest("GET", exp.host, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := exp.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
msg, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, errors.New(string(msg))
|
||||
}
|
||||
|
||||
data := make(map[string]interface{})
|
||||
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
109
example-project/cmd/sidecar/metrics/main.go
Normal file
109
example-project/cmd/sidecar/metrics/main.go
Normal file
@ -0,0 +1,109 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/cmd/sidecar/metrics/collector"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/cmd/sidecar/metrics/publisher"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/cmd/sidecar/metrics/publisher/expvar"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// =========================================================================
|
||||
// Logging
|
||||
|
||||
log := log.New(os.Stdout, "TRACER : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
|
||||
defer log.Println("main : Completed")
|
||||
|
||||
// =========================================================================
|
||||
// Configuration
|
||||
|
||||
var cfg struct {
|
||||
Web struct {
|
||||
DebugHost string `default:"0.0.0.0:4001" envconfig:"DEBUG_HOST"`
|
||||
ReadTimeout time.Duration `default:"5s" envconfig:"READ_TIMEOUT"`
|
||||
WriteTimeout time.Duration `default:"5s" envconfig:"WRITE_TIMEOUT"`
|
||||
ShutdownTimeout time.Duration `default:"5s" envconfig:"SHUTDOWN_TIMEOUT"`
|
||||
}
|
||||
Expvar struct {
|
||||
Host string `default:"0.0.0.0:3001" envconfig:"HOST"`
|
||||
Route string `default:"/metrics" envconfig:"ROUTE"`
|
||||
ReadTimeout time.Duration `default:"5s" envconfig:"READ_TIMEOUT"`
|
||||
WriteTimeout time.Duration `default:"5s" envconfig:"WRITE_TIMEOUT"`
|
||||
ShutdownTimeout time.Duration `default:"5s" envconfig:"SHUTDOWN_TIMEOUT"`
|
||||
}
|
||||
Collect struct {
|
||||
From string `default:"http://sales-api:4000/debug/vars" envconfig:"FROM"`
|
||||
}
|
||||
Publish struct {
|
||||
To string `default:"console" envconfig:"TO"`
|
||||
Interval time.Duration `default:"5s" envconfig:"INTERVAL"`
|
||||
}
|
||||
}
|
||||
|
||||
if err := envconfig.Process("METRICS", &cfg); err != nil {
|
||||
log.Fatalf("main : Parsing Config : %v", err)
|
||||
}
|
||||
|
||||
cfgJSON, err := json.MarshalIndent(cfg, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("main : Marshalling Config to JSON : %v", err)
|
||||
}
|
||||
log.Printf("config : %v\n", string(cfgJSON))
|
||||
|
||||
// =========================================================================
|
||||
// Start Debug Service. Not concerned with shutting this down when the
|
||||
// application is being shutdown.
|
||||
//
|
||||
// /debug/pprof - Added to the default mux by the net/http/pprof package.
|
||||
go func() {
|
||||
log.Printf("main : Debug Listening %s", cfg.Web.DebugHost)
|
||||
log.Printf("main : Debug Listener closed : %v", http.ListenAndServe(cfg.Web.DebugHost, http.DefaultServeMux))
|
||||
}()
|
||||
|
||||
// =========================================================================
|
||||
// Start expvar Service
|
||||
|
||||
exp := expvar.New(log, cfg.Expvar.Host, cfg.Expvar.Route, cfg.Expvar.ReadTimeout, cfg.Expvar.WriteTimeout)
|
||||
defer exp.Stop(cfg.Expvar.ShutdownTimeout)
|
||||
|
||||
// =========================================================================
|
||||
// Start collectors and publishers
|
||||
|
||||
// Initialize to allow for the collection of metrics.
|
||||
collector, err := collector.New(cfg.Collect.From)
|
||||
if err != nil {
|
||||
log.Fatalf("main : Starting collector : %v", err)
|
||||
}
|
||||
|
||||
// Create a stdout publisher.
|
||||
// TODO: Respect the cfg.publish.to config option.
|
||||
stdout := publisher.NewStdout(log)
|
||||
|
||||
// Start the publisher to collect/publish metrics.
|
||||
publish, err := publisher.New(log, collector, cfg.Publish.Interval, exp.Publish, stdout.Publish)
|
||||
if err != nil {
|
||||
log.Fatalf("main : Starting publisher : %v", err)
|
||||
}
|
||||
defer publish.Stop()
|
||||
|
||||
// =========================================================================
|
||||
// Shutdown
|
||||
|
||||
// Make a channel to listen for an interrupt or terminate signal from the OS.
|
||||
// Use a buffered channel because the signal package requires it.
|
||||
shutdown := make(chan os.Signal, 1)
|
||||
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
|
||||
<-shutdown
|
||||
|
||||
log.Println("main : Start shutdown...")
|
||||
}
|
164
example-project/cmd/sidecar/metrics/publisher/datadog/datadog.go
Normal file
164
example-project/cmd/sidecar/metrics/publisher/datadog/datadog.go
Normal file
@ -0,0 +1,164 @@
|
||||
package datadog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Datadog provides the ability to publish metrics to Datadog.
|
||||
type Datadog struct {
|
||||
log *log.Logger
|
||||
apiKey string
|
||||
host string
|
||||
tr *http.Transport
|
||||
client http.Client
|
||||
}
|
||||
|
||||
// New initializes Datadog access for publishing metrics.
|
||||
func New(log *log.Logger, apiKey string, host string) *Datadog {
|
||||
tr := http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
DualStack: true,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 2,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
|
||||
d := Datadog{
|
||||
log: log,
|
||||
apiKey: apiKey,
|
||||
host: host,
|
||||
tr: &tr,
|
||||
client: http.Client{
|
||||
Transport: &tr,
|
||||
Timeout: 1 * time.Second,
|
||||
},
|
||||
}
|
||||
|
||||
return &d
|
||||
}
|
||||
|
||||
// Publish handles the processing of metrics for deliver
|
||||
// to the DataDog.
|
||||
func (d *Datadog) Publish(data map[string]interface{}) {
|
||||
doc, err := marshalDatadog(d.log, data)
|
||||
if err != nil {
|
||||
d.log.Println("datadog.publish :", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := sendDatadog(d, doc); err != nil {
|
||||
d.log.Println("datadog.publish :", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("datadog.publish : published :", string(doc))
|
||||
}
|
||||
|
||||
// marshalDatadog converts the data map to datadog JSON document.
|
||||
func marshalDatadog(log *log.Logger, data map[string]interface{}) ([]byte, error) {
|
||||
/*
|
||||
{ "series" : [
|
||||
{
|
||||
"metric":"test.metric",
|
||||
"points": [
|
||||
[
|
||||
$currenttime,
|
||||
20
|
||||
]
|
||||
],
|
||||
"type":"gauge",
|
||||
"host":"test.example.com",
|
||||
"tags": [
|
||||
"environment:test"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
*/
|
||||
|
||||
// Extract the base keys/values.
|
||||
mType := "gauge"
|
||||
host, ok := data["host"].(string)
|
||||
if !ok {
|
||||
host = "unknown"
|
||||
}
|
||||
env := "dev"
|
||||
if host != "localhost" {
|
||||
env = "prod"
|
||||
}
|
||||
envTag := "environment:" + env
|
||||
|
||||
// Define the Datadog data format.
|
||||
type series struct {
|
||||
Metric string `json:"metric"`
|
||||
Points [][]interface{} `json:"points"`
|
||||
Type string `json:"type"`
|
||||
Host string `json:"host"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
// Populate the data into the data structure.
|
||||
var doc struct {
|
||||
Series []series `json:"series"`
|
||||
}
|
||||
for key, value := range data {
|
||||
switch value.(type) {
|
||||
case int, float64:
|
||||
doc.Series = append(doc.Series, series{
|
||||
Metric: env + "." + key,
|
||||
Points: [][]interface{}{{"$currenttime", value}},
|
||||
Type: mType,
|
||||
Host: host,
|
||||
Tags: []string{envTag},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Convert the data into JSON.
|
||||
out, err := json.MarshalIndent(doc, "", " ")
|
||||
if err != nil {
|
||||
log.Println("datadog.publish : marshaling :", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// sendDatadog sends data to the datadog servers.
|
||||
func sendDatadog(d *Datadog, data []byte) error {
|
||||
url := fmt.Sprintf("%s?api_key=%s", d.host, d.apiKey)
|
||||
b := bytes.NewBuffer(data)
|
||||
|
||||
r, err := http.NewRequest("POST", url, b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := d.client.Do(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusAccepted {
|
||||
out, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("status[%d] : %s", resp.StatusCode, out)
|
||||
}
|
||||
return fmt.Errorf("status[%d]", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
package expvar
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/dimfeld/httptreemux"
|
||||
)
|
||||
|
||||
// Expvar provide our basic publishing.
|
||||
type Expvar struct {
|
||||
log *log.Logger
|
||||
server http.Server
|
||||
data map[string]interface{}
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// New starts a service for consuming the raw expvar stats.
|
||||
func New(log *log.Logger, host string, route string, readTimeout, writeTimeout time.Duration) *Expvar {
|
||||
mux := httptreemux.New()
|
||||
exp := Expvar{
|
||||
log: log,
|
||||
server: http.Server{
|
||||
Addr: host,
|
||||
Handler: mux,
|
||||
ReadTimeout: readTimeout,
|
||||
WriteTimeout: writeTimeout,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
},
|
||||
}
|
||||
|
||||
mux.Handle("GET", route, exp.handler)
|
||||
|
||||
go func() {
|
||||
log.Println("expvar : API Listening", host)
|
||||
if err := exp.server.ListenAndServe(); err != nil {
|
||||
log.Println("expvar : ERROR :", err)
|
||||
}
|
||||
}()
|
||||
|
||||
return &exp
|
||||
}
|
||||
|
||||
// Stop shuts down the service.
|
||||
func (exp *Expvar) Stop(shutdownTimeout time.Duration) {
|
||||
exp.log.Println("expvar : Start shutdown...")
|
||||
defer exp.log.Println("expvar : Completed")
|
||||
|
||||
// Create context for Shutdown call.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Asking listener to shutdown and load shed.
|
||||
if err := exp.server.Shutdown(ctx); err != nil {
|
||||
exp.log.Printf("expvar : Graceful shutdown did not complete in %v : %v", shutdownTimeout, err)
|
||||
if err := exp.server.Close(); err != nil {
|
||||
exp.log.Fatalf("expvar : Could not stop http server: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Publish is called by the publisher goroutine and saves the raw stats.
|
||||
func (exp *Expvar) Publish(data map[string]interface{}) {
|
||||
exp.mu.Lock()
|
||||
{
|
||||
exp.data = data
|
||||
}
|
||||
exp.mu.Unlock()
|
||||
}
|
||||
|
||||
// handler is what consumers call to get the raw stats.
|
||||
func (exp *Expvar) handler(w http.ResponseWriter, r *http.Request, params map[string]string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
var data map[string]interface{}
|
||||
exp.mu.Lock()
|
||||
{
|
||||
data = exp.data
|
||||
}
|
||||
exp.mu.Unlock()
|
||||
|
||||
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||
exp.log.Println("expvar : ERROR :", err)
|
||||
}
|
||||
|
||||
log.Printf("expvar : (%d) : %s %s -> %s",
|
||||
http.StatusOK,
|
||||
r.Method, r.URL.Path,
|
||||
r.RemoteAddr,
|
||||
)
|
||||
}
|
128
example-project/cmd/sidecar/metrics/publisher/publisher.go
Normal file
128
example-project/cmd/sidecar/metrics/publisher/publisher.go
Normal file
@ -0,0 +1,128 @@
|
||||
package publisher
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Set of possible publisher types.
|
||||
const (
|
||||
TypeStdout = "stdout"
|
||||
TypeDatadog = "datadog"
|
||||
)
|
||||
|
||||
// =============================================================================
|
||||
|
||||
// Collector defines a contract a collector must support
|
||||
// so a consumer can retrieve metrics.
|
||||
type Collector interface {
|
||||
Collect() (map[string]interface{}, error)
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
// Publisher defines a handler function that will be called
|
||||
// on each interval.
|
||||
type Publisher func(map[string]interface{})
|
||||
|
||||
// Publish provides the ability to receive metrics
|
||||
// on an interval.
|
||||
type Publish struct {
|
||||
log *log.Logger
|
||||
collector Collector
|
||||
publisher []Publisher
|
||||
wg sync.WaitGroup
|
||||
timer *time.Timer
|
||||
shutdown chan struct{}
|
||||
}
|
||||
|
||||
// New creates a Publish for consuming and publishing metrics.
|
||||
func New(log *log.Logger, collector Collector, interval time.Duration, publisher ...Publisher) (*Publish, error) {
|
||||
p := Publish{
|
||||
log: log,
|
||||
collector: collector,
|
||||
publisher: publisher,
|
||||
timer: time.NewTimer(interval),
|
||||
shutdown: make(chan struct{}),
|
||||
}
|
||||
|
||||
p.wg.Add(1)
|
||||
go func() {
|
||||
defer p.wg.Done()
|
||||
for {
|
||||
p.timer.Reset(interval)
|
||||
select {
|
||||
case <-p.timer.C:
|
||||
p.update()
|
||||
case <-p.shutdown:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
// Stop is used to shutdown the goroutine collecting metrics.
|
||||
func (p *Publish) Stop() {
|
||||
close(p.shutdown)
|
||||
p.wg.Wait()
|
||||
}
|
||||
|
||||
// update pulls the metrics and publishes them to the specified system.
|
||||
func (p *Publish) update() {
|
||||
data, err := p.collector.Collect()
|
||||
if err != nil {
|
||||
p.log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, pub := range p.publisher {
|
||||
pub(data)
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
// Stdout provide our basic publishing.
|
||||
type Stdout struct {
|
||||
log *log.Logger
|
||||
}
|
||||
|
||||
// NewStdout initializes stdout for publishing metrics.
|
||||
func NewStdout(log *log.Logger) *Stdout {
|
||||
return &Stdout{log}
|
||||
}
|
||||
|
||||
// Publish publishers for writing to stdout.
|
||||
func (s *Stdout) Publish(data map[string]interface{}) {
|
||||
rawJSON, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
s.log.Println("Stdout : Marshal ERROR :", err)
|
||||
return
|
||||
}
|
||||
|
||||
var d map[string]interface{}
|
||||
if err := json.Unmarshal(rawJSON, &d); err != nil {
|
||||
s.log.Println("Stdout : Unmarshal ERROR :", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Add heap value into the data set.
|
||||
memStats, ok := (d["memstats"]).(map[string]interface{})
|
||||
if ok {
|
||||
d["heap"] = memStats["Alloc"]
|
||||
}
|
||||
|
||||
// Remove unnecessary keys.
|
||||
delete(d, "memstats")
|
||||
delete(d, "cmdline")
|
||||
|
||||
out, err := json.MarshalIndent(d, "", " ")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s.log.Println("Stdout :\n", string(out))
|
||||
}
|
23
example-project/cmd/sidecar/tracer/handlers/health.go
Normal file
23
example-project/cmd/sidecar/tracer/handlers/health.go
Normal file
@ -0,0 +1,23 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
)
|
||||
|
||||
// Health provides support for orchestration health checks.
|
||||
type Health struct{}
|
||||
|
||||
// Check validates the service is ready and healthy to accept requests.
|
||||
func (h *Health) Check(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
status := struct {
|
||||
Status string `json:"status"`
|
||||
}{
|
||||
Status: "ok",
|
||||
}
|
||||
|
||||
web.Respond(ctx, w, status, http.StatusOK)
|
||||
return nil
|
||||
}
|
25
example-project/cmd/sidecar/tracer/handlers/routes.go
Normal file
25
example-project/cmd/sidecar/tracer/handlers/routes.go
Normal file
@ -0,0 +1,25 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/mid"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
)
|
||||
|
||||
// API returns a handler for a set of routes.
|
||||
func API(shutdown chan os.Signal, log *log.Logger, zipkinHost string, apiHost string) http.Handler {
|
||||
|
||||
app := web.NewApp(shutdown, log, mid.Logger(log), mid.Errors(log))
|
||||
|
||||
z := NewZipkin(zipkinHost, apiHost, time.Second)
|
||||
app.Handle("POST", "/v1/publish", z.Publish)
|
||||
|
||||
h := Health{}
|
||||
app.Handle("GET", "/v1/health", h.Check)
|
||||
|
||||
return app
|
||||
}
|
326
example-project/cmd/sidecar/tracer/handlers/zipkin.go
Normal file
326
example-project/cmd/sidecar/tracer/handlers/zipkin.go
Normal file
@ -0,0 +1,326 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"github.com/openzipkin/zipkin-go/model"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// Zipkin represents the API to collect span data and send to zipkin.
|
||||
type Zipkin struct {
|
||||
zipkinHost string // IP:port of the zipkin service.
|
||||
localHost string // IP:port of the sidecare consuming the trace data.
|
||||
sendTimeout time.Duration // Time to wait for the sidecar to respond on send.
|
||||
client http.Client // Provides APIs for performing the http send.
|
||||
}
|
||||
|
||||
// NewZipkin provides support for publishing traces to zipkin.
|
||||
func NewZipkin(zipkinHost string, localHost string, sendTimeout time.Duration) *Zipkin {
|
||||
tr := http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
DualStack: true,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 2,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
|
||||
z := Zipkin{
|
||||
zipkinHost: zipkinHost,
|
||||
localHost: localHost,
|
||||
sendTimeout: sendTimeout,
|
||||
client: http.Client{
|
||||
Transport: &tr,
|
||||
},
|
||||
}
|
||||
|
||||
return &z
|
||||
}
|
||||
|
||||
// Publish takes a batch and publishes that to a host system.
|
||||
func (z *Zipkin) Publish(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
var sd []trace.SpanData
|
||||
if err := json.NewDecoder(r.Body).Decode(&sd); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := z.send(sd); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
web.Respond(ctx, w, nil, http.StatusNoContent)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// send uses HTTP to send the data to the tracing sidecar for processing.
|
||||
func (z *Zipkin) send(sendBatch []trace.SpanData) error {
|
||||
le, err := newEndpoint("sales-api", z.localHost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sm := convertForZipkin(sendBatch, le)
|
||||
data, err := json.Marshal(sm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", z.zipkinHost, bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(req.Context(), z.sendTimeout)
|
||||
defer cancel()
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
ch := make(chan error)
|
||||
go func() {
|
||||
resp, err := z.client.Do(req)
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusAccepted {
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
ch <- fmt.Errorf("error on call : status[%s]", resp.Status)
|
||||
return
|
||||
}
|
||||
ch <- fmt.Errorf("error on call : status[%s] : %s", resp.Status, string(data))
|
||||
return
|
||||
}
|
||||
|
||||
ch <- nil
|
||||
}()
|
||||
|
||||
return <-ch
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
const (
|
||||
statusCodeTagKey = "error"
|
||||
statusDescriptionTagKey = "opencensus.status_description"
|
||||
)
|
||||
|
||||
var (
|
||||
sampledTrue = true
|
||||
canonicalCodes = [...]string{
|
||||
"OK",
|
||||
"CANCELLED",
|
||||
"UNKNOWN",
|
||||
"INVALID_ARGUMENT",
|
||||
"DEADLINE_EXCEEDED",
|
||||
"NOT_FOUND",
|
||||
"ALREADY_EXISTS",
|
||||
"PERMISSION_DENIED",
|
||||
"RESOURCE_EXHAUSTED",
|
||||
"FAILED_PRECONDITION",
|
||||
"ABORTED",
|
||||
"OUT_OF_RANGE",
|
||||
"UNIMPLEMENTED",
|
||||
"INTERNAL",
|
||||
"UNAVAILABLE",
|
||||
"DATA_LOSS",
|
||||
"UNAUTHENTICATED",
|
||||
}
|
||||
)
|
||||
|
||||
func convertForZipkin(spanData []trace.SpanData, localEndpoint *model.Endpoint) []model.SpanModel {
|
||||
sm := make([]model.SpanModel, len(spanData))
|
||||
for i := range spanData {
|
||||
sm[i] = zipkinSpan(&spanData[i], localEndpoint)
|
||||
}
|
||||
return sm
|
||||
}
|
||||
|
||||
func newEndpoint(serviceName string, hostPort string) (*model.Endpoint, error) {
|
||||
e := &model.Endpoint{
|
||||
ServiceName: serviceName,
|
||||
}
|
||||
|
||||
if hostPort == "" || hostPort == ":0" {
|
||||
if serviceName == "" {
|
||||
// if all properties are empty we should not have an Endpoint object.
|
||||
return nil, nil
|
||||
}
|
||||
return e, nil
|
||||
}
|
||||
|
||||
if strings.IndexByte(hostPort, ':') < 0 {
|
||||
hostPort += ":0"
|
||||
}
|
||||
|
||||
host, port, err := net.SplitHostPort(hostPort)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p, err := strconv.ParseUint(port, 10, 16)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e.Port = uint16(p)
|
||||
|
||||
addrs, err := net.LookupIP(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range addrs {
|
||||
addr := addrs[i].To4()
|
||||
if addr == nil {
|
||||
// IPv6 - 16 bytes
|
||||
if e.IPv6 == nil {
|
||||
e.IPv6 = addrs[i].To16()
|
||||
}
|
||||
} else {
|
||||
// IPv4 - 4 bytes
|
||||
if e.IPv4 == nil {
|
||||
e.IPv4 = addr
|
||||
}
|
||||
}
|
||||
if e.IPv4 != nil && e.IPv6 != nil {
|
||||
// Both IPv4 & IPv6 have been set, done...
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// default to 0 filled 4 byte array for IPv4 if IPv6 only host was found
|
||||
if e.IPv4 == nil {
|
||||
e.IPv4 = make([]byte, 4)
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func canonicalCodeString(code int32) string {
|
||||
if code < 0 || int(code) >= len(canonicalCodes) {
|
||||
return "error code " + strconv.FormatInt(int64(code), 10)
|
||||
}
|
||||
return canonicalCodes[code]
|
||||
}
|
||||
|
||||
func convertTraceID(t trace.TraceID) model.TraceID {
|
||||
return model.TraceID{
|
||||
High: binary.BigEndian.Uint64(t[:8]),
|
||||
Low: binary.BigEndian.Uint64(t[8:]),
|
||||
}
|
||||
}
|
||||
|
||||
func convertSpanID(s trace.SpanID) model.ID {
|
||||
return model.ID(binary.BigEndian.Uint64(s[:]))
|
||||
}
|
||||
|
||||
func spanKind(s *trace.SpanData) model.Kind {
|
||||
switch s.SpanKind {
|
||||
case trace.SpanKindClient:
|
||||
return model.Client
|
||||
case trace.SpanKindServer:
|
||||
return model.Server
|
||||
}
|
||||
return model.Undetermined
|
||||
}
|
||||
|
||||
func zipkinSpan(s *trace.SpanData, localEndpoint *model.Endpoint) model.SpanModel {
|
||||
sc := s.SpanContext
|
||||
z := model.SpanModel{
|
||||
SpanContext: model.SpanContext{
|
||||
TraceID: convertTraceID(sc.TraceID),
|
||||
ID: convertSpanID(sc.SpanID),
|
||||
Sampled: &sampledTrue,
|
||||
},
|
||||
Kind: spanKind(s),
|
||||
Name: s.Name,
|
||||
Timestamp: s.StartTime,
|
||||
Shared: false,
|
||||
LocalEndpoint: localEndpoint,
|
||||
}
|
||||
|
||||
if s.ParentSpanID != (trace.SpanID{}) {
|
||||
id := convertSpanID(s.ParentSpanID)
|
||||
z.ParentID = &id
|
||||
}
|
||||
|
||||
if s, e := s.StartTime, s.EndTime; !s.IsZero() && !e.IsZero() {
|
||||
z.Duration = e.Sub(s)
|
||||
}
|
||||
|
||||
// construct Tags from s.Attributes and s.Status.
|
||||
if len(s.Attributes) != 0 {
|
||||
m := make(map[string]string, len(s.Attributes)+2)
|
||||
for key, value := range s.Attributes {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
m[key] = v
|
||||
case bool:
|
||||
if v {
|
||||
m[key] = "true"
|
||||
} else {
|
||||
m[key] = "false"
|
||||
}
|
||||
case int64:
|
||||
m[key] = strconv.FormatInt(v, 10)
|
||||
}
|
||||
}
|
||||
z.Tags = m
|
||||
}
|
||||
if s.Status.Code != 0 || s.Status.Message != "" {
|
||||
if z.Tags == nil {
|
||||
z.Tags = make(map[string]string, 2)
|
||||
}
|
||||
if s.Status.Code != 0 {
|
||||
z.Tags[statusCodeTagKey] = canonicalCodeString(s.Status.Code)
|
||||
}
|
||||
if s.Status.Message != "" {
|
||||
z.Tags[statusDescriptionTagKey] = s.Status.Message
|
||||
}
|
||||
}
|
||||
|
||||
// construct Annotations from s.Annotations and s.MessageEvents.
|
||||
if len(s.Annotations) != 0 || len(s.MessageEvents) != 0 {
|
||||
z.Annotations = make([]model.Annotation, 0, len(s.Annotations)+len(s.MessageEvents))
|
||||
for _, a := range s.Annotations {
|
||||
z.Annotations = append(z.Annotations, model.Annotation{
|
||||
Timestamp: a.Time,
|
||||
Value: a.Message,
|
||||
})
|
||||
}
|
||||
for _, m := range s.MessageEvents {
|
||||
a := model.Annotation{
|
||||
Timestamp: m.Time,
|
||||
}
|
||||
switch m.EventType {
|
||||
case trace.MessageEventTypeSent:
|
||||
a.Value = "SENT"
|
||||
case trace.MessageEventTypeRecv:
|
||||
a.Value = "RECV"
|
||||
default:
|
||||
a.Value = "<?>"
|
||||
}
|
||||
z.Annotations = append(z.Annotations, a)
|
||||
}
|
||||
}
|
||||
|
||||
return z
|
||||
}
|
118
example-project/cmd/sidecar/tracer/main.go
Normal file
118
example-project/cmd/sidecar/tracer/main.go
Normal file
@ -0,0 +1,118 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/cmd/sidecar/tracer/handlers"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// =========================================================================
|
||||
// Logging
|
||||
|
||||
log := log.New(os.Stdout, "TRACER : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
|
||||
defer log.Println("main : Completed")
|
||||
|
||||
// =========================================================================
|
||||
// Configuration
|
||||
|
||||
var cfg struct {
|
||||
Web struct {
|
||||
APIHost string `default:"0.0.0.0:3002" envconfig:"API_HOST"`
|
||||
DebugHost string `default:"0.0.0.0:4002" envconfig:"DEBUG_HOST"`
|
||||
ReadTimeout time.Duration `default:"5s" envconfig:"READ_TIMEOUT"`
|
||||
WriteTimeout time.Duration `default:"5s" envconfig:"WRITE_TIMEOUT"`
|
||||
ShutdownTimeout time.Duration `default:"5s" envconfig:"SHUTDOWN_TIMEOUT"`
|
||||
}
|
||||
Zipkin struct {
|
||||
Host string `default:"http://zipkin:9411/api/v2/spans" envconfig:"HOST"`
|
||||
}
|
||||
}
|
||||
|
||||
if err := envconfig.Process("TRACER", &cfg); err != nil {
|
||||
log.Fatalf("main : Parsing Config : %v", err)
|
||||
}
|
||||
|
||||
cfgJSON, err := json.MarshalIndent(cfg, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("main : Marshalling Config to JSON : %v", err)
|
||||
}
|
||||
log.Printf("config : %v\n", string(cfgJSON))
|
||||
|
||||
// =========================================================================
|
||||
// Start Debug Service. Not concerned with shutting this down when the
|
||||
// application is being shutdown.
|
||||
//
|
||||
// /debug/pprof - Added to the default mux by the net/http/pprof package.
|
||||
go func() {
|
||||
log.Printf("main : Debug Listening %s", cfg.Web.DebugHost)
|
||||
log.Printf("main : Debug Listener closed : %v", http.ListenAndServe(cfg.Web.DebugHost, http.DefaultServeMux))
|
||||
}()
|
||||
|
||||
// =========================================================================
|
||||
// Start API Service
|
||||
|
||||
// Make a channel to listen for an interrupt or terminate signal from the OS.
|
||||
// Use a buffered channel because the signal package requires it.
|
||||
shutdown := make(chan os.Signal, 1)
|
||||
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
api := http.Server{
|
||||
Addr: cfg.Web.APIHost,
|
||||
Handler: handlers.API(shutdown, log, cfg.Zipkin.Host, cfg.Web.APIHost),
|
||||
ReadTimeout: cfg.Web.ReadTimeout,
|
||||
WriteTimeout: cfg.Web.WriteTimeout,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
}
|
||||
|
||||
// Make a channel to listen for errors coming from the listener. Use a
|
||||
// buffered channel so the goroutine can exit if we don't collect this error.
|
||||
serverErrors := make(chan error, 1)
|
||||
|
||||
// Start the service listening for requests.
|
||||
go func() {
|
||||
log.Printf("main : API Listening %s", cfg.Web.APIHost)
|
||||
serverErrors <- api.ListenAndServe()
|
||||
}()
|
||||
|
||||
// =========================================================================
|
||||
// Shutdown
|
||||
|
||||
// Blocking main and waiting for shutdown.
|
||||
select {
|
||||
case err := <-serverErrors:
|
||||
log.Fatalf("main : Error starting server: %v", err)
|
||||
|
||||
case sig := <-shutdown:
|
||||
log.Printf("main : %v : Start shutdown..", sig)
|
||||
|
||||
// Create context for Shutdown call.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), cfg.Web.ShutdownTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Asking listener to shutdown and load shed.
|
||||
err := api.Shutdown(ctx)
|
||||
if err != nil {
|
||||
log.Printf("main : Graceful shutdown did not complete in %v : %v", cfg.Web.ShutdownTimeout, err)
|
||||
err = api.Close()
|
||||
}
|
||||
|
||||
// Log the status of this shutdown.
|
||||
switch {
|
||||
case sig == syscall.SIGSTOP:
|
||||
log.Fatal("main : Integrity issue caused shutdown")
|
||||
case err != nil:
|
||||
log.Fatalf("main : Could not stop server gracefully : %v", err)
|
||||
}
|
||||
}
|
||||
}
|
66
example-project/docker-compose.yaml
Normal file
66
example-project/docker-compose.yaml
Normal file
@ -0,0 +1,66 @@
|
||||
# https://docs.docker.com/compose/compose-file
|
||||
# docker-compose up
|
||||
# docker-compose stop
|
||||
# docker-compose down
|
||||
version: '3'
|
||||
|
||||
networks:
|
||||
shared-network:
|
||||
driver: bridge
|
||||
|
||||
services:
|
||||
|
||||
# This starts a local mongo DB.
|
||||
mongo:
|
||||
container_name: mongo
|
||||
networks:
|
||||
- shared-network
|
||||
image: mongo:3-jessie
|
||||
ports:
|
||||
- 27017:27017
|
||||
command: --bind_ip 0.0.0.0
|
||||
|
||||
# This is the core CRUD based service.
|
||||
sales-api:
|
||||
container_name: sales-api
|
||||
networks:
|
||||
- shared-network
|
||||
image: gcr.io/sales-api/sales-api-amd64:1.0
|
||||
ports:
|
||||
- 3000:3000 # CRUD API
|
||||
- 4000:4000 # DEBUG API
|
||||
environment:
|
||||
- SALES_AUTH_KEY_ID=1
|
||||
# - SALES_DB_HOST=got:got2015@ds039441.mongolab.com:39441/gotraining
|
||||
# - GODEBUG=gctrace=1
|
||||
|
||||
# This sidecar publishes metrics to the console by default.
|
||||
metrics:
|
||||
container_name: metrics
|
||||
networks:
|
||||
- shared-network
|
||||
image: gcr.io/sales-api/metrics-amd64:1.0
|
||||
ports:
|
||||
- 3001:3001 # EXPVAR API
|
||||
- 4001:4001 # DEBUG API
|
||||
|
||||
# This sidecar publishes tracing to the console by default.
|
||||
tracer:
|
||||
container_name: tracer
|
||||
networks:
|
||||
- shared-network
|
||||
image: gcr.io/sales-api/tracer-amd64:1.0
|
||||
ports:
|
||||
- 3002:3002 # TRACER API
|
||||
- 4002:4002 # DEBUG API
|
||||
# environment:
|
||||
# - SALES_ZIPKIN_HOST=http://zipkin:9411/api/v2/spans
|
||||
|
||||
# This sidecar allows for the viewing of traces.
|
||||
zipkin:
|
||||
container_name: zipkin
|
||||
networks:
|
||||
- shared-network
|
||||
image: openzipkin/zipkin:2.11
|
||||
ports:
|
||||
- 9411:9411
|
31
example-project/dockerfile
Normal file
31
example-project/dockerfile
Normal file
@ -0,0 +1,31 @@
|
||||
# Build the Go Binary.
|
||||
|
||||
FROM golang:1.12.1 as build
|
||||
ENV CGO_ENABLED 0
|
||||
ARG VCS_REF
|
||||
ARG PACKAGE_NAME
|
||||
ARG PACKAGE_PREFIX
|
||||
RUN mkdir -p /go/src/geeks-accelerator/oss/saas-starter-kit/example-project
|
||||
COPY . /go/src/geeks-accelerator/oss/saas-starter-kit/example-project
|
||||
WORKDIR /go/src/geeks-accelerator/oss/saas-starter-kit/example-project/cmd/${PACKAGE_PREFIX}${PACKAGE_NAME}
|
||||
RUN go build -ldflags "-s -w -X main.build=${VCS_REF}" -a -tags netgo
|
||||
|
||||
|
||||
# Run the Go Binary in Alpine.
|
||||
|
||||
FROM alpine:3.7
|
||||
ARG BUILD_DATE
|
||||
ARG VCS_REF
|
||||
ARG PACKAGE_NAME
|
||||
ARG PACKAGE_PREFIX
|
||||
COPY --from=build /go/src/geeks-accelerator/oss/saas-starter-kit/example-project/cmd/${PACKAGE_PREFIX}${PACKAGE_NAME}/${PACKAGE_NAME} /app/main
|
||||
COPY --from=build /go/src/geeks-accelerator/oss/saas-starter-kit/example-project/private.pem /app/private.pem
|
||||
WORKDIR /app
|
||||
CMD /app/main
|
||||
|
||||
LABEL org.opencontainers.image.created="${BUILD_DATE}" \
|
||||
org.opencontainers.image.title="${PACKAGE_NAME}" \
|
||||
org.opencontainers.image.authors="William Kennedy <bill@ardanlabs.com>" \
|
||||
org.opencontainers.image.source="https://geeks-accelerator/oss/saas-starter-kit/example-project/cmd/${PACKAGE_PREFIX}${PACKAGE_NAME}" \
|
||||
org.opencontainers.image.revision="${VCS_REF}" \
|
||||
org.opencontainers.image.vendor="Ardan Labs"
|
24
example-project/gke-deploy-database.yaml
Normal file
24
example-project/gke-deploy-database.yaml
Normal file
@ -0,0 +1,24 @@
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: mongo
|
||||
spec:
|
||||
replicas: 1
|
||||
strategy: {}
|
||||
template:
|
||||
metadata:
|
||||
name: mongo
|
||||
labels:
|
||||
database: mongo
|
||||
spec:
|
||||
containers:
|
||||
- name: mongo
|
||||
image: mongo:3-jessie
|
||||
args:
|
||||
- --bind_ip
|
||||
- 0.0.0.0
|
||||
ports:
|
||||
- name: mongo
|
||||
containerPort: 27017
|
||||
resources: {}
|
||||
status: {}
|
56
example-project/gke-deploy-sales-api.yaml
Normal file
56
example-project/gke-deploy-sales-api.yaml
Normal file
@ -0,0 +1,56 @@
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: sales-api
|
||||
spec:
|
||||
replicas: 1
|
||||
strategy: {}
|
||||
template:
|
||||
metadata:
|
||||
name: sales-api
|
||||
labels:
|
||||
service: sales-api
|
||||
spec:
|
||||
containers:
|
||||
- name: zipkin
|
||||
image: openzipkin/zipkin:2.11
|
||||
ports:
|
||||
- name: zipkin
|
||||
containerPort: 9411
|
||||
resources: {}
|
||||
- name: sales-api
|
||||
image: gcr.io/sales-api/sales-api-amd64:1.0
|
||||
env:
|
||||
- name: SALES_TRACE_HOST
|
||||
value: "http://localhost:3002/v1/publish"
|
||||
- name: SALES_AUTH_KEY_ID
|
||||
value: "1"
|
||||
ports:
|
||||
- name: sales-api
|
||||
containerPort: 3000
|
||||
- name: debug
|
||||
containerPort: 4000
|
||||
resources: {}
|
||||
- name: metrics
|
||||
image: gcr.io/sales-api/metrics-amd64:1.0
|
||||
env:
|
||||
- name: METRICS_COLLECT_FROM
|
||||
value: "http://localhost:4000/debug/vars"
|
||||
ports:
|
||||
- name: metrics
|
||||
containerPort: 3001
|
||||
- name: debug
|
||||
containerPort: 4001
|
||||
resources: {}
|
||||
- name: tracer
|
||||
image: gcr.io/sales-api/tracer-amd64:1.0
|
||||
env:
|
||||
- name: TRACER_ZIPKIN_HOST
|
||||
value: "http://localhost:9411/api/v2/spans"
|
||||
ports:
|
||||
- name: tracer
|
||||
containerPort: 3002
|
||||
- name: debug
|
||||
containerPort: 4002
|
||||
resources: {}
|
||||
status: {}
|
11
example-project/gke-expose-database.yaml
Normal file
11
example-project/gke-expose-database.yaml
Normal file
@ -0,0 +1,11 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: mongo
|
||||
spec:
|
||||
selector:
|
||||
database: mongo
|
||||
ports:
|
||||
- name: "db"
|
||||
port: 27017
|
||||
targetPort: 27017
|
19
example-project/gke-expose-sales-api.yaml
Normal file
19
example-project/gke-expose-sales-api.yaml
Normal file
@ -0,0 +1,19 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: sales-api
|
||||
spec:
|
||||
selector:
|
||||
service: sales-api
|
||||
ports:
|
||||
- name: "zipkin"
|
||||
port: 9411
|
||||
targetPort: 9411
|
||||
- name: "sales-api"
|
||||
port: 3000
|
||||
targetPort: 3000
|
||||
- name: "metrics"
|
||||
port: 3001
|
||||
targetPort: 3001
|
||||
status:
|
||||
loadBalancer: {}
|
25
example-project/go.mod
Normal file
25
example-project/go.mod
Normal file
@ -0,0 +1,25 @@
|
||||
module geeks-accelerator/oss/saas-starter-kit/example-project
|
||||
|
||||
require (
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||
github.com/dimfeld/httptreemux v5.0.1+incompatible
|
||||
github.com/go-playground/locales v0.12.1
|
||||
github.com/go-playground/universal-translator v0.16.0
|
||||
github.com/google/go-cmp v0.2.0
|
||||
github.com/kelseyhightower/envconfig v1.3.0
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/leodido/go-urn v1.1.0 // indirect
|
||||
github.com/openzipkin/zipkin-go v0.1.1
|
||||
github.com/pborman/uuid v0.0.0-20180122190007-c65b2f87fee3
|
||||
github.com/pkg/errors v0.8.0
|
||||
github.com/stretchr/testify v1.3.0 // indirect
|
||||
go.opencensus.io v0.14.0
|
||||
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225 // indirect
|
||||
golang.org/x/text v0.3.0 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
|
||||
gopkg.in/go-playground/validator.v9 v9.28.0
|
||||
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce
|
||||
gopkg.in/yaml.v2 v2.2.1 // indirect
|
||||
)
|
51
example-project/go.sum
Normal file
51
example-project/go.sum
Normal file
@ -0,0 +1,51 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/dimfeld/httptreemux v5.0.1+incompatible h1:Qj3gVcDNoOthBAqftuD596rm4wg/adLLz5xh5CmpiCA=
|
||||
github.com/dimfeld/httptreemux v5.0.1+incompatible/go.mod h1:rbUlSV+CCpv/SuqUTP/8Bk2O3LyUV436/yaRGkhP6Z0=
|
||||
github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc=
|
||||
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
|
||||
github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM=
|
||||
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
|
||||
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/kelseyhightower/envconfig v1.3.0 h1:IvRS4f2VcIQy6j4ORGIf9145T/AsUB+oY8LyvN8BXNM=
|
||||
github.com/kelseyhightower/envconfig v1.3.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8=
|
||||
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
|
||||
github.com/openzipkin/zipkin-go v0.1.1 h1:A/ADD6HaPnAKj3yS7HjGHRK77qi41Hi0DirOOIQAeIw=
|
||||
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
|
||||
github.com/pborman/uuid v0.0.0-20180122190007-c65b2f87fee3 h1:9J0mOv1rXIBlRjQCiAGyx9C3dZZh5uIa3HU0oTV8v1E=
|
||||
github.com/pborman/uuid v0.0.0-20180122190007-c65b2f87fee3/go.mod h1:VyrYX9gd7irzKovcSS6BIIEwPRkP2Wm2m9ufcdFSJ34=
|
||||
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
go.opencensus.io v0.14.0 h1:1eTLxqxSIAylcKoxnNkdhvvBNZDA8JwkKNXxgyma0IA=
|
||||
go.opencensus.io v0.14.0/go.mod h1:UffZAU+4sDEINUGP/B7UfBBkq4fqLu9zXAX7ke6CHW0=
|
||||
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b h1:2b9XGzhjiYsYPnKXoEfL7klWZQIt8IfyRCz62gCqqlQ=
|
||||
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225 h1:kNX+jCowfMYzvlSvJu5pQWEmyWFrBXJ3PBy10xKMXK8=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
|
||||
gopkg.in/go-playground/validator.v9 v9.28.0 h1:6pzvnzx1RWaaQiAmv6e1DvCFULRaz5cKoP5j1VcrLsc=
|
||||
gopkg.in/go-playground/validator.v9 v9.28.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
|
||||
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce h1:xcEWjVhvbDy+nHP67nPDDpbYrY+ILlfndk4bRioVHaU=
|
||||
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
|
||||
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
99
example-project/internal/mid/auth.go
Normal file
99
example-project/internal/mid/auth.go
Normal file
@ -0,0 +1,99 @@
|
||||
package mid
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"github.com/pkg/errors"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// ErrForbidden is returned when an authenticated user does not have a
|
||||
// sufficient role for an action.
|
||||
var ErrForbidden = web.NewRequestError(
|
||||
errors.New("you are not authorized for that action"),
|
||||
http.StatusForbidden,
|
||||
)
|
||||
|
||||
// Authenticate validates a JWT from the `Authorization` header.
|
||||
func Authenticate(authenticator *auth.Authenticator) web.Middleware {
|
||||
|
||||
// This is the actual middleware function to be executed.
|
||||
f := func(after web.Handler) web.Handler {
|
||||
|
||||
// Wrap this handler around the next one provided.
|
||||
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.mid.Authenticate")
|
||||
defer span.End()
|
||||
|
||||
authHdr := r.Header.Get("Authorization")
|
||||
if authHdr == "" {
|
||||
err := errors.New("missing Authorization header")
|
||||
return web.NewRequestError(err, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
tknStr, err := parseAuthHeader(authHdr)
|
||||
if err != nil {
|
||||
return web.NewRequestError(err, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
claims, err := authenticator.ParseClaims(tknStr)
|
||||
if err != nil {
|
||||
return web.NewRequestError(err, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
// Add claims to the context so they can be retrieved later.
|
||||
ctx = context.WithValue(ctx, auth.Key, claims)
|
||||
|
||||
return after(ctx, w, r, params)
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
// HasRole validates that an authenticated user has at least one role from a
|
||||
// specified list. This method constructs the actual function that is used.
|
||||
func HasRole(roles ...string) web.Middleware {
|
||||
|
||||
// This is the actual middleware function to be executed.
|
||||
f := func(after web.Handler) web.Handler {
|
||||
|
||||
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.mid.HasRole")
|
||||
defer span.End()
|
||||
|
||||
claims, ok := ctx.Value(auth.Key).(auth.Claims)
|
||||
if !ok {
|
||||
// TODO(jlw) should this be a web.Shutdown?
|
||||
return errors.New("claims missing from context: HasRole called without/before Authenticate")
|
||||
}
|
||||
|
||||
if !claims.HasRole(roles...) {
|
||||
return ErrForbidden
|
||||
}
|
||||
|
||||
return after(ctx, w, r, params)
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
// parseAuthHeader parses an authorization header. Expected header is of
|
||||
// the format `Bearer <token>`.
|
||||
func parseAuthHeader(bearerStr string) (string, error) {
|
||||
split := strings.Split(bearerStr, " ")
|
||||
if len(split) != 2 || strings.ToLower(split[0]) != "bearer" {
|
||||
return "", errors.New("Expected Authorization header format: Bearer <token>")
|
||||
}
|
||||
|
||||
return split[1], nil
|
||||
}
|
57
example-project/internal/mid/errors.go
Normal file
57
example-project/internal/mid/errors.go
Normal file
@ -0,0 +1,57 @@
|
||||
package mid
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// Errors handles errors coming out of the call chain. It detects normal
|
||||
// application errors which are used to respond to the client in a uniform way.
|
||||
// Unexpected errors (status >= 500) are logged.
|
||||
func Errors(log *log.Logger) web.Middleware {
|
||||
|
||||
// This is the actual middleware function to be executed.
|
||||
f := func(before web.Handler) web.Handler {
|
||||
|
||||
// Create the handler that will be attached in the middleware chain.
|
||||
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.mid.Errors")
|
||||
defer span.End()
|
||||
|
||||
// If the context is missing this value, request the service
|
||||
// to be shutdown gracefully.
|
||||
v, ok := ctx.Value(web.KeyValues).(*web.Values)
|
||||
if !ok {
|
||||
return web.NewShutdownError("web value missing from context")
|
||||
}
|
||||
|
||||
if err := before(ctx, w, r, params); err != nil {
|
||||
|
||||
// Log the error.
|
||||
log.Printf("%s : ERROR : %+v", v.TraceID, err)
|
||||
|
||||
// Respond to the error.
|
||||
if err := web.RespondError(ctx, w, err); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If we receive the shutdown err we need to return it
|
||||
// back to the base handler to shutdown the service.
|
||||
if ok := web.IsShutdown(err); ok {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// The error has been handled so we can stop propagating it.
|
||||
return nil
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
49
example-project/internal/mid/logger.go
Normal file
49
example-project/internal/mid/logger.go
Normal file
@ -0,0 +1,49 @@
|
||||
package mid
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// Logger writes some information about the request to the logs in the
|
||||
// format: TraceID : (200) GET /foo -> IP ADDR (latency)
|
||||
func Logger(log *log.Logger) web.Middleware {
|
||||
|
||||
// This is the actual middleware function to be executed.
|
||||
f := func(before web.Handler) web.Handler {
|
||||
|
||||
// Create the handler that will be attached in the middleware chain.
|
||||
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.mid.Logger")
|
||||
defer span.End()
|
||||
|
||||
// If the context is missing this value, request the service
|
||||
// to be shutdown gracefully.
|
||||
v, ok := ctx.Value(web.KeyValues).(*web.Values)
|
||||
if !ok {
|
||||
return web.NewShutdownError("web value missing from context")
|
||||
}
|
||||
|
||||
err := before(ctx, w, r, params)
|
||||
|
||||
log.Printf("%s : (%d) : %s %s -> %s (%s)\n",
|
||||
v.TraceID,
|
||||
v.StatusCode,
|
||||
r.Method, r.URL.Path,
|
||||
r.RemoteAddr, time.Since(v.Now),
|
||||
)
|
||||
|
||||
// Return the error so it can be handled further up the chain.
|
||||
return err
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
58
example-project/internal/mid/metrics.go
Normal file
58
example-project/internal/mid/metrics.go
Normal file
@ -0,0 +1,58 @@
|
||||
package mid
|
||||
|
||||
import (
|
||||
"context"
|
||||
"expvar"
|
||||
"net/http"
|
||||
"runtime"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// m contains the global program counters for the application.
|
||||
var m = struct {
|
||||
gr *expvar.Int
|
||||
req *expvar.Int
|
||||
err *expvar.Int
|
||||
}{
|
||||
gr: expvar.NewInt("goroutines"),
|
||||
req: expvar.NewInt("requests"),
|
||||
err: expvar.NewInt("errors"),
|
||||
}
|
||||
|
||||
// Metrics updates program counters.
|
||||
func Metrics() web.Middleware {
|
||||
|
||||
// This is the actual middleware function to be executed.
|
||||
f := func(before web.Handler) web.Handler {
|
||||
|
||||
// Wrap this handler around the next one provided.
|
||||
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.mid.Metrics")
|
||||
defer span.End()
|
||||
|
||||
err := before(ctx, w, r, params)
|
||||
|
||||
// Increment the request counter.
|
||||
m.req.Add(1)
|
||||
|
||||
// Update the count for the number of active goroutines every 100 requests.
|
||||
if m.req.Value()%100 == 0 {
|
||||
m.gr.Set(int64(runtime.NumGoroutine()))
|
||||
}
|
||||
|
||||
// Increment the errors counter if an error occurred on this request.
|
||||
if err != nil {
|
||||
m.err.Add(1)
|
||||
}
|
||||
|
||||
// Return the error so it can be handled further up the chain.
|
||||
return err
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
40
example-project/internal/mid/panics.go
Normal file
40
example-project/internal/mid/panics.go
Normal file
@ -0,0 +1,40 @@
|
||||
package mid
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"github.com/pkg/errors"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// Panics recovers from panics and converts the panic to an error so it is
|
||||
// reported in Metrics and handled in Errors.
|
||||
func Panics() web.Middleware {
|
||||
|
||||
// This is the actual middleware function to be executed.
|
||||
f := func(after web.Handler) web.Handler {
|
||||
|
||||
// Wrap this handler around the next one provided.
|
||||
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) (err error) {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.mid.Panics")
|
||||
defer span.End()
|
||||
|
||||
// Defer a function to recover from a panic and set the err return variable
|
||||
// after the fact. Using the errors package will generate a stack trace.
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = errors.Errorf("panic: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
// Call the next Handler and set its return value in the err variable.
|
||||
return after(ctx, w, r, params)
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
127
example-project/internal/platform/auth/auth.go
Normal file
127
example-project/internal/platform/auth/auth.go
Normal file
@ -0,0 +1,127 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"fmt"
|
||||
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// KeyFunc is used to map a JWT key id (kid) to the corresponding public key.
|
||||
// It is a requirement for creating an Authenticator.
|
||||
//
|
||||
// * Private keys should be rotated. During the transition period, tokens
|
||||
// signed with the old and new keys can coexist by looking up the correct
|
||||
// public key by key id (kid).
|
||||
//
|
||||
// * Key-id-to-public-key resolution is usually accomplished via a public JWKS
|
||||
// endpoint. See https://auth0.com/docs/jwks for more details.
|
||||
type KeyFunc func(keyID string) (*rsa.PublicKey, error)
|
||||
|
||||
// NewSingleKeyFunc is a simple implementation of KeyFunc that only ever
|
||||
// supports one key. This is easy for development but in production should be
|
||||
// replaced with a caching layer that calls a JWKS endpoint.
|
||||
func NewSingleKeyFunc(id string, key *rsa.PublicKey) KeyFunc {
|
||||
return func(kid string) (*rsa.PublicKey, error) {
|
||||
if id != kid {
|
||||
return nil, fmt.Errorf("unrecognized kid %q", kid)
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Authenticator is used to authenticate clients. It can generate a token for a
|
||||
// set of user claims and recreate the claims by parsing the token.
|
||||
type Authenticator struct {
|
||||
privateKey *rsa.PrivateKey
|
||||
keyID string
|
||||
algorithm string
|
||||
kf KeyFunc
|
||||
parser *jwt.Parser
|
||||
}
|
||||
|
||||
// NewAuthenticator creates an *Authenticator for use. It will error if:
|
||||
// - The private key is nil.
|
||||
// - The public key func is nil.
|
||||
// - The key ID is blank.
|
||||
// - The specified algorithm is unsupported.
|
||||
func NewAuthenticator(key *rsa.PrivateKey, keyID, algorithm string, publicKeyFunc KeyFunc) (*Authenticator, error) {
|
||||
if key == nil {
|
||||
return nil, errors.New("private key cannot be nil")
|
||||
}
|
||||
if publicKeyFunc == nil {
|
||||
return nil, errors.New("public key function cannot be nil")
|
||||
}
|
||||
if keyID == "" {
|
||||
return nil, errors.New("keyID cannot be blank")
|
||||
}
|
||||
if jwt.GetSigningMethod(algorithm) == nil {
|
||||
return nil, errors.Errorf("unknown algorithm %v", algorithm)
|
||||
}
|
||||
|
||||
// Create the token parser to use. The algorithm used to sign the JWT must be
|
||||
// validated to avoid a critical vulnerability:
|
||||
// https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
|
||||
parser := jwt.Parser{
|
||||
ValidMethods: []string{algorithm},
|
||||
}
|
||||
|
||||
a := Authenticator{
|
||||
privateKey: key,
|
||||
keyID: keyID,
|
||||
algorithm: algorithm,
|
||||
kf: publicKeyFunc,
|
||||
parser: &parser,
|
||||
}
|
||||
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
// GenerateToken generates a signed JWT token string representing the user Claims.
|
||||
func (a *Authenticator) GenerateToken(claims Claims) (string, error) {
|
||||
method := jwt.GetSigningMethod(a.algorithm)
|
||||
|
||||
tkn := jwt.NewWithClaims(method, claims)
|
||||
tkn.Header["kid"] = a.keyID
|
||||
|
||||
str, err := tkn.SignedString(a.privateKey)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "signing token")
|
||||
}
|
||||
|
||||
return str, nil
|
||||
}
|
||||
|
||||
// ParseClaims recreates the Claims that were used to generate a token. It
|
||||
// verifies that the token was signed using our key.
|
||||
func (a *Authenticator) ParseClaims(tknStr string) (Claims, error) {
|
||||
|
||||
// f is a function that returns the public key for validating a token. We use
|
||||
// the parsed (but unverified) token to find the key id. That ID is passed to
|
||||
// our KeyFunc to find the public key to use for verification.
|
||||
f := func(t *jwt.Token) (interface{}, error) {
|
||||
kid, ok := t.Header["kid"]
|
||||
if !ok {
|
||||
return nil, errors.New("Missing key id (kid) in token header")
|
||||
}
|
||||
kidStr, ok := kid.(string)
|
||||
if !ok {
|
||||
return nil, errors.New("Token key id (kid) must be string")
|
||||
}
|
||||
|
||||
return a.kf(kidStr)
|
||||
}
|
||||
|
||||
var claims Claims
|
||||
tkn, err := a.parser.ParseWithClaims(tknStr, &claims, f)
|
||||
if err != nil {
|
||||
return Claims{}, errors.Wrap(err, "parsing token")
|
||||
}
|
||||
|
||||
if !tkn.Valid {
|
||||
return Claims{}, errors.New("Invalid token")
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
97
example-project/internal/platform/auth/auth_test.go
Normal file
97
example-project/internal/platform/auth/auth_test.go
Normal file
@ -0,0 +1,97 @@
|
||||
package auth_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
)
|
||||
|
||||
func TestAuthenticator(t *testing.T) {
|
||||
|
||||
// Parse the private key used to generate the token.
|
||||
prvKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(privateRSAKey))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Parse the public key used to validate the token.
|
||||
pubKey, err := jwt.ParseRSAPublicKeyFromPEM([]byte(publicRSAKey))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
a, err := auth.NewAuthenticator(prvKey, privateRSAKeyID, "RS256", auth.NewSingleKeyFunc(privateRSAKeyID, pubKey))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Generate the token.
|
||||
signedClaims := auth.Claims{
|
||||
Roles: []string{auth.RoleAdmin},
|
||||
}
|
||||
|
||||
tknStr, err := a.GenerateToken(signedClaims)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
parsedClaims, err := a.ParseClaims(tknStr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Assert expected claims.
|
||||
if exp, got := len(signedClaims.Roles), len(parsedClaims.Roles); exp != got {
|
||||
t.Fatalf("expected %v roles, got %v", exp, got)
|
||||
}
|
||||
if exp, got := signedClaims.Roles[0], parsedClaims.Roles[0]; exp != got {
|
||||
t.Fatalf("expected roles[0] == %v, got %v", exp, got)
|
||||
}
|
||||
}
|
||||
|
||||
// The key id we would have generated for the private below key
|
||||
const privateRSAKeyID = "54bb2165-71e1-41a6-af3e-7da4a0e1e2c1"
|
||||
|
||||
// Output of:
|
||||
// openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048
|
||||
const privateRSAKey = `-----BEGIN PRIVATE KEY-----
|
||||
MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQDdiBDU4jqRYuHl
|
||||
yBmo5dWB1j9aeDrXzUTJbRKlgo+DWDQzIzJQvackvRu8/f7B5cseoqmeJcmBu6pc
|
||||
4DmQ+puGNHxzCyYVFSMwRtHBZvfWS3P+UqIXCKRAX/NZbLkUEeqPnn5WXjA+YXKk
|
||||
sfniE0xDH8W22o0OXHOzRhDWORjNTulpMpLv8tKnnLKh2Y/kCL/4vo0SZ+RWh8F9
|
||||
4+JTZx/47RHWb6fkxkikyTO3zO3efIkrKjfRx2CwFwO2rQ/3T04GQB/Lgr5lfJQU
|
||||
iofvvVYuj2xBJao+3t9Ir0OeSbw1T5Rz03VLtN8SZhvaxWaBfwkUuUNL1glJO+Yd
|
||||
LkMxGS0zAgMBAAECggEBAKM6m7RQUPlJE8u8qfOCDdSSKbIefrT9wZ5tKN0dG2Oa
|
||||
/TNkzrEhXOO8F5Ek0a7LA+Q51KL7ksNtpLS0XpZNoYS8bapS36ePIJN0yx8nIJwc
|
||||
koYlGtu/+U6ZpHQSoTiBjwRtswcudXuxT8i8frOupnWbFpKJ7H9Vbcb9bHB8N6Mm
|
||||
D63wSBR08ZMrZXheKHQCQcxSQ2ZQZ+X3LBIOdXZH1aaptU2KpMEU5oyxXPShTVMg
|
||||
0f748yU2njXCF0ZABEanXgp13egr/MPqHwnS/h0PH45bNy3IgFtMEHEouQFsAzoS
|
||||
qNe8/9WnrpY87UdSZMnzF/IAXV0bmollDnqfM8/EqxkCgYEA96ThXYGzAK5RKNqp
|
||||
RqVdRVA0UTT48sJvrxLMuHpyUzg6cl8FZE5rrNxFbouxvyN192Ctv1q8yfv4/HfM
|
||||
KpmtEjt3fYtITHVXII6O3qNaRoIEPwKT4eK/ar+JO59vI0YvweXvDH5TkS9aiFr+
|
||||
pPGf3a7EbE24BKhgiI8eT6K0VuUCgYEA5QGg11ZVoUut4ERAPouwuSdWwNe0HYqJ
|
||||
A1m5vTvF5ghUHAb023lrr7Psq9DPJQQe7GzPfXafsat9hGenyqiyxo1gwClIyoEH
|
||||
fOg753kdHcy60VVzumsPXece3OOSnd0rRMgfsSsclgYO7z0g9YZPAjt2w9NVw6uN
|
||||
UDqX3eO2WjcCgYEA015eoNHv99fRG96udsbz+hI/5UQibAl7C+Iu7BJO/CrU8AOc
|
||||
dYXdr5f+hyEioDLjIDbbdaU71+aCGPMjRwUNzK8HCRfVqLTKndYvqWWhyuZ0O1e2
|
||||
4ykHGlTLDCHD2Uaxwny/8VjteNEDI7kO+bfmLG9b5djcBNW2Nzh4tZ348OUCgYEA
|
||||
vIrTppbhF1QciqkGj7govrgBt/GfzDaTyZtkzcTZkSNIRG8Bx3S3UUh8UZUwBpTW
|
||||
9OY9ClnQ7tF3HLzOq46q6cfaYTtcP8Vtqcv2DgRsEW3OXazSBChC1ZgEk+4Vdz1x
|
||||
c0akuRP6jBXe099rNFno0LiudlmXoeqrBOPIxxnEt48CgYEAxNZBc/GKiHXz/ZRi
|
||||
IZtRT5rRRof7TEiDxSKOXHSG7HhIRDCrpwn4Dfi+GWNHIwsIlom8FzZTSHAN6pqP
|
||||
E8Imrlt3vuxnUE1UMkhDXrlhrxslRXU9enynVghAcSrg6ijs8KuN/9RB/I7H03cT
|
||||
77mx9eHMcYcRUciY5C8AOaArmMA=
|
||||
-----END PRIVATE KEY-----`
|
||||
|
||||
// Output of:
|
||||
// openssl rsa -pubout -in private.pem -out public.pem
|
||||
const publicRSAKey = `-----BEGIN PUBLIC KEY-----
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3YgQ1OI6kWLh5cgZqOXV
|
||||
gdY/Wng6181EyW0SpYKPg1g0MyMyUL2nJL0bvP3+weXLHqKpniXJgbuqXOA5kPqb
|
||||
hjR8cwsmFRUjMEbRwWb31ktz/lKiFwikQF/zWWy5FBHqj55+Vl4wPmFypLH54hNM
|
||||
Qx/FttqNDlxzs0YQ1jkYzU7paTKS7/LSp5yyodmP5Ai/+L6NEmfkVofBfePiU2cf
|
||||
+O0R1m+n5MZIpMkzt8zt3nyJKyo30cdgsBcDtq0P909OBkAfy4K+ZXyUFIqH771W
|
||||
Lo9sQSWqPt7fSK9Dnkm8NU+Uc9N1S7TfEmYb2sVmgX8JFLlDS9YJSTvmHS5DMRkt
|
||||
MwIDAQAB
|
||||
-----END PUBLIC KEY-----`
|
70
example-project/internal/platform/auth/claims.go
Normal file
70
example-project/internal/platform/auth/claims.go
Normal file
@ -0,0 +1,70 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// These are the expected values for Claims.Roles.
|
||||
const (
|
||||
RoleAdmin = "ADMIN"
|
||||
RoleUser = "USER"
|
||||
)
|
||||
|
||||
// ctxKey represents the type of value for the context key.
|
||||
type ctxKey int
|
||||
|
||||
// Key is used to store/retrieve a Claims value from a context.Context.
|
||||
const Key ctxKey = 1
|
||||
|
||||
// Claims represents the authorization claims transmitted via a JWT.
|
||||
type Claims struct {
|
||||
Roles []string `json:"roles"`
|
||||
jwt.StandardClaims
|
||||
}
|
||||
|
||||
// NewClaims constructs a Claims value for the identified user. The Claims
|
||||
// expire within a specified duration of the provided time. Additional fields
|
||||
// of the Claims can be set after calling NewClaims is desired.
|
||||
func NewClaims(subject string, roles []string, now time.Time, expires time.Duration) Claims {
|
||||
c := Claims{
|
||||
Roles: roles,
|
||||
StandardClaims: jwt.StandardClaims{
|
||||
Subject: subject,
|
||||
IssuedAt: now.Unix(),
|
||||
ExpiresAt: now.Add(expires).Unix(),
|
||||
},
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// Valid is called during the parsing of a token.
|
||||
func (c Claims) Valid() error {
|
||||
for _, r := range c.Roles {
|
||||
switch r {
|
||||
case RoleAdmin, RoleUser: // Role is valid.
|
||||
default:
|
||||
return fmt.Errorf("invalid role %q", r)
|
||||
}
|
||||
}
|
||||
if err := c.StandardClaims.Valid(); err != nil {
|
||||
return errors.Wrap(err, "validating standard claims")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// HasRole returns true if the claims has at least one of the provided roles.
|
||||
func (c Claims) HasRole(roles ...string) bool {
|
||||
for _, has := range c.Roles {
|
||||
for _, want := range roles {
|
||||
if has == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
124
example-project/internal/platform/db/db.go
Normal file
124
example-project/internal/platform/db/db.go
Normal file
@ -0,0 +1,124 @@
|
||||
// All material is licensed under the Apache License Version 2.0, January 2004
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.opencensus.io/trace"
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
)
|
||||
|
||||
// ErrInvalidDBProvided is returned in the event that an uninitialized db is
|
||||
// used to perform actions against.
|
||||
var ErrInvalidDBProvided = errors.New("invalid DB provided")
|
||||
|
||||
// DB is a collection of support for different DB technologies. Currently
|
||||
// only MongoDB has been implemented. We want to be able to access the raw
|
||||
// database support for the given DB so an interface does not work. Each
|
||||
// database is too different.
|
||||
type DB struct {
|
||||
|
||||
// MongoDB Support.
|
||||
database *mgo.Database
|
||||
session *mgo.Session
|
||||
}
|
||||
|
||||
// New returns a new DB value for use with MongoDB based on a registered
|
||||
// master session.
|
||||
func New(url string, timeout time.Duration) (*DB, error) {
|
||||
|
||||
// Set the default timeout for the session.
|
||||
if timeout == 0 {
|
||||
timeout = 60 * time.Second
|
||||
}
|
||||
|
||||
// Create a session which maintains a pool of socket connections
|
||||
// to our MongoDB.
|
||||
ses, err := mgo.DialWithTimeout(url, timeout)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "mgo.DialWithTimeout: %s,%v", url, timeout)
|
||||
}
|
||||
|
||||
// Reads may not be entirely up-to-date, but they will always see the
|
||||
// history of changes moving forward, the data read will be consistent
|
||||
// across sequential queries in the same session, and modifications made
|
||||
// within the session will be observed in following queries (read-your-writes).
|
||||
// http://godoc.org/labix.org/v2/mgo#Session.SetMode
|
||||
ses.SetMode(mgo.Monotonic, true)
|
||||
|
||||
db := DB{
|
||||
database: ses.DB(""),
|
||||
session: ses,
|
||||
}
|
||||
|
||||
return &db, nil
|
||||
}
|
||||
|
||||
// Close closes a DB value being used with MongoDB.
|
||||
func (db *DB) Close() {
|
||||
db.session.Close()
|
||||
}
|
||||
|
||||
// Copy returns a new DB value for use with MongoDB based on master session.
|
||||
func (db *DB) Copy() *DB {
|
||||
ses := db.session.Copy()
|
||||
|
||||
// As per the mgo documentation, https://godoc.org/gopkg.in/mgo.v2#Session.DB
|
||||
// if no database name is specified, then use the default one, or the one that
|
||||
// the connection was dialed with.
|
||||
newDB := DB{
|
||||
database: ses.DB(""),
|
||||
session: ses,
|
||||
}
|
||||
|
||||
return &newDB
|
||||
}
|
||||
|
||||
// Execute is used to execute MongoDB commands.
|
||||
func (db *DB) Execute(ctx context.Context, collName string, f func(*mgo.Collection) error) error {
|
||||
ctx, span := trace.StartSpan(ctx, "platform.DB.Execute")
|
||||
defer span.End()
|
||||
|
||||
if db == nil || db.session == nil {
|
||||
return errors.Wrap(ErrInvalidDBProvided, "db == nil || db.session == nil")
|
||||
}
|
||||
|
||||
return f(db.database.C(collName))
|
||||
}
|
||||
|
||||
// ExecuteTimeout is used to execute MongoDB commands with a timeout.
|
||||
func (db *DB) ExecuteTimeout(ctx context.Context, timeout time.Duration, collName string, f func(*mgo.Collection) error) error {
|
||||
ctx, span := trace.StartSpan(ctx, "platform.DB.ExecuteTimeout")
|
||||
defer span.End()
|
||||
|
||||
if db == nil || db.session == nil {
|
||||
return errors.Wrap(ErrInvalidDBProvided, "db == nil || db.session == nil")
|
||||
}
|
||||
|
||||
db.session.SetSocketTimeout(timeout)
|
||||
|
||||
return f(db.database.C(collName))
|
||||
}
|
||||
|
||||
// StatusCheck validates the DB status good.
|
||||
func (db *DB) StatusCheck(ctx context.Context) error {
|
||||
ctx, span := trace.StartSpan(ctx, "platform.DB.StatusCheck")
|
||||
defer span.End()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Query provides a string version of the value
|
||||
func Query(value interface{}) string {
|
||||
json, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return string(json)
|
||||
}
|
72
example-project/internal/platform/docker/docker.go
Normal file
72
example-project/internal/platform/docker/docker.go
Normal file
@ -0,0 +1,72 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// Container contains the information about the container.
|
||||
type Container struct {
|
||||
ID string
|
||||
Port string
|
||||
}
|
||||
|
||||
// StartMongo runs a mongo container to execute commands.
|
||||
func StartMongo(log *log.Logger) (*Container, error) {
|
||||
cmd := exec.Command("docker", "run", "-P", "-d", "mongo:3-jessie")
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
if err := cmd.Run(); err != nil {
|
||||
return nil, fmt.Errorf("starting container: %v", err)
|
||||
}
|
||||
|
||||
id := out.String()[:12]
|
||||
log.Println("DB ContainerID:", id)
|
||||
|
||||
cmd = exec.Command("docker", "inspect", id)
|
||||
out.Reset()
|
||||
cmd.Stdout = &out
|
||||
if err := cmd.Run(); err != nil {
|
||||
return nil, fmt.Errorf("inspect container: %v", err)
|
||||
}
|
||||
|
||||
var doc []struct {
|
||||
NetworkSettings struct {
|
||||
Ports struct {
|
||||
TCP27017 []struct {
|
||||
HostPort string `json:"HostPort"`
|
||||
} `json:"27017/tcp"`
|
||||
} `json:"Ports"`
|
||||
} `json:"NetworkSettings"`
|
||||
}
|
||||
if err := json.Unmarshal(out.Bytes(), &doc); err != nil {
|
||||
return nil, fmt.Errorf("decoding json: %v", err)
|
||||
}
|
||||
|
||||
c := Container{
|
||||
ID: id,
|
||||
Port: doc[0].NetworkSettings.Ports.TCP27017[0].HostPort,
|
||||
}
|
||||
|
||||
log.Println("DB Port:", c.Port)
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
// StopMongo stops and removes the specified container.
|
||||
func StopMongo(log *log.Logger, c *Container) error {
|
||||
if err := exec.Command("docker", "stop", c.ID).Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Println("Stopped:", c.ID)
|
||||
|
||||
if err := exec.Command("docker", "rm", c.ID, "-v").Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Println("Removed:", c.ID)
|
||||
|
||||
return nil
|
||||
}
|
65
example-project/internal/platform/flag/doc.go
Normal file
65
example-project/internal/platform/flag/doc.go
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
Package flag is compatible with the GNU extensions to the POSIX recommendations
|
||||
for command-line options. See
|
||||
http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
|
||||
|
||||
There are no hard bindings for this package. This package takes a struct
|
||||
value and parses it for flags. It supports three tags to customize the
|
||||
flag options.
|
||||
|
||||
flag - Denotes a shorthand option
|
||||
flagdesc - Provides a description for the help
|
||||
default - Provides the default value for the help
|
||||
|
||||
The field name and any parent struct name will be used for the long form of
|
||||
the command name.
|
||||
|
||||
As an example, this config struct:
|
||||
|
||||
var cfg struct {
|
||||
Web struct {
|
||||
APIHost string `default:"0.0.0.0:3000" flag:"a" flagdesc:"The ip:port for the api endpoint."`
|
||||
BatchSize int `default:"1000" flagdesc:"Represents number of items to move."`
|
||||
ReadTimeout time.Duration `default:"5s"`
|
||||
}
|
||||
DialTimeout time.Duration `default:"5s"`
|
||||
Host string `default:"mongo:27017/gotraining" flag:"h"`
|
||||
Insecure bool `flag:"i"`
|
||||
}
|
||||
|
||||
Would produce the following flag output:
|
||||
|
||||
Usage of <app name>
|
||||
-a --web_apihost string <0.0.0.0:3000> : The ip:port for the api endpoint.
|
||||
--web_batchsize int <1000> : Represents number of items to move.
|
||||
--web_readtimeout Duration <5s>
|
||||
--dialtimeout Duration <5s>
|
||||
-h --host string <mongo:27017/gotraining>
|
||||
-i --insecure bool
|
||||
|
||||
The command line flag syntax assumes a regular or shorthand version based on the
|
||||
type of dash used.
|
||||
Regular versions
|
||||
--flag=x
|
||||
--flag x
|
||||
|
||||
Shorthand versions
|
||||
-f=x
|
||||
-f x
|
||||
|
||||
The API is a single call to `Process`
|
||||
|
||||
if err := envconfig.Process("CRUD", &cfg); err != nil {
|
||||
log.Fatalf("main : Parsing Config : %v", err)
|
||||
}
|
||||
|
||||
if err := flag.Process(&cfg); err != nil {
|
||||
if err != flag.ErrHelp {
|
||||
log.Fatalf("main : Parsing Command Line : %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
This call should be done after the call to process the environmental variables.
|
||||
*/
|
||||
package flag
|
236
example-project/internal/platform/flag/flag.go
Normal file
236
example-project/internal/platform/flag/flag.go
Normal file
@ -0,0 +1,236 @@
|
||||
package flag
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrHelp is provided to identify when help is being displayed.
|
||||
var ErrHelp = errors.New("providing help")
|
||||
|
||||
// Process compares the specified command line arguments against the provided
|
||||
// struct value and updates the fields that are identified.
|
||||
func Process(v interface{}) error {
|
||||
if len(os.Args) == 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if os.Args[1] == "-h" || os.Args[1] == "--help" {
|
||||
fmt.Print(display(os.Args[0], v))
|
||||
return ErrHelp
|
||||
}
|
||||
|
||||
args, err := parse("", v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := apply(os.Args, args); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// display provides a pretty print display of the command line arguments.
|
||||
func display(appName string, v interface{}) string {
|
||||
/*
|
||||
Current display format for a field.
|
||||
Usage of <app name>
|
||||
-short --long type <default> : description
|
||||
-a --web_apihost string <0.0.0.0:3000> : The ip:port for the api endpoint.
|
||||
*/
|
||||
|
||||
args, err := parse("", v)
|
||||
if err != nil {
|
||||
return fmt.Sprint("unable to display help", err)
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
b.WriteString(fmt.Sprintf("\nUsage of %s\n", appName))
|
||||
for _, arg := range args {
|
||||
if arg.Short != "" {
|
||||
b.WriteString(fmt.Sprintf("-%s ", arg.Short))
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("--%s %s", arg.Long, arg.Type))
|
||||
if arg.Default != "" {
|
||||
b.WriteString(fmt.Sprintf(" <%s>", arg.Default))
|
||||
}
|
||||
if arg.Desc != "" {
|
||||
b.WriteString(fmt.Sprintf(" : %s", arg.Desc))
|
||||
}
|
||||
b.WriteString("\n")
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// configArg represents a single argument for a given field
|
||||
// in the config structure.
|
||||
type configArg struct {
|
||||
Short string
|
||||
Long string
|
||||
Default string
|
||||
Type string
|
||||
Desc string
|
||||
|
||||
field reflect.Value
|
||||
}
|
||||
|
||||
// parse will reflect over the provided struct value and build a
|
||||
// collection of all possible config arguments.
|
||||
func parse(parentField string, v interface{}) ([]configArg, error) {
|
||||
|
||||
// Reflect on the value to get started.
|
||||
rawValue := reflect.ValueOf(v)
|
||||
|
||||
// If a parent field is provided we are recursing. We are now
|
||||
// processing a struct within a struct. We need the parent struct
|
||||
// name for namespacing.
|
||||
if parentField != "" {
|
||||
parentField = strings.ToLower(parentField) + "_"
|
||||
}
|
||||
|
||||
// We need to check we have a pointer else we can't modify anything
|
||||
// later. With the pointer, get the value that the pointer points to.
|
||||
// With a struct, that means we are recursing and we need to assert to
|
||||
// get the inner struct value to process it.
|
||||
var val reflect.Value
|
||||
switch rawValue.Kind() {
|
||||
case reflect.Ptr:
|
||||
val = rawValue.Elem()
|
||||
if val.Kind() != reflect.Struct {
|
||||
return nil, fmt.Errorf("incompatible type `%v` looking for a pointer", val.Kind())
|
||||
}
|
||||
case reflect.Struct:
|
||||
var ok bool
|
||||
if val, ok = v.(reflect.Value); !ok {
|
||||
return nil, fmt.Errorf("internal recurse error")
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("incompatible type `%v`", rawValue.Kind())
|
||||
}
|
||||
|
||||
var cfgArgs []configArg
|
||||
|
||||
// We need to iterate over the fields of the struct value we are processing.
|
||||
// If the field is a struct then recurse to process its fields. If we have
|
||||
// a field that is not a struct, pull the metadata. The `field` field is
|
||||
// important because it is how we update things later.
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
field := val.Type().Field(i)
|
||||
if field.Type.Kind() == reflect.Struct {
|
||||
args, err := parse(parentField+field.Name, val.Field(i))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfgArgs = append(cfgArgs, args...)
|
||||
continue
|
||||
}
|
||||
|
||||
cfgArg := configArg{
|
||||
Short: field.Tag.Get("flag"),
|
||||
Long: parentField + strings.ToLower(field.Name),
|
||||
Type: field.Type.Name(),
|
||||
Default: field.Tag.Get("default"),
|
||||
Desc: field.Tag.Get("flagdesc"),
|
||||
field: val.Field(i),
|
||||
}
|
||||
cfgArgs = append(cfgArgs, cfgArg)
|
||||
}
|
||||
|
||||
return cfgArgs, nil
|
||||
}
|
||||
|
||||
// apply reads the command line arguments and applies any overrides to
|
||||
// the provided struct value.
|
||||
func apply(osArgs []string, cfgArgs []configArg) (err error) {
|
||||
|
||||
// There is so much room for panics here it hurts.
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = fmt.Errorf("unhandled exception %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
lArgs := len(osArgs[1:])
|
||||
for i := 1; i <= lArgs; i++ {
|
||||
osArg := osArgs[i]
|
||||
|
||||
// Capture the next flag.
|
||||
var flag string
|
||||
switch {
|
||||
case strings.HasPrefix(osArg, "-test"):
|
||||
return nil
|
||||
case strings.HasPrefix(osArg, "--"):
|
||||
flag = osArg[2:]
|
||||
case strings.HasPrefix(osArg, "-"):
|
||||
flag = osArg[1:]
|
||||
default:
|
||||
return fmt.Errorf("invalid command line %q", osArg)
|
||||
}
|
||||
|
||||
// Is this flag represented in the config struct.
|
||||
var cfgArg configArg
|
||||
for _, arg := range cfgArgs {
|
||||
if arg.Short == flag || arg.Long == flag {
|
||||
cfgArg = arg
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Did we find this flag represented in the struct?
|
||||
if !cfgArg.field.IsValid() {
|
||||
return fmt.Errorf("unknown flag %q", flag)
|
||||
}
|
||||
|
||||
if cfgArg.Type == "bool" {
|
||||
if err := update(cfgArg, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Capture the value for this flag.
|
||||
i++
|
||||
value := osArgs[i]
|
||||
|
||||
// Process the struct value.
|
||||
if err := update(cfgArg, value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// update applies the value provided on the command line to the struct.
|
||||
func update(cfgArg configArg, value string) error {
|
||||
switch cfgArg.Type {
|
||||
case "string":
|
||||
cfgArg.field.SetString(value)
|
||||
case "int":
|
||||
i, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to convert value %q to int", value)
|
||||
}
|
||||
cfgArg.field.SetInt(int64(i))
|
||||
case "Duration":
|
||||
d, err := time.ParseDuration(value)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to convert value %q to duration", value)
|
||||
}
|
||||
cfgArg.field.SetInt(int64(d))
|
||||
case "bool":
|
||||
cfgArg.field.SetBool(true)
|
||||
default:
|
||||
return fmt.Errorf("type not supported %q", cfgArg.Type)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
188
example-project/internal/platform/flag/flag_test.go
Normal file
188
example-project/internal/platform/flag/flag_test.go
Normal file
@ -0,0 +1,188 @@
|
||||
package flag
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
success = "\u2713"
|
||||
failed = "\u2717"
|
||||
)
|
||||
|
||||
// TestProcessNoArgs validates when no arguments are passed to the Process API.
|
||||
func TestProcessNoArgs(t *testing.T) {
|
||||
var cfg struct {
|
||||
Web struct {
|
||||
APIHost string `default:"0.0.0.0:3000" flag:"a" flagdesc:"The ip:port for the api endpoint."`
|
||||
BatchSize int `default:"1000" flagdesc:"Represets number of items to move."`
|
||||
ReadTimeout time.Duration `default:"5s"`
|
||||
}
|
||||
DialTimeout time.Duration `default:"5s"`
|
||||
Host string `default:"mongo:27017/gotraining" flag:"h"`
|
||||
}
|
||||
|
||||
t.Log("Given the need to validate was handle no arguments.")
|
||||
{
|
||||
t.Log("\tWhen there are no OS arguments.")
|
||||
{
|
||||
if err := Process(&cfg); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to call Process with no arguments : %s.", failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to call Process with no arguments.", success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestParse validates the ability to reflect and parse out the argument
|
||||
// metadata from the provided struct value.
|
||||
func TestParse(t *testing.T) {
|
||||
var cfg struct {
|
||||
Web struct {
|
||||
APIHost string `default:"0.0.0.0:3000" flag:"a" flagdesc:"The ip:port for the api endpoint."`
|
||||
BatchSize int `default:"1000" flagdesc:"Represets number of items to move."`
|
||||
ReadTimeout time.Duration `default:"5s"`
|
||||
}
|
||||
DialTimeout time.Duration `default:"5s"`
|
||||
Host string `default:"mongo:27017/gotraining" flag:"h"`
|
||||
Insecure bool `flag:"i"`
|
||||
}
|
||||
parseOutput := `[{"Short":"a","Long":"web_apihost","Default":"0.0.0.0:3000","Type":"string","Desc":"The ip:port for the api endpoint."},{"Short":"","Long":"web_batchsize","Default":"1000","Type":"int","Desc":"Represets number of items to move."},{"Short":"","Long":"web_readtimeout","Default":"5s","Type":"Duration","Desc":""},{"Short":"","Long":"dialtimeout","Default":"5s","Type":"Duration","Desc":""},{"Short":"h","Long":"host","Default":"mongo:27017/gotraining","Type":"string","Desc":""},{"Short":"i","Long":"insecure","Default":"","Type":"bool","Desc":""}]`
|
||||
|
||||
t.Log("Given the need to validate we can parse a struct value.")
|
||||
{
|
||||
t.Log("\tWhen parsing the test config.")
|
||||
{
|
||||
args, err := parse("", &cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to parse arguments without error : %s.", failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to parse arguments without error.", success)
|
||||
|
||||
d, _ := json.Marshal(args)
|
||||
if string(d) != parseOutput {
|
||||
t.Log("\t\tGot :", string(d))
|
||||
t.Log("\t\tWant:", parseOutput)
|
||||
t.Fatalf("\t%s\tShould get back the expected arguments.", failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get back the expected arguments.", success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestApply validates the ability to apply overrides to a struct value
|
||||
// based on provided flag arguments.
|
||||
func TestApply(t *testing.T) {
|
||||
var cfg struct {
|
||||
Web struct {
|
||||
APIHost string `default:"0.0.0.0:3000" flag:"a" flagdesc:"The ip:port for the api endpoint."`
|
||||
BatchSize int `default:"1000" flagdesc:"Represets number of items to move."`
|
||||
ReadTimeout time.Duration `default:"5s"`
|
||||
}
|
||||
DialTimeout time.Duration `default:"5s"`
|
||||
Host string `default:"mongo:27017/gotraining" flag:"h"`
|
||||
Insecure bool `flag:"i"`
|
||||
}
|
||||
osArgs := []string{"./sales-api", "-i", "-a", "0.0.1.1:5000", "--web_batchsize", "300", "--dialtimeout", "10s"}
|
||||
expected := `{"Web":{"APIHost":"0.0.1.1:5000","BatchSize":300,"ReadTimeout":0},"DialTimeout":10000000000,"Host":"","Insecure":true}`
|
||||
|
||||
t.Log("Given the need to validate we can apply overrides a struct value.")
|
||||
{
|
||||
t.Log("\tWhen parsing the test config.")
|
||||
{
|
||||
args, err := parse("", &cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to parse arguments without error : %s.", failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to parse arguments without error.", success)
|
||||
|
||||
if err := apply(osArgs, args); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to apply arguments without error : %s.", failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to apply arguments without error.", success)
|
||||
|
||||
d, _ := json.Marshal(&cfg)
|
||||
if string(d) != expected {
|
||||
t.Log("\t\tGot :", string(d))
|
||||
t.Log("\t\tWant:", expected)
|
||||
t.Fatalf("\t%s\tShould get back the expected struct value.", failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get back the expected struct value.", success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplyBad validates the ability to handle bad arguments on the command line.
|
||||
func TestApplyBad(t *testing.T) {
|
||||
var cfg struct {
|
||||
Web struct {
|
||||
APIHost string `default:"0.0.0.0:3000" flag:"a" flagdesc:"The ip:port for the api endpoint."`
|
||||
BatchSize int `default:"1000" flagdesc:"Represets number of items to move."`
|
||||
ReadTimeout time.Duration `default:"5s"`
|
||||
}
|
||||
DialTimeout time.Duration `default:"5s"`
|
||||
Host string `default:"mongo:27017/gotraining" flag:"h"`
|
||||
Insecure bool
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
osArg []string
|
||||
}{
|
||||
{[]string{"testapp", "-help"}},
|
||||
{[]string{"testapp", "-bad", "value"}},
|
||||
{[]string{"testapp", "-insecure", "value"}},
|
||||
}
|
||||
|
||||
t.Log("Given the need to validate we can parse a struct value with bad OS arguments.")
|
||||
{
|
||||
for i, tt := range tests {
|
||||
t.Logf("\tTest: %d\tWhen checking %v", i, tt.osArg)
|
||||
{
|
||||
args, err := parse("", &cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to parse arguments without error : %s.", failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to parse arguments without error.", success)
|
||||
|
||||
if err := apply(tt.osArg, args); err != nil {
|
||||
t.Logf("\t%s\tShould not be able to apply arguments.", success)
|
||||
} else {
|
||||
t.Errorf("\t%s\tShould not be able to apply arguments.", failed)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestDisplay provides a test for displaying the command line arguments.
|
||||
func TestDisplay(t *testing.T) {
|
||||
var cfg struct {
|
||||
Web struct {
|
||||
APIHost string `default:"0.0.0.0:3000" flag:"a" flagdesc:"The ip:port for the api endpoint."`
|
||||
BatchSize int `default:"1000" flagdesc:"Represets number of items to move."`
|
||||
ReadTimeout time.Duration `default:"5s"`
|
||||
}
|
||||
DialTimeout time.Duration `default:"5s"`
|
||||
Host string `default:"mongo:27017/gotraining" flag:"h"`
|
||||
Insecure bool `flag:"i"`
|
||||
}
|
||||
|
||||
want := `
|
||||
Usage of TestApp
|
||||
-a --web_apihost string <0.0.0.0:3000> : The ip:port for the api endpoint.
|
||||
--web_batchsize int <1000> : Represets number of items to move.
|
||||
--web_readtimeout Duration <5s>
|
||||
--dialtimeout Duration <5s>
|
||||
-h --host string <mongo:27017/gotraining>
|
||||
-i --insecure bool
|
||||
`
|
||||
|
||||
got := display("TestApp", &cfg)
|
||||
if got != want {
|
||||
t.Log("\t\tGot :", []byte(got))
|
||||
t.Log("\t\tWant:", []byte(want))
|
||||
t.Fatalf("\t%s\tShould get back the expected help output.", failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould get back the expected help output.", success)
|
||||
}
|
89
example-project/internal/platform/tests/main.go
Normal file
89
example-project/internal/platform/tests/main.go
Normal file
@ -0,0 +1,89 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/db"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/docker"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
|
||||
"github.com/pborman/uuid"
|
||||
)
|
||||
|
||||
// Success and failure markers.
|
||||
const (
|
||||
Success = "\u2713"
|
||||
Failed = "\u2717"
|
||||
)
|
||||
|
||||
// Test owns state for running/shutting down tests.
|
||||
type Test struct {
|
||||
Log *log.Logger
|
||||
MasterDB *db.DB
|
||||
container *docker.Container
|
||||
}
|
||||
|
||||
// New is the entry point for tests.
|
||||
func New() *Test {
|
||||
|
||||
// =========================================================================
|
||||
// Logging
|
||||
|
||||
log := log.New(os.Stdout, "TEST : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
|
||||
|
||||
// ============================================================
|
||||
// Startup Mongo container
|
||||
|
||||
container, err := docker.StartMongo(log)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Configuration
|
||||
|
||||
dbDialTimeout := 25 * time.Second
|
||||
dbHost := fmt.Sprintf("mongodb://localhost:%s/gotraining", container.Port)
|
||||
|
||||
// ============================================================
|
||||
// Start Mongo
|
||||
|
||||
log.Println("main : Started : Initialize Mongo")
|
||||
masterDB, err := db.New(dbHost, dbDialTimeout)
|
||||
if err != nil {
|
||||
log.Fatalf("startup : Register DB : %v", err)
|
||||
}
|
||||
|
||||
return &Test{log, masterDB, container}
|
||||
}
|
||||
|
||||
// TearDown is used for shutting down tests. Calling this should be
|
||||
// done in a defer immediately after calling New.
|
||||
func (t *Test) TearDown() {
|
||||
t.MasterDB.Close()
|
||||
if err := docker.StopMongo(t.Log, t.container); err != nil {
|
||||
t.Log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Recover is used to prevent panics from allowing the test to cleanup.
|
||||
func Recover(t *testing.T) {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatal("Unhandled Exception:", string(debug.Stack()))
|
||||
}
|
||||
}
|
||||
|
||||
// Context returns an app level context for testing.
|
||||
func Context() context.Context {
|
||||
values := web.Values{
|
||||
TraceID: uuid.New(),
|
||||
Now: time.Now(),
|
||||
}
|
||||
|
||||
return context.WithValue(context.Background(), web.KeyValues, &values)
|
||||
}
|
15
example-project/internal/platform/tests/type_helpers.go
Normal file
15
example-project/internal/platform/tests/type_helpers.go
Normal file
@ -0,0 +1,15 @@
|
||||
package tests
|
||||
|
||||
// StringPointer is a helper to get a *string from a string. It is in the tests
|
||||
// package because we normally don't want to deal with pointers to basic types
|
||||
// but it's useful in some tests.
|
||||
func StringPointer(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
||||
// IntPointer is a helper to get a *int from a int. It is in the tests package
|
||||
// because we normally don't want to deal with pointers to basic types but it's
|
||||
// useful in some tests.
|
||||
func IntPointer(i int) *int {
|
||||
return &i
|
||||
}
|
194
example-project/internal/platform/trace/trace.go
Normal file
194
example-project/internal/platform/trace/trace.go
Normal file
@ -0,0 +1,194 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// Error variables for factory validation.
|
||||
var (
|
||||
ErrLoggerNotProvided = errors.New("logger not provided")
|
||||
ErrHostNotProvided = errors.New("host not provided")
|
||||
)
|
||||
|
||||
// Log provides support for logging inside this package.
|
||||
// Unfortunately, the opentrace API calls into the ExportSpan
|
||||
// function directly with no means to pass user defined arguments.
|
||||
type Log func(format string, v ...interface{})
|
||||
|
||||
// Exporter provides support to batch spans and send them
|
||||
// to the sidecar for processing.
|
||||
type Exporter struct {
|
||||
log Log // Handler function for logging.
|
||||
host string // IP:port of the sidecare consuming the trace data.
|
||||
batchSize int // Size of the batch of spans before sending.
|
||||
sendInterval time.Duration // Time to send a batch if batch size is not met.
|
||||
sendTimeout time.Duration // Time to wait for the sidecar to respond on send.
|
||||
client http.Client // Provides APIs for performing the http send.
|
||||
batch []*trace.SpanData // Maintains the batch of span data to be sent.
|
||||
mu sync.Mutex // Provide synchronization to access the batch safely.
|
||||
timer *time.Timer // Signals when the sendInterval is met.
|
||||
}
|
||||
|
||||
// NewExporter creates an exporter for use.
|
||||
func NewExporter(log Log, host string, batchSize int, sendInterval, sendTimeout time.Duration) (*Exporter, error) {
|
||||
if log == nil {
|
||||
return nil, ErrLoggerNotProvided
|
||||
}
|
||||
if host == "" {
|
||||
return nil, ErrHostNotProvided
|
||||
}
|
||||
|
||||
tr := http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
DualStack: true,
|
||||
}).DialContext,
|
||||
MaxIdleConns: 2,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
|
||||
e := Exporter{
|
||||
log: log,
|
||||
host: host,
|
||||
batchSize: batchSize,
|
||||
sendInterval: sendInterval,
|
||||
sendTimeout: sendTimeout,
|
||||
client: http.Client{
|
||||
Transport: &tr,
|
||||
},
|
||||
batch: make([]*trace.SpanData, 0, batchSize),
|
||||
timer: time.NewTimer(sendInterval),
|
||||
}
|
||||
|
||||
return &e, nil
|
||||
}
|
||||
|
||||
// Close sends the remaining spans that have not been sent yet.
|
||||
func (e *Exporter) Close() (int, error) {
|
||||
var sendBatch []*trace.SpanData
|
||||
e.mu.Lock()
|
||||
{
|
||||
sendBatch = e.batch
|
||||
}
|
||||
e.mu.Unlock()
|
||||
|
||||
err := e.send(sendBatch)
|
||||
if err != nil {
|
||||
return len(sendBatch), err
|
||||
}
|
||||
|
||||
return len(sendBatch), nil
|
||||
}
|
||||
|
||||
// ExportSpan is called by opentracing when spans are created. It implements
|
||||
// the Exporter interface.
|
||||
func (e *Exporter) ExportSpan(span *trace.SpanData) {
|
||||
sendBatch := e.saveBatch(span)
|
||||
if sendBatch != nil {
|
||||
go func() {
|
||||
e.log("trace : Exporter : ExportSpan : Sending Batch[%d]", len(sendBatch))
|
||||
if err := e.send(sendBatch); err != nil {
|
||||
e.log("trace : Exporter : ExportSpan : ERROR : %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// Saves the span data to the batch. If the batch should be sent,
|
||||
// returns a batch to send.
|
||||
func (e *Exporter) saveBatch(span *trace.SpanData) []*trace.SpanData {
|
||||
var sendBatch []*trace.SpanData
|
||||
|
||||
e.mu.Lock()
|
||||
{
|
||||
// We want to append this new span to the collection.
|
||||
e.batch = append(e.batch, span)
|
||||
|
||||
// Do we need to send the current batch?
|
||||
switch {
|
||||
case len(e.batch) == e.batchSize:
|
||||
|
||||
// We hit the batch size. Now save the current
|
||||
// batch for sending and start a new batch.
|
||||
sendBatch = e.batch
|
||||
e.batch = make([]*trace.SpanData, 0, e.batchSize)
|
||||
e.timer.Reset(e.sendInterval)
|
||||
|
||||
default:
|
||||
|
||||
// We did not hit the batch size but maybe send what
|
||||
// we have based on time.
|
||||
select {
|
||||
case <-e.timer.C:
|
||||
|
||||
// The time has expired so save the current
|
||||
// batch for sending and start a new batch.
|
||||
sendBatch = e.batch
|
||||
e.batch = make([]*trace.SpanData, 0, e.batchSize)
|
||||
e.timer.Reset(e.sendInterval)
|
||||
|
||||
// It's not time yet, just move on.
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
e.mu.Unlock()
|
||||
|
||||
return sendBatch
|
||||
}
|
||||
|
||||
// send uses HTTP to send the data to the tracing sidecare for processing.
|
||||
func (e *Exporter) send(sendBatch []*trace.SpanData) error {
|
||||
data, err := json.Marshal(sendBatch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", e.host, bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(req.Context(), e.sendTimeout)
|
||||
defer cancel()
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
ch := make(chan error)
|
||||
go func() {
|
||||
resp, err := e.client.Do(req)
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusNoContent {
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
ch <- fmt.Errorf("error on call : status[%s]", resp.Status)
|
||||
return
|
||||
}
|
||||
ch <- fmt.Errorf("error on call : status[%s] : %s", resp.Status, string(data))
|
||||
return
|
||||
}
|
||||
|
||||
ch <- nil
|
||||
}()
|
||||
|
||||
return <-ch
|
||||
}
|
278
example-project/internal/platform/trace/trace_test.go
Normal file
278
example-project/internal/platform/trace/trace_test.go
Normal file
@ -0,0 +1,278 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// Success and failure markers.
|
||||
const (
|
||||
success = "\u2713"
|
||||
failed = "\u2717"
|
||||
)
|
||||
|
||||
// inputSpans represents spans of data for the tests.
|
||||
var inputSpans = []*trace.SpanData{
|
||||
{Name: "span1"},
|
||||
{Name: "span2"},
|
||||
{Name: "span3"},
|
||||
}
|
||||
|
||||
// inputSpansJSON represents a JSON representation of the span data.
|
||||
var inputSpansJSON = `[{"TraceID":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"SpanID":[0,0,0,0,0,0,0,0],"TraceOptions":0,"ParentSpanID":[0,0,0,0,0,0,0,0],"SpanKind":0,"Name":"span1","StartTime":"0001-01-01T00:00:00Z","EndTime":"0001-01-01T00:00:00Z","Attributes":null,"Annotations":null,"MessageEvents":null,"Code":0,"Message":"","Links":null,"HasRemoteParent":false},{"TraceID":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"SpanID":[0,0,0,0,0,0,0,0],"TraceOptions":0,"ParentSpanID":[0,0,0,0,0,0,0,0],"SpanKind":0,"Name":"span2","StartTime":"0001-01-01T00:00:00Z","EndTime":"0001-01-01T00:00:00Z","Attributes":null,"Annotations":null,"MessageEvents":null,"Code":0,"Message":"","Links":null,"HasRemoteParent":false},{"TraceID":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"SpanID":[0,0,0,0,0,0,0,0],"TraceOptions":0,"ParentSpanID":[0,0,0,0,0,0,0,0],"SpanKind":0,"Name":"span3","StartTime":"0001-01-01T00:00:00Z","EndTime":"0001-01-01T00:00:00Z","Attributes":null,"Annotations":null,"MessageEvents":null,"Code":0,"Message":"","Links":null,"HasRemoteParent":false}]`
|
||||
|
||||
// =============================================================================
|
||||
|
||||
// logger is required to create an Exporter.
|
||||
var logger = func(format string, v ...interface{}) {
|
||||
log.Printf(format, v)
|
||||
}
|
||||
|
||||
// MakeExporter abstracts the error handling aspects of creating an Exporter.
|
||||
func makeExporter(host string, batchSize int, sendInterval, sendTimeout time.Duration) *Exporter {
|
||||
exporter, err := NewExporter(logger, host, batchSize, sendInterval, sendTimeout)
|
||||
if err != nil {
|
||||
log.Fatalln("Unable to create exporter, ", err)
|
||||
}
|
||||
return exporter
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
var saveTests = []struct {
|
||||
name string
|
||||
e *Exporter
|
||||
input []*trace.SpanData
|
||||
output []*trace.SpanData
|
||||
lastSaveDelay time.Duration // The delay before the last save. For testing intervals.
|
||||
isInputMatchBatch bool // If the input should match the internal exporter collection after the last save.
|
||||
isSendBatch bool // If the last save should return nil or batch data.
|
||||
}{
|
||||
{"NoSend", makeExporter("test", 10, time.Minute, time.Second), inputSpans, nil, time.Nanosecond, true, false},
|
||||
{"SendOnBatchSize", makeExporter("test", 3, time.Minute, time.Second), inputSpans, inputSpans, time.Nanosecond, false, true},
|
||||
{"SendOnTime", makeExporter("test", 4, time.Millisecond, time.Second), inputSpans, inputSpans, 2 * time.Millisecond, false, true},
|
||||
}
|
||||
|
||||
// TestSave validates the save batch functionality is working.
|
||||
func TestSave(t *testing.T) {
|
||||
t.Log("Given the need to validate saving span data to a batch.")
|
||||
{
|
||||
for i, tt := range saveTests {
|
||||
t.Logf("\tTest: %d\tWhen running test: %s", i, tt.name)
|
||||
{
|
||||
// Save the input of span data.
|
||||
l := len(tt.input) - 1
|
||||
var batch []*trace.SpanData
|
||||
for i, span := range tt.input {
|
||||
|
||||
// If this is the last save, take the configured delay.
|
||||
// We might be testing invertal based batching.
|
||||
if l == i {
|
||||
time.Sleep(tt.lastSaveDelay)
|
||||
}
|
||||
batch = tt.e.saveBatch(span)
|
||||
}
|
||||
|
||||
// Compare the internal collection with what we saved.
|
||||
if tt.isInputMatchBatch {
|
||||
if len(tt.e.batch) != len(tt.input) {
|
||||
t.Log("\t\tGot :", len(tt.e.batch))
|
||||
t.Log("\t\tWant:", len(tt.input))
|
||||
t.Errorf("\t%s\tShould have the same number of spans as input.", failed)
|
||||
} else {
|
||||
t.Logf("\t%s\tShould have the same number of spans as input.", success)
|
||||
}
|
||||
} else {
|
||||
if len(tt.e.batch) != 0 {
|
||||
t.Log("\t\tGot :", len(tt.e.batch))
|
||||
t.Log("\t\tWant:", 0)
|
||||
t.Errorf("\t%s\tShould have zero spans.", failed)
|
||||
} else {
|
||||
t.Logf("\t%s\tShould have zero spans.", success)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate the return provided or didn't provide a batch to send.
|
||||
if !tt.isSendBatch && batch != nil {
|
||||
t.Errorf("\t%s\tShould not have a batch to send.", failed)
|
||||
} else if !tt.isSendBatch {
|
||||
t.Logf("\t%s\tShould not have a batch to send.", success)
|
||||
}
|
||||
if tt.isSendBatch && batch == nil {
|
||||
t.Errorf("\t%s\tShould have a batch to send.", failed)
|
||||
} else if tt.isSendBatch {
|
||||
t.Logf("\t%s\tShould have a batch to send.", success)
|
||||
}
|
||||
|
||||
// Compare the batch to send.
|
||||
if !reflect.DeepEqual(tt.output, batch) {
|
||||
t.Log("\t\tGot :", batch)
|
||||
t.Log("\t\tWant:", tt.output)
|
||||
t.Errorf("\t%s\tShould have an expected match of the batch to send.", failed)
|
||||
} else {
|
||||
t.Logf("\t%s\tShould have an expected match of the batch to send.", success)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
var sendTests = []struct {
|
||||
name string
|
||||
e *Exporter
|
||||
input []*trace.SpanData
|
||||
pass bool
|
||||
}{
|
||||
{"success", makeExporter("test", 3, time.Minute, time.Hour), inputSpans, true},
|
||||
{"failure", makeExporter("test", 3, time.Minute, time.Hour), inputSpans[:2], false},
|
||||
{"timeout", makeExporter("test", 3, time.Minute, time.Nanosecond), inputSpans, false},
|
||||
}
|
||||
|
||||
// mockServer returns a pointer to a server to handle the mock get call.
|
||||
func mockServer() *httptest.Server {
|
||||
f := func(w http.ResponseWriter, r *http.Request) {
|
||||
d, _ := ioutil.ReadAll(r.Body)
|
||||
data := string(d)
|
||||
if data != inputSpansJSON {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
fmt.Fprint(w, data)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
return httptest.NewServer(http.HandlerFunc(f))
|
||||
}
|
||||
|
||||
// TestSend validates spans can be sent to the sidecar.
|
||||
func TestSend(t *testing.T) {
|
||||
s := mockServer()
|
||||
defer s.Close()
|
||||
|
||||
t.Log("Given the need to validate sending span data to the sidecar.")
|
||||
{
|
||||
for i, tt := range sendTests {
|
||||
t.Logf("\tTest: %d\tWhen running test: %s", i, tt.name)
|
||||
{
|
||||
// Set the URL for the call.
|
||||
tt.e.host = s.URL
|
||||
|
||||
// Send the span data.
|
||||
err := tt.e.send(tt.input)
|
||||
if tt.pass {
|
||||
if err != nil {
|
||||
t.Errorf("\t%s\tShould be able to send the batch successfully: %v", failed, err)
|
||||
} else {
|
||||
t.Logf("\t%s\tShould be able to send the batch successfully.", success)
|
||||
}
|
||||
} else {
|
||||
if err == nil {
|
||||
t.Errorf("\t%s\tShould not be able to send the batch successfully : %v", failed, err)
|
||||
} else {
|
||||
t.Logf("\t%s\tShould not be able to send the batch successfully.", success)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestClose validates the flushing of the final batched spans.
|
||||
func TestClose(t *testing.T) {
|
||||
s := mockServer()
|
||||
defer s.Close()
|
||||
|
||||
t.Log("Given the need to validate flushing the remaining batched spans.")
|
||||
{
|
||||
t.Logf("\tTest: %d\tWhen running test: %s", 0, "FlushWithData")
|
||||
{
|
||||
e, err := NewExporter(logger, "test", 10, time.Minute, time.Hour)
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to create an Exporter : %v", failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to create an Exporter.", success)
|
||||
|
||||
// Set the URL for the call.
|
||||
e.host = s.URL
|
||||
|
||||
// Save the input of span data.
|
||||
for _, span := range inputSpans {
|
||||
e.saveBatch(span)
|
||||
}
|
||||
|
||||
// Close the Exporter and we should get those spans sent.
|
||||
sent, err := e.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to flush the Exporter : %v", failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to flush the Exporter.", success)
|
||||
|
||||
if sent != len(inputSpans) {
|
||||
t.Log("\t\tGot :", sent)
|
||||
t.Log("\t\tWant:", len(inputSpans))
|
||||
t.Fatalf("\t%s\tShould have flushed the expected number of spans.", failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould have flushed the expected number of spans.", success)
|
||||
}
|
||||
|
||||
t.Logf("\tTest: %d\tWhen running test: %s", 0, "FlushWithError")
|
||||
{
|
||||
e, err := NewExporter(logger, "test", 10, time.Minute, time.Hour)
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to create an Exporter : %v", failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to create an Exporter.", success)
|
||||
|
||||
// Set the URL for the call.
|
||||
e.host = s.URL
|
||||
|
||||
// Save the input of span data.
|
||||
for _, span := range inputSpans[:2] {
|
||||
e.saveBatch(span)
|
||||
}
|
||||
|
||||
// Close the Exporter and we should get those spans sent.
|
||||
if _, err := e.Close(); err == nil {
|
||||
t.Fatalf("\t%s\tShould not be able to flush the Exporter.", failed)
|
||||
}
|
||||
t.Logf("\t%s\tShould not be able to flush the Exporter.", success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
// TestExporterFailure validates misuse cases are covered.
|
||||
func TestExporterFailure(t *testing.T) {
|
||||
t.Log("Given the need to validate Exporter initializes properly.")
|
||||
{
|
||||
t.Logf("\tTest: %d\tWhen not passing a proper logger.", 0)
|
||||
{
|
||||
_, err := NewExporter(nil, "test", 10, time.Minute, time.Hour)
|
||||
if err == nil {
|
||||
t.Errorf("\t%s\tShould not be able to create an Exporter.", failed)
|
||||
} else {
|
||||
t.Logf("\t%s\tShould not be able to create an Exporter.", success)
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("\tTest: %d\tWhen not passing a proper host.", 1)
|
||||
{
|
||||
_, err := NewExporter(logger, "", 10, time.Minute, time.Hour)
|
||||
if err == nil {
|
||||
t.Errorf("\t%s\tShould not be able to create an Exporter.", failed)
|
||||
} else {
|
||||
t.Logf("\t%s\tShould not be able to create an Exporter.", success)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
62
example-project/internal/platform/web/errors.go
Normal file
62
example-project/internal/platform/web/errors.go
Normal file
@ -0,0 +1,62 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// FieldError is used to indicate an error with a specific request field.
|
||||
type FieldError struct {
|
||||
Field string `json:"field"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
// ErrorResponse is the form used for API responses from failures in the API.
|
||||
type ErrorResponse struct {
|
||||
Error string `json:"error"`
|
||||
Fields []FieldError `json:"fields,omitempty"`
|
||||
}
|
||||
|
||||
// Error is used to pass an error during the request through the
|
||||
// application with web specific context.
|
||||
type Error struct {
|
||||
Err error
|
||||
Status int
|
||||
Fields []FieldError
|
||||
}
|
||||
|
||||
// NewRequestError wraps a provided error with an HTTP status code. This
|
||||
// function should be used when handlers encounter expected errors.
|
||||
func NewRequestError(err error, status int) error {
|
||||
return &Error{err, status, nil}
|
||||
}
|
||||
|
||||
// Error implements the error interface. It uses the default message of the
|
||||
// wrapped error. This is what will be shown in the services' logs.
|
||||
func (err *Error) Error() string {
|
||||
return err.Err.Error()
|
||||
}
|
||||
|
||||
// shutdown is a type used to help with the graceful termination of the service.
|
||||
type shutdown struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
// Error is the implementation of the error interface.
|
||||
func (s *shutdown) Error() string {
|
||||
return s.Message
|
||||
}
|
||||
|
||||
// NewShutdownError returns an error that causes the framework to signal
|
||||
// a graceful shutdown.
|
||||
func NewShutdownError(message string) error {
|
||||
return &shutdown{message}
|
||||
}
|
||||
|
||||
// IsShutdown checks to see if the shutdown error is contained
|
||||
// in the specified error value.
|
||||
func IsShutdown(err error) bool {
|
||||
if _, ok := errors.Cause(err).(*shutdown); ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
24
example-project/internal/platform/web/middleware.go
Normal file
24
example-project/internal/platform/web/middleware.go
Normal file
@ -0,0 +1,24 @@
|
||||
package web
|
||||
|
||||
// Middleware is a function designed to run some code before and/or after
|
||||
// another Handler. It is designed to remove boilerplate or other concerns not
|
||||
// direct to any given Handler.
|
||||
type Middleware func(Handler) Handler
|
||||
|
||||
// wrapMiddleware creates a new handler by wrapping middleware around a final
|
||||
// handler. The middlewares' Handlers will be executed by requests in the order
|
||||
// they are provided.
|
||||
func wrapMiddleware(mw []Middleware, handler Handler) Handler {
|
||||
|
||||
// Loop backwards through the middleware invoking each one. Replace the
|
||||
// handler with the new wrapped handler. Looping backwards ensures that the
|
||||
// first middleware of the slice is the first to be executed by requests.
|
||||
for i := len(mw) - 1; i >= 0; i-- {
|
||||
h := mw[i]
|
||||
if h != nil {
|
||||
handler = h(handler)
|
||||
}
|
||||
}
|
||||
|
||||
return handler
|
||||
}
|
85
example-project/internal/platform/web/request.go
Normal file
85
example-project/internal/platform/web/request.go
Normal file
@ -0,0 +1,85 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
en "github.com/go-playground/locales/en"
|
||||
ut "github.com/go-playground/universal-translator"
|
||||
validator "gopkg.in/go-playground/validator.v9"
|
||||
en_translations "gopkg.in/go-playground/validator.v9/translations/en"
|
||||
)
|
||||
|
||||
// validate holds the settings and caches for validating request struct values.
|
||||
var validate = validator.New()
|
||||
|
||||
// translator is a cache of locale and translation information.
|
||||
var translator *ut.UniversalTranslator
|
||||
|
||||
func init() {
|
||||
|
||||
// Instantiate the english locale for the validator library.
|
||||
enLocale := en.New()
|
||||
|
||||
// Create a value using English as the fallback locale (first argument).
|
||||
// Provide one or more arguments for additional supported locales.
|
||||
translator = ut.New(enLocale, enLocale)
|
||||
|
||||
// Register the english error messages for validation errors.
|
||||
lang, _ := translator.GetTranslator("en")
|
||||
en_translations.RegisterDefaultTranslations(validate, lang)
|
||||
|
||||
// Use JSON tag names for errors instead of Go struct names.
|
||||
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
|
||||
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
|
||||
if name == "-" {
|
||||
return ""
|
||||
}
|
||||
return name
|
||||
})
|
||||
}
|
||||
|
||||
// Decode reads the body of an HTTP request looking for a JSON document. The
|
||||
// body is decoded into the provided value.
|
||||
//
|
||||
// If the provided value is a struct then it is checked for validation tags.
|
||||
func Decode(r *http.Request, val interface{}) error {
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(val); err != nil {
|
||||
return NewRequestError(err, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if err := validate.Struct(val); err != nil {
|
||||
|
||||
// Use a type assertion to get the real error value.
|
||||
verrors, ok := err.(validator.ValidationErrors)
|
||||
if !ok {
|
||||
return err
|
||||
}
|
||||
|
||||
// lang controls the language of the error messages. You could look at the
|
||||
// Accept-Language header if you intend to support multiple languages.
|
||||
lang, _ := translator.GetTranslator("en")
|
||||
|
||||
var fields []FieldError
|
||||
for _, verror := range verrors {
|
||||
field := FieldError{
|
||||
Field: verror.Field(),
|
||||
Error: verror.Translate(lang),
|
||||
}
|
||||
fields = append(fields, field)
|
||||
}
|
||||
|
||||
return &Error{
|
||||
Err: errors.New("field validation error"),
|
||||
Status: http.StatusBadRequest,
|
||||
Fields: fields,
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
74
example-project/internal/platform/web/response.go
Normal file
74
example-project/internal/platform/web/response.go
Normal file
@ -0,0 +1,74 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// RespondError sends an error reponse back to the client.
|
||||
func RespondError(ctx context.Context, w http.ResponseWriter, err error) error {
|
||||
|
||||
// If the error was of the type *Error, the handler has
|
||||
// a specific status code and error to return.
|
||||
if webErr, ok := errors.Cause(err).(*Error); ok {
|
||||
er := ErrorResponse{
|
||||
Error: webErr.Err.Error(),
|
||||
Fields: webErr.Fields,
|
||||
}
|
||||
if err := Respond(ctx, w, er, webErr.Status); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// If not, the handler sent any arbitrary error value so use 500.
|
||||
er := ErrorResponse{
|
||||
Error: http.StatusText(http.StatusInternalServerError),
|
||||
}
|
||||
if err := Respond(ctx, w, er, http.StatusInternalServerError); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Respond converts a Go value to JSON and sends it to the client.
|
||||
// If code is StatusNoContent, v is expected to be nil.
|
||||
func Respond(ctx context.Context, w http.ResponseWriter, data interface{}, statusCode int) error {
|
||||
|
||||
// Set the status code for the request logger middleware.
|
||||
// If the context is missing this value, request the service
|
||||
// to be shutdown gracefully.
|
||||
v, ok := ctx.Value(KeyValues).(*Values)
|
||||
if !ok {
|
||||
return NewShutdownError("web value missing from context")
|
||||
}
|
||||
v.StatusCode = statusCode
|
||||
|
||||
// If there is nothing to marshal then set status code and return.
|
||||
if statusCode == http.StatusNoContent {
|
||||
w.WriteHeader(statusCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Convert the response value to JSON.
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Set the content type and headers once we know marshaling has succeeded.
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
// Write the status code to the response.
|
||||
w.WriteHeader(statusCode)
|
||||
|
||||
// Send the result back to the client.
|
||||
if _, err := w.Write(jsonData); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
115
example-project/internal/platform/web/web.go
Normal file
115
example-project/internal/platform/web/web.go
Normal file
@ -0,0 +1,115 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/dimfeld/httptreemux"
|
||||
"go.opencensus.io/plugin/ochttp"
|
||||
"go.opencensus.io/plugin/ochttp/propagation/tracecontext"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
// ctxKey represents the type of value for the context key.
|
||||
type ctxKey int
|
||||
|
||||
// KeyValues is how request values or stored/retrieved.
|
||||
const KeyValues ctxKey = 1
|
||||
|
||||
// Values represent state for each request.
|
||||
type Values struct {
|
||||
TraceID string
|
||||
Now time.Time
|
||||
StatusCode int
|
||||
}
|
||||
|
||||
// A Handler is a type that handles an http request within our own little mini
|
||||
// framework.
|
||||
type Handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error
|
||||
|
||||
// App is the entrypoint into our application and what configures our context
|
||||
// object for each of our http handlers. Feel free to add any configuration
|
||||
// data/logic on this App struct
|
||||
type App struct {
|
||||
*httptreemux.TreeMux
|
||||
och *ochttp.Handler
|
||||
shutdown chan os.Signal
|
||||
log *log.Logger
|
||||
mw []Middleware
|
||||
}
|
||||
|
||||
// NewApp creates an App value that handle a set of routes for the application.
|
||||
func NewApp(shutdown chan os.Signal, log *log.Logger, mw ...Middleware) *App {
|
||||
app := App{
|
||||
TreeMux: httptreemux.New(),
|
||||
shutdown: shutdown,
|
||||
log: log,
|
||||
mw: mw,
|
||||
}
|
||||
|
||||
// Create an OpenCensus HTTP Handler which wraps the router. This will start
|
||||
// the initial span and annotate it with information about the request/response.
|
||||
//
|
||||
// This is configured to use the W3C TraceContext standard to set the remote
|
||||
// parent if an client request includes the appropriate headers.
|
||||
// https://w3c.github.io/trace-context/
|
||||
app.och = &ochttp.Handler{
|
||||
Handler: app.TreeMux,
|
||||
Propagation: &tracecontext.HTTPFormat{},
|
||||
}
|
||||
|
||||
return &app
|
||||
}
|
||||
|
||||
// SignalShutdown is used to gracefully shutdown the app when an integrity
|
||||
// issue is identified.
|
||||
func (a *App) SignalShutdown() {
|
||||
a.log.Println("error returned from handler indicated integrity issue, shutting down service")
|
||||
a.shutdown <- syscall.SIGSTOP
|
||||
}
|
||||
|
||||
// Handle is our mechanism for mounting Handlers for a given HTTP verb and path
|
||||
// pair, this makes for really easy, convenient routing.
|
||||
func (a *App) Handle(verb, path string, handler Handler, mw ...Middleware) {
|
||||
|
||||
// First wrap handler specific middleware around this handler.
|
||||
handler = wrapMiddleware(mw, handler)
|
||||
|
||||
// Add the application's general middleware to the handler chain.
|
||||
handler = wrapMiddleware(a.mw, handler)
|
||||
|
||||
// The function to execute for each request.
|
||||
h := func(w http.ResponseWriter, r *http.Request, params map[string]string) {
|
||||
ctx, span := trace.StartSpan(r.Context(), "internal.platform.web")
|
||||
defer span.End()
|
||||
|
||||
// Set the context with the required values to
|
||||
// process the request.
|
||||
v := Values{
|
||||
TraceID: span.SpanContext().TraceID.String(),
|
||||
Now: time.Now(),
|
||||
}
|
||||
ctx = context.WithValue(ctx, KeyValues, &v)
|
||||
|
||||
// Call the wrapped handler functions.
|
||||
if err := handler(ctx, w, r, params); err != nil {
|
||||
a.log.Printf("*****> critical shutdown error: %v", err)
|
||||
a.SignalShutdown()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Add this handler for the specified verb and route.
|
||||
a.TreeMux.Handle(verb, path, h)
|
||||
}
|
||||
|
||||
// ServeHTTP implements the http.Handler interface. It overrides the ServeHTTP
|
||||
// of the embedded TreeMux by using the ochttp.Handler instead. That Handler
|
||||
// wraps the TreeMux handler so the routes are served.
|
||||
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
a.och.ServeHTTP(w, r)
|
||||
}
|
43
example-project/internal/product/models.go
Normal file
43
example-project/internal/product/models.go
Normal file
@ -0,0 +1,43 @@
|
||||
package product
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
// Product is an item we sell.
|
||||
type Product struct {
|
||||
ID bson.ObjectId `bson:"_id" json:"id"` // Unique identifier.
|
||||
Name string `bson:"name" json:"name"` // Display name of the product.
|
||||
Cost int `bson:"cost" json:"cost"` // Price for one item in cents.
|
||||
Quantity int `bson:"quantity" json:"quantity"` // Original number of items available.
|
||||
DateCreated time.Time `bson:"date_created" json:"date_created"` // When the product was added.
|
||||
DateModified time.Time `bson:"date_modified" json:"date_modified"` // When the product record was lost modified.
|
||||
}
|
||||
|
||||
// NewProduct is what we require from clients when adding a Product.
|
||||
type NewProduct struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Cost int `json:"cost" validate:"required,gte=0"`
|
||||
Quantity int `json:"quantity" validate:"required,gte=1"`
|
||||
}
|
||||
|
||||
// UpdateProduct defines what information may be provided to modify an
|
||||
// existing Product. All fields are optional so clients can send just the
|
||||
// fields they want changed. It uses pointer fields so we can differentiate
|
||||
// between a field that was not provided and a field that was provided as
|
||||
// explicitly blank. Normally we do not want to use pointers to basic types but
|
||||
// we make exceptions around marshalling/unmarshalling.
|
||||
type UpdateProduct struct {
|
||||
Name *string `json:"name"`
|
||||
Cost *int `json:"cost" validate:"omitempty,gte=0"`
|
||||
Quantity *int `json:"quantity" validate:"omitempty,gte=1"`
|
||||
}
|
||||
|
||||
// Sale represents a transaction where we sold some quantity of a
|
||||
// Product.
|
||||
type Sale struct{}
|
||||
|
||||
// NewSale defines what we require when creating a Sale record.
|
||||
type NewSale struct{}
|
161
example-project/internal/product/product.go
Normal file
161
example-project/internal/product/product.go
Normal file
@ -0,0 +1,161 @@
|
||||
package product
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/db"
|
||||
"github.com/pkg/errors"
|
||||
"go.opencensus.io/trace"
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
const productsCollection = "products"
|
||||
|
||||
var (
|
||||
// ErrNotFound abstracts the mgo not found error.
|
||||
ErrNotFound = errors.New("Entity not found")
|
||||
|
||||
// ErrInvalidID occurs when an ID is not in a valid form.
|
||||
ErrInvalidID = errors.New("ID is not in its proper form")
|
||||
)
|
||||
|
||||
// List retrieves a list of existing products from the database.
|
||||
func List(ctx context.Context, dbConn *db.DB) ([]Product, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.product.List")
|
||||
defer span.End()
|
||||
|
||||
p := []Product{}
|
||||
|
||||
f := func(collection *mgo.Collection) error {
|
||||
return collection.Find(nil).All(&p)
|
||||
}
|
||||
if err := dbConn.Execute(ctx, productsCollection, f); err != nil {
|
||||
return nil, errors.Wrap(err, "db.products.find()")
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Retrieve gets the specified product from the database.
|
||||
func Retrieve(ctx context.Context, dbConn *db.DB, id string) (*Product, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.product.Retrieve")
|
||||
defer span.End()
|
||||
|
||||
if !bson.IsObjectIdHex(id) {
|
||||
return nil, ErrInvalidID
|
||||
}
|
||||
|
||||
q := bson.M{"_id": bson.ObjectIdHex(id)}
|
||||
|
||||
var p *Product
|
||||
f := func(collection *mgo.Collection) error {
|
||||
return collection.Find(q).One(&p)
|
||||
}
|
||||
if err := dbConn.Execute(ctx, productsCollection, f); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, errors.Wrap(err, fmt.Sprintf("db.products.find(%s)", db.Query(q)))
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Create inserts a new product into the database.
|
||||
func Create(ctx context.Context, dbConn *db.DB, cp *NewProduct, now time.Time) (*Product, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.product.Create")
|
||||
defer span.End()
|
||||
|
||||
// Mongo truncates times to milliseconds when storing. We and do the same
|
||||
// here so the value we return is consistent with what we store.
|
||||
now = now.Truncate(time.Millisecond)
|
||||
|
||||
p := Product{
|
||||
ID: bson.NewObjectId(),
|
||||
Name: cp.Name,
|
||||
Cost: cp.Cost,
|
||||
Quantity: cp.Quantity,
|
||||
DateCreated: now,
|
||||
DateModified: now,
|
||||
}
|
||||
|
||||
f := func(collection *mgo.Collection) error {
|
||||
return collection.Insert(&p)
|
||||
}
|
||||
if err := dbConn.Execute(ctx, productsCollection, f); err != nil {
|
||||
return nil, errors.Wrap(err, fmt.Sprintf("db.products.insert(%s)", db.Query(&p)))
|
||||
}
|
||||
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
// Update replaces a product document in the database.
|
||||
func Update(ctx context.Context, dbConn *db.DB, id string, upd UpdateProduct, now time.Time) error {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.product.Update")
|
||||
defer span.End()
|
||||
|
||||
if !bson.IsObjectIdHex(id) {
|
||||
return ErrInvalidID
|
||||
}
|
||||
|
||||
fields := make(bson.M)
|
||||
|
||||
if upd.Name != nil {
|
||||
fields["name"] = *upd.Name
|
||||
}
|
||||
if upd.Cost != nil {
|
||||
fields["cost"] = *upd.Cost
|
||||
}
|
||||
if upd.Quantity != nil {
|
||||
fields["quantity"] = *upd.Quantity
|
||||
}
|
||||
|
||||
// If there's nothing to update we can quit early.
|
||||
if len(fields) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
fields["date_modified"] = now
|
||||
|
||||
m := bson.M{"$set": fields}
|
||||
q := bson.M{"_id": bson.ObjectIdHex(id)}
|
||||
|
||||
f := func(collection *mgo.Collection) error {
|
||||
return collection.Update(q, m)
|
||||
}
|
||||
if err := dbConn.Execute(ctx, productsCollection, f); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
return ErrNotFound
|
||||
}
|
||||
return errors.Wrap(err, fmt.Sprintf("db.customers.update(%s, %s)", db.Query(q), db.Query(m)))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete removes a product from the database.
|
||||
func Delete(ctx context.Context, dbConn *db.DB, id string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.product.Delete")
|
||||
defer span.End()
|
||||
|
||||
if !bson.IsObjectIdHex(id) {
|
||||
return ErrInvalidID
|
||||
}
|
||||
|
||||
q := bson.M{"_id": bson.ObjectIdHex(id)}
|
||||
|
||||
f := func(collection *mgo.Collection) error {
|
||||
return collection.Remove(q)
|
||||
}
|
||||
if err := dbConn.Execute(ctx, productsCollection, f); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
return ErrNotFound
|
||||
}
|
||||
return errors.Wrap(err, fmt.Sprintf("db.products.remove(%v)", q))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
129
example-project/internal/product/product_test.go
Normal file
129
example-project/internal/product/product_test.go
Normal file
@ -0,0 +1,129 @@
|
||||
package product_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/tests"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/product"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var test *tests.Test
|
||||
|
||||
// TestMain is the entry point for testing.
|
||||
func TestMain(m *testing.M) {
|
||||
os.Exit(testMain(m))
|
||||
}
|
||||
|
||||
func testMain(m *testing.M) int {
|
||||
test = tests.New()
|
||||
defer test.TearDown()
|
||||
return m.Run()
|
||||
}
|
||||
|
||||
// TestProduct validates the full set of CRUD operations on Product values.
|
||||
func TestProduct(t *testing.T) {
|
||||
defer tests.Recover(t)
|
||||
|
||||
t.Log("Given the need to work with Product records.")
|
||||
{
|
||||
t.Log("\tWhen handling a single Product.")
|
||||
{
|
||||
ctx := tests.Context()
|
||||
|
||||
dbConn := test.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
np := product.NewProduct{
|
||||
Name: "Comic Books",
|
||||
Cost: 25,
|
||||
Quantity: 60,
|
||||
}
|
||||
|
||||
p, err := product.Create(ctx, dbConn, &np, time.Now().UTC())
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to create a product : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to create a product.", tests.Success)
|
||||
|
||||
savedP, err := product.Retrieve(ctx, dbConn, p.ID.Hex())
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to retrieve product by ID: %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to retrieve product by ID.", tests.Success)
|
||||
|
||||
if diff := cmp.Diff(p, savedP); diff != "" {
|
||||
t.Fatalf("\t%s\tShould get back the same product. Diff:\n%s", tests.Failed, diff)
|
||||
}
|
||||
t.Logf("\t%s\tShould get back the same product.", tests.Success)
|
||||
|
||||
upd := product.UpdateProduct{
|
||||
Name: tests.StringPointer("Comics"),
|
||||
Cost: tests.IntPointer(50),
|
||||
Quantity: tests.IntPointer(40),
|
||||
}
|
||||
|
||||
if err := product.Update(ctx, dbConn, p.ID.Hex(), upd, time.Now().UTC()); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to update product : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to update product.", tests.Success)
|
||||
|
||||
savedP, err = product.Retrieve(ctx, dbConn, p.ID.Hex())
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to retrieve updated product : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to retrieve updated product.", tests.Success)
|
||||
|
||||
// Build a product matching what we expect to see. We just use the
|
||||
// modified time from the database.
|
||||
want := &product.Product{
|
||||
ID: p.ID,
|
||||
Name: *upd.Name,
|
||||
Cost: *upd.Cost,
|
||||
Quantity: *upd.Quantity,
|
||||
DateCreated: p.DateCreated,
|
||||
DateModified: savedP.DateModified,
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(want, savedP); diff != "" {
|
||||
t.Fatalf("\t%s\tShould get back the same product. Diff:\n%s", tests.Failed, diff)
|
||||
}
|
||||
t.Logf("\t%s\tShould get back the same product.", tests.Success)
|
||||
|
||||
upd = product.UpdateProduct{
|
||||
Name: tests.StringPointer("Graphic Novels"),
|
||||
}
|
||||
|
||||
if err := product.Update(ctx, dbConn, p.ID.Hex(), upd, time.Now().UTC()); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to update just some fields of product : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to update just some fields of product.", tests.Success)
|
||||
|
||||
savedP, err = product.Retrieve(ctx, dbConn, p.ID.Hex())
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to retrieve updated product : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to retrieve updated product.", tests.Success)
|
||||
|
||||
if savedP.Name != *upd.Name {
|
||||
t.Fatalf("\t%s\tShould be able to see updated Name field : got %q want %q.", tests.Failed, savedP.Name, *upd.Name)
|
||||
} else {
|
||||
t.Logf("\t%s\tShould be able to see updated Name field.", tests.Success)
|
||||
}
|
||||
|
||||
if err := product.Delete(ctx, dbConn, p.ID.Hex()); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to delete product : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to delete product.", tests.Success)
|
||||
|
||||
savedP, err = product.Retrieve(ctx, dbConn, p.ID.Hex())
|
||||
if errors.Cause(err) != product.ErrNotFound {
|
||||
t.Fatalf("\t%s\tShould NOT be able to retrieve deleted product : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould NOT be able to retrieve deleted product.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
48
example-project/internal/user/models.go
Normal file
48
example-project/internal/user/models.go
Normal file
@ -0,0 +1,48 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
// User represents someone with access to our system.
|
||||
type User struct {
|
||||
ID bson.ObjectId `bson:"_id" json:"id"`
|
||||
Name string `bson:"name" json:"name"`
|
||||
Email string `bson:"email" json:"email"` // TODO(jlw) enforce uniqueness
|
||||
Roles []string `bson:"roles" json:"roles"`
|
||||
|
||||
PasswordHash []byte `bson:"password_hash" json:"-"`
|
||||
|
||||
DateModified time.Time `bson:"date_modified" json:"date_modified"`
|
||||
DateCreated time.Time `bson:"date_created,omitempty" json:"date_created"`
|
||||
}
|
||||
|
||||
// NewUser contains information needed to create a new User.
|
||||
type NewUser struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Email string `json:"email" validate:"required"` // TODO(jlw) enforce uniqueness.
|
||||
Roles []string `json:"roles" validate:"required"` // TODO(jlw) Ensure only includes valid roles.
|
||||
Password string `json:"password" validate:"required"`
|
||||
PasswordConfirm string `json:"password_confirm" validate:"eqfield=Password"`
|
||||
}
|
||||
|
||||
// UpdateUser defines what information may be provided to modify an existing
|
||||
// User. All fields are optional so clients can send just the fields they want
|
||||
// changed. It uses pointer fields so we can differentiate between a field that
|
||||
// was not provided and a field that was provided as explicitly blank. Normally
|
||||
// we do not want to use pointers to basic types but we make exceptions around
|
||||
// marshalling/unmarshalling.
|
||||
type UpdateUser struct {
|
||||
Name *string `json:"name"`
|
||||
Email *string `json:"email"` // TODO(jlw) enforce uniqueness.
|
||||
Roles []string `json:"roles"` // TODO(jlw) Ensure only includes valid roles.
|
||||
Password *string `json:"password"`
|
||||
PasswordConfirm *string `json:"password_confirm" validate:"omitempty,eqfield=Password"`
|
||||
}
|
||||
|
||||
// Token is the payload we deliver to users when they authenticate.
|
||||
type Token struct {
|
||||
Token string `json:"token"`
|
||||
}
|
234
example-project/internal/user/user.go
Normal file
234
example-project/internal/user/user.go
Normal file
@ -0,0 +1,234 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/db"
|
||||
"github.com/pkg/errors"
|
||||
"go.opencensus.io/trace"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
const usersCollection = "users"
|
||||
|
||||
var (
|
||||
// ErrNotFound abstracts the mgo not found error.
|
||||
ErrNotFound = errors.New("Entity not found")
|
||||
|
||||
// ErrInvalidID occurs when an ID is not in a valid form.
|
||||
ErrInvalidID = errors.New("ID is not in its proper form")
|
||||
|
||||
// ErrAuthenticationFailure occurs when a user attempts to authenticate but
|
||||
// anything goes wrong.
|
||||
ErrAuthenticationFailure = errors.New("Authentication failed")
|
||||
|
||||
// ErrForbidden occurs when a user tries to do something that is forbidden to them according to our access control policies.
|
||||
ErrForbidden = errors.New("Attempted action is not allowed")
|
||||
)
|
||||
|
||||
// List retrieves a list of existing users from the database.
|
||||
func List(ctx context.Context, dbConn *db.DB) ([]User, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.user.List")
|
||||
defer span.End()
|
||||
|
||||
u := []User{}
|
||||
|
||||
f := func(collection *mgo.Collection) error {
|
||||
return collection.Find(nil).All(&u)
|
||||
}
|
||||
if err := dbConn.Execute(ctx, usersCollection, f); err != nil {
|
||||
return nil, errors.Wrap(err, "db.users.find()")
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// Retrieve gets the specified user from the database.
|
||||
func Retrieve(ctx context.Context, claims auth.Claims, dbConn *db.DB, id string) (*User, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.user.Retrieve")
|
||||
defer span.End()
|
||||
|
||||
if !bson.IsObjectIdHex(id) {
|
||||
return nil, ErrInvalidID
|
||||
}
|
||||
|
||||
// If you are not an admin and looking to retrieve someone else then you are rejected.
|
||||
if !claims.HasRole(auth.RoleAdmin) && claims.Subject != id {
|
||||
return nil, ErrForbidden
|
||||
}
|
||||
|
||||
q := bson.M{"_id": bson.ObjectIdHex(id)}
|
||||
|
||||
var u *User
|
||||
f := func(collection *mgo.Collection) error {
|
||||
return collection.Find(q).One(&u)
|
||||
}
|
||||
if err := dbConn.Execute(ctx, usersCollection, f); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, errors.Wrap(err, fmt.Sprintf("db.users.find(%s)", db.Query(q)))
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// Create inserts a new user into the database.
|
||||
func Create(ctx context.Context, dbConn *db.DB, nu *NewUser, now time.Time) (*User, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.user.Create")
|
||||
defer span.End()
|
||||
|
||||
// Mongo truncates times to milliseconds when storing. We and do the same
|
||||
// here so the value we return is consistent with what we store.
|
||||
now = now.Truncate(time.Millisecond)
|
||||
|
||||
pw, err := bcrypt.GenerateFromPassword([]byte(nu.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "generating password hash")
|
||||
}
|
||||
|
||||
u := User{
|
||||
ID: bson.NewObjectId(),
|
||||
Name: nu.Name,
|
||||
Email: nu.Email,
|
||||
PasswordHash: pw,
|
||||
Roles: nu.Roles,
|
||||
DateCreated: now,
|
||||
DateModified: now,
|
||||
}
|
||||
|
||||
f := func(collection *mgo.Collection) error {
|
||||
return collection.Insert(&u)
|
||||
}
|
||||
if err := dbConn.Execute(ctx, usersCollection, f); err != nil {
|
||||
return nil, errors.Wrap(err, fmt.Sprintf("db.users.insert(%s)", db.Query(&u)))
|
||||
}
|
||||
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
// Update replaces a user document in the database.
|
||||
func Update(ctx context.Context, dbConn *db.DB, id string, upd *UpdateUser, now time.Time) error {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.user.Update")
|
||||
defer span.End()
|
||||
|
||||
if !bson.IsObjectIdHex(id) {
|
||||
return ErrInvalidID
|
||||
}
|
||||
|
||||
fields := make(bson.M)
|
||||
|
||||
if upd.Name != nil {
|
||||
fields["name"] = *upd.Name
|
||||
}
|
||||
if upd.Email != nil {
|
||||
fields["email"] = *upd.Email
|
||||
}
|
||||
if upd.Roles != nil {
|
||||
fields["roles"] = upd.Roles
|
||||
}
|
||||
if upd.Password != nil {
|
||||
pw, err := bcrypt.GenerateFromPassword([]byte(*upd.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "generating password hash")
|
||||
}
|
||||
fields["password_hash"] = pw
|
||||
}
|
||||
|
||||
// If there's nothing to update we can quit early.
|
||||
if len(fields) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
fields["date_modified"] = now
|
||||
|
||||
m := bson.M{"$set": fields}
|
||||
q := bson.M{"_id": bson.ObjectIdHex(id)}
|
||||
|
||||
f := func(collection *mgo.Collection) error {
|
||||
return collection.Update(q, m)
|
||||
}
|
||||
if err := dbConn.Execute(ctx, usersCollection, f); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
return ErrNotFound
|
||||
}
|
||||
return errors.Wrap(err, fmt.Sprintf("db.customers.update(%s, %s)", db.Query(q), db.Query(m)))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete removes a user from the database.
|
||||
func Delete(ctx context.Context, dbConn *db.DB, id string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.user.Delete")
|
||||
defer span.End()
|
||||
|
||||
if !bson.IsObjectIdHex(id) {
|
||||
return ErrInvalidID
|
||||
}
|
||||
|
||||
q := bson.M{"_id": bson.ObjectIdHex(id)}
|
||||
|
||||
f := func(collection *mgo.Collection) error {
|
||||
return collection.Remove(q)
|
||||
}
|
||||
if err := dbConn.Execute(ctx, usersCollection, f); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
return ErrNotFound
|
||||
}
|
||||
return errors.Wrap(err, fmt.Sprintf("db.users.remove(%s)", db.Query(q)))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// TokenGenerator is the behavior we need in our Authenticate to generate
|
||||
// tokens for authenticated users.
|
||||
type TokenGenerator interface {
|
||||
GenerateToken(auth.Claims) (string, error)
|
||||
}
|
||||
|
||||
// Authenticate finds a user by their email and verifies their password. On
|
||||
// success it returns a Token that can be used to authenticate in the future.
|
||||
func Authenticate(ctx context.Context, dbConn *db.DB, tknGen TokenGenerator, now time.Time, email, password string) (Token, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.user.Authenticate")
|
||||
defer span.End()
|
||||
|
||||
q := bson.M{"email": email}
|
||||
|
||||
var u *User
|
||||
f := func(collection *mgo.Collection) error {
|
||||
return collection.Find(q).One(&u)
|
||||
}
|
||||
if err := dbConn.Execute(ctx, usersCollection, f); err != nil {
|
||||
|
||||
// Normally we would return ErrNotFound in this scenario but we do not want
|
||||
// to leak to an unauthenticated user which emails are in the system.
|
||||
if err == mgo.ErrNotFound {
|
||||
return Token{}, ErrAuthenticationFailure
|
||||
}
|
||||
return Token{}, errors.Wrap(err, fmt.Sprintf("db.users.find(%s)", db.Query(q)))
|
||||
}
|
||||
|
||||
// Compare the provided password with the saved hash. Use the bcrypt
|
||||
// comparison function so it is cryptographically secure.
|
||||
if err := bcrypt.CompareHashAndPassword(u.PasswordHash, []byte(password)); err != nil {
|
||||
return Token{}, ErrAuthenticationFailure
|
||||
}
|
||||
|
||||
// If we are this far the request is valid. Create some claims for the user
|
||||
// and generate their token.
|
||||
claims := auth.NewClaims(u.ID.Hex(), u.Roles, now, time.Hour)
|
||||
|
||||
tkn, err := tknGen.GenerateToken(claims)
|
||||
if err != nil {
|
||||
return Token{}, errors.Wrap(err, "generating token")
|
||||
}
|
||||
|
||||
return Token{Token: tkn}, nil
|
||||
}
|
179
example-project/internal/user/user_test.go
Normal file
179
example-project/internal/user/user_test.go
Normal file
@ -0,0 +1,179 @@
|
||||
package user_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/tests"
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/user"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/pkg/errors"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
var test *tests.Test
|
||||
|
||||
// TestMain is the entry point for testing.
|
||||
func TestMain(m *testing.M) {
|
||||
os.Exit(testMain(m))
|
||||
}
|
||||
|
||||
func testMain(m *testing.M) int {
|
||||
test = tests.New()
|
||||
defer test.TearDown()
|
||||
return m.Run()
|
||||
}
|
||||
|
||||
// TestUser validates the full set of CRUD operations on User values.
|
||||
func TestUser(t *testing.T) {
|
||||
defer tests.Recover(t)
|
||||
|
||||
t.Log("Given the need to work with User records.")
|
||||
{
|
||||
t.Log("\tWhen handling a single User.")
|
||||
{
|
||||
ctx := tests.Context()
|
||||
|
||||
dbConn := test.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
now := time.Date(2018, time.October, 1, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
// claims is information about the person making the request.
|
||||
claims := auth.NewClaims(bson.NewObjectId().Hex(), []string{auth.RoleAdmin}, now, time.Hour)
|
||||
|
||||
nu := user.NewUser{
|
||||
Name: "Bill Kennedy",
|
||||
Email: "bill@ardanlabs.com",
|
||||
Roles: []string{auth.RoleAdmin},
|
||||
Password: "gophers",
|
||||
PasswordConfirm: "gophers",
|
||||
}
|
||||
|
||||
u, err := user.Create(ctx, dbConn, &nu, now)
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to create user : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to create user.", tests.Success)
|
||||
|
||||
savedU, err := user.Retrieve(ctx, claims, dbConn, u.ID.Hex())
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to retrieve user by ID: %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to retrieve user by ID.", tests.Success)
|
||||
|
||||
if diff := cmp.Diff(u, savedU); diff != "" {
|
||||
t.Fatalf("\t%s\tShould get back the same user. Diff:\n%s", tests.Failed, diff)
|
||||
}
|
||||
t.Logf("\t%s\tShould get back the same user.", tests.Success)
|
||||
|
||||
upd := user.UpdateUser{
|
||||
Name: tests.StringPointer("Jacob Walker"),
|
||||
Email: tests.StringPointer("jacob@ardanlabs.com"),
|
||||
}
|
||||
|
||||
if err := user.Update(ctx, dbConn, u.ID.Hex(), &upd, now); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to update user : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to update user.", tests.Success)
|
||||
|
||||
savedU, err = user.Retrieve(ctx, claims, dbConn, u.ID.Hex())
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to retrieve user : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to retrieve user.", tests.Success)
|
||||
|
||||
if savedU.Name != *upd.Name {
|
||||
t.Errorf("\t%s\tShould be able to see updates to Name.", tests.Failed)
|
||||
t.Log("\t\tGot:", savedU.Name)
|
||||
t.Log("\t\tExp:", *upd.Name)
|
||||
} else {
|
||||
t.Logf("\t%s\tShould be able to see updates to Name.", tests.Success)
|
||||
}
|
||||
|
||||
if savedU.Email != *upd.Email {
|
||||
t.Errorf("\t%s\tShould be able to see updates to Email.", tests.Failed)
|
||||
t.Log("\t\tGot:", savedU.Email)
|
||||
t.Log("\t\tExp:", *upd.Email)
|
||||
} else {
|
||||
t.Logf("\t%s\tShould be able to see updates to Email.", tests.Success)
|
||||
}
|
||||
|
||||
if err := user.Delete(ctx, dbConn, u.ID.Hex()); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to delete user : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to delete user.", tests.Success)
|
||||
|
||||
savedU, err = user.Retrieve(ctx, claims, dbConn, u.ID.Hex())
|
||||
if errors.Cause(err) != user.ErrNotFound {
|
||||
t.Fatalf("\t%s\tShould NOT be able to retrieve user : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould NOT be able to retrieve user.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// mockTokenGenerator is used for testing that Authenticate calls its provided
|
||||
// token generator in a specific way.
|
||||
type mockTokenGenerator struct{}
|
||||
|
||||
// GenerateToken implements the TokenGenerator interface. It returns a "token"
|
||||
// that includes some information about the claims it was passed.
|
||||
func (mockTokenGenerator) GenerateToken(claims auth.Claims) (string, error) {
|
||||
return fmt.Sprintf("sub:%q iss:%d", claims.Subject, claims.IssuedAt), nil
|
||||
}
|
||||
|
||||
// TestAuthenticate validates the behavior around authenticating users.
|
||||
func TestAuthenticate(t *testing.T) {
|
||||
defer tests.Recover(t)
|
||||
|
||||
t.Log("Given the need to authenticate users")
|
||||
{
|
||||
t.Log("\tWhen handling a single User.")
|
||||
{
|
||||
ctx := tests.Context()
|
||||
|
||||
dbConn := test.MasterDB.Copy()
|
||||
defer dbConn.Close()
|
||||
|
||||
nu := user.NewUser{
|
||||
Name: "Anna Walker",
|
||||
Email: "anna@ardanlabs.com",
|
||||
Roles: []string{auth.RoleAdmin},
|
||||
Password: "goroutines",
|
||||
PasswordConfirm: "goroutines",
|
||||
}
|
||||
|
||||
now := time.Date(2018, time.October, 1, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
u, err := user.Create(ctx, dbConn, &nu, now)
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to create user : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to create user.", tests.Success)
|
||||
|
||||
var tknGen mockTokenGenerator
|
||||
tkn, err := user.Authenticate(ctx, dbConn, tknGen, now, "anna@ardanlabs.com", "goroutines")
|
||||
if err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to generate a token : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to generate a token.", tests.Success)
|
||||
|
||||
want := fmt.Sprintf("sub:%q iss:1538352000", u.ID.Hex())
|
||||
if tkn.Token != want {
|
||||
t.Log("\t\tGot :", tkn.Token)
|
||||
t.Log("\t\tWant:", want)
|
||||
t.Fatalf("\t%s\tToken should indicate the specified user and time were used.", tests.Failed)
|
||||
}
|
||||
t.Logf("\t%s\tToken should indicate the specified user and time were used.", tests.Success)
|
||||
|
||||
if err := user.Delete(ctx, dbConn, u.ID.Hex()); err != nil {
|
||||
t.Fatalf("\t%s\tShould be able to delete user : %s.", tests.Failed, err)
|
||||
}
|
||||
t.Logf("\t%s\tShould be able to delete user.", tests.Success)
|
||||
}
|
||||
}
|
||||
}
|
116
example-project/makefile
Normal file
116
example-project/makefile
Normal file
@ -0,0 +1,116 @@
|
||||
SHELL := /bin/bash
|
||||
|
||||
all: keys sales-api metrics tracer
|
||||
|
||||
keys:
|
||||
go run ./cmd/sales-admin/main.go --cmd keygen
|
||||
|
||||
admin:
|
||||
go run ./cmd/sales-admin/main.go --cmd useradd --user_email admin@example.com --user_password gophers
|
||||
|
||||
sales-api:
|
||||
docker build \
|
||||
-t gcr.io/sales-api/sales-api-amd64:1.0 \
|
||||
--build-arg PACKAGE_NAME=sales-api \
|
||||
--build-arg VCS_REF=`git rev-parse HEAD` \
|
||||
--build-arg BUILD_DATE=`date -u +”%Y-%m-%dT%H:%M:%SZ”` \
|
||||
.
|
||||
docker system prune -f
|
||||
|
||||
metrics:
|
||||
docker build \
|
||||
-t gcr.io/sales-api/metrics-amd64:1.0 \
|
||||
--build-arg PACKAGE_NAME=metrics \
|
||||
--build-arg PACKAGE_PREFIX=sidecar/ \
|
||||
--build-arg VCS_REF=`git rev-parse HEAD` \
|
||||
--build-arg BUILD_DATE=`date -u +”%Y-%m-%dT%H:%M:%SZ”` \
|
||||
.
|
||||
docker system prune -f
|
||||
|
||||
tracer:
|
||||
cd "$$GOPATH/src/geeks-accelerator/oss/saas-starter-kit/example-project"
|
||||
docker build \
|
||||
-t gcr.io/sales-api/tracer-amd64:1.0 \
|
||||
--build-arg PACKAGE_NAME=tracer \
|
||||
--build-arg PACKAGE_PREFIX=sidecar/ \
|
||||
--build-arg VCS_REF=`git rev-parse HEAD` \
|
||||
--build-arg BUILD_DATE=`date -u +”%Y-%m-%dT%H:%M:%SZ”` \
|
||||
.
|
||||
docker system prune -f
|
||||
|
||||
up:
|
||||
docker-compose up
|
||||
|
||||
down:
|
||||
docker-compose down
|
||||
|
||||
test:
|
||||
cd "$$GOPATH/src/geeks-accelerator/oss/saas-starter-kit/example-project"
|
||||
go test ./...
|
||||
|
||||
clean:
|
||||
docker system prune -f
|
||||
|
||||
stop-all:
|
||||
docker stop $(docker ps -aq)
|
||||
|
||||
remove-all:
|
||||
docker rm $(docker ps -aq)
|
||||
|
||||
#===============================================================================
|
||||
# GKE
|
||||
|
||||
config:
|
||||
@echo Setting environment for sales-api
|
||||
gcloud config set project sales-api
|
||||
gcloud config set compute/zone us-central1-b
|
||||
gcloud auth configure-docker
|
||||
@echo ======================================================================
|
||||
|
||||
project:
|
||||
gcloud projects create sales-api
|
||||
gcloud beta billing projects link sales-api --billing-account=$(ACCOUNT_ID)
|
||||
gcloud services enable container.googleapis.com
|
||||
@echo ======================================================================
|
||||
|
||||
cluster:
|
||||
gcloud container clusters create sales-api-cluster --num-nodes=2 --machine-type=n1-standard-2
|
||||
gcloud compute instances list
|
||||
@echo ======================================================================
|
||||
|
||||
upload:
|
||||
docker push gcr.io/sales-api/sales-api-amd64:1.0
|
||||
docker push gcr.io/sales-api/metrics-amd64:1.0
|
||||
docker push gcr.io/sales-api/tracer-amd64:1.0
|
||||
@echo ======================================================================
|
||||
|
||||
database:
|
||||
kubectl create -f gke-deploy-database.yaml
|
||||
kubectl expose -f gke-expose-database.yaml --type=LoadBalancer
|
||||
@echo ======================================================================
|
||||
|
||||
services:
|
||||
kubectl create -f gke-deploy-sales-api.yaml
|
||||
kubectl expose -f gke-expose-sales-api.yaml --type=LoadBalancer
|
||||
@echo ======================================================================
|
||||
|
||||
shell:
|
||||
kubectl exec -it pod-name --container name -- /bin/bash
|
||||
@echo ======================================================================
|
||||
|
||||
status:
|
||||
gcloud container clusters list
|
||||
kubectl get nodes
|
||||
kubectl get pods
|
||||
kubectl get services sales-api
|
||||
@echo ======================================================================
|
||||
|
||||
delete:
|
||||
kubectl delete services sales-api
|
||||
kubectl delete deployment sales-api
|
||||
gcloud container clusters delete sales-api-cluster
|
||||
gcloud projects delete sales-api
|
||||
docker image remove gcr.io/sales-api/sales-api-amd64:1.0
|
||||
docker image remove gcr.io/sales-api/metrics-amd64:1.0
|
||||
docker image remove gcr.io/sales-api/tracer-amd64:1.0
|
||||
@echo ======================================================================
|
4
example-project/vendor/github.com/dgrijalva/jwt-go/.gitignore
generated
vendored
Normal file
4
example-project/vendor/github.com/dgrijalva/jwt-go/.gitignore
generated
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
.DS_Store
|
||||
bin
|
||||
|
||||
|
13
example-project/vendor/github.com/dgrijalva/jwt-go/.travis.yml
generated
vendored
Normal file
13
example-project/vendor/github.com/dgrijalva/jwt-go/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
language: go
|
||||
|
||||
script:
|
||||
- go vet ./...
|
||||
- go test -v ./...
|
||||
|
||||
go:
|
||||
- 1.3
|
||||
- 1.4
|
||||
- 1.5
|
||||
- 1.6
|
||||
- 1.7
|
||||
- tip
|
8
example-project/vendor/github.com/dgrijalva/jwt-go/LICENSE
generated
vendored
Normal file
8
example-project/vendor/github.com/dgrijalva/jwt-go/LICENSE
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
Copyright (c) 2012 Dave Grijalva
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
97
example-project/vendor/github.com/dgrijalva/jwt-go/MIGRATION_GUIDE.md
generated
vendored
Normal file
97
example-project/vendor/github.com/dgrijalva/jwt-go/MIGRATION_GUIDE.md
generated
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
## Migration Guide from v2 -> v3
|
||||
|
||||
Version 3 adds several new, frequently requested features. To do so, it introduces a few breaking changes. We've worked to keep these as minimal as possible. This guide explains the breaking changes and how you can quickly update your code.
|
||||
|
||||
### `Token.Claims` is now an interface type
|
||||
|
||||
The most requested feature from the 2.0 verison of this library was the ability to provide a custom type to the JSON parser for claims. This was implemented by introducing a new interface, `Claims`, to replace `map[string]interface{}`. We also included two concrete implementations of `Claims`: `MapClaims` and `StandardClaims`.
|
||||
|
||||
`MapClaims` is an alias for `map[string]interface{}` with built in validation behavior. It is the default claims type when using `Parse`. The usage is unchanged except you must type cast the claims property.
|
||||
|
||||
The old example for parsing a token looked like this..
|
||||
|
||||
```go
|
||||
if token, err := jwt.Parse(tokenString, keyLookupFunc); err == nil {
|
||||
fmt.Printf("Token for user %v expires %v", token.Claims["user"], token.Claims["exp"])
|
||||
}
|
||||
```
|
||||
|
||||
is now directly mapped to...
|
||||
|
||||
```go
|
||||
if token, err := jwt.Parse(tokenString, keyLookupFunc); err == nil {
|
||||
claims := token.Claims.(jwt.MapClaims)
|
||||
fmt.Printf("Token for user %v expires %v", claims["user"], claims["exp"])
|
||||
}
|
||||
```
|
||||
|
||||
`StandardClaims` is designed to be embedded in your custom type. You can supply a custom claims type with the new `ParseWithClaims` function. Here's an example of using a custom claims type.
|
||||
|
||||
```go
|
||||
type MyCustomClaims struct {
|
||||
User string
|
||||
*StandardClaims
|
||||
}
|
||||
|
||||
if token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, keyLookupFunc); err == nil {
|
||||
claims := token.Claims.(*MyCustomClaims)
|
||||
fmt.Printf("Token for user %v expires %v", claims.User, claims.StandardClaims.ExpiresAt)
|
||||
}
|
||||
```
|
||||
|
||||
### `ParseFromRequest` has been moved
|
||||
|
||||
To keep this library focused on the tokens without becoming overburdened with complex request processing logic, `ParseFromRequest` and its new companion `ParseFromRequestWithClaims` have been moved to a subpackage, `request`. The method signatues have also been augmented to receive a new argument: `Extractor`.
|
||||
|
||||
`Extractors` do the work of picking the token string out of a request. The interface is simple and composable.
|
||||
|
||||
This simple parsing example:
|
||||
|
||||
```go
|
||||
if token, err := jwt.ParseFromRequest(tokenString, req, keyLookupFunc); err == nil {
|
||||
fmt.Printf("Token for user %v expires %v", token.Claims["user"], token.Claims["exp"])
|
||||
}
|
||||
```
|
||||
|
||||
is directly mapped to:
|
||||
|
||||
```go
|
||||
if token, err := request.ParseFromRequest(req, request.OAuth2Extractor, keyLookupFunc); err == nil {
|
||||
claims := token.Claims.(jwt.MapClaims)
|
||||
fmt.Printf("Token for user %v expires %v", claims["user"], claims["exp"])
|
||||
}
|
||||
```
|
||||
|
||||
There are several concrete `Extractor` types provided for your convenience:
|
||||
|
||||
* `HeaderExtractor` will search a list of headers until one contains content.
|
||||
* `ArgumentExtractor` will search a list of keys in request query and form arguments until one contains content.
|
||||
* `MultiExtractor` will try a list of `Extractors` in order until one returns content.
|
||||
* `AuthorizationHeaderExtractor` will look in the `Authorization` header for a `Bearer` token.
|
||||
* `OAuth2Extractor` searches the places an OAuth2 token would be specified (per the spec): `Authorization` header and `access_token` argument
|
||||
* `PostExtractionFilter` wraps an `Extractor`, allowing you to process the content before it's parsed. A simple example is stripping the `Bearer ` text from a header
|
||||
|
||||
|
||||
### RSA signing methods no longer accept `[]byte` keys
|
||||
|
||||
Due to a [critical vulnerability](https://auth0.com/blog/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/), we've decided the convenience of accepting `[]byte` instead of `rsa.PublicKey` or `rsa.PrivateKey` isn't worth the risk of misuse.
|
||||
|
||||
To replace this behavior, we've added two helper methods: `ParseRSAPrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error)` and `ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error)`. These are just simple helpers for unpacking PEM encoded PKCS1 and PKCS8 keys. If your keys are encoded any other way, all you need to do is convert them to the `crypto/rsa` package's types.
|
||||
|
||||
```go
|
||||
func keyLookupFunc(*Token) (interface{}, error) {
|
||||
// Don't forget to validate the alg is what you expect:
|
||||
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
|
||||
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
|
||||
// Look up key
|
||||
key, err := lookupPublicKey(token.Header["kid"])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Unpack key from PEM encoded PKCS8
|
||||
return jwt.ParseRSAPublicKeyFromPEM(key)
|
||||
}
|
||||
```
|
100
example-project/vendor/github.com/dgrijalva/jwt-go/README.md
generated
vendored
Normal file
100
example-project/vendor/github.com/dgrijalva/jwt-go/README.md
generated
vendored
Normal file
@ -0,0 +1,100 @@
|
||||
# jwt-go
|
||||
|
||||
[](https://travis-ci.org/dgrijalva/jwt-go)
|
||||
[](https://godoc.org/github.com/dgrijalva/jwt-go)
|
||||
|
||||
A [go](http://www.golang.org) (or 'golang' for search engine friendliness) implementation of [JSON Web Tokens](http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html)
|
||||
|
||||
**NEW VERSION COMING:** There have been a lot of improvements suggested since the version 3.0.0 released in 2016. I'm working now on cutting two different releases: 3.2.0 will contain any non-breaking changes or enhancements. 4.0.0 will follow shortly which will include breaking changes. See the 4.0.0 milestone to get an idea of what's coming. If you have other ideas, or would like to participate in 4.0.0, now's the time. If you depend on this library and don't want to be interrupted, I recommend you use your dependency mangement tool to pin to version 3.
|
||||
|
||||
**SECURITY NOTICE:** Some older versions of Go have a security issue in the cryotp/elliptic. Recommendation is to upgrade to at least 1.8.3. See issue #216 for more detail.
|
||||
|
||||
**SECURITY NOTICE:** It's important that you [validate the `alg` presented is what you expect](https://auth0.com/blog/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/). This library attempts to make it easy to do the right thing by requiring key types match the expected alg, but you should take the extra step to verify it in your usage. See the examples provided.
|
||||
|
||||
## What the heck is a JWT?
|
||||
|
||||
JWT.io has [a great introduction](https://jwt.io/introduction) to JSON Web Tokens.
|
||||
|
||||
In short, it's a signed JSON object that does something useful (for example, authentication). It's commonly used for `Bearer` tokens in Oauth 2. A token is made of three parts, separated by `.`'s. The first two parts are JSON objects, that have been [base64url](http://tools.ietf.org/html/rfc4648) encoded. The last part is the signature, encoded the same way.
|
||||
|
||||
The first part is called the header. It contains the necessary information for verifying the last part, the signature. For example, which encryption method was used for signing and what key was used.
|
||||
|
||||
The part in the middle is the interesting bit. It's called the Claims and contains the actual stuff you care about. Refer to [the RFC](http://self-issued.info/docs/draft-jones-json-web-token.html) for information about reserved keys and the proper way to add your own.
|
||||
|
||||
## What's in the box?
|
||||
|
||||
This library supports the parsing and verification as well as the generation and signing of JWTs. Current supported signing algorithms are HMAC SHA, RSA, RSA-PSS, and ECDSA, though hooks are present for adding your own.
|
||||
|
||||
## Examples
|
||||
|
||||
See [the project documentation](https://godoc.org/github.com/dgrijalva/jwt-go) for examples of usage:
|
||||
|
||||
* [Simple example of parsing and validating a token](https://godoc.org/github.com/dgrijalva/jwt-go#example-Parse--Hmac)
|
||||
* [Simple example of building and signing a token](https://godoc.org/github.com/dgrijalva/jwt-go#example-New--Hmac)
|
||||
* [Directory of Examples](https://godoc.org/github.com/dgrijalva/jwt-go#pkg-examples)
|
||||
|
||||
## Extensions
|
||||
|
||||
This library publishes all the necessary components for adding your own signing methods. Simply implement the `SigningMethod` interface and register a factory method using `RegisterSigningMethod`.
|
||||
|
||||
Here's an example of an extension that integrates with the Google App Engine signing tools: https://github.com/someone1/gcp-jwt-go
|
||||
|
||||
## Compliance
|
||||
|
||||
This library was last reviewed to comply with [RTF 7519](http://www.rfc-editor.org/info/rfc7519) dated May 2015 with a few notable differences:
|
||||
|
||||
* In order to protect against accidental use of [Unsecured JWTs](http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#UnsecuredJWT), tokens using `alg=none` will only be accepted if the constant `jwt.UnsafeAllowNoneSignatureType` is provided as the key.
|
||||
|
||||
## Project Status & Versioning
|
||||
|
||||
This library is considered production ready. Feedback and feature requests are appreciated. The API should be considered stable. There should be very few backwards-incompatible changes outside of major version updates (and only with good reason).
|
||||
|
||||
This project uses [Semantic Versioning 2.0.0](http://semver.org). Accepted pull requests will land on `master`. Periodically, versions will be tagged from `master`. You can find all the releases on [the project releases page](https://github.com/dgrijalva/jwt-go/releases).
|
||||
|
||||
While we try to make it obvious when we make breaking changes, there isn't a great mechanism for pushing announcements out to users. You may want to use this alternative package include: `gopkg.in/dgrijalva/jwt-go.v3`. It will do the right thing WRT semantic versioning.
|
||||
|
||||
**BREAKING CHANGES:***
|
||||
* Version 3.0.0 includes _a lot_ of changes from the 2.x line, including a few that break the API. We've tried to break as few things as possible, so there should just be a few type signature changes. A full list of breaking changes is available in `VERSION_HISTORY.md`. See `MIGRATION_GUIDE.md` for more information on updating your code.
|
||||
|
||||
## Usage Tips
|
||||
|
||||
### Signing vs Encryption
|
||||
|
||||
A token is simply a JSON object that is signed by its author. this tells you exactly two things about the data:
|
||||
|
||||
* The author of the token was in the possession of the signing secret
|
||||
* The data has not been modified since it was signed
|
||||
|
||||
It's important to know that JWT does not provide encryption, which means anyone who has access to the token can read its contents. If you need to protect (encrypt) the data, there is a companion spec, `JWE`, that provides this functionality. JWE is currently outside the scope of this library.
|
||||
|
||||
### Choosing a Signing Method
|
||||
|
||||
There are several signing methods available, and you should probably take the time to learn about the various options before choosing one. The principal design decision is most likely going to be symmetric vs asymmetric.
|
||||
|
||||
Symmetric signing methods, such as HSA, use only a single secret. This is probably the simplest signing method to use since any `[]byte` can be used as a valid secret. They are also slightly computationally faster to use, though this rarely is enough to matter. Symmetric signing methods work the best when both producers and consumers of tokens are trusted, or even the same system. Since the same secret is used to both sign and validate tokens, you can't easily distribute the key for validation.
|
||||
|
||||
Asymmetric signing methods, such as RSA, use different keys for signing and verifying tokens. This makes it possible to produce tokens with a private key, and allow any consumer to access the public key for verification.
|
||||
|
||||
### Signing Methods and Key Types
|
||||
|
||||
Each signing method expects a different object type for its signing keys. See the package documentation for details. Here are the most common ones:
|
||||
|
||||
* The [HMAC signing method](https://godoc.org/github.com/dgrijalva/jwt-go#SigningMethodHMAC) (`HS256`,`HS384`,`HS512`) expect `[]byte` values for signing and validation
|
||||
* The [RSA signing method](https://godoc.org/github.com/dgrijalva/jwt-go#SigningMethodRSA) (`RS256`,`RS384`,`RS512`) expect `*rsa.PrivateKey` for signing and `*rsa.PublicKey` for validation
|
||||
* The [ECDSA signing method](https://godoc.org/github.com/dgrijalva/jwt-go#SigningMethodECDSA) (`ES256`,`ES384`,`ES512`) expect `*ecdsa.PrivateKey` for signing and `*ecdsa.PublicKey` for validation
|
||||
|
||||
### JWT and OAuth
|
||||
|
||||
It's worth mentioning that OAuth and JWT are not the same thing. A JWT token is simply a signed JSON object. It can be used anywhere such a thing is useful. There is some confusion, though, as JWT is the most common type of bearer token used in OAuth2 authentication.
|
||||
|
||||
Without going too far down the rabbit hole, here's a description of the interaction of these technologies:
|
||||
|
||||
* OAuth is a protocol for allowing an identity provider to be separate from the service a user is logging in to. For example, whenever you use Facebook to log into a different service (Yelp, Spotify, etc), you are using OAuth.
|
||||
* OAuth defines several options for passing around authentication data. One popular method is called a "bearer token". A bearer token is simply a string that _should_ only be held by an authenticated user. Thus, simply presenting this token proves your identity. You can probably derive from here why a JWT might make a good bearer token.
|
||||
* Because bearer tokens are used for authentication, it's important they're kept secret. This is why transactions that use bearer tokens typically happen over SSL.
|
||||
|
||||
## More
|
||||
|
||||
Documentation can be found [on godoc.org](http://godoc.org/github.com/dgrijalva/jwt-go).
|
||||
|
||||
The command line utility included in this project (cmd/jwt) provides a straightforward example of token creation and parsing as well as a useful tool for debugging your own integration. You'll also find several implementation examples in the documentation.
|
118
example-project/vendor/github.com/dgrijalva/jwt-go/VERSION_HISTORY.md
generated
vendored
Normal file
118
example-project/vendor/github.com/dgrijalva/jwt-go/VERSION_HISTORY.md
generated
vendored
Normal file
@ -0,0 +1,118 @@
|
||||
## `jwt-go` Version History
|
||||
|
||||
#### 3.2.0
|
||||
|
||||
* Added method `ParseUnverified` to allow users to split up the tasks of parsing and validation
|
||||
* HMAC signing method returns `ErrInvalidKeyType` instead of `ErrInvalidKey` where appropriate
|
||||
* Added options to `request.ParseFromRequest`, which allows for an arbitrary list of modifiers to parsing behavior. Initial set include `WithClaims` and `WithParser`. Existing usage of this function will continue to work as before.
|
||||
* Deprecated `ParseFromRequestWithClaims` to simplify API in the future.
|
||||
|
||||
#### 3.1.0
|
||||
|
||||
* Improvements to `jwt` command line tool
|
||||
* Added `SkipClaimsValidation` option to `Parser`
|
||||
* Documentation updates
|
||||
|
||||
#### 3.0.0
|
||||
|
||||
* **Compatibility Breaking Changes**: See MIGRATION_GUIDE.md for tips on updating your code
|
||||
* Dropped support for `[]byte` keys when using RSA signing methods. This convenience feature could contribute to security vulnerabilities involving mismatched key types with signing methods.
|
||||
* `ParseFromRequest` has been moved to `request` subpackage and usage has changed
|
||||
* The `Claims` property on `Token` is now type `Claims` instead of `map[string]interface{}`. The default value is type `MapClaims`, which is an alias to `map[string]interface{}`. This makes it possible to use a custom type when decoding claims.
|
||||
* Other Additions and Changes
|
||||
* Added `Claims` interface type to allow users to decode the claims into a custom type
|
||||
* Added `ParseWithClaims`, which takes a third argument of type `Claims`. Use this function instead of `Parse` if you have a custom type you'd like to decode into.
|
||||
* Dramatically improved the functionality and flexibility of `ParseFromRequest`, which is now in the `request` subpackage
|
||||
* Added `ParseFromRequestWithClaims` which is the `FromRequest` equivalent of `ParseWithClaims`
|
||||
* Added new interface type `Extractor`, which is used for extracting JWT strings from http requests. Used with `ParseFromRequest` and `ParseFromRequestWithClaims`.
|
||||
* Added several new, more specific, validation errors to error type bitmask
|
||||
* Moved examples from README to executable example files
|
||||
* Signing method registry is now thread safe
|
||||
* Added new property to `ValidationError`, which contains the raw error returned by calls made by parse/verify (such as those returned by keyfunc or json parser)
|
||||
|
||||
#### 2.7.0
|
||||
|
||||
This will likely be the last backwards compatible release before 3.0.0, excluding essential bug fixes.
|
||||
|
||||
* Added new option `-show` to the `jwt` command that will just output the decoded token without verifying
|
||||
* Error text for expired tokens includes how long it's been expired
|
||||
* Fixed incorrect error returned from `ParseRSAPublicKeyFromPEM`
|
||||
* Documentation updates
|
||||
|
||||
#### 2.6.0
|
||||
|
||||
* Exposed inner error within ValidationError
|
||||
* Fixed validation errors when using UseJSONNumber flag
|
||||
* Added several unit tests
|
||||
|
||||
#### 2.5.0
|
||||
|
||||
* Added support for signing method none. You shouldn't use this. The API tries to make this clear.
|
||||
* Updated/fixed some documentation
|
||||
* Added more helpful error message when trying to parse tokens that begin with `BEARER `
|
||||
|
||||
#### 2.4.0
|
||||
|
||||
* Added new type, Parser, to allow for configuration of various parsing parameters
|
||||
* You can now specify a list of valid signing methods. Anything outside this set will be rejected.
|
||||
* You can now opt to use the `json.Number` type instead of `float64` when parsing token JSON
|
||||
* Added support for [Travis CI](https://travis-ci.org/dgrijalva/jwt-go)
|
||||
* Fixed some bugs with ECDSA parsing
|
||||
|
||||
#### 2.3.0
|
||||
|
||||
* Added support for ECDSA signing methods
|
||||
* Added support for RSA PSS signing methods (requires go v1.4)
|
||||
|
||||
#### 2.2.0
|
||||
|
||||
* Gracefully handle a `nil` `Keyfunc` being passed to `Parse`. Result will now be the parsed token and an error, instead of a panic.
|
||||
|
||||
#### 2.1.0
|
||||
|
||||
Backwards compatible API change that was missed in 2.0.0.
|
||||
|
||||
* The `SignedString` method on `Token` now takes `interface{}` instead of `[]byte`
|
||||
|
||||
#### 2.0.0
|
||||
|
||||
There were two major reasons for breaking backwards compatibility with this update. The first was a refactor required to expand the width of the RSA and HMAC-SHA signing implementations. There will likely be no required code changes to support this change.
|
||||
|
||||
The second update, while unfortunately requiring a small change in integration, is required to open up this library to other signing methods. Not all keys used for all signing methods have a single standard on-disk representation. Requiring `[]byte` as the type for all keys proved too limiting. Additionally, this implementation allows for pre-parsed tokens to be reused, which might matter in an application that parses a high volume of tokens with a small set of keys. Backwards compatibilty has been maintained for passing `[]byte` to the RSA signing methods, but they will also accept `*rsa.PublicKey` and `*rsa.PrivateKey`.
|
||||
|
||||
It is likely the only integration change required here will be to change `func(t *jwt.Token) ([]byte, error)` to `func(t *jwt.Token) (interface{}, error)` when calling `Parse`.
|
||||
|
||||
* **Compatibility Breaking Changes**
|
||||
* `SigningMethodHS256` is now `*SigningMethodHMAC` instead of `type struct`
|
||||
* `SigningMethodRS256` is now `*SigningMethodRSA` instead of `type struct`
|
||||
* `KeyFunc` now returns `interface{}` instead of `[]byte`
|
||||
* `SigningMethod.Sign` now takes `interface{}` instead of `[]byte` for the key
|
||||
* `SigningMethod.Verify` now takes `interface{}` instead of `[]byte` for the key
|
||||
* Renamed type `SigningMethodHS256` to `SigningMethodHMAC`. Specific sizes are now just instances of this type.
|
||||
* Added public package global `SigningMethodHS256`
|
||||
* Added public package global `SigningMethodHS384`
|
||||
* Added public package global `SigningMethodHS512`
|
||||
* Renamed type `SigningMethodRS256` to `SigningMethodRSA`. Specific sizes are now just instances of this type.
|
||||
* Added public package global `SigningMethodRS256`
|
||||
* Added public package global `SigningMethodRS384`
|
||||
* Added public package global `SigningMethodRS512`
|
||||
* Moved sample private key for HMAC tests from an inline value to a file on disk. Value is unchanged.
|
||||
* Refactored the RSA implementation to be easier to read
|
||||
* Exposed helper methods `ParseRSAPrivateKeyFromPEM` and `ParseRSAPublicKeyFromPEM`
|
||||
|
||||
#### 1.0.2
|
||||
|
||||
* Fixed bug in parsing public keys from certificates
|
||||
* Added more tests around the parsing of keys for RS256
|
||||
* Code refactoring in RS256 implementation. No functional changes
|
||||
|
||||
#### 1.0.1
|
||||
|
||||
* Fixed panic if RS256 signing method was passed an invalid key
|
||||
|
||||
#### 1.0.0
|
||||
|
||||
* First versioned release
|
||||
* API stabilized
|
||||
* Supports creating, signing, parsing, and validating JWT tokens
|
||||
* Supports RS256 and HS256 signing methods
|
134
example-project/vendor/github.com/dgrijalva/jwt-go/claims.go
generated
vendored
Normal file
134
example-project/vendor/github.com/dgrijalva/jwt-go/claims.go
generated
vendored
Normal file
@ -0,0 +1,134 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// For a type to be a Claims object, it must just have a Valid method that determines
|
||||
// if the token is invalid for any supported reason
|
||||
type Claims interface {
|
||||
Valid() error
|
||||
}
|
||||
|
||||
// Structured version of Claims Section, as referenced at
|
||||
// https://tools.ietf.org/html/rfc7519#section-4.1
|
||||
// See examples for how to use this with your own claim types
|
||||
type StandardClaims struct {
|
||||
Audience string `json:"aud,omitempty"`
|
||||
ExpiresAt int64 `json:"exp,omitempty"`
|
||||
Id string `json:"jti,omitempty"`
|
||||
IssuedAt int64 `json:"iat,omitempty"`
|
||||
Issuer string `json:"iss,omitempty"`
|
||||
NotBefore int64 `json:"nbf,omitempty"`
|
||||
Subject string `json:"sub,omitempty"`
|
||||
}
|
||||
|
||||
// Validates time based claims "exp, iat, nbf".
|
||||
// There is no accounting for clock skew.
|
||||
// As well, if any of the above claims are not in the token, it will still
|
||||
// be considered a valid claim.
|
||||
func (c StandardClaims) Valid() error {
|
||||
vErr := new(ValidationError)
|
||||
now := TimeFunc().Unix()
|
||||
|
||||
// The claims below are optional, by default, so if they are set to the
|
||||
// default value in Go, let's not fail the verification for them.
|
||||
if c.VerifyExpiresAt(now, false) == false {
|
||||
delta := time.Unix(now, 0).Sub(time.Unix(c.ExpiresAt, 0))
|
||||
vErr.Inner = fmt.Errorf("token is expired by %v", delta)
|
||||
vErr.Errors |= ValidationErrorExpired
|
||||
}
|
||||
|
||||
if c.VerifyIssuedAt(now, false) == false {
|
||||
vErr.Inner = fmt.Errorf("Token used before issued")
|
||||
vErr.Errors |= ValidationErrorIssuedAt
|
||||
}
|
||||
|
||||
if c.VerifyNotBefore(now, false) == false {
|
||||
vErr.Inner = fmt.Errorf("token is not valid yet")
|
||||
vErr.Errors |= ValidationErrorNotValidYet
|
||||
}
|
||||
|
||||
if vErr.valid() {
|
||||
return nil
|
||||
}
|
||||
|
||||
return vErr
|
||||
}
|
||||
|
||||
// Compares the aud claim against cmp.
|
||||
// If required is false, this method will return true if the value matches or is unset
|
||||
func (c *StandardClaims) VerifyAudience(cmp string, req bool) bool {
|
||||
return verifyAud(c.Audience, cmp, req)
|
||||
}
|
||||
|
||||
// Compares the exp claim against cmp.
|
||||
// If required is false, this method will return true if the value matches or is unset
|
||||
func (c *StandardClaims) VerifyExpiresAt(cmp int64, req bool) bool {
|
||||
return verifyExp(c.ExpiresAt, cmp, req)
|
||||
}
|
||||
|
||||
// Compares the iat claim against cmp.
|
||||
// If required is false, this method will return true if the value matches or is unset
|
||||
func (c *StandardClaims) VerifyIssuedAt(cmp int64, req bool) bool {
|
||||
return verifyIat(c.IssuedAt, cmp, req)
|
||||
}
|
||||
|
||||
// Compares the iss claim against cmp.
|
||||
// If required is false, this method will return true if the value matches or is unset
|
||||
func (c *StandardClaims) VerifyIssuer(cmp string, req bool) bool {
|
||||
return verifyIss(c.Issuer, cmp, req)
|
||||
}
|
||||
|
||||
// Compares the nbf claim against cmp.
|
||||
// If required is false, this method will return true if the value matches or is unset
|
||||
func (c *StandardClaims) VerifyNotBefore(cmp int64, req bool) bool {
|
||||
return verifyNbf(c.NotBefore, cmp, req)
|
||||
}
|
||||
|
||||
// ----- helpers
|
||||
|
||||
func verifyAud(aud string, cmp string, required bool) bool {
|
||||
if aud == "" {
|
||||
return !required
|
||||
}
|
||||
if subtle.ConstantTimeCompare([]byte(aud), []byte(cmp)) != 0 {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func verifyExp(exp int64, now int64, required bool) bool {
|
||||
if exp == 0 {
|
||||
return !required
|
||||
}
|
||||
return now <= exp
|
||||
}
|
||||
|
||||
func verifyIat(iat int64, now int64, required bool) bool {
|
||||
if iat == 0 {
|
||||
return !required
|
||||
}
|
||||
return now >= iat
|
||||
}
|
||||
|
||||
func verifyIss(iss string, cmp string, required bool) bool {
|
||||
if iss == "" {
|
||||
return !required
|
||||
}
|
||||
if subtle.ConstantTimeCompare([]byte(iss), []byte(cmp)) != 0 {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func verifyNbf(nbf int64, now int64, required bool) bool {
|
||||
if nbf == 0 {
|
||||
return !required
|
||||
}
|
||||
return now >= nbf
|
||||
}
|
4
example-project/vendor/github.com/dgrijalva/jwt-go/doc.go
generated
vendored
Normal file
4
example-project/vendor/github.com/dgrijalva/jwt-go/doc.go
generated
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
// Package jwt is a Go implementation of JSON Web Tokens: http://self-issued.info/docs/draft-jones-json-web-token.html
|
||||
//
|
||||
// See README.md for more info.
|
||||
package jwt
|
148
example-project/vendor/github.com/dgrijalva/jwt-go/ecdsa.go
generated
vendored
Normal file
148
example-project/vendor/github.com/dgrijalva/jwt-go/ecdsa.go
generated
vendored
Normal file
@ -0,0 +1,148 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/ecdsa"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
var (
|
||||
// Sadly this is missing from crypto/ecdsa compared to crypto/rsa
|
||||
ErrECDSAVerification = errors.New("crypto/ecdsa: verification error")
|
||||
)
|
||||
|
||||
// Implements the ECDSA family of signing methods signing methods
|
||||
// Expects *ecdsa.PrivateKey for signing and *ecdsa.PublicKey for verification
|
||||
type SigningMethodECDSA struct {
|
||||
Name string
|
||||
Hash crypto.Hash
|
||||
KeySize int
|
||||
CurveBits int
|
||||
}
|
||||
|
||||
// Specific instances for EC256 and company
|
||||
var (
|
||||
SigningMethodES256 *SigningMethodECDSA
|
||||
SigningMethodES384 *SigningMethodECDSA
|
||||
SigningMethodES512 *SigningMethodECDSA
|
||||
)
|
||||
|
||||
func init() {
|
||||
// ES256
|
||||
SigningMethodES256 = &SigningMethodECDSA{"ES256", crypto.SHA256, 32, 256}
|
||||
RegisterSigningMethod(SigningMethodES256.Alg(), func() SigningMethod {
|
||||
return SigningMethodES256
|
||||
})
|
||||
|
||||
// ES384
|
||||
SigningMethodES384 = &SigningMethodECDSA{"ES384", crypto.SHA384, 48, 384}
|
||||
RegisterSigningMethod(SigningMethodES384.Alg(), func() SigningMethod {
|
||||
return SigningMethodES384
|
||||
})
|
||||
|
||||
// ES512
|
||||
SigningMethodES512 = &SigningMethodECDSA{"ES512", crypto.SHA512, 66, 521}
|
||||
RegisterSigningMethod(SigningMethodES512.Alg(), func() SigningMethod {
|
||||
return SigningMethodES512
|
||||
})
|
||||
}
|
||||
|
||||
func (m *SigningMethodECDSA) Alg() string {
|
||||
return m.Name
|
||||
}
|
||||
|
||||
// Implements the Verify method from SigningMethod
|
||||
// For this verify method, key must be an ecdsa.PublicKey struct
|
||||
func (m *SigningMethodECDSA) Verify(signingString, signature string, key interface{}) error {
|
||||
var err error
|
||||
|
||||
// Decode the signature
|
||||
var sig []byte
|
||||
if sig, err = DecodeSegment(signature); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get the key
|
||||
var ecdsaKey *ecdsa.PublicKey
|
||||
switch k := key.(type) {
|
||||
case *ecdsa.PublicKey:
|
||||
ecdsaKey = k
|
||||
default:
|
||||
return ErrInvalidKeyType
|
||||
}
|
||||
|
||||
if len(sig) != 2*m.KeySize {
|
||||
return ErrECDSAVerification
|
||||
}
|
||||
|
||||
r := big.NewInt(0).SetBytes(sig[:m.KeySize])
|
||||
s := big.NewInt(0).SetBytes(sig[m.KeySize:])
|
||||
|
||||
// Create hasher
|
||||
if !m.Hash.Available() {
|
||||
return ErrHashUnavailable
|
||||
}
|
||||
hasher := m.Hash.New()
|
||||
hasher.Write([]byte(signingString))
|
||||
|
||||
// Verify the signature
|
||||
if verifystatus := ecdsa.Verify(ecdsaKey, hasher.Sum(nil), r, s); verifystatus == true {
|
||||
return nil
|
||||
} else {
|
||||
return ErrECDSAVerification
|
||||
}
|
||||
}
|
||||
|
||||
// Implements the Sign method from SigningMethod
|
||||
// For this signing method, key must be an ecdsa.PrivateKey struct
|
||||
func (m *SigningMethodECDSA) Sign(signingString string, key interface{}) (string, error) {
|
||||
// Get the key
|
||||
var ecdsaKey *ecdsa.PrivateKey
|
||||
switch k := key.(type) {
|
||||
case *ecdsa.PrivateKey:
|
||||
ecdsaKey = k
|
||||
default:
|
||||
return "", ErrInvalidKeyType
|
||||
}
|
||||
|
||||
// Create the hasher
|
||||
if !m.Hash.Available() {
|
||||
return "", ErrHashUnavailable
|
||||
}
|
||||
|
||||
hasher := m.Hash.New()
|
||||
hasher.Write([]byte(signingString))
|
||||
|
||||
// Sign the string and return r, s
|
||||
if r, s, err := ecdsa.Sign(rand.Reader, ecdsaKey, hasher.Sum(nil)); err == nil {
|
||||
curveBits := ecdsaKey.Curve.Params().BitSize
|
||||
|
||||
if m.CurveBits != curveBits {
|
||||
return "", ErrInvalidKey
|
||||
}
|
||||
|
||||
keyBytes := curveBits / 8
|
||||
if curveBits%8 > 0 {
|
||||
keyBytes += 1
|
||||
}
|
||||
|
||||
// We serialize the outpus (r and s) into big-endian byte arrays and pad
|
||||
// them with zeros on the left to make sure the sizes work out. Both arrays
|
||||
// must be keyBytes long, and the output must be 2*keyBytes long.
|
||||
rBytes := r.Bytes()
|
||||
rBytesPadded := make([]byte, keyBytes)
|
||||
copy(rBytesPadded[keyBytes-len(rBytes):], rBytes)
|
||||
|
||||
sBytes := s.Bytes()
|
||||
sBytesPadded := make([]byte, keyBytes)
|
||||
copy(sBytesPadded[keyBytes-len(sBytes):], sBytes)
|
||||
|
||||
out := append(rBytesPadded, sBytesPadded...)
|
||||
|
||||
return EncodeSegment(out), nil
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
}
|
67
example-project/vendor/github.com/dgrijalva/jwt-go/ecdsa_utils.go
generated
vendored
Normal file
67
example-project/vendor/github.com/dgrijalva/jwt-go/ecdsa_utils.go
generated
vendored
Normal file
@ -0,0 +1,67 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotECPublicKey = errors.New("Key is not a valid ECDSA public key")
|
||||
ErrNotECPrivateKey = errors.New("Key is not a valid ECDSA private key")
|
||||
)
|
||||
|
||||
// Parse PEM encoded Elliptic Curve Private Key Structure
|
||||
func ParseECPrivateKeyFromPEM(key []byte) (*ecdsa.PrivateKey, error) {
|
||||
var err error
|
||||
|
||||
// Parse PEM block
|
||||
var block *pem.Block
|
||||
if block, _ = pem.Decode(key); block == nil {
|
||||
return nil, ErrKeyMustBePEMEncoded
|
||||
}
|
||||
|
||||
// Parse the key
|
||||
var parsedKey interface{}
|
||||
if parsedKey, err = x509.ParseECPrivateKey(block.Bytes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var pkey *ecdsa.PrivateKey
|
||||
var ok bool
|
||||
if pkey, ok = parsedKey.(*ecdsa.PrivateKey); !ok {
|
||||
return nil, ErrNotECPrivateKey
|
||||
}
|
||||
|
||||
return pkey, nil
|
||||
}
|
||||
|
||||
// Parse PEM encoded PKCS1 or PKCS8 public key
|
||||
func ParseECPublicKeyFromPEM(key []byte) (*ecdsa.PublicKey, error) {
|
||||
var err error
|
||||
|
||||
// Parse PEM block
|
||||
var block *pem.Block
|
||||
if block, _ = pem.Decode(key); block == nil {
|
||||
return nil, ErrKeyMustBePEMEncoded
|
||||
}
|
||||
|
||||
// Parse the key
|
||||
var parsedKey interface{}
|
||||
if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil {
|
||||
if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
|
||||
parsedKey = cert.PublicKey
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var pkey *ecdsa.PublicKey
|
||||
var ok bool
|
||||
if pkey, ok = parsedKey.(*ecdsa.PublicKey); !ok {
|
||||
return nil, ErrNotECPublicKey
|
||||
}
|
||||
|
||||
return pkey, nil
|
||||
}
|
59
example-project/vendor/github.com/dgrijalva/jwt-go/errors.go
generated
vendored
Normal file
59
example-project/vendor/github.com/dgrijalva/jwt-go/errors.go
generated
vendored
Normal file
@ -0,0 +1,59 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Error constants
|
||||
var (
|
||||
ErrInvalidKey = errors.New("key is invalid")
|
||||
ErrInvalidKeyType = errors.New("key is of invalid type")
|
||||
ErrHashUnavailable = errors.New("the requested hash function is unavailable")
|
||||
)
|
||||
|
||||
// The errors that might occur when parsing and validating a token
|
||||
const (
|
||||
ValidationErrorMalformed uint32 = 1 << iota // Token is malformed
|
||||
ValidationErrorUnverifiable // Token could not be verified because of signing problems
|
||||
ValidationErrorSignatureInvalid // Signature validation failed
|
||||
|
||||
// Standard Claim validation errors
|
||||
ValidationErrorAudience // AUD validation failed
|
||||
ValidationErrorExpired // EXP validation failed
|
||||
ValidationErrorIssuedAt // IAT validation failed
|
||||
ValidationErrorIssuer // ISS validation failed
|
||||
ValidationErrorNotValidYet // NBF validation failed
|
||||
ValidationErrorId // JTI validation failed
|
||||
ValidationErrorClaimsInvalid // Generic claims validation error
|
||||
)
|
||||
|
||||
// Helper for constructing a ValidationError with a string error message
|
||||
func NewValidationError(errorText string, errorFlags uint32) *ValidationError {
|
||||
return &ValidationError{
|
||||
text: errorText,
|
||||
Errors: errorFlags,
|
||||
}
|
||||
}
|
||||
|
||||
// The error from Parse if token is not valid
|
||||
type ValidationError struct {
|
||||
Inner error // stores the error returned by external dependencies, i.e.: KeyFunc
|
||||
Errors uint32 // bitfield. see ValidationError... constants
|
||||
text string // errors that do not have a valid error just have text
|
||||
}
|
||||
|
||||
// Validation error is an error type
|
||||
func (e ValidationError) Error() string {
|
||||
if e.Inner != nil {
|
||||
return e.Inner.Error()
|
||||
} else if e.text != "" {
|
||||
return e.text
|
||||
} else {
|
||||
return "token is invalid"
|
||||
}
|
||||
}
|
||||
|
||||
// No errors
|
||||
func (e *ValidationError) valid() bool {
|
||||
return e.Errors == 0
|
||||
}
|
95
example-project/vendor/github.com/dgrijalva/jwt-go/hmac.go
generated
vendored
Normal file
95
example-project/vendor/github.com/dgrijalva/jwt-go/hmac.go
generated
vendored
Normal file
@ -0,0 +1,95 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/hmac"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Implements the HMAC-SHA family of signing methods signing methods
|
||||
// Expects key type of []byte for both signing and validation
|
||||
type SigningMethodHMAC struct {
|
||||
Name string
|
||||
Hash crypto.Hash
|
||||
}
|
||||
|
||||
// Specific instances for HS256 and company
|
||||
var (
|
||||
SigningMethodHS256 *SigningMethodHMAC
|
||||
SigningMethodHS384 *SigningMethodHMAC
|
||||
SigningMethodHS512 *SigningMethodHMAC
|
||||
ErrSignatureInvalid = errors.New("signature is invalid")
|
||||
)
|
||||
|
||||
func init() {
|
||||
// HS256
|
||||
SigningMethodHS256 = &SigningMethodHMAC{"HS256", crypto.SHA256}
|
||||
RegisterSigningMethod(SigningMethodHS256.Alg(), func() SigningMethod {
|
||||
return SigningMethodHS256
|
||||
})
|
||||
|
||||
// HS384
|
||||
SigningMethodHS384 = &SigningMethodHMAC{"HS384", crypto.SHA384}
|
||||
RegisterSigningMethod(SigningMethodHS384.Alg(), func() SigningMethod {
|
||||
return SigningMethodHS384
|
||||
})
|
||||
|
||||
// HS512
|
||||
SigningMethodHS512 = &SigningMethodHMAC{"HS512", crypto.SHA512}
|
||||
RegisterSigningMethod(SigningMethodHS512.Alg(), func() SigningMethod {
|
||||
return SigningMethodHS512
|
||||
})
|
||||
}
|
||||
|
||||
func (m *SigningMethodHMAC) Alg() string {
|
||||
return m.Name
|
||||
}
|
||||
|
||||
// Verify the signature of HSXXX tokens. Returns nil if the signature is valid.
|
||||
func (m *SigningMethodHMAC) Verify(signingString, signature string, key interface{}) error {
|
||||
// Verify the key is the right type
|
||||
keyBytes, ok := key.([]byte)
|
||||
if !ok {
|
||||
return ErrInvalidKeyType
|
||||
}
|
||||
|
||||
// Decode signature, for comparison
|
||||
sig, err := DecodeSegment(signature)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Can we use the specified hashing method?
|
||||
if !m.Hash.Available() {
|
||||
return ErrHashUnavailable
|
||||
}
|
||||
|
||||
// This signing method is symmetric, so we validate the signature
|
||||
// by reproducing the signature from the signing string and key, then
|
||||
// comparing that against the provided signature.
|
||||
hasher := hmac.New(m.Hash.New, keyBytes)
|
||||
hasher.Write([]byte(signingString))
|
||||
if !hmac.Equal(sig, hasher.Sum(nil)) {
|
||||
return ErrSignatureInvalid
|
||||
}
|
||||
|
||||
// No validation errors. Signature is good.
|
||||
return nil
|
||||
}
|
||||
|
||||
// Implements the Sign method from SigningMethod for this signing method.
|
||||
// Key must be []byte
|
||||
func (m *SigningMethodHMAC) Sign(signingString string, key interface{}) (string, error) {
|
||||
if keyBytes, ok := key.([]byte); ok {
|
||||
if !m.Hash.Available() {
|
||||
return "", ErrHashUnavailable
|
||||
}
|
||||
|
||||
hasher := hmac.New(m.Hash.New, keyBytes)
|
||||
hasher.Write([]byte(signingString))
|
||||
|
||||
return EncodeSegment(hasher.Sum(nil)), nil
|
||||
}
|
||||
|
||||
return "", ErrInvalidKeyType
|
||||
}
|
94
example-project/vendor/github.com/dgrijalva/jwt-go/map_claims.go
generated
vendored
Normal file
94
example-project/vendor/github.com/dgrijalva/jwt-go/map_claims.go
generated
vendored
Normal file
@ -0,0 +1,94 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
// "fmt"
|
||||
)
|
||||
|
||||
// Claims type that uses the map[string]interface{} for JSON decoding
|
||||
// This is the default claims type if you don't supply one
|
||||
type MapClaims map[string]interface{}
|
||||
|
||||
// Compares the aud claim against cmp.
|
||||
// If required is false, this method will return true if the value matches or is unset
|
||||
func (m MapClaims) VerifyAudience(cmp string, req bool) bool {
|
||||
aud, _ := m["aud"].(string)
|
||||
return verifyAud(aud, cmp, req)
|
||||
}
|
||||
|
||||
// Compares the exp claim against cmp.
|
||||
// If required is false, this method will return true if the value matches or is unset
|
||||
func (m MapClaims) VerifyExpiresAt(cmp int64, req bool) bool {
|
||||
switch exp := m["exp"].(type) {
|
||||
case float64:
|
||||
return verifyExp(int64(exp), cmp, req)
|
||||
case json.Number:
|
||||
v, _ := exp.Int64()
|
||||
return verifyExp(v, cmp, req)
|
||||
}
|
||||
return req == false
|
||||
}
|
||||
|
||||
// Compares the iat claim against cmp.
|
||||
// If required is false, this method will return true if the value matches or is unset
|
||||
func (m MapClaims) VerifyIssuedAt(cmp int64, req bool) bool {
|
||||
switch iat := m["iat"].(type) {
|
||||
case float64:
|
||||
return verifyIat(int64(iat), cmp, req)
|
||||
case json.Number:
|
||||
v, _ := iat.Int64()
|
||||
return verifyIat(v, cmp, req)
|
||||
}
|
||||
return req == false
|
||||
}
|
||||
|
||||
// Compares the iss claim against cmp.
|
||||
// If required is false, this method will return true if the value matches or is unset
|
||||
func (m MapClaims) VerifyIssuer(cmp string, req bool) bool {
|
||||
iss, _ := m["iss"].(string)
|
||||
return verifyIss(iss, cmp, req)
|
||||
}
|
||||
|
||||
// Compares the nbf claim against cmp.
|
||||
// If required is false, this method will return true if the value matches or is unset
|
||||
func (m MapClaims) VerifyNotBefore(cmp int64, req bool) bool {
|
||||
switch nbf := m["nbf"].(type) {
|
||||
case float64:
|
||||
return verifyNbf(int64(nbf), cmp, req)
|
||||
case json.Number:
|
||||
v, _ := nbf.Int64()
|
||||
return verifyNbf(v, cmp, req)
|
||||
}
|
||||
return req == false
|
||||
}
|
||||
|
||||
// Validates time based claims "exp, iat, nbf".
|
||||
// There is no accounting for clock skew.
|
||||
// As well, if any of the above claims are not in the token, it will still
|
||||
// be considered a valid claim.
|
||||
func (m MapClaims) Valid() error {
|
||||
vErr := new(ValidationError)
|
||||
now := TimeFunc().Unix()
|
||||
|
||||
if m.VerifyExpiresAt(now, false) == false {
|
||||
vErr.Inner = errors.New("Token is expired")
|
||||
vErr.Errors |= ValidationErrorExpired
|
||||
}
|
||||
|
||||
if m.VerifyIssuedAt(now, false) == false {
|
||||
vErr.Inner = errors.New("Token used before issued")
|
||||
vErr.Errors |= ValidationErrorIssuedAt
|
||||
}
|
||||
|
||||
if m.VerifyNotBefore(now, false) == false {
|
||||
vErr.Inner = errors.New("Token is not valid yet")
|
||||
vErr.Errors |= ValidationErrorNotValidYet
|
||||
}
|
||||
|
||||
if vErr.valid() {
|
||||
return nil
|
||||
}
|
||||
|
||||
return vErr
|
||||
}
|
52
example-project/vendor/github.com/dgrijalva/jwt-go/none.go
generated
vendored
Normal file
52
example-project/vendor/github.com/dgrijalva/jwt-go/none.go
generated
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
package jwt
|
||||
|
||||
// Implements the none signing method. This is required by the spec
|
||||
// but you probably should never use it.
|
||||
var SigningMethodNone *signingMethodNone
|
||||
|
||||
const UnsafeAllowNoneSignatureType unsafeNoneMagicConstant = "none signing method allowed"
|
||||
|
||||
var NoneSignatureTypeDisallowedError error
|
||||
|
||||
type signingMethodNone struct{}
|
||||
type unsafeNoneMagicConstant string
|
||||
|
||||
func init() {
|
||||
SigningMethodNone = &signingMethodNone{}
|
||||
NoneSignatureTypeDisallowedError = NewValidationError("'none' signature type is not allowed", ValidationErrorSignatureInvalid)
|
||||
|
||||
RegisterSigningMethod(SigningMethodNone.Alg(), func() SigningMethod {
|
||||
return SigningMethodNone
|
||||
})
|
||||
}
|
||||
|
||||
func (m *signingMethodNone) Alg() string {
|
||||
return "none"
|
||||
}
|
||||
|
||||
// Only allow 'none' alg type if UnsafeAllowNoneSignatureType is specified as the key
|
||||
func (m *signingMethodNone) Verify(signingString, signature string, key interface{}) (err error) {
|
||||
// Key must be UnsafeAllowNoneSignatureType to prevent accidentally
|
||||
// accepting 'none' signing method
|
||||
if _, ok := key.(unsafeNoneMagicConstant); !ok {
|
||||
return NoneSignatureTypeDisallowedError
|
||||
}
|
||||
// If signing method is none, signature must be an empty string
|
||||
if signature != "" {
|
||||
return NewValidationError(
|
||||
"'none' signing method with non-empty signature",
|
||||
ValidationErrorSignatureInvalid,
|
||||
)
|
||||
}
|
||||
|
||||
// Accept 'none' signing method.
|
||||
return nil
|
||||
}
|
||||
|
||||
// Only allow 'none' signing if UnsafeAllowNoneSignatureType is specified as the key
|
||||
func (m *signingMethodNone) Sign(signingString string, key interface{}) (string, error) {
|
||||
if _, ok := key.(unsafeNoneMagicConstant); ok {
|
||||
return "", nil
|
||||
}
|
||||
return "", NoneSignatureTypeDisallowedError
|
||||
}
|
148
example-project/vendor/github.com/dgrijalva/jwt-go/parser.go
generated
vendored
Normal file
148
example-project/vendor/github.com/dgrijalva/jwt-go/parser.go
generated
vendored
Normal file
@ -0,0 +1,148 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Parser struct {
|
||||
ValidMethods []string // If populated, only these methods will be considered valid
|
||||
UseJSONNumber bool // Use JSON Number format in JSON decoder
|
||||
SkipClaimsValidation bool // Skip claims validation during token parsing
|
||||
}
|
||||
|
||||
// Parse, validate, and return a token.
|
||||
// keyFunc will receive the parsed token and should return the key for validating.
|
||||
// If everything is kosher, err will be nil
|
||||
func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
||||
return p.ParseWithClaims(tokenString, MapClaims{}, keyFunc)
|
||||
}
|
||||
|
||||
func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
|
||||
token, parts, err := p.ParseUnverified(tokenString, claims)
|
||||
if err != nil {
|
||||
return token, err
|
||||
}
|
||||
|
||||
// Verify signing method is in the required set
|
||||
if p.ValidMethods != nil {
|
||||
var signingMethodValid = false
|
||||
var alg = token.Method.Alg()
|
||||
for _, m := range p.ValidMethods {
|
||||
if m == alg {
|
||||
signingMethodValid = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !signingMethodValid {
|
||||
// signing method is not in the listed set
|
||||
return token, NewValidationError(fmt.Sprintf("signing method %v is invalid", alg), ValidationErrorSignatureInvalid)
|
||||
}
|
||||
}
|
||||
|
||||
// Lookup key
|
||||
var key interface{}
|
||||
if keyFunc == nil {
|
||||
// keyFunc was not provided. short circuiting validation
|
||||
return token, NewValidationError("no Keyfunc was provided.", ValidationErrorUnverifiable)
|
||||
}
|
||||
if key, err = keyFunc(token); err != nil {
|
||||
// keyFunc returned an error
|
||||
if ve, ok := err.(*ValidationError); ok {
|
||||
return token, ve
|
||||
}
|
||||
return token, &ValidationError{Inner: err, Errors: ValidationErrorUnverifiable}
|
||||
}
|
||||
|
||||
vErr := &ValidationError{}
|
||||
|
||||
// Validate Claims
|
||||
if !p.SkipClaimsValidation {
|
||||
if err := token.Claims.Valid(); err != nil {
|
||||
|
||||
// If the Claims Valid returned an error, check if it is a validation error,
|
||||
// If it was another error type, create a ValidationError with a generic ClaimsInvalid flag set
|
||||
if e, ok := err.(*ValidationError); !ok {
|
||||
vErr = &ValidationError{Inner: err, Errors: ValidationErrorClaimsInvalid}
|
||||
} else {
|
||||
vErr = e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Perform validation
|
||||
token.Signature = parts[2]
|
||||
if err = token.Method.Verify(strings.Join(parts[0:2], "."), token.Signature, key); err != nil {
|
||||
vErr.Inner = err
|
||||
vErr.Errors |= ValidationErrorSignatureInvalid
|
||||
}
|
||||
|
||||
if vErr.valid() {
|
||||
token.Valid = true
|
||||
return token, nil
|
||||
}
|
||||
|
||||
return token, vErr
|
||||
}
|
||||
|
||||
// WARNING: Don't use this method unless you know what you're doing
|
||||
//
|
||||
// This method parses the token but doesn't validate the signature. It's only
|
||||
// ever useful in cases where you know the signature is valid (because it has
|
||||
// been checked previously in the stack) and you want to extract values from
|
||||
// it.
|
||||
func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Token, parts []string, err error) {
|
||||
parts = strings.Split(tokenString, ".")
|
||||
if len(parts) != 3 {
|
||||
return nil, parts, NewValidationError("token contains an invalid number of segments", ValidationErrorMalformed)
|
||||
}
|
||||
|
||||
token = &Token{Raw: tokenString}
|
||||
|
||||
// parse Header
|
||||
var headerBytes []byte
|
||||
if headerBytes, err = DecodeSegment(parts[0]); err != nil {
|
||||
if strings.HasPrefix(strings.ToLower(tokenString), "bearer ") {
|
||||
return token, parts, NewValidationError("tokenstring should not contain 'bearer '", ValidationErrorMalformed)
|
||||
}
|
||||
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
||||
}
|
||||
if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
|
||||
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
||||
}
|
||||
|
||||
// parse Claims
|
||||
var claimBytes []byte
|
||||
token.Claims = claims
|
||||
|
||||
if claimBytes, err = DecodeSegment(parts[1]); err != nil {
|
||||
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
||||
}
|
||||
dec := json.NewDecoder(bytes.NewBuffer(claimBytes))
|
||||
if p.UseJSONNumber {
|
||||
dec.UseNumber()
|
||||
}
|
||||
// JSON Decode. Special case for map type to avoid weird pointer behavior
|
||||
if c, ok := token.Claims.(MapClaims); ok {
|
||||
err = dec.Decode(&c)
|
||||
} else {
|
||||
err = dec.Decode(&claims)
|
||||
}
|
||||
// Handle decode error
|
||||
if err != nil {
|
||||
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
|
||||
}
|
||||
|
||||
// Lookup signature method
|
||||
if method, ok := token.Header["alg"].(string); ok {
|
||||
if token.Method = GetSigningMethod(method); token.Method == nil {
|
||||
return token, parts, NewValidationError("signing method (alg) is unavailable.", ValidationErrorUnverifiable)
|
||||
}
|
||||
} else {
|
||||
return token, parts, NewValidationError("signing method (alg) is unspecified.", ValidationErrorUnverifiable)
|
||||
}
|
||||
|
||||
return token, parts, nil
|
||||
}
|
101
example-project/vendor/github.com/dgrijalva/jwt-go/rsa.go
generated
vendored
Normal file
101
example-project/vendor/github.com/dgrijalva/jwt-go/rsa.go
generated
vendored
Normal file
@ -0,0 +1,101 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
)
|
||||
|
||||
// Implements the RSA family of signing methods signing methods
|
||||
// Expects *rsa.PrivateKey for signing and *rsa.PublicKey for validation
|
||||
type SigningMethodRSA struct {
|
||||
Name string
|
||||
Hash crypto.Hash
|
||||
}
|
||||
|
||||
// Specific instances for RS256 and company
|
||||
var (
|
||||
SigningMethodRS256 *SigningMethodRSA
|
||||
SigningMethodRS384 *SigningMethodRSA
|
||||
SigningMethodRS512 *SigningMethodRSA
|
||||
)
|
||||
|
||||
func init() {
|
||||
// RS256
|
||||
SigningMethodRS256 = &SigningMethodRSA{"RS256", crypto.SHA256}
|
||||
RegisterSigningMethod(SigningMethodRS256.Alg(), func() SigningMethod {
|
||||
return SigningMethodRS256
|
||||
})
|
||||
|
||||
// RS384
|
||||
SigningMethodRS384 = &SigningMethodRSA{"RS384", crypto.SHA384}
|
||||
RegisterSigningMethod(SigningMethodRS384.Alg(), func() SigningMethod {
|
||||
return SigningMethodRS384
|
||||
})
|
||||
|
||||
// RS512
|
||||
SigningMethodRS512 = &SigningMethodRSA{"RS512", crypto.SHA512}
|
||||
RegisterSigningMethod(SigningMethodRS512.Alg(), func() SigningMethod {
|
||||
return SigningMethodRS512
|
||||
})
|
||||
}
|
||||
|
||||
func (m *SigningMethodRSA) Alg() string {
|
||||
return m.Name
|
||||
}
|
||||
|
||||
// Implements the Verify method from SigningMethod
|
||||
// For this signing method, must be an *rsa.PublicKey structure.
|
||||
func (m *SigningMethodRSA) Verify(signingString, signature string, key interface{}) error {
|
||||
var err error
|
||||
|
||||
// Decode the signature
|
||||
var sig []byte
|
||||
if sig, err = DecodeSegment(signature); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rsaKey *rsa.PublicKey
|
||||
var ok bool
|
||||
|
||||
if rsaKey, ok = key.(*rsa.PublicKey); !ok {
|
||||
return ErrInvalidKeyType
|
||||
}
|
||||
|
||||
// Create hasher
|
||||
if !m.Hash.Available() {
|
||||
return ErrHashUnavailable
|
||||
}
|
||||
hasher := m.Hash.New()
|
||||
hasher.Write([]byte(signingString))
|
||||
|
||||
// Verify the signature
|
||||
return rsa.VerifyPKCS1v15(rsaKey, m.Hash, hasher.Sum(nil), sig)
|
||||
}
|
||||
|
||||
// Implements the Sign method from SigningMethod
|
||||
// For this signing method, must be an *rsa.PrivateKey structure.
|
||||
func (m *SigningMethodRSA) Sign(signingString string, key interface{}) (string, error) {
|
||||
var rsaKey *rsa.PrivateKey
|
||||
var ok bool
|
||||
|
||||
// Validate type of key
|
||||
if rsaKey, ok = key.(*rsa.PrivateKey); !ok {
|
||||
return "", ErrInvalidKey
|
||||
}
|
||||
|
||||
// Create the hasher
|
||||
if !m.Hash.Available() {
|
||||
return "", ErrHashUnavailable
|
||||
}
|
||||
|
||||
hasher := m.Hash.New()
|
||||
hasher.Write([]byte(signingString))
|
||||
|
||||
// Sign the string and return the encoded bytes
|
||||
if sigBytes, err := rsa.SignPKCS1v15(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil)); err == nil {
|
||||
return EncodeSegment(sigBytes), nil
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
}
|
126
example-project/vendor/github.com/dgrijalva/jwt-go/rsa_pss.go
generated
vendored
Normal file
126
example-project/vendor/github.com/dgrijalva/jwt-go/rsa_pss.go
generated
vendored
Normal file
@ -0,0 +1,126 @@
|
||||
// +build go1.4
|
||||
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
)
|
||||
|
||||
// Implements the RSAPSS family of signing methods signing methods
|
||||
type SigningMethodRSAPSS struct {
|
||||
*SigningMethodRSA
|
||||
Options *rsa.PSSOptions
|
||||
}
|
||||
|
||||
// Specific instances for RS/PS and company
|
||||
var (
|
||||
SigningMethodPS256 *SigningMethodRSAPSS
|
||||
SigningMethodPS384 *SigningMethodRSAPSS
|
||||
SigningMethodPS512 *SigningMethodRSAPSS
|
||||
)
|
||||
|
||||
func init() {
|
||||
// PS256
|
||||
SigningMethodPS256 = &SigningMethodRSAPSS{
|
||||
&SigningMethodRSA{
|
||||
Name: "PS256",
|
||||
Hash: crypto.SHA256,
|
||||
},
|
||||
&rsa.PSSOptions{
|
||||
SaltLength: rsa.PSSSaltLengthAuto,
|
||||
Hash: crypto.SHA256,
|
||||
},
|
||||
}
|
||||
RegisterSigningMethod(SigningMethodPS256.Alg(), func() SigningMethod {
|
||||
return SigningMethodPS256
|
||||
})
|
||||
|
||||
// PS384
|
||||
SigningMethodPS384 = &SigningMethodRSAPSS{
|
||||
&SigningMethodRSA{
|
||||
Name: "PS384",
|
||||
Hash: crypto.SHA384,
|
||||
},
|
||||
&rsa.PSSOptions{
|
||||
SaltLength: rsa.PSSSaltLengthAuto,
|
||||
Hash: crypto.SHA384,
|
||||
},
|
||||
}
|
||||
RegisterSigningMethod(SigningMethodPS384.Alg(), func() SigningMethod {
|
||||
return SigningMethodPS384
|
||||
})
|
||||
|
||||
// PS512
|
||||
SigningMethodPS512 = &SigningMethodRSAPSS{
|
||||
&SigningMethodRSA{
|
||||
Name: "PS512",
|
||||
Hash: crypto.SHA512,
|
||||
},
|
||||
&rsa.PSSOptions{
|
||||
SaltLength: rsa.PSSSaltLengthAuto,
|
||||
Hash: crypto.SHA512,
|
||||
},
|
||||
}
|
||||
RegisterSigningMethod(SigningMethodPS512.Alg(), func() SigningMethod {
|
||||
return SigningMethodPS512
|
||||
})
|
||||
}
|
||||
|
||||
// Implements the Verify method from SigningMethod
|
||||
// For this verify method, key must be an rsa.PublicKey struct
|
||||
func (m *SigningMethodRSAPSS) Verify(signingString, signature string, key interface{}) error {
|
||||
var err error
|
||||
|
||||
// Decode the signature
|
||||
var sig []byte
|
||||
if sig, err = DecodeSegment(signature); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rsaKey *rsa.PublicKey
|
||||
switch k := key.(type) {
|
||||
case *rsa.PublicKey:
|
||||
rsaKey = k
|
||||
default:
|
||||
return ErrInvalidKey
|
||||
}
|
||||
|
||||
// Create hasher
|
||||
if !m.Hash.Available() {
|
||||
return ErrHashUnavailable
|
||||
}
|
||||
hasher := m.Hash.New()
|
||||
hasher.Write([]byte(signingString))
|
||||
|
||||
return rsa.VerifyPSS(rsaKey, m.Hash, hasher.Sum(nil), sig, m.Options)
|
||||
}
|
||||
|
||||
// Implements the Sign method from SigningMethod
|
||||
// For this signing method, key must be an rsa.PrivateKey struct
|
||||
func (m *SigningMethodRSAPSS) Sign(signingString string, key interface{}) (string, error) {
|
||||
var rsaKey *rsa.PrivateKey
|
||||
|
||||
switch k := key.(type) {
|
||||
case *rsa.PrivateKey:
|
||||
rsaKey = k
|
||||
default:
|
||||
return "", ErrInvalidKeyType
|
||||
}
|
||||
|
||||
// Create the hasher
|
||||
if !m.Hash.Available() {
|
||||
return "", ErrHashUnavailable
|
||||
}
|
||||
|
||||
hasher := m.Hash.New()
|
||||
hasher.Write([]byte(signingString))
|
||||
|
||||
// Sign the string and return the encoded bytes
|
||||
if sigBytes, err := rsa.SignPSS(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil), m.Options); err == nil {
|
||||
return EncodeSegment(sigBytes), nil
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
}
|
101
example-project/vendor/github.com/dgrijalva/jwt-go/rsa_utils.go
generated
vendored
Normal file
101
example-project/vendor/github.com/dgrijalva/jwt-go/rsa_utils.go
generated
vendored
Normal file
@ -0,0 +1,101 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrKeyMustBePEMEncoded = errors.New("Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key")
|
||||
ErrNotRSAPrivateKey = errors.New("Key is not a valid RSA private key")
|
||||
ErrNotRSAPublicKey = errors.New("Key is not a valid RSA public key")
|
||||
)
|
||||
|
||||
// Parse PEM encoded PKCS1 or PKCS8 private key
|
||||
func ParseRSAPrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) {
|
||||
var err error
|
||||
|
||||
// Parse PEM block
|
||||
var block *pem.Block
|
||||
if block, _ = pem.Decode(key); block == nil {
|
||||
return nil, ErrKeyMustBePEMEncoded
|
||||
}
|
||||
|
||||
var parsedKey interface{}
|
||||
if parsedKey, err = x509.ParsePKCS1PrivateKey(block.Bytes); err != nil {
|
||||
if parsedKey, err = x509.ParsePKCS8PrivateKey(block.Bytes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var pkey *rsa.PrivateKey
|
||||
var ok bool
|
||||
if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok {
|
||||
return nil, ErrNotRSAPrivateKey
|
||||
}
|
||||
|
||||
return pkey, nil
|
||||
}
|
||||
|
||||
// Parse PEM encoded PKCS1 or PKCS8 private key protected with password
|
||||
func ParseRSAPrivateKeyFromPEMWithPassword(key []byte, password string) (*rsa.PrivateKey, error) {
|
||||
var err error
|
||||
|
||||
// Parse PEM block
|
||||
var block *pem.Block
|
||||
if block, _ = pem.Decode(key); block == nil {
|
||||
return nil, ErrKeyMustBePEMEncoded
|
||||
}
|
||||
|
||||
var parsedKey interface{}
|
||||
|
||||
var blockDecrypted []byte
|
||||
if blockDecrypted, err = x509.DecryptPEMBlock(block, []byte(password)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if parsedKey, err = x509.ParsePKCS1PrivateKey(blockDecrypted); err != nil {
|
||||
if parsedKey, err = x509.ParsePKCS8PrivateKey(blockDecrypted); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var pkey *rsa.PrivateKey
|
||||
var ok bool
|
||||
if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok {
|
||||
return nil, ErrNotRSAPrivateKey
|
||||
}
|
||||
|
||||
return pkey, nil
|
||||
}
|
||||
|
||||
// Parse PEM encoded PKCS1 or PKCS8 public key
|
||||
func ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) {
|
||||
var err error
|
||||
|
||||
// Parse PEM block
|
||||
var block *pem.Block
|
||||
if block, _ = pem.Decode(key); block == nil {
|
||||
return nil, ErrKeyMustBePEMEncoded
|
||||
}
|
||||
|
||||
// Parse the key
|
||||
var parsedKey interface{}
|
||||
if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil {
|
||||
if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
|
||||
parsedKey = cert.PublicKey
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var pkey *rsa.PublicKey
|
||||
var ok bool
|
||||
if pkey, ok = parsedKey.(*rsa.PublicKey); !ok {
|
||||
return nil, ErrNotRSAPublicKey
|
||||
}
|
||||
|
||||
return pkey, nil
|
||||
}
|
35
example-project/vendor/github.com/dgrijalva/jwt-go/signing_method.go
generated
vendored
Normal file
35
example-project/vendor/github.com/dgrijalva/jwt-go/signing_method.go
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var signingMethods = map[string]func() SigningMethod{}
|
||||
var signingMethodLock = new(sync.RWMutex)
|
||||
|
||||
// Implement SigningMethod to add new methods for signing or verifying tokens.
|
||||
type SigningMethod interface {
|
||||
Verify(signingString, signature string, key interface{}) error // Returns nil if signature is valid
|
||||
Sign(signingString string, key interface{}) (string, error) // Returns encoded signature or error
|
||||
Alg() string // returns the alg identifier for this method (example: 'HS256')
|
||||
}
|
||||
|
||||
// Register the "alg" name and a factory function for signing method.
|
||||
// This is typically done during init() in the method's implementation
|
||||
func RegisterSigningMethod(alg string, f func() SigningMethod) {
|
||||
signingMethodLock.Lock()
|
||||
defer signingMethodLock.Unlock()
|
||||
|
||||
signingMethods[alg] = f
|
||||
}
|
||||
|
||||
// Get a signing method from an "alg" string
|
||||
func GetSigningMethod(alg string) (method SigningMethod) {
|
||||
signingMethodLock.RLock()
|
||||
defer signingMethodLock.RUnlock()
|
||||
|
||||
if methodF, ok := signingMethods[alg]; ok {
|
||||
method = methodF()
|
||||
}
|
||||
return
|
||||
}
|
108
example-project/vendor/github.com/dgrijalva/jwt-go/token.go
generated
vendored
Normal file
108
example-project/vendor/github.com/dgrijalva/jwt-go/token.go
generated
vendored
Normal file
@ -0,0 +1,108 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TimeFunc provides the current time when parsing token to validate "exp" claim (expiration time).
|
||||
// You can override it to use another time value. This is useful for testing or if your
|
||||
// server uses a different time zone than your tokens.
|
||||
var TimeFunc = time.Now
|
||||
|
||||
// Parse methods use this callback function to supply
|
||||
// the key for verification. The function receives the parsed,
|
||||
// but unverified Token. This allows you to use properties in the
|
||||
// Header of the token (such as `kid`) to identify which key to use.
|
||||
type Keyfunc func(*Token) (interface{}, error)
|
||||
|
||||
// A JWT Token. Different fields will be used depending on whether you're
|
||||
// creating or parsing/verifying a token.
|
||||
type Token struct {
|
||||
Raw string // The raw token. Populated when you Parse a token
|
||||
Method SigningMethod // The signing method used or to be used
|
||||
Header map[string]interface{} // The first segment of the token
|
||||
Claims Claims // The second segment of the token
|
||||
Signature string // The third segment of the token. Populated when you Parse a token
|
||||
Valid bool // Is the token valid? Populated when you Parse/Verify a token
|
||||
}
|
||||
|
||||
// Create a new Token. Takes a signing method
|
||||
func New(method SigningMethod) *Token {
|
||||
return NewWithClaims(method, MapClaims{})
|
||||
}
|
||||
|
||||
func NewWithClaims(method SigningMethod, claims Claims) *Token {
|
||||
return &Token{
|
||||
Header: map[string]interface{}{
|
||||
"typ": "JWT",
|
||||
"alg": method.Alg(),
|
||||
},
|
||||
Claims: claims,
|
||||
Method: method,
|
||||
}
|
||||
}
|
||||
|
||||
// Get the complete, signed token
|
||||
func (t *Token) SignedString(key interface{}) (string, error) {
|
||||
var sig, sstr string
|
||||
var err error
|
||||
if sstr, err = t.SigningString(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if sig, err = t.Method.Sign(sstr, key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.Join([]string{sstr, sig}, "."), nil
|
||||
}
|
||||
|
||||
// Generate the signing string. This is the
|
||||
// most expensive part of the whole deal. Unless you
|
||||
// need this for something special, just go straight for
|
||||
// the SignedString.
|
||||
func (t *Token) SigningString() (string, error) {
|
||||
var err error
|
||||
parts := make([]string, 2)
|
||||
for i, _ := range parts {
|
||||
var jsonValue []byte
|
||||
if i == 0 {
|
||||
if jsonValue, err = json.Marshal(t.Header); err != nil {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
if jsonValue, err = json.Marshal(t.Claims); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
parts[i] = EncodeSegment(jsonValue)
|
||||
}
|
||||
return strings.Join(parts, "."), nil
|
||||
}
|
||||
|
||||
// Parse, validate, and return a token.
|
||||
// keyFunc will receive the parsed token and should return the key for validating.
|
||||
// If everything is kosher, err will be nil
|
||||
func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
||||
return new(Parser).Parse(tokenString, keyFunc)
|
||||
}
|
||||
|
||||
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
|
||||
return new(Parser).ParseWithClaims(tokenString, claims, keyFunc)
|
||||
}
|
||||
|
||||
// Encode JWT specific base64url encoding with padding stripped
|
||||
func EncodeSegment(seg []byte) string {
|
||||
return strings.TrimRight(base64.URLEncoding.EncodeToString(seg), "=")
|
||||
}
|
||||
|
||||
// Decode JWT specific base64url encoding with padding stripped
|
||||
func DecodeSegment(seg string) ([]byte, error) {
|
||||
if l := len(seg) % 4; l > 0 {
|
||||
seg += strings.Repeat("=", 4-l)
|
||||
}
|
||||
|
||||
return base64.URLEncoding.DecodeString(seg)
|
||||
}
|
23
example-project/vendor/github.com/dimfeld/httptreemux/.gitignore
generated
vendored
Normal file
23
example-project/vendor/github.com/dimfeld/httptreemux/.gitignore
generated
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
14
example-project/vendor/github.com/dimfeld/httptreemux/.travis.yml
generated
vendored
Normal file
14
example-project/vendor/github.com/dimfeld/httptreemux/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
language: go
|
||||
|
||||
gobuild_args: "-v -race"
|
||||
go:
|
||||
- 1.5
|
||||
- 1.6
|
||||
- 1.7
|
||||
- 1.8
|
||||
- 1.9
|
||||
- tip
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- go: tip
|
21
example-project/vendor/github.com/dimfeld/httptreemux/LICENSE
generated
vendored
Normal file
21
example-project/vendor/github.com/dimfeld/httptreemux/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014,2015 Daniel Imfeld
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
240
example-project/vendor/github.com/dimfeld/httptreemux/README.md
generated
vendored
Normal file
240
example-project/vendor/github.com/dimfeld/httptreemux/README.md
generated
vendored
Normal file
@ -0,0 +1,240 @@
|
||||
httptreemux [](https://travis-ci.org/dimfeld/httptreemux) [](https://godoc.org/github.com/dimfeld/httptreemux)
|
||||
===========
|
||||
|
||||
High-speed, flexible, tree-based HTTP router for Go.
|
||||
|
||||
This is inspired by [Julien Schmidt's httprouter](https://www.github.com/julienschmidt/httprouter), in that it uses a patricia tree, but the implementation is rather different. Specifically, the routing rules are relaxed so that a single path segment may be a wildcard in one route and a static token in another. This gives a nice combination of high performance with a lot of convenience in designing the routing patterns. In [benchmarks](https://github.com/julienschmidt/go-http-routing-benchmark), httptreemux is close to, but slightly slower than, httprouter.
|
||||
|
||||
Release notes may be found using the [Github releases tab](https://github.com/dimfeld/httptreemux/releases). Version numbers are compatible with the [Semantic Versioning 2.0.0](http://semver.org/) convention, and a new release is made after every change to the code.
|
||||
|
||||
## Why?
|
||||
There are a lot of good routers out there. But looking at the ones that were really lightweight, I couldn't quite get something that fit with the route patterns I wanted. The code itself is simple enough, so I spent an evening writing this.
|
||||
|
||||
## Handler
|
||||
The handler is a simple function with the prototype `func(w http.ResponseWriter, r *http.Request, params map[string]string)`. The params argument contains the parameters parsed from wildcards and catch-alls in the URL, as described below. This type is aliased as httptreemux.HandlerFunc.
|
||||
|
||||
### Using http.HandlerFunc
|
||||
Due to the inclusion of the [context](https://godoc.org/context) package as of Go 1.7, `httptreemux` now supports handlers of type [http.HandlerFunc](https://godoc.org/net/http#HandlerFunc). There are two ways to enable this support.
|
||||
|
||||
#### Adapting an Existing Router
|
||||
|
||||
The `UsingContext` method will wrap the router or group in a new group at the same path, but adapted for use with `context` and `http.HandlerFunc`.
|
||||
|
||||
```go
|
||||
router := httptreemux.New()
|
||||
|
||||
group := router.NewGroup("/api")
|
||||
group.GET("/v1/:id", func(w http.ResponseWriter, r *http.Request, params map[string]string) {
|
||||
id := params["id"]
|
||||
fmt.Fprintf(w, "GET /api/v1/%s", id)
|
||||
})
|
||||
|
||||
// UsingContext returns a version of the router or group with context support.
|
||||
ctxGroup := group.UsingContext() // sibling to 'group' node in tree
|
||||
ctxGroup.GET("/v2/:id", func(w http.ResponseWriter, r *http.Request) {
|
||||
params := httptreemux.ContextParams(r.Context())
|
||||
id := params["id"]
|
||||
fmt.Fprintf(w, "GET /api/v2/%s", id)
|
||||
})
|
||||
|
||||
http.ListenAndServe(":8080", router)
|
||||
```
|
||||
|
||||
#### New Router with Context Support
|
||||
|
||||
The `NewContextMux` function returns a router preconfigured for use with `context` and `http.HandlerFunc`.
|
||||
|
||||
```go
|
||||
router := httptreemux.NewContextMux()
|
||||
|
||||
router.GET("/:page", func(w http.ResponseWriter, r *http.Request) {
|
||||
params := httptreemux.ContextParams(r.Context())
|
||||
fmt.Fprintf(w, "GET /%s", params["page"])
|
||||
})
|
||||
|
||||
group := tree.NewGroup("/api")
|
||||
group.GET("/v1/:id", func(w http.ResponseWriter, r *http.Request) {
|
||||
params := httptreemux.ContextParams(r.Context())
|
||||
id := params["id"]
|
||||
fmt.Fprintf(w, "GET /api/v1/%s", id)
|
||||
})
|
||||
|
||||
http.ListenAndServe(":8080", router)
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Routing Rules
|
||||
The syntax here is also modeled after httprouter. Each variable in a path may match on one segment only, except for an optional catch-all variable at the end of the URL.
|
||||
|
||||
Some examples of valid URL patterns are:
|
||||
* `/post/all`
|
||||
* `/post/:postid`
|
||||
* `/post/:postid/page/:page`
|
||||
* `/post/:postid/:page`
|
||||
* `/images/*path`
|
||||
* `/favicon.ico`
|
||||
* `/:year/:month/`
|
||||
* `/:year/:month/:post`
|
||||
* `/:page`
|
||||
|
||||
Note that all of the above URL patterns may exist concurrently in the router.
|
||||
|
||||
Path elements starting with `:` indicate a wildcard in the path. A wildcard will only match on a single path segment. That is, the pattern `/post/:postid` will match on `/post/1` or `/post/1/`, but not `/post/1/2`.
|
||||
|
||||
A path element starting with `*` is a catch-all, whose value will be a string containing all text in the URL matched by the wildcards. For example, with a pattern of `/images/*path` and a requested URL `images/abc/def`, path would contain `abc/def`.
|
||||
|
||||
#### Using : and * in routing patterns
|
||||
|
||||
The characters `:` and `*` can be used at the beginning of a path segment by escaping them with a backslash. A double backslash at the beginning of a segment is interpreted as a single backslash. These escapes are only checked at the very beginning of a path segment; they are not necessary or processed elsewhere in a token.
|
||||
|
||||
```go
|
||||
router.GET("/foo/\\*starToken", handler) // matches /foo/*starToken
|
||||
router.GET("/foo/star*inTheMiddle", handler) // matches /foo/star*inTheMiddle
|
||||
router.GET("/foo/starBackslash\\*", handler) // matches /foo/starBackslash\*
|
||||
router.GET("/foo/\\\\*backslashWithStar") // matches /foo/\*backslashWithStar
|
||||
```
|
||||
|
||||
### Routing Groups
|
||||
Lets you create a new group of routes with a given path prefix. Makes it easier to create clusters of paths like:
|
||||
* `/api/v1/foo`
|
||||
* `/api/v1/bar`
|
||||
|
||||
To use this you do:
|
||||
```go
|
||||
router = httptreemux.New()
|
||||
api := router.NewGroup("/api/v1")
|
||||
api.GET("/foo", fooHandler) // becomes /api/v1/foo
|
||||
api.GET("/bar", barHandler) // becomes /api/v1/bar
|
||||
```
|
||||
|
||||
### Routing Priority
|
||||
The priority rules in the router are simple.
|
||||
|
||||
1. Static path segments take the highest priority. If a segment and its subtree are able to match the URL, that match is returned.
|
||||
2. Wildcards take second priority. For a particular wildcard to match, that wildcard and its subtree must match the URL.
|
||||
3. Finally, a catch-all rule will match when the earlier path segments have matched, and none of the static or wildcard conditions have matched. Catch-all rules must be at the end of a pattern.
|
||||
|
||||
So with the following patterns adapted from [simpleblog](https://www.github.com/dimfeld/simpleblog), we'll see certain matches:
|
||||
```go
|
||||
router = httptreemux.New()
|
||||
router.GET("/:page", pageHandler)
|
||||
router.GET("/:year/:month/:post", postHandler)
|
||||
router.GET("/:year/:month", archiveHandler)
|
||||
router.GET("/images/*path", staticHandler)
|
||||
router.GET("/favicon.ico", staticHandler)
|
||||
```
|
||||
|
||||
#### Example scenarios
|
||||
|
||||
- `/abc` will match `/:page`
|
||||
- `/2014/05` will match `/:year/:month`
|
||||
- `/2014/05/really-great-blog-post` will match `/:year/:month/:post`
|
||||
- `/images/CoolImage.gif` will match `/images/*path`
|
||||
- `/images/2014/05/MayImage.jpg` will also match `/images/*path`, with all the text after `/images` stored in the variable path.
|
||||
- `/favicon.ico` will match `/favicon.ico`
|
||||
|
||||
### Special Method Behavior
|
||||
If TreeMux.HeadCanUseGet is set to true, the router will call the GET handler for a pattern when a HEAD request is processed, if no HEAD handler has been added for that pattern. This behavior is enabled by default.
|
||||
|
||||
Go's http.ServeContent and related functions already handle the HEAD method correctly by sending only the header, so in most cases your handlers will not need any special cases for it.
|
||||
|
||||
By default TreeMux.OptionsHandler is a null handler that doesn't affect your routing. If you set the handler, it will be called on OPTIONS requests to a path already registered by another method. If you set a path specific handler by using `router.OPTIONS`, it will override the global Options Handler for that path.
|
||||
|
||||
### Trailing Slashes
|
||||
The router has special handling for paths with trailing slashes. If a pattern is added to the router with a trailing slash, any matches on that pattern without a trailing slash will be redirected to the version with the slash. If a pattern does not have a trailing slash, matches on that pattern with a trailing slash will be redirected to the version without.
|
||||
|
||||
The trailing slash flag is only stored once for a pattern. That is, if a pattern is added for a method with a trailing slash, all other methods for that pattern will also be considered to have a trailing slash, regardless of whether or not it is specified for those methods too.
|
||||
However this behavior can be turned off by setting TreeMux.RedirectTrailingSlash to false. By default it is set to true.
|
||||
|
||||
One exception to this rule is catch-all patterns. By default, trailing slash redirection is disabled on catch-all patterns, since the structure of the entire URL and the desired patterns can not be predicted. If trailing slash removal is desired on catch-all patterns, set TreeMux.RemoveCatchAllTrailingSlash to true.
|
||||
|
||||
```go
|
||||
router = httptreemux.New()
|
||||
router.GET("/about", pageHandler)
|
||||
router.GET("/posts/", postIndexHandler)
|
||||
router.POST("/posts", postFormHandler)
|
||||
|
||||
GET /about will match normally.
|
||||
GET /about/ will redirect to /about.
|
||||
GET /posts will redirect to /posts/.
|
||||
GET /posts/ will match normally.
|
||||
POST /posts will redirect to /posts/, because the GET method used a trailing slash.
|
||||
```
|
||||
|
||||
### Custom Redirects
|
||||
|
||||
RedirectBehavior sets the behavior when the router redirects the request to the canonical version of the requested URL using RedirectTrailingSlash or RedirectClean. The default behavior is to return a 301 status, redirecting the browser to the version of the URL that matches the given pattern.
|
||||
|
||||
These are the values accepted for RedirectBehavior. You may also add these values to the RedirectMethodBehavior map to define custom per-method redirect behavior.
|
||||
|
||||
* Redirect301 - HTTP 301 Moved Permanently; this is the default.
|
||||
* Redirect307 - HTTP/1.1 Temporary Redirect
|
||||
* Redirect308 - RFC7538 Permanent Redirect
|
||||
* UseHandler - Don't redirect to the canonical path. Just call the handler instead.
|
||||
|
||||
#### Rationale/Usage
|
||||
On a POST request, most browsers that receive a 301 will submit a GET request to the redirected URL, meaning that any data will likely be lost. If you want to handle and avoid this behavior, you may use Redirect307, which causes most browsers to resubmit the request using the original method and request body.
|
||||
|
||||
Since 307 is supposed to be a temporary redirect, the new 308 status code has been proposed, which is treated the same, except it indicates correctly that the redirection is permanent. The big caveat here is that the RFC is relatively recent, and older or non-compliant browsers will not handle it. Therefore its use is not recommended unless you really know what you're doing.
|
||||
|
||||
Finally, the UseHandler value will simply call the handler function for the pattern, without redirecting to the canonical version of the URL.
|
||||
|
||||
### RequestURI vs. URL.Path
|
||||
|
||||
#### Escaped Slashes
|
||||
Go automatically processes escaped characters in a URL, converting + to a space and %XX to the corresponding character. This can present issues when the URL contains a %2f, which is unescaped to '/'. This isn't an issue for most applications, but it will prevent the router from correctly matching paths and wildcards.
|
||||
|
||||
For example, the pattern `/post/:post` would not match on `/post/abc%2fdef`, which is unescaped to `/post/abc/def`. The desired behavior is that it matches, and the `post` wildcard is set to `abc/def`.
|
||||
|
||||
Therefore, this router defaults to using the raw URL, stored in the Request.RequestURI variable. Matching wildcards and catch-alls are then unescaped, to give the desired behavior.
|
||||
|
||||
TL;DR: If a requested URL contains a %2f, this router will still do the right thing. Some Go HTTP routers may not due to [Go issue 3659](https://code.google.com/p/go/issues/detail?id=3659).
|
||||
|
||||
#### Escaped Characters
|
||||
|
||||
As mentioned above, characters in the URL are not unescaped when using RequestURI to determine the matched route. If this is a problem for you and you are unable to switch to URL.Path for the above reasons, you may set `router.EscapeAddedRoutes` to `true`. This option will run each added route through the `URL.EscapedPath` function, and add an additional route if the escaped version differs.
|
||||
|
||||
#### http Package Utility Functions
|
||||
|
||||
Although using RequestURI avoids the issue described above, certain utility functions such as `http.StripPrefix` modify URL.Path, and expect that the underlying router is using that field to make its decision. If you are using some of these functions, set the router's `PathSource` member to `URLPath`. This will give up the proper handling of escaped slashes described above, while allowing the router to work properly with these utility functions.
|
||||
|
||||
## Concurrency
|
||||
|
||||
The router contains an `RWMutex` that arbitrates access to the tree. This allows routes to be safely added from multiple goroutines at once.
|
||||
|
||||
No concurrency controls are needed when only reading from the tree, so the default behavior is to not use the `RWMutex` when serving a request. This avoids a theoretical slowdown under high-usage scenarios from competing atomic integer operations inside the `RWMutex`. If your application adds routes to the router after it has begun serving requests, you should avoid potential race conditions by setting `router.SafeAddRoutesWhileRunning` to `true` to use the `RWMutex` when serving requests.
|
||||
|
||||
## Error Handlers
|
||||
|
||||
### NotFoundHandler
|
||||
TreeMux.NotFoundHandler can be set to provide custom 404-error handling. The default implementation is Go's `http.NotFound` function.
|
||||
|
||||
### MethodNotAllowedHandler
|
||||
If a pattern matches, but the pattern does not have an associated handler for the requested method, the router calls the MethodNotAllowedHandler. The default
|
||||
version of this handler just writes the status code `http.StatusMethodNotAllowed` and sets the response header's `Allowed` field appropriately.
|
||||
|
||||
### Panic Handling
|
||||
TreeMux.PanicHandler can be set to provide custom panic handling. The `SimplePanicHandler` just writes the status code `http.StatusInternalServerError`. The function `ShowErrorsPanicHandler`, adapted from [gocraft/web](https://github.com/gocraft/web), will print panic errors to the browser in an easily-readable format.
|
||||
|
||||
## Unexpected Differences from Other Routers
|
||||
|
||||
This router is intentionally light on features in the name of simplicity and
|
||||
performance. When coming from another router that does heavier processing behind
|
||||
the scenes, you may encounter some unexpected behavior. This list is by no means
|
||||
exhaustive, but covers some nonobvious cases that users have encountered.
|
||||
|
||||
### gorilla/pat query string modifications
|
||||
|
||||
When matching on parameters in a route, the `gorilla/pat` router will modify
|
||||
`Request.URL.RawQuery` to make it appear like the parameters were in the
|
||||
query string. `httptreemux` does not do this. See [Issue #26](https://github.com/dimfeld/httptreemux/issues/26) for more details and a
|
||||
code snippet that can perform this transformation for you, should you want it.
|
||||
|
||||
## Middleware
|
||||
This package provides no middleware. But there are a lot of great options out there and it's pretty easy to write your own.
|
||||
|
||||
# Acknowledgements
|
||||
|
||||
* Inspiration from Julien Schmidt's [httprouter](https://github.com/julienschmidt/httprouter)
|
||||
* Show Errors panic handler from [gocraft/web](https://github.com/gocraft/web)
|
123
example-project/vendor/github.com/dimfeld/httptreemux/context.go
generated
vendored
Normal file
123
example-project/vendor/github.com/dimfeld/httptreemux/context.go
generated
vendored
Normal file
@ -0,0 +1,123 @@
|
||||
// +build go1.7
|
||||
|
||||
package httptreemux
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// ContextGroup is a wrapper around Group, with the purpose of mimicking its API, but with the use of http.HandlerFunc-based handlers.
|
||||
// Instead of passing a parameter map via the handler (i.e. httptreemux.HandlerFunc), the path parameters are accessed via the request
|
||||
// object's context.
|
||||
type ContextGroup struct {
|
||||
group *Group
|
||||
}
|
||||
|
||||
// UsingContext wraps the receiver to return a new instance of a ContextGroup.
|
||||
// The returned ContextGroup is a sibling to its wrapped Group, within the parent TreeMux.
|
||||
// The choice of using a *Group as the receiver, as opposed to a function parameter, allows chaining
|
||||
// while method calls between a TreeMux, Group, and ContextGroup. For example:
|
||||
//
|
||||
// tree := httptreemux.New()
|
||||
// group := tree.NewGroup("/api")
|
||||
//
|
||||
// group.GET("/v1", func(w http.ResponseWriter, r *http.Request, params map[string]string) {
|
||||
// w.Write([]byte(`GET /api/v1`))
|
||||
// })
|
||||
//
|
||||
// group.UsingContext().GET("/v2", func(w http.ResponseWriter, r *http.Request) {
|
||||
// w.Write([]byte(`GET /api/v2`))
|
||||
// })
|
||||
//
|
||||
// http.ListenAndServe(":8080", tree)
|
||||
//
|
||||
func (g *Group) UsingContext() *ContextGroup {
|
||||
return &ContextGroup{g}
|
||||
}
|
||||
|
||||
// NewContextGroup adds a child context group to its path.
|
||||
func (cg *ContextGroup) NewContextGroup(path string) *ContextGroup {
|
||||
return &ContextGroup{cg.group.NewGroup(path)}
|
||||
}
|
||||
|
||||
func (cg *ContextGroup) NewGroup(path string) *ContextGroup {
|
||||
return cg.NewContextGroup(path)
|
||||
}
|
||||
|
||||
// Handle allows handling HTTP requests via an http.HandlerFunc, as opposed to an httptreemux.HandlerFunc.
|
||||
// Any parameters from the request URL are stored in a map[string]string in the request's context.
|
||||
func (cg *ContextGroup) Handle(method, path string, handler http.HandlerFunc) {
|
||||
cg.group.Handle(method, path, func(w http.ResponseWriter, r *http.Request, params map[string]string) {
|
||||
if params != nil {
|
||||
r = r.WithContext(AddParamsToContext(r.Context(), params))
|
||||
}
|
||||
handler(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// Handler allows handling HTTP requests via an http.Handler interface, as opposed to an httptreemux.HandlerFunc.
|
||||
// Any parameters from the request URL are stored in a map[string]string in the request's context.
|
||||
func (cg *ContextGroup) Handler(method, path string, handler http.Handler) {
|
||||
cg.group.Handle(method, path, func(w http.ResponseWriter, r *http.Request, params map[string]string) {
|
||||
if params != nil {
|
||||
r = r.WithContext(AddParamsToContext(r.Context(), params))
|
||||
}
|
||||
handler.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// GET is convenience method for handling GET requests on a context group.
|
||||
func (cg *ContextGroup) GET(path string, handler http.HandlerFunc) {
|
||||
cg.Handle("GET", path, handler)
|
||||
}
|
||||
|
||||
// POST is convenience method for handling POST requests on a context group.
|
||||
func (cg *ContextGroup) POST(path string, handler http.HandlerFunc) {
|
||||
cg.Handle("POST", path, handler)
|
||||
}
|
||||
|
||||
// PUT is convenience method for handling PUT requests on a context group.
|
||||
func (cg *ContextGroup) PUT(path string, handler http.HandlerFunc) {
|
||||
cg.Handle("PUT", path, handler)
|
||||
}
|
||||
|
||||
// DELETE is convenience method for handling DELETE requests on a context group.
|
||||
func (cg *ContextGroup) DELETE(path string, handler http.HandlerFunc) {
|
||||
cg.Handle("DELETE", path, handler)
|
||||
}
|
||||
|
||||
// PATCH is convenience method for handling PATCH requests on a context group.
|
||||
func (cg *ContextGroup) PATCH(path string, handler http.HandlerFunc) {
|
||||
cg.Handle("PATCH", path, handler)
|
||||
}
|
||||
|
||||
// HEAD is convenience method for handling HEAD requests on a context group.
|
||||
func (cg *ContextGroup) HEAD(path string, handler http.HandlerFunc) {
|
||||
cg.Handle("HEAD", path, handler)
|
||||
}
|
||||
|
||||
// OPTIONS is convenience method for handling OPTIONS requests on a context group.
|
||||
func (cg *ContextGroup) OPTIONS(path string, handler http.HandlerFunc) {
|
||||
cg.Handle("OPTIONS", path, handler)
|
||||
}
|
||||
|
||||
// ContextParams returns the params map associated with the given context if one exists. Otherwise, an empty map is returned.
|
||||
func ContextParams(ctx context.Context) map[string]string {
|
||||
if p, ok := ctx.Value(paramsContextKey).(map[string]string); ok {
|
||||
return p
|
||||
}
|
||||
return map[string]string{}
|
||||
}
|
||||
|
||||
// AddParamsToContext inserts a parameters map into a context using
|
||||
// the package's internal context key. Clients of this package should
|
||||
// really only use this for unit tests.
|
||||
func AddParamsToContext(ctx context.Context, params map[string]string) context.Context {
|
||||
return context.WithValue(ctx, paramsContextKey, params)
|
||||
}
|
||||
|
||||
type contextKey int
|
||||
|
||||
// paramsContextKey is used to retrieve a path's params map from a request's context.
|
||||
const paramsContextKey contextKey = 0
|
195
example-project/vendor/github.com/dimfeld/httptreemux/group.go
generated
vendored
Normal file
195
example-project/vendor/github.com/dimfeld/httptreemux/group.go
generated
vendored
Normal file
@ -0,0 +1,195 @@
|
||||
package httptreemux
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Group struct {
|
||||
path string
|
||||
mux *TreeMux
|
||||
}
|
||||
|
||||
// Add a sub-group to this group
|
||||
func (g *Group) NewGroup(path string) *Group {
|
||||
if len(path) < 1 {
|
||||
panic("Group path must not be empty")
|
||||
}
|
||||
|
||||
checkPath(path)
|
||||
path = g.path + path
|
||||
//Don't want trailing slash as all sub-paths start with slash
|
||||
if path[len(path)-1] == '/' {
|
||||
path = path[:len(path)-1]
|
||||
}
|
||||
return &Group{path, g.mux}
|
||||
}
|
||||
|
||||
// Path elements starting with : indicate a wildcard in the path. A wildcard will only match on a
|
||||
// single path segment. That is, the pattern `/post/:postid` will match on `/post/1` or `/post/1/`,
|
||||
// but not `/post/1/2`.
|
||||
//
|
||||
// A path element starting with * is a catch-all, whose value will be a string containing all text
|
||||
// in the URL matched by the wildcards. For example, with a pattern of `/images/*path` and a
|
||||
// requested URL `images/abc/def`, path would contain `abc/def`.
|
||||
//
|
||||
// # Routing Rule Priority
|
||||
//
|
||||
// The priority rules in the router are simple.
|
||||
//
|
||||
// 1. Static path segments take the highest priority. If a segment and its subtree are able to match the URL, that match is returned.
|
||||
//
|
||||
// 2. Wildcards take second priority. For a particular wildcard to match, that wildcard and its subtree must match the URL.
|
||||
//
|
||||
// 3. Finally, a catch-all rule will match when the earlier path segments have matched, and none of the static or wildcard conditions have matched. Catch-all rules must be at the end of a pattern.
|
||||
//
|
||||
// So with the following patterns, we'll see certain matches:
|
||||
// router = httptreemux.New()
|
||||
// router.GET("/:page", pageHandler)
|
||||
// router.GET("/:year/:month/:post", postHandler)
|
||||
// router.GET("/:year/:month", archiveHandler)
|
||||
// router.GET("/images/*path", staticHandler)
|
||||
// router.GET("/favicon.ico", staticHandler)
|
||||
//
|
||||
// /abc will match /:page
|
||||
// /2014/05 will match /:year/:month
|
||||
// /2014/05/really-great-blog-post will match /:year/:month/:post
|
||||
// /images/CoolImage.gif will match /images/*path
|
||||
// /images/2014/05/MayImage.jpg will also match /images/*path, with all the text after /images stored in the variable path.
|
||||
// /favicon.ico will match /favicon.ico
|
||||
//
|
||||
// # Trailing Slashes
|
||||
//
|
||||
// The router has special handling for paths with trailing slashes. If a pattern is added to the
|
||||
// router with a trailing slash, any matches on that pattern without a trailing slash will be
|
||||
// redirected to the version with the slash. If a pattern does not have a trailing slash, matches on
|
||||
// that pattern with a trailing slash will be redirected to the version without.
|
||||
//
|
||||
// The trailing slash flag is only stored once for a pattern. That is, if a pattern is added for a
|
||||
// method with a trailing slash, all other methods for that pattern will also be considered to have a
|
||||
// trailing slash, regardless of whether or not it is specified for those methods too.
|
||||
//
|
||||
// This behavior can be turned off by setting TreeMux.RedirectTrailingSlash to false. By
|
||||
// default it is set to true. The specifics of the redirect depend on RedirectBehavior.
|
||||
//
|
||||
// One exception to this rule is catch-all patterns. By default, trailing slash redirection is
|
||||
// disabled on catch-all patterns, since the structure of the entire URL and the desired patterns
|
||||
// can not be predicted. If trailing slash removal is desired on catch-all patterns, set
|
||||
// TreeMux.RemoveCatchAllTrailingSlash to true.
|
||||
//
|
||||
// router = httptreemux.New()
|
||||
// router.GET("/about", pageHandler)
|
||||
// router.GET("/posts/", postIndexHandler)
|
||||
// router.POST("/posts", postFormHandler)
|
||||
//
|
||||
// GET /about will match normally.
|
||||
// GET /about/ will redirect to /about.
|
||||
// GET /posts will redirect to /posts/.
|
||||
// GET /posts/ will match normally.
|
||||
// POST /posts will redirect to /posts/, because the GET method used a trailing slash.
|
||||
func (g *Group) Handle(method string, path string, handler HandlerFunc) {
|
||||
g.mux.mutex.Lock()
|
||||
defer g.mux.mutex.Unlock()
|
||||
|
||||
addSlash := false
|
||||
addOne := func(thePath string) {
|
||||
node := g.mux.root.addPath(thePath[1:], nil, false)
|
||||
if addSlash {
|
||||
node.addSlash = true
|
||||
}
|
||||
node.setHandler(method, handler, false)
|
||||
|
||||
if g.mux.HeadCanUseGet && method == "GET" && node.leafHandler["HEAD"] == nil {
|
||||
node.setHandler("HEAD", handler, true)
|
||||
}
|
||||
}
|
||||
|
||||
checkPath(path)
|
||||
path = g.path + path
|
||||
if len(path) == 0 {
|
||||
panic("Cannot map an empty path")
|
||||
}
|
||||
|
||||
if len(path) > 1 && path[len(path)-1] == '/' && g.mux.RedirectTrailingSlash {
|
||||
addSlash = true
|
||||
path = path[:len(path)-1]
|
||||
}
|
||||
|
||||
if g.mux.EscapeAddedRoutes {
|
||||
u, err := url.ParseRequestURI(path)
|
||||
if err != nil {
|
||||
panic("URL parsing error " + err.Error() + " on url " + path)
|
||||
}
|
||||
escapedPath := unescapeSpecial(u.String())
|
||||
|
||||
if escapedPath != path {
|
||||
addOne(escapedPath)
|
||||
}
|
||||
}
|
||||
|
||||
addOne(path)
|
||||
}
|
||||
|
||||
// Syntactic sugar for Handle("GET", path, handler)
|
||||
func (g *Group) GET(path string, handler HandlerFunc) {
|
||||
g.Handle("GET", path, handler)
|
||||
}
|
||||
|
||||
// Syntactic sugar for Handle("POST", path, handler)
|
||||
func (g *Group) POST(path string, handler HandlerFunc) {
|
||||
g.Handle("POST", path, handler)
|
||||
}
|
||||
|
||||
// Syntactic sugar for Handle("PUT", path, handler)
|
||||
func (g *Group) PUT(path string, handler HandlerFunc) {
|
||||
g.Handle("PUT", path, handler)
|
||||
}
|
||||
|
||||
// Syntactic sugar for Handle("DELETE", path, handler)
|
||||
func (g *Group) DELETE(path string, handler HandlerFunc) {
|
||||
g.Handle("DELETE", path, handler)
|
||||
}
|
||||
|
||||
// Syntactic sugar for Handle("PATCH", path, handler)
|
||||
func (g *Group) PATCH(path string, handler HandlerFunc) {
|
||||
g.Handle("PATCH", path, handler)
|
||||
}
|
||||
|
||||
// Syntactic sugar for Handle("HEAD", path, handler)
|
||||
func (g *Group) HEAD(path string, handler HandlerFunc) {
|
||||
g.Handle("HEAD", path, handler)
|
||||
}
|
||||
|
||||
// Syntactic sugar for Handle("OPTIONS", path, handler)
|
||||
func (g *Group) OPTIONS(path string, handler HandlerFunc) {
|
||||
g.Handle("OPTIONS", path, handler)
|
||||
}
|
||||
|
||||
func checkPath(path string) {
|
||||
// All non-empty paths must start with a slash
|
||||
if len(path) > 0 && path[0] != '/' {
|
||||
panic(fmt.Sprintf("Path %s must start with slash", path))
|
||||
}
|
||||
}
|
||||
|
||||
func unescapeSpecial(s string) string {
|
||||
// Look for sequences of \*, *, and \: that were escaped, and undo some of that escaping.
|
||||
|
||||
// Unescape /* since it references a wildcard token.
|
||||
s = strings.Replace(s, "/%2A", "/*", -1)
|
||||
|
||||
// Unescape /\: since it references a literal colon
|
||||
s = strings.Replace(s, "/%5C:", "/\\:", -1)
|
||||
|
||||
// Replace escaped /\\: with /\:
|
||||
s = strings.Replace(s, "/%5C%5C:", "/%5C:", -1)
|
||||
|
||||
// Replace escaped /\* with /*
|
||||
s = strings.Replace(s, "/%5C%2A", "/%2A", -1)
|
||||
|
||||
// Replace escaped /\\* with /\*
|
||||
s = strings.Replace(s, "/%5C%5C%2A", "/%5C%2A", -1)
|
||||
|
||||
return s
|
||||
}
|
211
example-project/vendor/github.com/dimfeld/httptreemux/panichandler.go
generated
vendored
Normal file
211
example-project/vendor/github.com/dimfeld/httptreemux/panichandler.go
generated
vendored
Normal file
@ -0,0 +1,211 @@
|
||||
package httptreemux
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SimplePanicHandler just returns error 500.
|
||||
func SimplePanicHandler(w http.ResponseWriter, r *http.Request, err interface{}) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// ShowErrorsPanicHandler prints a nice representation of an error to the browser.
|
||||
// This was taken from github.com/gocraft/web, which adapted it from the Traffic project.
|
||||
func ShowErrorsPanicHandler(w http.ResponseWriter, r *http.Request, err interface{}) {
|
||||
const size = 4096
|
||||
stack := make([]byte, size)
|
||||
stack = stack[:runtime.Stack(stack, false)]
|
||||
renderPrettyError(w, r, err, stack)
|
||||
}
|
||||
|
||||
func makeErrorData(r *http.Request, err interface{}, stack []byte, filePath string, line int) map[string]interface{} {
|
||||
|
||||
data := map[string]interface{}{
|
||||
"Stack": string(stack),
|
||||
"Params": r.URL.Query(),
|
||||
"Method": r.Method,
|
||||
"FilePath": filePath,
|
||||
"Line": line,
|
||||
"Lines": readErrorFileLines(filePath, line),
|
||||
}
|
||||
|
||||
if e, ok := err.(error); ok {
|
||||
data["Error"] = e.Error()
|
||||
} else {
|
||||
data["Error"] = err
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func renderPrettyError(rw http.ResponseWriter, req *http.Request, err interface{}, stack []byte) {
|
||||
_, filePath, line, _ := runtime.Caller(5)
|
||||
|
||||
data := makeErrorData(req, err, stack, filePath, line)
|
||||
rw.Header().Set("Content-Type", "text/html")
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
tpl := template.Must(template.New("ErrorPage").Parse(panicPageTpl))
|
||||
tpl.Execute(rw, data)
|
||||
}
|
||||
|
||||
func ShowErrorsJsonPanicHandler(w http.ResponseWriter, r *http.Request, err interface{}) {
|
||||
const size = 4096
|
||||
stack := make([]byte, size)
|
||||
stack = stack[:runtime.Stack(stack, false)]
|
||||
|
||||
_, filePath, line, _ := runtime.Caller(4)
|
||||
data := makeErrorData(r, err, stack, filePath, line)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
||||
func readErrorFileLines(filePath string, errorLine int) map[int]string {
|
||||
lines := make(map[int]string)
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return lines
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
reader := bufio.NewReader(file)
|
||||
currentLine := 0
|
||||
for {
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil || currentLine > errorLine+5 {
|
||||
break
|
||||
}
|
||||
|
||||
currentLine++
|
||||
|
||||
if currentLine >= errorLine-5 {
|
||||
lines[currentLine] = strings.Replace(line, "\n", "", -1)
|
||||
}
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
const panicPageTpl string = `
|
||||
<html>
|
||||
<head>
|
||||
<title>Panic</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<style>
|
||||
html, body{ padding: 0; margin: 0; }
|
||||
header { background: #C52F24; color: white; border-bottom: 2px solid #9C0606; }
|
||||
h1 { padding: 10px 0; margin: 0; }
|
||||
.container { margin: 0 20px; }
|
||||
.error { font-size: 18px; background: #FFCCCC; color: #9C0606; padding: 10px 0; }
|
||||
.file-info .file-name { font-weight: bold; }
|
||||
.stack { height: 300px; overflow-y: scroll; border: 1px solid #e5e5e5; padding: 10px; }
|
||||
|
||||
table.source {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
border: 1px solid #e5e5e5;
|
||||
}
|
||||
|
||||
table.source td {
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
table.source .numbers {
|
||||
font-size: 14px;
|
||||
vertical-align: top;
|
||||
width: 1%;
|
||||
color: rgba(0,0,0,0.3);
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
table.source .numbers .number {
|
||||
display: block;
|
||||
padding: 0 5px;
|
||||
border-right: 1px solid #e5e5e5;
|
||||
}
|
||||
|
||||
table.source .numbers .number.line-{{ .Line }} {
|
||||
border-right: 1px solid #ffcccc;
|
||||
}
|
||||
|
||||
table.source .numbers pre {
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
table.source .code {
|
||||
font-size: 14px;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
table.source .code .line {
|
||||
padding-left: 10px;
|
||||
display: block;
|
||||
}
|
||||
|
||||
table.source .numbers .number,
|
||||
table.source .code .line {
|
||||
padding-top: 1px;
|
||||
padding-bottom: 1px;
|
||||
}
|
||||
|
||||
table.source .code .line:hover {
|
||||
background-color: #f6f6f6;
|
||||
}
|
||||
|
||||
table.source .line-{{ .Line }},
|
||||
table.source line-{{ .Line }},
|
||||
table.source .code .line.line-{{ .Line }}:hover {
|
||||
background: #ffcccc;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<div class="container">
|
||||
<h1>Error</h1>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="error">
|
||||
<p class="container">{{ .Error }}</p>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<p class="file-info">
|
||||
In <span class="file-name">{{ .FilePath }}:{{ .Line }}</span></p>
|
||||
</p>
|
||||
|
||||
<table class="source">
|
||||
<tr>
|
||||
<td class="numbers">
|
||||
<pre>{{ range $lineNumber, $line := .Lines }}<span class="number line-{{ $lineNumber }}">{{ $lineNumber }}</span>{{ end }}</pre>
|
||||
</td>
|
||||
<td class="code">
|
||||
<pre>{{ range $lineNumber, $line := .Lines }}<span class="line line-{{ $lineNumber }}">{{ $line }}<br /></span>{{ end }}</pre>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<h2>Stack</h2>
|
||||
<pre class="stack">{{ .Stack }}</pre>
|
||||
<h2>Request</h2>
|
||||
<p><strong>Method:</strong> {{ .Method }}</p>
|
||||
<h3>Parameters:</h3>
|
||||
<ul>
|
||||
{{ range $key, $value := .Params }}
|
||||
<li><strong>{{ $key }}:</strong> {{ $value }}</li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`
|
127
example-project/vendor/github.com/dimfeld/httptreemux/path.go
generated
vendored
Normal file
127
example-project/vendor/github.com/dimfeld/httptreemux/path.go
generated
vendored
Normal file
@ -0,0 +1,127 @@
|
||||
// Copyright 2013 Julien Schmidt. All rights reserved.
|
||||
// Based on the path package, Copyright 2009 The Go Authors.
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
package httptreemux
|
||||
|
||||
// Clean is the URL version of path.Clean, it returns a canonical URL path
|
||||
// for p, eliminating . and .. elements.
|
||||
//
|
||||
// The following rules are applied iteratively until no further processing can
|
||||
// be done:
|
||||
// 1. Replace multiple slashes with a single slash.
|
||||
// 2. Eliminate each . path name element (the current directory).
|
||||
// 3. Eliminate each inner .. path name element (the parent directory)
|
||||
// along with the non-.. element that precedes it.
|
||||
// 4. Eliminate .. elements that begin a rooted path:
|
||||
// that is, replace "/.." by "/" at the beginning of a path.
|
||||
//
|
||||
// If the result of this process is an empty string, "/" is returned
|
||||
func Clean(p string) string {
|
||||
if p == "" {
|
||||
return "/"
|
||||
}
|
||||
|
||||
n := len(p)
|
||||
var buf []byte
|
||||
|
||||
// Invariants:
|
||||
// reading from path; r is index of next byte to process.
|
||||
// writing to buf; w is index of next byte to write.
|
||||
|
||||
// path must start with '/'
|
||||
r := 1
|
||||
w := 1
|
||||
|
||||
if p[0] != '/' {
|
||||
r = 0
|
||||
buf = make([]byte, n+1)
|
||||
buf[0] = '/'
|
||||
}
|
||||
|
||||
trailing := n > 2 && p[n-1] == '/'
|
||||
|
||||
// A bit more clunky without a 'lazybuf' like the path package, but the loop
|
||||
// gets completely inlined (bufApp). So in contrast to the path package this
|
||||
// loop has no expensive function calls (except 1x make)
|
||||
|
||||
for r < n {
|
||||
switch {
|
||||
case p[r] == '/':
|
||||
// empty path element, trailing slash is added after the end
|
||||
r++
|
||||
|
||||
case p[r] == '.' && r+1 == n:
|
||||
trailing = true
|
||||
r++
|
||||
|
||||
case p[r] == '.' && p[r+1] == '/':
|
||||
// . element
|
||||
r++
|
||||
|
||||
case p[r] == '.' && p[r+1] == '.' && (r+2 == n || p[r+2] == '/'):
|
||||
// .. element: remove to last /
|
||||
r += 2
|
||||
|
||||
if w > 1 {
|
||||
// can backtrack
|
||||
w--
|
||||
|
||||
if buf == nil {
|
||||
for w > 1 && p[w] != '/' {
|
||||
w--
|
||||
}
|
||||
} else {
|
||||
for w > 1 && buf[w] != '/' {
|
||||
w--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
// real path element.
|
||||
// add slash if needed
|
||||
if w > 1 {
|
||||
bufApp(&buf, p, w, '/')
|
||||
w++
|
||||
}
|
||||
|
||||
// copy element
|
||||
for r < n && p[r] != '/' {
|
||||
bufApp(&buf, p, w, p[r])
|
||||
w++
|
||||
r++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// re-append trailing slash
|
||||
if trailing && w > 1 {
|
||||
bufApp(&buf, p, w, '/')
|
||||
w++
|
||||
}
|
||||
|
||||
// Turn empty string into "/"
|
||||
if w == 0 {
|
||||
return "/"
|
||||
}
|
||||
|
||||
if buf == nil {
|
||||
return p[:w]
|
||||
}
|
||||
return string(buf[:w])
|
||||
}
|
||||
|
||||
// internal helper to lazily create a buffer if necessary
|
||||
func bufApp(buf *[]byte, s string, w int, c byte) {
|
||||
if *buf == nil {
|
||||
if s[w] == c {
|
||||
return
|
||||
}
|
||||
|
||||
*buf = make([]byte, len(s))
|
||||
copy(*buf, s[:w])
|
||||
}
|
||||
(*buf)[w] = c
|
||||
}
|
300
example-project/vendor/github.com/dimfeld/httptreemux/router.go
generated
vendored
Normal file
300
example-project/vendor/github.com/dimfeld/httptreemux/router.go
generated
vendored
Normal file
@ -0,0 +1,300 @@
|
||||
// This is inspired by Julien Schmidt's httprouter, in that it uses a patricia tree, but the
|
||||
// implementation is rather different. Specifically, the routing rules are relaxed so that a
|
||||
// single path segment may be a wildcard in one route and a static token in another. This gives a
|
||||
// nice combination of high performance with a lot of convenience in designing the routing patterns.
|
||||
package httptreemux
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// The params argument contains the parameters parsed from wildcards and catch-alls in the URL.
|
||||
type HandlerFunc func(http.ResponseWriter, *http.Request, map[string]string)
|
||||
type PanicHandler func(http.ResponseWriter, *http.Request, interface{})
|
||||
|
||||
// RedirectBehavior sets the behavior when the router redirects the request to the
|
||||
// canonical version of the requested URL using RedirectTrailingSlash or RedirectClean.
|
||||
// The default behavior is to return a 301 status, redirecting the browser to the version
|
||||
// of the URL that matches the given pattern.
|
||||
//
|
||||
// On a POST request, most browsers that receive a 301 will submit a GET request to
|
||||
// the redirected URL, meaning that any data will likely be lost. If you want to handle
|
||||
// and avoid this behavior, you may use Redirect307, which causes most browsers to
|
||||
// resubmit the request using the original method and request body.
|
||||
//
|
||||
// Since 307 is supposed to be a temporary redirect, the new 308 status code has been
|
||||
// proposed, which is treated the same, except it indicates correctly that the redirection
|
||||
// is permanent. The big caveat here is that the RFC is relatively recent, and older
|
||||
// browsers will not know what to do with it. Therefore its use is not recommended
|
||||
// unless you really know what you're doing.
|
||||
//
|
||||
// Finally, the UseHandler value will simply call the handler function for the pattern.
|
||||
type RedirectBehavior int
|
||||
|
||||
type PathSource int
|
||||
|
||||
const (
|
||||
Redirect301 RedirectBehavior = iota // Return 301 Moved Permanently
|
||||
Redirect307 // Return 307 HTTP/1.1 Temporary Redirect
|
||||
Redirect308 // Return a 308 RFC7538 Permanent Redirect
|
||||
UseHandler // Just call the handler function
|
||||
|
||||
RequestURI PathSource = iota // Use r.RequestURI
|
||||
URLPath // Use r.URL.Path
|
||||
)
|
||||
|
||||
// LookupResult contains information about a route lookup, which is returned from Lookup and
|
||||
// can be passed to ServeLookupResult if the request should be served.
|
||||
type LookupResult struct {
|
||||
// StatusCode informs the caller about the result of the lookup.
|
||||
// This will generally be `http.StatusNotFound` or `http.StatusMethodNotAllowed` for an
|
||||
// error case. On a normal success, the statusCode will be `http.StatusOK`. A redirect code
|
||||
// will also be used in the case
|
||||
StatusCode int
|
||||
handler HandlerFunc
|
||||
params map[string]string
|
||||
leafHandler map[string]HandlerFunc // Only has a value when StatusCode is MethodNotAllowed.
|
||||
}
|
||||
|
||||
// Dump returns a text representation of the routing tree.
|
||||
func (t *TreeMux) Dump() string {
|
||||
return t.root.dumpTree("", "")
|
||||
}
|
||||
|
||||
func (t *TreeMux) serveHTTPPanic(w http.ResponseWriter, r *http.Request) {
|
||||
if err := recover(); err != nil {
|
||||
t.PanicHandler(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TreeMux) redirectStatusCode(method string) (int, bool) {
|
||||
var behavior RedirectBehavior
|
||||
var ok bool
|
||||
if behavior, ok = t.RedirectMethodBehavior[method]; !ok {
|
||||
behavior = t.RedirectBehavior
|
||||
}
|
||||
switch behavior {
|
||||
case Redirect301:
|
||||
return http.StatusMovedPermanently, true
|
||||
case Redirect307:
|
||||
return http.StatusTemporaryRedirect, true
|
||||
case Redirect308:
|
||||
// Go doesn't have a constant for this yet. Yet another sign
|
||||
// that you probably shouldn't use it.
|
||||
return 308, true
|
||||
case UseHandler:
|
||||
return 0, false
|
||||
default:
|
||||
return http.StatusMovedPermanently, true
|
||||
}
|
||||
}
|
||||
|
||||
func redirectHandler(newPath string, statusCode int) HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request, params map[string]string) {
|
||||
redirect(w, r, newPath, statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func redirect(w http.ResponseWriter, r *http.Request, newPath string, statusCode int) {
|
||||
newURL := url.URL{
|
||||
Path: newPath,
|
||||
RawQuery: r.URL.RawQuery,
|
||||
Fragment: r.URL.Fragment,
|
||||
}
|
||||
http.Redirect(w, r, newURL.String(), statusCode)
|
||||
}
|
||||
|
||||
func (t *TreeMux) lookup(w http.ResponseWriter, r *http.Request) (result LookupResult, found bool) {
|
||||
result.StatusCode = http.StatusNotFound
|
||||
path := r.RequestURI
|
||||
unescapedPath := r.URL.Path
|
||||
pathLen := len(path)
|
||||
if pathLen > 0 && t.PathSource == RequestURI {
|
||||
rawQueryLen := len(r.URL.RawQuery)
|
||||
|
||||
if rawQueryLen != 0 || path[pathLen-1] == '?' {
|
||||
// Remove any query string and the ?.
|
||||
path = path[:pathLen-rawQueryLen-1]
|
||||
pathLen = len(path)
|
||||
}
|
||||
} else {
|
||||
// In testing with http.NewRequest,
|
||||
// RequestURI is not set so just grab URL.Path instead.
|
||||
path = r.URL.Path
|
||||
pathLen = len(path)
|
||||
}
|
||||
|
||||
trailingSlash := path[pathLen-1] == '/' && pathLen > 1
|
||||
if trailingSlash && t.RedirectTrailingSlash {
|
||||
path = path[:pathLen-1]
|
||||
unescapedPath = unescapedPath[:len(unescapedPath)-1]
|
||||
}
|
||||
|
||||
n, handler, params := t.root.search(r.Method, path[1:])
|
||||
if n == nil {
|
||||
if t.RedirectCleanPath {
|
||||
// Path was not found. Try cleaning it up and search again.
|
||||
// TODO Test this
|
||||
cleanPath := Clean(unescapedPath)
|
||||
n, handler, params = t.root.search(r.Method, cleanPath[1:])
|
||||
if n == nil {
|
||||
// Still nothing found.
|
||||
return
|
||||
}
|
||||
if statusCode, ok := t.redirectStatusCode(r.Method); ok {
|
||||
// Redirect to the actual path
|
||||
return LookupResult{statusCode, redirectHandler(cleanPath, statusCode), nil, nil}, true
|
||||
}
|
||||
} else {
|
||||
// Not found.
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if handler == nil {
|
||||
if r.Method == "OPTIONS" && t.OptionsHandler != nil {
|
||||
handler = t.OptionsHandler
|
||||
}
|
||||
|
||||
if handler == nil {
|
||||
result.leafHandler = n.leafHandler
|
||||
result.StatusCode = http.StatusMethodNotAllowed
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if !n.isCatchAll || t.RemoveCatchAllTrailingSlash {
|
||||
if trailingSlash != n.addSlash && t.RedirectTrailingSlash {
|
||||
if statusCode, ok := t.redirectStatusCode(r.Method); ok {
|
||||
var h HandlerFunc
|
||||
if n.addSlash {
|
||||
// Need to add a slash.
|
||||
h = redirectHandler(unescapedPath+"/", statusCode)
|
||||
} else if path != "/" {
|
||||
// We need to remove the slash. This was already done at the
|
||||
// beginning of the function.
|
||||
h = redirectHandler(unescapedPath, statusCode)
|
||||
}
|
||||
|
||||
if h != nil {
|
||||
return LookupResult{statusCode, h, nil, nil}, true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var paramMap map[string]string
|
||||
if len(params) != 0 {
|
||||
if len(params) != len(n.leafWildcardNames) {
|
||||
// Need better behavior here. Should this be a panic?
|
||||
panic(fmt.Sprintf("httptreemux parameter list length mismatch: %v, %v",
|
||||
params, n.leafWildcardNames))
|
||||
}
|
||||
|
||||
paramMap = make(map[string]string)
|
||||
numParams := len(params)
|
||||
for index := 0; index < numParams; index++ {
|
||||
paramMap[n.leafWildcardNames[numParams-index-1]] = params[index]
|
||||
}
|
||||
}
|
||||
|
||||
return LookupResult{http.StatusOK, handler, paramMap, nil}, true
|
||||
}
|
||||
|
||||
// Lookup performs a lookup without actually serving the request or mutating the request or response.
|
||||
// The return values are a LookupResult and a boolean. The boolean will be true when a handler
|
||||
// was found or the lookup resulted in a redirect which will point to a real handler. It is false
|
||||
// for requests which would result in a `StatusNotFound` or `StatusMethodNotAllowed`.
|
||||
//
|
||||
// Regardless of the returned boolean's value, the LookupResult may be passed to ServeLookupResult
|
||||
// to be served appropriately.
|
||||
func (t *TreeMux) Lookup(w http.ResponseWriter, r *http.Request) (LookupResult, bool) {
|
||||
if t.SafeAddRoutesWhileRunning {
|
||||
// In concurrency safe mode, we acquire a read lock on the mutex for any access.
|
||||
// This is optional to avoid potential performance loss in high-usage scenarios.
|
||||
t.mutex.RLock()
|
||||
}
|
||||
|
||||
result, found := t.lookup(w, r)
|
||||
|
||||
if t.SafeAddRoutesWhileRunning {
|
||||
t.mutex.RUnlock()
|
||||
}
|
||||
|
||||
return result, found
|
||||
}
|
||||
|
||||
// ServeLookupResult serves a request, given a lookup result from the Lookup function.
|
||||
func (t *TreeMux) ServeLookupResult(w http.ResponseWriter, r *http.Request, lr LookupResult) {
|
||||
if lr.handler == nil {
|
||||
if lr.StatusCode == http.StatusMethodNotAllowed && lr.leafHandler != nil {
|
||||
if t.SafeAddRoutesWhileRunning {
|
||||
t.mutex.RLock()
|
||||
}
|
||||
|
||||
t.MethodNotAllowedHandler(w, r, lr.leafHandler)
|
||||
|
||||
if t.SafeAddRoutesWhileRunning {
|
||||
t.mutex.RUnlock()
|
||||
}
|
||||
} else {
|
||||
t.NotFoundHandler(w, r)
|
||||
}
|
||||
} else {
|
||||
r = t.setDefaultRequestContext(r)
|
||||
lr.handler(w, r, lr.params)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TreeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if t.PanicHandler != nil {
|
||||
defer t.serveHTTPPanic(w, r)
|
||||
}
|
||||
|
||||
if t.SafeAddRoutesWhileRunning {
|
||||
// In concurrency safe mode, we acquire a read lock on the mutex for any access.
|
||||
// This is optional to avoid potential performance loss in high-usage scenarios.
|
||||
t.mutex.RLock()
|
||||
}
|
||||
|
||||
result, _ := t.lookup(w, r)
|
||||
|
||||
if t.SafeAddRoutesWhileRunning {
|
||||
t.mutex.RUnlock()
|
||||
}
|
||||
|
||||
t.ServeLookupResult(w, r, result)
|
||||
}
|
||||
|
||||
// MethodNotAllowedHandler is the default handler for TreeMux.MethodNotAllowedHandler,
|
||||
// which is called for patterns that match, but do not have a handler installed for the
|
||||
// requested method. It simply writes the status code http.StatusMethodNotAllowed and fills
|
||||
// in the `Allow` header value appropriately.
|
||||
func MethodNotAllowedHandler(w http.ResponseWriter, r *http.Request,
|
||||
methods map[string]HandlerFunc) {
|
||||
|
||||
for m := range methods {
|
||||
w.Header().Add("Allow", m)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
|
||||
func New() *TreeMux {
|
||||
tm := &TreeMux{
|
||||
root: &node{path: "/"},
|
||||
NotFoundHandler: http.NotFound,
|
||||
MethodNotAllowedHandler: MethodNotAllowedHandler,
|
||||
HeadCanUseGet: true,
|
||||
RedirectTrailingSlash: true,
|
||||
RedirectCleanPath: true,
|
||||
RedirectBehavior: Redirect301,
|
||||
RedirectMethodBehavior: make(map[string]RedirectBehavior),
|
||||
PathSource: RequestURI,
|
||||
EscapeAddedRoutes: false,
|
||||
}
|
||||
tm.Group.mux = tm
|
||||
return tm
|
||||
}
|
340
example-project/vendor/github.com/dimfeld/httptreemux/tree.go
generated
vendored
Normal file
340
example-project/vendor/github.com/dimfeld/httptreemux/tree.go
generated
vendored
Normal file
@ -0,0 +1,340 @@
|
||||
package httptreemux
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type node struct {
|
||||
path string
|
||||
|
||||
priority int
|
||||
|
||||
// The list of static children to check.
|
||||
staticIndices []byte
|
||||
staticChild []*node
|
||||
|
||||
// If none of the above match, check the wildcard children
|
||||
wildcardChild *node
|
||||
|
||||
// If none of the above match, then we use the catch-all, if applicable.
|
||||
catchAllChild *node
|
||||
|
||||
// Data for the node is below.
|
||||
|
||||
addSlash bool
|
||||
isCatchAll bool
|
||||
// If true, the head handler was set implicitly, so let it also be set explicitly.
|
||||
implicitHead bool
|
||||
// If this node is the end of the URL, then call the handler, if applicable.
|
||||
leafHandler map[string]HandlerFunc
|
||||
|
||||
// The names of the parameters to apply.
|
||||
leafWildcardNames []string
|
||||
}
|
||||
|
||||
func (n *node) sortStaticChild(i int) {
|
||||
for i > 0 && n.staticChild[i].priority > n.staticChild[i-1].priority {
|
||||
n.staticChild[i], n.staticChild[i-1] = n.staticChild[i-1], n.staticChild[i]
|
||||
n.staticIndices[i], n.staticIndices[i-1] = n.staticIndices[i-1], n.staticIndices[i]
|
||||
i -= 1
|
||||
}
|
||||
}
|
||||
|
||||
func (n *node) setHandler(verb string, handler HandlerFunc, implicitHead bool) {
|
||||
if n.leafHandler == nil {
|
||||
n.leafHandler = make(map[string]HandlerFunc)
|
||||
}
|
||||
_, ok := n.leafHandler[verb]
|
||||
if ok && (verb != "HEAD" || !n.implicitHead) {
|
||||
panic(fmt.Sprintf("%s already handles %s", n.path, verb))
|
||||
}
|
||||
n.leafHandler[verb] = handler
|
||||
|
||||
if verb == "HEAD" {
|
||||
n.implicitHead = implicitHead
|
||||
}
|
||||
}
|
||||
|
||||
func (n *node) addPath(path string, wildcards []string, inStaticToken bool) *node {
|
||||
leaf := len(path) == 0
|
||||
if leaf {
|
||||
if wildcards != nil {
|
||||
// Make sure the current wildcards are the same as the old ones.
|
||||
// If not then we have an ambiguous path.
|
||||
if n.leafWildcardNames != nil {
|
||||
if len(n.leafWildcardNames) != len(wildcards) {
|
||||
// This should never happen.
|
||||
panic("Reached leaf node with differing wildcard array length. Please report this as a bug.")
|
||||
}
|
||||
|
||||
for i := 0; i < len(wildcards); i++ {
|
||||
if n.leafWildcardNames[i] != wildcards[i] {
|
||||
panic(fmt.Sprintf("Wildcards %v are ambiguous with wildcards %v",
|
||||
n.leafWildcardNames, wildcards))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No wildcards yet, so just add the existing set.
|
||||
n.leafWildcardNames = wildcards
|
||||
}
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
c := path[0]
|
||||
nextSlash := strings.Index(path, "/")
|
||||
var thisToken string
|
||||
var tokenEnd int
|
||||
|
||||
if c == '/' {
|
||||
// Done processing the previous token, so reset inStaticToken to false.
|
||||
thisToken = "/"
|
||||
tokenEnd = 1
|
||||
} else if nextSlash == -1 {
|
||||
thisToken = path
|
||||
tokenEnd = len(path)
|
||||
} else {
|
||||
thisToken = path[0:nextSlash]
|
||||
tokenEnd = nextSlash
|
||||
}
|
||||
remainingPath := path[tokenEnd:]
|
||||
|
||||
if c == '*' && !inStaticToken {
|
||||
// Token starts with a *, so it's a catch-all
|
||||
thisToken = thisToken[1:]
|
||||
if n.catchAllChild == nil {
|
||||
n.catchAllChild = &node{path: thisToken, isCatchAll: true}
|
||||
}
|
||||
|
||||
if path[1:] != n.catchAllChild.path {
|
||||
panic(fmt.Sprintf("Catch-all name in %s doesn't match %s. You probably tried to define overlapping catchalls",
|
||||
path, n.catchAllChild.path))
|
||||
}
|
||||
|
||||
if nextSlash != -1 {
|
||||
panic("/ after catch-all found in " + path)
|
||||
}
|
||||
|
||||
if wildcards == nil {
|
||||
wildcards = []string{thisToken}
|
||||
} else {
|
||||
wildcards = append(wildcards, thisToken)
|
||||
}
|
||||
n.catchAllChild.leafWildcardNames = wildcards
|
||||
|
||||
return n.catchAllChild
|
||||
} else if c == ':' && !inStaticToken {
|
||||
// Token starts with a :
|
||||
thisToken = thisToken[1:]
|
||||
|
||||
if wildcards == nil {
|
||||
wildcards = []string{thisToken}
|
||||
} else {
|
||||
wildcards = append(wildcards, thisToken)
|
||||
}
|
||||
|
||||
if n.wildcardChild == nil {
|
||||
n.wildcardChild = &node{path: "wildcard"}
|
||||
}
|
||||
|
||||
return n.wildcardChild.addPath(remainingPath, wildcards, false)
|
||||
|
||||
} else {
|
||||
// if strings.ContainsAny(thisToken, ":*") {
|
||||
// panic("* or : in middle of path component " + path)
|
||||
// }
|
||||
|
||||
unescaped := false
|
||||
if len(thisToken) >= 2 && !inStaticToken {
|
||||
if thisToken[0] == '\\' && (thisToken[1] == '*' || thisToken[1] == ':' || thisToken[1] == '\\') {
|
||||
// The token starts with a character escaped by a backslash. Drop the backslash.
|
||||
c = thisToken[1]
|
||||
thisToken = thisToken[1:]
|
||||
unescaped = true
|
||||
}
|
||||
}
|
||||
|
||||
// Set inStaticToken to ensure that the rest of this token is not mistaken
|
||||
// for a wildcard if a prefix split occurs at a '*' or ':'.
|
||||
inStaticToken = (c != '/')
|
||||
|
||||
// Do we have an existing node that starts with the same letter?
|
||||
for i, index := range n.staticIndices {
|
||||
if c == index {
|
||||
// Yes. Split it based on the common prefix of the existing
|
||||
// node and the new one.
|
||||
child, prefixSplit := n.splitCommonPrefix(i, thisToken)
|
||||
|
||||
child.priority++
|
||||
n.sortStaticChild(i)
|
||||
if unescaped {
|
||||
// Account for the removed backslash.
|
||||
prefixSplit++
|
||||
}
|
||||
return child.addPath(path[prefixSplit:], wildcards, inStaticToken)
|
||||
}
|
||||
}
|
||||
|
||||
// No existing node starting with this letter, so create it.
|
||||
child := &node{path: thisToken}
|
||||
|
||||
if n.staticIndices == nil {
|
||||
n.staticIndices = []byte{c}
|
||||
n.staticChild = []*node{child}
|
||||
} else {
|
||||
n.staticIndices = append(n.staticIndices, c)
|
||||
n.staticChild = append(n.staticChild, child)
|
||||
}
|
||||
return child.addPath(remainingPath, wildcards, inStaticToken)
|
||||
}
|
||||
}
|
||||
|
||||
func (n *node) splitCommonPrefix(existingNodeIndex int, path string) (*node, int) {
|
||||
childNode := n.staticChild[existingNodeIndex]
|
||||
|
||||
if strings.HasPrefix(path, childNode.path) {
|
||||
// No split needs to be done. Rather, the new path shares the entire
|
||||
// prefix with the existing node, so the new node is just a child of
|
||||
// the existing one. Or the new path is the same as the existing path,
|
||||
// which means that we just move on to the next token. Either way,
|
||||
// this return accomplishes that
|
||||
return childNode, len(childNode.path)
|
||||
}
|
||||
|
||||
var i int
|
||||
// Find the length of the common prefix of the child node and the new path.
|
||||
for i = range childNode.path {
|
||||
if i == len(path) {
|
||||
break
|
||||
}
|
||||
if path[i] != childNode.path[i] {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
commonPrefix := path[0:i]
|
||||
childNode.path = childNode.path[i:]
|
||||
|
||||
// Create a new intermediary node in the place of the existing node, with
|
||||
// the existing node as a child.
|
||||
newNode := &node{
|
||||
path: commonPrefix,
|
||||
priority: childNode.priority,
|
||||
// Index is the first letter of the non-common part of the path.
|
||||
staticIndices: []byte{childNode.path[0]},
|
||||
staticChild: []*node{childNode},
|
||||
}
|
||||
n.staticChild[existingNodeIndex] = newNode
|
||||
|
||||
return newNode, i
|
||||
}
|
||||
|
||||
func (n *node) search(method, path string) (found *node, handler HandlerFunc, params []string) {
|
||||
// if test != nil {
|
||||
// test.Logf("Searching for %s in %s", path, n.dumpTree("", ""))
|
||||
// }
|
||||
pathLen := len(path)
|
||||
if pathLen == 0 {
|
||||
if len(n.leafHandler) == 0 {
|
||||
return nil, nil, nil
|
||||
} else {
|
||||
return n, n.leafHandler[method], nil
|
||||
}
|
||||
}
|
||||
|
||||
// First see if this matches a static token.
|
||||
firstChar := path[0]
|
||||
for i, staticIndex := range n.staticIndices {
|
||||
if staticIndex == firstChar {
|
||||
child := n.staticChild[i]
|
||||
childPathLen := len(child.path)
|
||||
if pathLen >= childPathLen && child.path == path[:childPathLen] {
|
||||
nextPath := path[childPathLen:]
|
||||
found, handler, params = child.search(method, nextPath)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// If we found a node and it had a valid handler, then return here. Otherwise
|
||||
// let's remember that we found this one, but look for a better match.
|
||||
if handler != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if n.wildcardChild != nil {
|
||||
// Didn't find a static token, so check for a wildcard.
|
||||
nextSlash := strings.IndexByte(path, '/')
|
||||
if nextSlash < 0 {
|
||||
nextSlash = pathLen
|
||||
}
|
||||
|
||||
thisToken := path[0:nextSlash]
|
||||
nextToken := path[nextSlash:]
|
||||
|
||||
if len(thisToken) > 0 { // Don't match on empty tokens.
|
||||
wcNode, wcHandler, wcParams := n.wildcardChild.search(method, nextToken)
|
||||
if wcHandler != nil || (found == nil && wcNode != nil) {
|
||||
unescaped, err := unescape(thisToken)
|
||||
if err != nil {
|
||||
unescaped = thisToken
|
||||
}
|
||||
|
||||
if wcParams == nil {
|
||||
wcParams = []string{unescaped}
|
||||
} else {
|
||||
wcParams = append(wcParams, unescaped)
|
||||
}
|
||||
|
||||
if wcHandler != nil {
|
||||
return wcNode, wcHandler, wcParams
|
||||
}
|
||||
|
||||
// Didn't actually find a handler here, so remember that we
|
||||
// found a node but also see if we can fall through to the
|
||||
// catchall.
|
||||
found = wcNode
|
||||
handler = wcHandler
|
||||
params = wcParams
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
catchAllChild := n.catchAllChild
|
||||
if catchAllChild != nil {
|
||||
// Hit the catchall, so just assign the whole remaining path if it
|
||||
// has a matching handler.
|
||||
handler = catchAllChild.leafHandler[method]
|
||||
// Found a handler, or we found a catchall node without a handler.
|
||||
// Either way, return it since there's nothing left to check after this.
|
||||
if handler != nil || found == nil {
|
||||
unescaped, err := unescape(path)
|
||||
if err != nil {
|
||||
unescaped = path
|
||||
}
|
||||
|
||||
return catchAllChild, handler, []string{unescaped}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return found, handler, params
|
||||
}
|
||||
|
||||
func (n *node) dumpTree(prefix, nodeType string) string {
|
||||
line := fmt.Sprintf("%s %02d %s%s [%d] %v wildcards %v\n", prefix, n.priority, nodeType, n.path,
|
||||
len(n.staticChild), n.leafHandler, n.leafWildcardNames)
|
||||
prefix += " "
|
||||
for _, node := range n.staticChild {
|
||||
line += node.dumpTree(prefix, "")
|
||||
}
|
||||
if n.wildcardChild != nil {
|
||||
line += n.wildcardChild.dumpTree(prefix, ":")
|
||||
}
|
||||
if n.catchAllChild != nil {
|
||||
line += n.catchAllChild.dumpTree(prefix, "*")
|
||||
}
|
||||
return line
|
||||
}
|
86
example-project/vendor/github.com/dimfeld/httptreemux/treemux_16.go
generated
vendored
Normal file
86
example-project/vendor/github.com/dimfeld/httptreemux/treemux_16.go
generated
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
// +build !go1.7
|
||||
|
||||
package httptreemux
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type TreeMux struct {
|
||||
root *node
|
||||
mutex sync.RWMutex
|
||||
|
||||
Group
|
||||
|
||||
// The default PanicHandler just returns a 500 code.
|
||||
PanicHandler PanicHandler
|
||||
|
||||
// The default NotFoundHandler is http.NotFound.
|
||||
NotFoundHandler func(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
// Any OPTIONS request that matches a path without its own OPTIONS handler will use this handler,
|
||||
// if set, instead of calling MethodNotAllowedHandler.
|
||||
OptionsHandler HandlerFunc
|
||||
|
||||
// MethodNotAllowedHandler is called when a pattern matches, but that
|
||||
// pattern does not have a handler for the requested method. The default
|
||||
// handler just writes the status code http.StatusMethodNotAllowed and adds
|
||||
// the required Allowed header.
|
||||
// The methods parameter contains the map of each method to the corresponding
|
||||
// handler function.
|
||||
MethodNotAllowedHandler func(w http.ResponseWriter, r *http.Request,
|
||||
methods map[string]HandlerFunc)
|
||||
|
||||
// HeadCanUseGet allows the router to use the GET handler to respond to
|
||||
// HEAD requests if no explicit HEAD handler has been added for the
|
||||
// matching pattern. This is true by default.
|
||||
HeadCanUseGet bool
|
||||
|
||||
// RedirectCleanPath allows the router to try clean the current request path,
|
||||
// if no handler is registered for it, using CleanPath from github.com/dimfeld/httppath.
|
||||
// This is true by default.
|
||||
RedirectCleanPath bool
|
||||
|
||||
// RedirectTrailingSlash enables automatic redirection in case router doesn't find a matching route
|
||||
// for the current request path but a handler for the path with or without the trailing
|
||||
// slash exists. This is true by default.
|
||||
RedirectTrailingSlash bool
|
||||
|
||||
// RemoveCatchAllTrailingSlash removes the trailing slash when a catch-all pattern
|
||||
// is matched, if set to true. By default, catch-all paths are never redirected.
|
||||
RemoveCatchAllTrailingSlash bool
|
||||
|
||||
// RedirectBehavior sets the default redirect behavior when RedirectTrailingSlash or
|
||||
// RedirectCleanPath are true. The default value is Redirect301.
|
||||
RedirectBehavior RedirectBehavior
|
||||
|
||||
// RedirectMethodBehavior overrides the default behavior for a particular HTTP method.
|
||||
// The key is the method name, and the value is the behavior to use for that method.
|
||||
RedirectMethodBehavior map[string]RedirectBehavior
|
||||
|
||||
// PathSource determines from where the router gets its path to search.
|
||||
// By default it pulls the data from the RequestURI member, but this can
|
||||
// be overridden to use URL.Path instead.
|
||||
//
|
||||
// There is a small tradeoff here. Using RequestURI allows the router to handle
|
||||
// encoded slashes (i.e. %2f) in the URL properly, while URL.Path provides
|
||||
// better compatibility with some utility functions in the http
|
||||
// library that modify the Request before passing it to the router.
|
||||
PathSource PathSource
|
||||
|
||||
// EscapeAddedRoutes controls URI escaping behavior when adding a route to the tree.
|
||||
// If set to true, the router will add both the route as originally passed, and
|
||||
// a version passed through URL.EscapedPath. This behavior is disabled by default.
|
||||
EscapeAddedRoutes bool
|
||||
|
||||
// SafeAddRoutesWhileRunning tells the router to protect all accesses to the tree with an RWMutex. This is only needed
|
||||
// if you are going to add routes after the router has already begun serving requests. There is a potential
|
||||
// performance penalty at high load.
|
||||
SafeAddRoutesWhileRunning bool
|
||||
}
|
||||
|
||||
func (t *TreeMux) setDefaultRequestContext(r *http.Request) *http.Request {
|
||||
// Nothing to do on Go 1.6 and before
|
||||
return r
|
||||
}
|
149
example-project/vendor/github.com/dimfeld/httptreemux/treemux_17.go
generated
vendored
Normal file
149
example-project/vendor/github.com/dimfeld/httptreemux/treemux_17.go
generated
vendored
Normal file
@ -0,0 +1,149 @@
|
||||
// +build go1.7
|
||||
|
||||
package httptreemux
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type TreeMux struct {
|
||||
root *node
|
||||
mutex sync.RWMutex
|
||||
|
||||
Group
|
||||
|
||||
// The default PanicHandler just returns a 500 code.
|
||||
PanicHandler PanicHandler
|
||||
|
||||
// The default NotFoundHandler is http.NotFound.
|
||||
NotFoundHandler func(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
// Any OPTIONS request that matches a path without its own OPTIONS handler will use this handler,
|
||||
// if set, instead of calling MethodNotAllowedHandler.
|
||||
OptionsHandler HandlerFunc
|
||||
|
||||
// MethodNotAllowedHandler is called when a pattern matches, but that
|
||||
// pattern does not have a handler for the requested method. The default
|
||||
// handler just writes the status code http.StatusMethodNotAllowed and adds
|
||||
// the required Allowed header.
|
||||
// The methods parameter contains the map of each method to the corresponding
|
||||
// handler function.
|
||||
MethodNotAllowedHandler func(w http.ResponseWriter, r *http.Request,
|
||||
methods map[string]HandlerFunc)
|
||||
|
||||
// HeadCanUseGet allows the router to use the GET handler to respond to
|
||||
// HEAD requests if no explicit HEAD handler has been added for the
|
||||
// matching pattern. This is true by default.
|
||||
HeadCanUseGet bool
|
||||
|
||||
// RedirectCleanPath allows the router to try clean the current request path,
|
||||
// if no handler is registered for it, using CleanPath from github.com/dimfeld/httppath.
|
||||
// This is true by default.
|
||||
RedirectCleanPath bool
|
||||
|
||||
// RedirectTrailingSlash enables automatic redirection in case router doesn't find a matching route
|
||||
// for the current request path but a handler for the path with or without the trailing
|
||||
// slash exists. This is true by default.
|
||||
RedirectTrailingSlash bool
|
||||
|
||||
// RemoveCatchAllTrailingSlash removes the trailing slash when a catch-all pattern
|
||||
// is matched, if set to true. By default, catch-all paths are never redirected.
|
||||
RemoveCatchAllTrailingSlash bool
|
||||
|
||||
// RedirectBehavior sets the default redirect behavior when RedirectTrailingSlash or
|
||||
// RedirectCleanPath are true. The default value is Redirect301.
|
||||
RedirectBehavior RedirectBehavior
|
||||
|
||||
// RedirectMethodBehavior overrides the default behavior for a particular HTTP method.
|
||||
// The key is the method name, and the value is the behavior to use for that method.
|
||||
RedirectMethodBehavior map[string]RedirectBehavior
|
||||
|
||||
// PathSource determines from where the router gets its path to search.
|
||||
// By default it pulls the data from the RequestURI member, but this can
|
||||
// be overridden to use URL.Path instead.
|
||||
//
|
||||
// There is a small tradeoff here. Using RequestURI allows the router to handle
|
||||
// encoded slashes (i.e. %2f) in the URL properly, while URL.Path provides
|
||||
// better compatibility with some utility functions in the http
|
||||
// library that modify the Request before passing it to the router.
|
||||
PathSource PathSource
|
||||
|
||||
// EscapeAddedRoutes controls URI escaping behavior when adding a route to the tree.
|
||||
// If set to true, the router will add both the route as originally passed, and
|
||||
// a version passed through URL.EscapedPath. This behavior is disabled by default.
|
||||
EscapeAddedRoutes bool
|
||||
|
||||
// If present, override the default context with this one.
|
||||
DefaultContext context.Context
|
||||
|
||||
// SafeAddRoutesWhileRunning tells the router to protect all accesses to the tree with an RWMutex. This is only needed
|
||||
// if you are going to add routes after the router has already begun serving requests. There is a potential
|
||||
// performance penalty at high load.
|
||||
SafeAddRoutesWhileRunning bool
|
||||
}
|
||||
|
||||
func (t *TreeMux) setDefaultRequestContext(r *http.Request) *http.Request {
|
||||
if t.DefaultContext != nil {
|
||||
r = r.WithContext(t.DefaultContext)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
type ContextMux struct {
|
||||
*TreeMux
|
||||
*ContextGroup
|
||||
}
|
||||
|
||||
// NewContextMux returns a TreeMux preconfigured to work with standard http
|
||||
// Handler functions and context objects.
|
||||
func NewContextMux() *ContextMux {
|
||||
mux := New()
|
||||
cg := mux.UsingContext()
|
||||
|
||||
return &ContextMux{
|
||||
TreeMux: mux,
|
||||
ContextGroup: cg,
|
||||
}
|
||||
}
|
||||
|
||||
func (cm *ContextMux) NewGroup(path string) *ContextGroup {
|
||||
return cm.ContextGroup.NewGroup(path)
|
||||
}
|
||||
|
||||
// GET is convenience method for handling GET requests on a context group.
|
||||
func (cm *ContextMux) GET(path string, handler http.HandlerFunc) {
|
||||
cm.ContextGroup.Handle("GET", path, handler)
|
||||
}
|
||||
|
||||
// POST is convenience method for handling POST requests on a context group.
|
||||
func (cm *ContextMux) POST(path string, handler http.HandlerFunc) {
|
||||
cm.ContextGroup.Handle("POST", path, handler)
|
||||
}
|
||||
|
||||
// PUT is convenience method for handling PUT requests on a context group.
|
||||
func (cm *ContextMux) PUT(path string, handler http.HandlerFunc) {
|
||||
cm.ContextGroup.Handle("PUT", path, handler)
|
||||
}
|
||||
|
||||
// DELETE is convenience method for handling DELETE requests on a context group.
|
||||
func (cm *ContextMux) DELETE(path string, handler http.HandlerFunc) {
|
||||
cm.ContextGroup.Handle("DELETE", path, handler)
|
||||
}
|
||||
|
||||
// PATCH is convenience method for handling PATCH requests on a context group.
|
||||
func (cm *ContextMux) PATCH(path string, handler http.HandlerFunc) {
|
||||
cm.ContextGroup.Handle("PATCH", path, handler)
|
||||
}
|
||||
|
||||
// HEAD is convenience method for handling HEAD requests on a context group.
|
||||
func (cm *ContextMux) HEAD(path string, handler http.HandlerFunc) {
|
||||
cm.ContextGroup.Handle("HEAD", path, handler)
|
||||
}
|
||||
|
||||
// OPTIONS is convenience method for handling OPTIONS requests on a context group.
|
||||
func (cm *ContextMux) OPTIONS(path string, handler http.HandlerFunc) {
|
||||
cm.ContextGroup.Handle("OPTIONS", path, handler)
|
||||
}
|
9
example-project/vendor/github.com/dimfeld/httptreemux/unescape_17.go
generated
vendored
Normal file
9
example-project/vendor/github.com/dimfeld/httptreemux/unescape_17.go
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
// +build !go1.8
|
||||
|
||||
package httptreemux
|
||||
|
||||
import "net/url"
|
||||
|
||||
func unescape(path string) (string, error) {
|
||||
return url.QueryUnescape(path)
|
||||
}
|
9
example-project/vendor/github.com/dimfeld/httptreemux/unescape_18.go
generated
vendored
Normal file
9
example-project/vendor/github.com/dimfeld/httptreemux/unescape_18.go
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
// +build go1.8
|
||||
|
||||
package httptreemux
|
||||
|
||||
import "net/url"
|
||||
|
||||
func unescape(path string) (string, error) {
|
||||
return url.PathUnescape(path)
|
||||
}
|
24
example-project/vendor/github.com/go-playground/locales/.gitignore
generated
vendored
Normal file
24
example-project/vendor/github.com/go-playground/locales/.gitignore
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
21
example-project/vendor/github.com/go-playground/locales/LICENSE
generated
vendored
Normal file
21
example-project/vendor/github.com/go-playground/locales/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Go Playground
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
172
example-project/vendor/github.com/go-playground/locales/README.md
generated
vendored
Normal file
172
example-project/vendor/github.com/go-playground/locales/README.md
generated
vendored
Normal file
@ -0,0 +1,172 @@
|
||||
## locales
|
||||
<img align="right" src="https://raw.githubusercontent.com/go-playground/locales/master/logo.png">
|
||||
[](https://semaphoreci.com/joeybloggs/locales)
|
||||
[](https://goreportcard.com/report/github.com/go-playground/locales)
|
||||
[](https://godoc.org/github.com/go-playground/locales)
|
||||

|
||||
[](https://gitter.im/go-playground/locales?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||
|
||||
Locales is a set of locales generated from the [Unicode CLDR Project](http://cldr.unicode.org/) which can be used independently or within
|
||||
an i18n package; these were built for use with, but not exclusive to, [Universal Translator](https://github.com/go-playground/universal-translator).
|
||||
|
||||
Features
|
||||
--------
|
||||
- [x] Rules generated from the latest [CLDR](http://cldr.unicode.org/index/downloads) data, v31.0.1
|
||||
- [x] Contains Cardinal, Ordinal and Range Plural Rules
|
||||
- [x] Contains Month, Weekday and Timezone translations built in
|
||||
- [x] Contains Date & Time formatting functions
|
||||
- [x] Contains Number, Currency, Accounting and Percent formatting functions
|
||||
- [x] Supports the "Gregorian" calendar only ( my time isn't unlimited, had to draw the line somewhere )
|
||||
|
||||
Full Tests
|
||||
--------------------
|
||||
I could sure use your help adding tests for every locale, it is a huge undertaking and I just don't have the free time to do it all at the moment;
|
||||
any help would be **greatly appreciated!!!!** please see [issue](https://github.com/go-playground/locales/issues/1) for details.
|
||||
|
||||
Installation
|
||||
-----------
|
||||
|
||||
Use go get
|
||||
|
||||
```shell
|
||||
go get github.com/go-playground/locales
|
||||
```
|
||||
|
||||
NOTES
|
||||
--------
|
||||
You'll notice most return types are []byte, this is because most of the time the results will be concatenated with a larger body
|
||||
of text and can avoid some allocations if already appending to a byte array, otherwise just cast as string.
|
||||
|
||||
Usage
|
||||
-------
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/go-playground/locales/currency"
|
||||
"github.com/go-playground/locales/en_CA"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
loc, _ := time.LoadLocation("America/Toronto")
|
||||
datetime := time.Date(2016, 02, 03, 9, 0, 1, 0, loc)
|
||||
|
||||
l := en_CA.New()
|
||||
|
||||
// Dates
|
||||
fmt.Println(l.FmtDateFull(datetime))
|
||||
fmt.Println(l.FmtDateLong(datetime))
|
||||
fmt.Println(l.FmtDateMedium(datetime))
|
||||
fmt.Println(l.FmtDateShort(datetime))
|
||||
|
||||
// Times
|
||||
fmt.Println(l.FmtTimeFull(datetime))
|
||||
fmt.Println(l.FmtTimeLong(datetime))
|
||||
fmt.Println(l.FmtTimeMedium(datetime))
|
||||
fmt.Println(l.FmtTimeShort(datetime))
|
||||
|
||||
// Months Wide
|
||||
fmt.Println(l.MonthWide(time.January))
|
||||
fmt.Println(l.MonthWide(time.February))
|
||||
fmt.Println(l.MonthWide(time.March))
|
||||
// ...
|
||||
|
||||
// Months Abbreviated
|
||||
fmt.Println(l.MonthAbbreviated(time.January))
|
||||
fmt.Println(l.MonthAbbreviated(time.February))
|
||||
fmt.Println(l.MonthAbbreviated(time.March))
|
||||
// ...
|
||||
|
||||
// Months Narrow
|
||||
fmt.Println(l.MonthNarrow(time.January))
|
||||
fmt.Println(l.MonthNarrow(time.February))
|
||||
fmt.Println(l.MonthNarrow(time.March))
|
||||
// ...
|
||||
|
||||
// Weekdays Wide
|
||||
fmt.Println(l.WeekdayWide(time.Sunday))
|
||||
fmt.Println(l.WeekdayWide(time.Monday))
|
||||
fmt.Println(l.WeekdayWide(time.Tuesday))
|
||||
// ...
|
||||
|
||||
// Weekdays Abbreviated
|
||||
fmt.Println(l.WeekdayAbbreviated(time.Sunday))
|
||||
fmt.Println(l.WeekdayAbbreviated(time.Monday))
|
||||
fmt.Println(l.WeekdayAbbreviated(time.Tuesday))
|
||||
// ...
|
||||
|
||||
// Weekdays Short
|
||||
fmt.Println(l.WeekdayShort(time.Sunday))
|
||||
fmt.Println(l.WeekdayShort(time.Monday))
|
||||
fmt.Println(l.WeekdayShort(time.Tuesday))
|
||||
// ...
|
||||
|
||||
// Weekdays Narrow
|
||||
fmt.Println(l.WeekdayNarrow(time.Sunday))
|
||||
fmt.Println(l.WeekdayNarrow(time.Monday))
|
||||
fmt.Println(l.WeekdayNarrow(time.Tuesday))
|
||||
// ...
|
||||
|
||||
var f64 float64
|
||||
|
||||
f64 = -10356.4523
|
||||
|
||||
// Number
|
||||
fmt.Println(l.FmtNumber(f64, 2))
|
||||
|
||||
// Currency
|
||||
fmt.Println(l.FmtCurrency(f64, 2, currency.CAD))
|
||||
fmt.Println(l.FmtCurrency(f64, 2, currency.USD))
|
||||
|
||||
// Accounting
|
||||
fmt.Println(l.FmtAccounting(f64, 2, currency.CAD))
|
||||
fmt.Println(l.FmtAccounting(f64, 2, currency.USD))
|
||||
|
||||
f64 = 78.12
|
||||
|
||||
// Percent
|
||||
fmt.Println(l.FmtPercent(f64, 0))
|
||||
|
||||
// Plural Rules for locale, so you know what rules you must cover
|
||||
fmt.Println(l.PluralsCardinal())
|
||||
fmt.Println(l.PluralsOrdinal())
|
||||
|
||||
// Cardinal Plural Rules
|
||||
fmt.Println(l.CardinalPluralRule(1, 0))
|
||||
fmt.Println(l.CardinalPluralRule(1.0, 0))
|
||||
fmt.Println(l.CardinalPluralRule(1.0, 1))
|
||||
fmt.Println(l.CardinalPluralRule(3, 0))
|
||||
|
||||
// Ordinal Plural Rules
|
||||
fmt.Println(l.OrdinalPluralRule(21, 0)) // 21st
|
||||
fmt.Println(l.OrdinalPluralRule(22, 0)) // 22nd
|
||||
fmt.Println(l.OrdinalPluralRule(33, 0)) // 33rd
|
||||
fmt.Println(l.OrdinalPluralRule(34, 0)) // 34th
|
||||
|
||||
// Range Plural Rules
|
||||
fmt.Println(l.RangePluralRule(1, 0, 1, 0)) // 1-1
|
||||
fmt.Println(l.RangePluralRule(1, 0, 2, 0)) // 1-2
|
||||
fmt.Println(l.RangePluralRule(5, 0, 8, 0)) // 5-8
|
||||
}
|
||||
```
|
||||
|
||||
NOTES:
|
||||
-------
|
||||
These rules were generated from the [Unicode CLDR Project](http://cldr.unicode.org/), if you encounter any issues
|
||||
I strongly encourage contributing to the CLDR project to get the locale information corrected and the next time
|
||||
these locales are regenerated the fix will come with.
|
||||
|
||||
I do however realize that time constraints are often important and so there are two options:
|
||||
|
||||
1. Create your own locale, copy, paste and modify, and ensure it complies with the `Translator` interface.
|
||||
2. Add an exception in the locale generation code directly and once regenerated, fix will be in place.
|
||||
|
||||
Please to not make fixes inside the locale files, they WILL get overwritten when the locales are regenerated.
|
||||
|
||||
License
|
||||
------
|
||||
Distributed under MIT License, please see license file in code for more details.
|
308
example-project/vendor/github.com/go-playground/locales/currency/currency.go
generated
vendored
Normal file
308
example-project/vendor/github.com/go-playground/locales/currency/currency.go
generated
vendored
Normal file
@ -0,0 +1,308 @@
|
||||
package currency
|
||||
|
||||
// Type is the currency type associated with the locales currency enum
|
||||
type Type int
|
||||
|
||||
// locale currencies
|
||||
const (
|
||||
ADP Type = iota
|
||||
AED
|
||||
AFA
|
||||
AFN
|
||||
ALK
|
||||
ALL
|
||||
AMD
|
||||
ANG
|
||||
AOA
|
||||
AOK
|
||||
AON
|
||||
AOR
|
||||
ARA
|
||||
ARL
|
||||
ARM
|
||||
ARP
|
||||
ARS
|
||||
ATS
|
||||
AUD
|
||||
AWG
|
||||
AZM
|
||||
AZN
|
||||
BAD
|
||||
BAM
|
||||
BAN
|
||||
BBD
|
||||
BDT
|
||||
BEC
|
||||
BEF
|
||||
BEL
|
||||
BGL
|
||||
BGM
|
||||
BGN
|
||||
BGO
|
||||
BHD
|
||||
BIF
|
||||
BMD
|
||||
BND
|
||||
BOB
|
||||
BOL
|
||||
BOP
|
||||
BOV
|
||||
BRB
|
||||
BRC
|
||||
BRE
|
||||
BRL
|
||||
BRN
|
||||
BRR
|
||||
BRZ
|
||||
BSD
|
||||
BTN
|
||||
BUK
|
||||
BWP
|
||||
BYB
|
||||
BYN
|
||||
BYR
|
||||
BZD
|
||||
CAD
|
||||
CDF
|
||||
CHE
|
||||
CHF
|
||||
CHW
|
||||
CLE
|
||||
CLF
|
||||
CLP
|
||||
CNH
|
||||
CNX
|
||||
CNY
|
||||
COP
|
||||
COU
|
||||
CRC
|
||||
CSD
|
||||
CSK
|
||||
CUC
|
||||
CUP
|
||||
CVE
|
||||
CYP
|
||||
CZK
|
||||
DDM
|
||||
DEM
|
||||
DJF
|
||||
DKK
|
||||
DOP
|
||||
DZD
|
||||
ECS
|
||||
ECV
|
||||
EEK
|
||||
EGP
|
||||
ERN
|
||||
ESA
|
||||
ESB
|
||||
ESP
|
||||
ETB
|
||||
EUR
|
||||
FIM
|
||||
FJD
|
||||
FKP
|
||||
FRF
|
||||
GBP
|
||||
GEK
|
||||
GEL
|
||||
GHC
|
||||
GHS
|
||||
GIP
|
||||
GMD
|
||||
GNF
|
||||
GNS
|
||||
GQE
|
||||
GRD
|
||||
GTQ
|
||||
GWE
|
||||
GWP
|
||||
GYD
|
||||
HKD
|
||||
HNL
|
||||
HRD
|
||||
HRK
|
||||
HTG
|
||||
HUF
|
||||
IDR
|
||||
IEP
|
||||
ILP
|
||||
ILR
|
||||
ILS
|
||||
INR
|
||||
IQD
|
||||
IRR
|
||||
ISJ
|
||||
ISK
|
||||
ITL
|
||||
JMD
|
||||
JOD
|
||||
JPY
|
||||
KES
|
||||
KGS
|
||||
KHR
|
||||
KMF
|
||||
KPW
|
||||
KRH
|
||||
KRO
|
||||
KRW
|
||||
KWD
|
||||
KYD
|
||||
KZT
|
||||
LAK
|
||||
LBP
|
||||
LKR
|
||||
LRD
|
||||
LSL
|
||||
LTL
|
||||
LTT
|
||||
LUC
|
||||
LUF
|
||||
LUL
|
||||
LVL
|
||||
LVR
|
||||
LYD
|
||||
MAD
|
||||
MAF
|
||||
MCF
|
||||
MDC
|
||||
MDL
|
||||
MGA
|
||||
MGF
|
||||
MKD
|
||||
MKN
|
||||
MLF
|
||||
MMK
|
||||
MNT
|
||||
MOP
|
||||
MRO
|
||||
MTL
|
||||
MTP
|
||||
MUR
|
||||
MVP
|
||||
MVR
|
||||
MWK
|
||||
MXN
|
||||
MXP
|
||||
MXV
|
||||
MYR
|
||||
MZE
|
||||
MZM
|
||||
MZN
|
||||
NAD
|
||||
NGN
|
||||
NIC
|
||||
NIO
|
||||
NLG
|
||||
NOK
|
||||
NPR
|
||||
NZD
|
||||
OMR
|
||||
PAB
|
||||
PEI
|
||||
PEN
|
||||
PES
|
||||
PGK
|
||||
PHP
|
||||
PKR
|
||||
PLN
|
||||
PLZ
|
||||
PTE
|
||||
PYG
|
||||
QAR
|
||||
RHD
|
||||
ROL
|
||||
RON
|
||||
RSD
|
||||
RUB
|
||||
RUR
|
||||
RWF
|
||||
SAR
|
||||
SBD
|
||||
SCR
|
||||
SDD
|
||||
SDG
|
||||
SDP
|
||||
SEK
|
||||
SGD
|
||||
SHP
|
||||
SIT
|
||||
SKK
|
||||
SLL
|
||||
SOS
|
||||
SRD
|
||||
SRG
|
||||
SSP
|
||||
STD
|
||||
STN
|
||||
SUR
|
||||
SVC
|
||||
SYP
|
||||
SZL
|
||||
THB
|
||||
TJR
|
||||
TJS
|
||||
TMM
|
||||
TMT
|
||||
TND
|
||||
TOP
|
||||
TPE
|
||||
TRL
|
||||
TRY
|
||||
TTD
|
||||
TWD
|
||||
TZS
|
||||
UAH
|
||||
UAK
|
||||
UGS
|
||||
UGX
|
||||
USD
|
||||
USN
|
||||
USS
|
||||
UYI
|
||||
UYP
|
||||
UYU
|
||||
UZS
|
||||
VEB
|
||||
VEF
|
||||
VND
|
||||
VNN
|
||||
VUV
|
||||
WST
|
||||
XAF
|
||||
XAG
|
||||
XAU
|
||||
XBA
|
||||
XBB
|
||||
XBC
|
||||
XBD
|
||||
XCD
|
||||
XDR
|
||||
XEU
|
||||
XFO
|
||||
XFU
|
||||
XOF
|
||||
XPD
|
||||
XPF
|
||||
XPT
|
||||
XRE
|
||||
XSU
|
||||
XTS
|
||||
XUA
|
||||
XXX
|
||||
YDD
|
||||
YER
|
||||
YUD
|
||||
YUM
|
||||
YUN
|
||||
YUR
|
||||
ZAL
|
||||
ZAR
|
||||
ZMK
|
||||
ZMW
|
||||
ZRN
|
||||
ZRZ
|
||||
ZWD
|
||||
ZWL
|
||||
ZWR
|
||||
)
|
650
example-project/vendor/github.com/go-playground/locales/en/en.go
generated
vendored
Normal file
650
example-project/vendor/github.com/go-playground/locales/en/en.go
generated
vendored
Normal file
@ -0,0 +1,650 @@
|
||||
package en
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-playground/locales"
|
||||
"github.com/go-playground/locales/currency"
|
||||
)
|
||||
|
||||
type en struct {
|
||||
locale string
|
||||
pluralsCardinal []locales.PluralRule
|
||||
pluralsOrdinal []locales.PluralRule
|
||||
pluralsRange []locales.PluralRule
|
||||
decimal string
|
||||
group string
|
||||
minus string
|
||||
percent string
|
||||
perMille string
|
||||
timeSeparator string
|
||||
inifinity string
|
||||
currencies []string // idx = enum of currency code
|
||||
currencyNegativePrefix string
|
||||
currencyNegativeSuffix string
|
||||
monthsAbbreviated []string
|
||||
monthsNarrow []string
|
||||
monthsWide []string
|
||||
daysAbbreviated []string
|
||||
daysNarrow []string
|
||||
daysShort []string
|
||||
daysWide []string
|
||||
periodsAbbreviated []string
|
||||
periodsNarrow []string
|
||||
periodsShort []string
|
||||
periodsWide []string
|
||||
erasAbbreviated []string
|
||||
erasNarrow []string
|
||||
erasWide []string
|
||||
timezones map[string]string
|
||||
}
|
||||
|
||||
// New returns a new instance of translator for the 'en' locale
|
||||
func New() locales.Translator {
|
||||
return &en{
|
||||
locale: "en",
|
||||
pluralsCardinal: []locales.PluralRule{2, 6},
|
||||
pluralsOrdinal: []locales.PluralRule{2, 3, 4, 6},
|
||||
pluralsRange: []locales.PluralRule{6},
|
||||
decimal: ".",
|
||||
group: ",",
|
||||
minus: "-",
|
||||
percent: "%",
|
||||
perMille: "‰",
|
||||
timeSeparator: ":",
|
||||
inifinity: "∞",
|
||||
currencies: []string{"ADP", "AED", "AFA", "AFN", "ALK", "ALL", "AMD", "ANG", "AOA", "AOK", "AON", "AOR", "ARA", "ARL", "ARM", "ARP", "ARS", "ATS", "AUD", "AWG", "AZM", "AZN", "BAD", "BAM", "BAN", "BBD", "BDT", "BEC", "BEF", "BEL", "BGL", "BGM", "BGN", "BGO", "BHD", "BIF", "BMD", "BND", "BOB", "BOL", "BOP", "BOV", "BRB", "BRC", "BRE", "BRL", "BRN", "BRR", "BRZ", "BSD", "BTN", "BUK", "BWP", "BYB", "BYN", "BYR", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "CNX", "CNY", "COP", "COU", "CRC", "CSD", "CSK", "CUC", "CUP", "CVE", "CYP", "CZK", "DDM", "DEM", "DJF", "DKK", "DOP", "DZD", "ECS", "ECV", "EEK", "EGP", "ERN", "ESA", "ESB", "ESP", "ETB", "EUR", "FIM", "FJD", "FKP", "FRF", "GBP", "GEK", "GEL", "GHC", "GHS", "GIP", "GMD", "GNF", "GNS", "GQE", "GRD", "GTQ", "GWE", "GWP", "GYD", "HKD", "HNL", "HRD", "HRK", "HTG", "HUF", "IDR", "IEP", "ILP", "ILR", "ILS", "INR", "IQD", "IRR", "ISJ", "ISK", "ITL", "JMD", "JOD", "¥", "KES", "KGS", "KHR", "KMF", "KPW", "KRH", "KRO", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LTL", "LTT", "LUC", "LUF", "LUL", "LVL", "LVR", "LYD", "MAD", "MAF", "MCF", "MDC", "MDL", "MGA", "MGF", "MKD", "MKN", "MLF", "MMK", "MNT", "MOP", "MRO", "MTL", "MTP", "MUR", "MVP", "MVR", "MWK", "MXN", "MXP", "MXV", "MYR", "MZE", "MZM", "MZN", "NAD", "NGN", "NIC", "NIO", "NLG", "NOK", "NPR", "NZD", "OMR", "PAB", "PEI", "PEN", "PES", "PGK", "PHP", "PKR", "PLN", "PLZ", "PTE", "PYG", "QAR", "RHD", "ROL", "RON", "RSD", "RUB", "RUR", "RWF", "SAR", "SBD", "SCR", "SDD", "SDG", "SDP", "SEK", "SGD", "SHP", "SIT", "SKK", "SLL", "SOS", "SRD", "SRG", "SSP", "STD", "STN", "SUR", "SVC", "SYP", "SZL", "THB", "TJR", "TJS", "TMM", "TMT", "TND", "TOP", "TPE", "TRL", "TRY", "TTD", "TWD", "TZS", "UAH", "UAK", "UGS", "UGX", "$", "USN", "USS", "UYI", "UYP", "UYU", "UZS", "VEB", "VEF", "VND", "VNN", "VUV", "WST", "XAF", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "XCD", "XDR", "XEU", "XFO", "XFU", "XOF", "XPD", "XPF", "XPT", "XRE", "XSU", "XTS", "XUA", "XXX", "YDD", "YER", "YUD", "YUM", "YUN", "YUR", "ZAL", "ZAR", "ZMK", "ZMW", "ZRN", "ZRZ", "ZWD", "ZWL", "ZWR"},
|
||||
currencyNegativePrefix: "(",
|
||||
currencyNegativeSuffix: ")",
|
||||
monthsAbbreviated: []string{"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"},
|
||||
monthsNarrow: []string{"", "J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"},
|
||||
monthsWide: []string{"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"},
|
||||
daysAbbreviated: []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"},
|
||||
daysNarrow: []string{"S", "M", "T", "W", "T", "F", "S"},
|
||||
daysShort: []string{"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"},
|
||||
daysWide: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
|
||||
periodsAbbreviated: []string{"am", "pm"},
|
||||
periodsNarrow: []string{"a", "p"},
|
||||
periodsWide: []string{"am", "pm"},
|
||||
erasAbbreviated: []string{"BC", "AD"},
|
||||
erasNarrow: []string{"B", "A"},
|
||||
erasWide: []string{"Before Christ", "Anno Domini"},
|
||||
timezones: map[string]string{"IST": "India Standard Time", "WARST": "Western Argentina Summer Time", "HNT": "Newfoundland Standard Time", "VET": "Venezuela Time", "HAST": "Hawaii-Aleutian Standard Time", "CDT": "Central Daylight Time", "HEEG": "East Greenland Summer Time", "HKT": "Hong Kong Standard Time", "SGT": "Singapore Standard Time", "EDT": "Eastern Daylight Time", "HEPM": "St. Pierre & Miquelon Daylight Time", "CST": "Central Standard Time", "HNPMX": "Mexican Pacific Standard Time", "WAT": "West Africa Standard Time", "JDT": "Japan Daylight Time", "ACST": "Australian Central Standard Time", "TMST": "Turkmenistan Summer Time", "LHDT": "Lord Howe Daylight Time", "CAT": "Central Africa Time", "UYT": "Uruguay Standard Time", "HEPMX": "Mexican Pacific Daylight Time", "WEZ": "Western European Standard Time", "BOT": "Bolivia Time", "GFT": "French Guiana Time", "HNNOMX": "Northwest Mexico Standard Time", "OEZ": "Eastern European Standard Time", "AEST": "Australian Eastern Standard Time", "MDT": "Mountain Daylight Time", "SAST": "South Africa Standard Time", "BT": "Bhutan Time", "SRT": "Suriname Time", "TMT": "Turkmenistan Standard Time", "CHADT": "Chatham Daylight Time", "PST": "Pacific Standard Time", "ADT": "Atlantic Daylight Time", "HENOMX": "Northwest Mexico Daylight Time", "EAT": "East Africa Time", "CLT": "Chile Standard Time", "∅∅∅": "Brasilia Summer Time", "WESZ": "Western European Summer Time", "HAT": "Newfoundland Daylight Time", "WIB": "Western Indonesia Time", "NZST": "New Zealand Standard Time", "HNEG": "East Greenland Standard Time", "HNPM": "St. Pierre & Miquelon Standard Time", "WITA": "Central Indonesia Time", "GMT": "Greenwich Mean Time", "UYST": "Uruguay Summer Time", "HNCU": "Cuba Standard Time", "GYT": "Guyana Time", "MYT": "Malaysia Time", "COT": "Colombia Standard Time", "AST": "Atlantic Standard Time", "ACDT": "Australian Central Daylight Time", "MEZ": "Central European Standard Time", "AKDT": "Alaska Daylight Time", "EST": "Eastern Standard Time", "HNOG": "West Greenland Standard Time", "ECT": "Ecuador Time", "ART": "Argentina Standard Time", "HEOG": "West Greenland Summer Time", "MESZ": "Central European Summer Time", "LHST": "Lord Howe Standard Time", "OESZ": "Eastern European Summer Time", "AWST": "Australian Western Standard Time", "AEDT": "Australian Eastern Daylight Time", "ACWDT": "Australian Central Western Daylight Time", "NZDT": "New Zealand Daylight Time", "JST": "Japan Standard Time", "WART": "Western Argentina Standard Time", "CHAST": "Chatham Standard Time", "HECU": "Cuba Daylight Time", "WAST": "West Africa Summer Time", "ACWST": "Australian Central Western Standard Time", "HKST": "Hong Kong Summer Time", "ARST": "Argentina Summer Time", "MST": "Mountain Standard Time", "AKST": "Alaska Standard Time", "CLST": "Chile Summer Time", "WIT": "Eastern Indonesia Time", "HADT": "Hawaii-Aleutian Daylight Time", "ChST": "Chamorro Standard Time", "PDT": "Pacific Daylight Time", "AWDT": "Australian Western Daylight Time", "COST": "Colombia Summer Time"},
|
||||
}
|
||||
}
|
||||
|
||||
// Locale returns the current translators string locale
|
||||
func (en *en) Locale() string {
|
||||
return en.locale
|
||||
}
|
||||
|
||||
// PluralsCardinal returns the list of cardinal plural rules associated with 'en'
|
||||
func (en *en) PluralsCardinal() []locales.PluralRule {
|
||||
return en.pluralsCardinal
|
||||
}
|
||||
|
||||
// PluralsOrdinal returns the list of ordinal plural rules associated with 'en'
|
||||
func (en *en) PluralsOrdinal() []locales.PluralRule {
|
||||
return en.pluralsOrdinal
|
||||
}
|
||||
|
||||
// PluralsRange returns the list of range plural rules associated with 'en'
|
||||
func (en *en) PluralsRange() []locales.PluralRule {
|
||||
return en.pluralsRange
|
||||
}
|
||||
|
||||
// CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'en'
|
||||
func (en *en) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
|
||||
|
||||
n := math.Abs(num)
|
||||
i := int64(n)
|
||||
|
||||
if i == 1 && v == 0 {
|
||||
return locales.PluralRuleOne
|
||||
}
|
||||
|
||||
return locales.PluralRuleOther
|
||||
}
|
||||
|
||||
// OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'en'
|
||||
func (en *en) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
|
||||
|
||||
n := math.Abs(num)
|
||||
nMod100 := math.Mod(n, 100)
|
||||
nMod10 := math.Mod(n, 10)
|
||||
|
||||
if nMod10 == 1 && nMod100 != 11 {
|
||||
return locales.PluralRuleOne
|
||||
} else if nMod10 == 2 && nMod100 != 12 {
|
||||
return locales.PluralRuleTwo
|
||||
} else if nMod10 == 3 && nMod100 != 13 {
|
||||
return locales.PluralRuleFew
|
||||
}
|
||||
|
||||
return locales.PluralRuleOther
|
||||
}
|
||||
|
||||
// RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'en'
|
||||
func (en *en) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
|
||||
return locales.PluralRuleOther
|
||||
}
|
||||
|
||||
// MonthAbbreviated returns the locales abbreviated month given the 'month' provided
|
||||
func (en *en) MonthAbbreviated(month time.Month) string {
|
||||
return en.monthsAbbreviated[month]
|
||||
}
|
||||
|
||||
// MonthsAbbreviated returns the locales abbreviated months
|
||||
func (en *en) MonthsAbbreviated() []string {
|
||||
return en.monthsAbbreviated[1:]
|
||||
}
|
||||
|
||||
// MonthNarrow returns the locales narrow month given the 'month' provided
|
||||
func (en *en) MonthNarrow(month time.Month) string {
|
||||
return en.monthsNarrow[month]
|
||||
}
|
||||
|
||||
// MonthsNarrow returns the locales narrow months
|
||||
func (en *en) MonthsNarrow() []string {
|
||||
return en.monthsNarrow[1:]
|
||||
}
|
||||
|
||||
// MonthWide returns the locales wide month given the 'month' provided
|
||||
func (en *en) MonthWide(month time.Month) string {
|
||||
return en.monthsWide[month]
|
||||
}
|
||||
|
||||
// MonthsWide returns the locales wide months
|
||||
func (en *en) MonthsWide() []string {
|
||||
return en.monthsWide[1:]
|
||||
}
|
||||
|
||||
// WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
|
||||
func (en *en) WeekdayAbbreviated(weekday time.Weekday) string {
|
||||
return en.daysAbbreviated[weekday]
|
||||
}
|
||||
|
||||
// WeekdaysAbbreviated returns the locales abbreviated weekdays
|
||||
func (en *en) WeekdaysAbbreviated() []string {
|
||||
return en.daysAbbreviated
|
||||
}
|
||||
|
||||
// WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
|
||||
func (en *en) WeekdayNarrow(weekday time.Weekday) string {
|
||||
return en.daysNarrow[weekday]
|
||||
}
|
||||
|
||||
// WeekdaysNarrow returns the locales narrow weekdays
|
||||
func (en *en) WeekdaysNarrow() []string {
|
||||
return en.daysNarrow
|
||||
}
|
||||
|
||||
// WeekdayShort returns the locales short weekday given the 'weekday' provided
|
||||
func (en *en) WeekdayShort(weekday time.Weekday) string {
|
||||
return en.daysShort[weekday]
|
||||
}
|
||||
|
||||
// WeekdaysShort returns the locales short weekdays
|
||||
func (en *en) WeekdaysShort() []string {
|
||||
return en.daysShort
|
||||
}
|
||||
|
||||
// WeekdayWide returns the locales wide weekday given the 'weekday' provided
|
||||
func (en *en) WeekdayWide(weekday time.Weekday) string {
|
||||
return en.daysWide[weekday]
|
||||
}
|
||||
|
||||
// WeekdaysWide returns the locales wide weekdays
|
||||
func (en *en) WeekdaysWide() []string {
|
||||
return en.daysWide
|
||||
}
|
||||
|
||||
// Decimal returns the decimal point of number
|
||||
func (en *en) Decimal() string {
|
||||
return en.decimal
|
||||
}
|
||||
|
||||
// Group returns the group of number
|
||||
func (en *en) Group() string {
|
||||
return en.group
|
||||
}
|
||||
|
||||
// Group returns the minus sign of number
|
||||
func (en *en) Minus() string {
|
||||
return en.minus
|
||||
}
|
||||
|
||||
// FmtNumber returns 'num' with digits/precision of 'v' for 'en' and handles both Whole and Real numbers based on 'v'
|
||||
func (en *en) FmtNumber(num float64, v uint64) string {
|
||||
|
||||
s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
|
||||
l := len(s) + 2 + 1*len(s[:len(s)-int(v)-1])/3
|
||||
count := 0
|
||||
inWhole := v == 0
|
||||
b := make([]byte, 0, l)
|
||||
|
||||
for i := len(s) - 1; i >= 0; i-- {
|
||||
|
||||
if s[i] == '.' {
|
||||
b = append(b, en.decimal[0])
|
||||
inWhole = true
|
||||
continue
|
||||
}
|
||||
|
||||
if inWhole {
|
||||
if count == 3 {
|
||||
b = append(b, en.group[0])
|
||||
count = 1
|
||||
} else {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
b = append(b, s[i])
|
||||
}
|
||||
|
||||
if num < 0 {
|
||||
b = append(b, en.minus[0])
|
||||
}
|
||||
|
||||
// reverse
|
||||
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
|
||||
b[i], b[j] = b[j], b[i]
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// FmtPercent returns 'num' with digits/precision of 'v' for 'en' and handles both Whole and Real numbers based on 'v'
|
||||
// NOTE: 'num' passed into FmtPercent is assumed to be in percent already
|
||||
func (en *en) FmtPercent(num float64, v uint64) string {
|
||||
s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
|
||||
l := len(s) + 3
|
||||
b := make([]byte, 0, l)
|
||||
|
||||
for i := len(s) - 1; i >= 0; i-- {
|
||||
|
||||
if s[i] == '.' {
|
||||
b = append(b, en.decimal[0])
|
||||
continue
|
||||
}
|
||||
|
||||
b = append(b, s[i])
|
||||
}
|
||||
|
||||
if num < 0 {
|
||||
b = append(b, en.minus[0])
|
||||
}
|
||||
|
||||
// reverse
|
||||
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
|
||||
b[i], b[j] = b[j], b[i]
|
||||
}
|
||||
|
||||
b = append(b, en.percent...)
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'en'
|
||||
func (en *en) FmtCurrency(num float64, v uint64, currency currency.Type) string {
|
||||
|
||||
s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
|
||||
symbol := en.currencies[currency]
|
||||
l := len(s) + len(symbol) + 2 + 1*len(s[:len(s)-int(v)-1])/3
|
||||
count := 0
|
||||
inWhole := v == 0
|
||||
b := make([]byte, 0, l)
|
||||
|
||||
for i := len(s) - 1; i >= 0; i-- {
|
||||
|
||||
if s[i] == '.' {
|
||||
b = append(b, en.decimal[0])
|
||||
inWhole = true
|
||||
continue
|
||||
}
|
||||
|
||||
if inWhole {
|
||||
if count == 3 {
|
||||
b = append(b, en.group[0])
|
||||
count = 1
|
||||
} else {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
b = append(b, s[i])
|
||||
}
|
||||
|
||||
for j := len(symbol) - 1; j >= 0; j-- {
|
||||
b = append(b, symbol[j])
|
||||
}
|
||||
|
||||
if num < 0 {
|
||||
b = append(b, en.minus[0])
|
||||
}
|
||||
|
||||
// reverse
|
||||
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
|
||||
b[i], b[j] = b[j], b[i]
|
||||
}
|
||||
|
||||
if int(v) < 2 {
|
||||
|
||||
if v == 0 {
|
||||
b = append(b, en.decimal...)
|
||||
}
|
||||
|
||||
for i := 0; i < 2-int(v); i++ {
|
||||
b = append(b, '0')
|
||||
}
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'en'
|
||||
// in accounting notation.
|
||||
func (en *en) FmtAccounting(num float64, v uint64, currency currency.Type) string {
|
||||
|
||||
s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
|
||||
symbol := en.currencies[currency]
|
||||
l := len(s) + len(symbol) + 4 + 1*len(s[:len(s)-int(v)-1])/3
|
||||
count := 0
|
||||
inWhole := v == 0
|
||||
b := make([]byte, 0, l)
|
||||
|
||||
for i := len(s) - 1; i >= 0; i-- {
|
||||
|
||||
if s[i] == '.' {
|
||||
b = append(b, en.decimal[0])
|
||||
inWhole = true
|
||||
continue
|
||||
}
|
||||
|
||||
if inWhole {
|
||||
if count == 3 {
|
||||
b = append(b, en.group[0])
|
||||
count = 1
|
||||
} else {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
b = append(b, s[i])
|
||||
}
|
||||
|
||||
if num < 0 {
|
||||
|
||||
for j := len(symbol) - 1; j >= 0; j-- {
|
||||
b = append(b, symbol[j])
|
||||
}
|
||||
|
||||
b = append(b, en.currencyNegativePrefix[0])
|
||||
|
||||
} else {
|
||||
|
||||
for j := len(symbol) - 1; j >= 0; j-- {
|
||||
b = append(b, symbol[j])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// reverse
|
||||
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
|
||||
b[i], b[j] = b[j], b[i]
|
||||
}
|
||||
|
||||
if int(v) < 2 {
|
||||
|
||||
if v == 0 {
|
||||
b = append(b, en.decimal...)
|
||||
}
|
||||
|
||||
for i := 0; i < 2-int(v); i++ {
|
||||
b = append(b, '0')
|
||||
}
|
||||
}
|
||||
|
||||
if num < 0 {
|
||||
b = append(b, en.currencyNegativeSuffix...)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// FmtDateShort returns the short date representation of 't' for 'en'
|
||||
func (en *en) FmtDateShort(t time.Time) string {
|
||||
|
||||
b := make([]byte, 0, 32)
|
||||
|
||||
b = strconv.AppendInt(b, int64(t.Month()), 10)
|
||||
b = append(b, []byte{0x2f}...)
|
||||
b = strconv.AppendInt(b, int64(t.Day()), 10)
|
||||
b = append(b, []byte{0x2f}...)
|
||||
|
||||
if t.Year() > 9 {
|
||||
b = append(b, strconv.Itoa(t.Year())[2:]...)
|
||||
} else {
|
||||
b = append(b, strconv.Itoa(t.Year())[1:]...)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// FmtDateMedium returns the medium date representation of 't' for 'en'
|
||||
func (en *en) FmtDateMedium(t time.Time) string {
|
||||
|
||||
b := make([]byte, 0, 32)
|
||||
|
||||
b = append(b, en.monthsAbbreviated[t.Month()]...)
|
||||
b = append(b, []byte{0x20}...)
|
||||
b = strconv.AppendInt(b, int64(t.Day()), 10)
|
||||
b = append(b, []byte{0x2c, 0x20}...)
|
||||
|
||||
if t.Year() > 0 {
|
||||
b = strconv.AppendInt(b, int64(t.Year()), 10)
|
||||
} else {
|
||||
b = strconv.AppendInt(b, int64(-t.Year()), 10)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// FmtDateLong returns the long date representation of 't' for 'en'
|
||||
func (en *en) FmtDateLong(t time.Time) string {
|
||||
|
||||
b := make([]byte, 0, 32)
|
||||
|
||||
b = append(b, en.monthsWide[t.Month()]...)
|
||||
b = append(b, []byte{0x20}...)
|
||||
b = strconv.AppendInt(b, int64(t.Day()), 10)
|
||||
b = append(b, []byte{0x2c, 0x20}...)
|
||||
|
||||
if t.Year() > 0 {
|
||||
b = strconv.AppendInt(b, int64(t.Year()), 10)
|
||||
} else {
|
||||
b = strconv.AppendInt(b, int64(-t.Year()), 10)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// FmtDateFull returns the full date representation of 't' for 'en'
|
||||
func (en *en) FmtDateFull(t time.Time) string {
|
||||
|
||||
b := make([]byte, 0, 32)
|
||||
|
||||
b = append(b, en.daysWide[t.Weekday()]...)
|
||||
b = append(b, []byte{0x2c, 0x20}...)
|
||||
b = append(b, en.monthsWide[t.Month()]...)
|
||||
b = append(b, []byte{0x20}...)
|
||||
b = strconv.AppendInt(b, int64(t.Day()), 10)
|
||||
b = append(b, []byte{0x2c, 0x20}...)
|
||||
|
||||
if t.Year() > 0 {
|
||||
b = strconv.AppendInt(b, int64(t.Year()), 10)
|
||||
} else {
|
||||
b = strconv.AppendInt(b, int64(-t.Year()), 10)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// FmtTimeShort returns the short time representation of 't' for 'en'
|
||||
func (en *en) FmtTimeShort(t time.Time) string {
|
||||
|
||||
b := make([]byte, 0, 32)
|
||||
|
||||
h := t.Hour()
|
||||
|
||||
if h > 12 {
|
||||
h -= 12
|
||||
}
|
||||
|
||||
b = strconv.AppendInt(b, int64(h), 10)
|
||||
b = append(b, en.timeSeparator...)
|
||||
|
||||
if t.Minute() < 10 {
|
||||
b = append(b, '0')
|
||||
}
|
||||
|
||||
b = strconv.AppendInt(b, int64(t.Minute()), 10)
|
||||
b = append(b, []byte{0x20}...)
|
||||
|
||||
if t.Hour() < 12 {
|
||||
b = append(b, en.periodsAbbreviated[0]...)
|
||||
} else {
|
||||
b = append(b, en.periodsAbbreviated[1]...)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// FmtTimeMedium returns the medium time representation of 't' for 'en'
|
||||
func (en *en) FmtTimeMedium(t time.Time) string {
|
||||
|
||||
b := make([]byte, 0, 32)
|
||||
|
||||
h := t.Hour()
|
||||
|
||||
if h > 12 {
|
||||
h -= 12
|
||||
}
|
||||
|
||||
b = strconv.AppendInt(b, int64(h), 10)
|
||||
b = append(b, en.timeSeparator...)
|
||||
|
||||
if t.Minute() < 10 {
|
||||
b = append(b, '0')
|
||||
}
|
||||
|
||||
b = strconv.AppendInt(b, int64(t.Minute()), 10)
|
||||
b = append(b, en.timeSeparator...)
|
||||
|
||||
if t.Second() < 10 {
|
||||
b = append(b, '0')
|
||||
}
|
||||
|
||||
b = strconv.AppendInt(b, int64(t.Second()), 10)
|
||||
b = append(b, []byte{0x20}...)
|
||||
|
||||
if t.Hour() < 12 {
|
||||
b = append(b, en.periodsAbbreviated[0]...)
|
||||
} else {
|
||||
b = append(b, en.periodsAbbreviated[1]...)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// FmtTimeLong returns the long time representation of 't' for 'en'
|
||||
func (en *en) FmtTimeLong(t time.Time) string {
|
||||
|
||||
b := make([]byte, 0, 32)
|
||||
|
||||
h := t.Hour()
|
||||
|
||||
if h > 12 {
|
||||
h -= 12
|
||||
}
|
||||
|
||||
b = strconv.AppendInt(b, int64(h), 10)
|
||||
b = append(b, en.timeSeparator...)
|
||||
|
||||
if t.Minute() < 10 {
|
||||
b = append(b, '0')
|
||||
}
|
||||
|
||||
b = strconv.AppendInt(b, int64(t.Minute()), 10)
|
||||
b = append(b, en.timeSeparator...)
|
||||
|
||||
if t.Second() < 10 {
|
||||
b = append(b, '0')
|
||||
}
|
||||
|
||||
b = strconv.AppendInt(b, int64(t.Second()), 10)
|
||||
b = append(b, []byte{0x20}...)
|
||||
|
||||
if t.Hour() < 12 {
|
||||
b = append(b, en.periodsAbbreviated[0]...)
|
||||
} else {
|
||||
b = append(b, en.periodsAbbreviated[1]...)
|
||||
}
|
||||
|
||||
b = append(b, []byte{0x20}...)
|
||||
|
||||
tz, _ := t.Zone()
|
||||
b = append(b, tz...)
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// FmtTimeFull returns the full time representation of 't' for 'en'
|
||||
func (en *en) FmtTimeFull(t time.Time) string {
|
||||
|
||||
b := make([]byte, 0, 32)
|
||||
|
||||
h := t.Hour()
|
||||
|
||||
if h > 12 {
|
||||
h -= 12
|
||||
}
|
||||
|
||||
b = strconv.AppendInt(b, int64(h), 10)
|
||||
b = append(b, en.timeSeparator...)
|
||||
|
||||
if t.Minute() < 10 {
|
||||
b = append(b, '0')
|
||||
}
|
||||
|
||||
b = strconv.AppendInt(b, int64(t.Minute()), 10)
|
||||
b = append(b, en.timeSeparator...)
|
||||
|
||||
if t.Second() < 10 {
|
||||
b = append(b, '0')
|
||||
}
|
||||
|
||||
b = strconv.AppendInt(b, int64(t.Second()), 10)
|
||||
b = append(b, []byte{0x20}...)
|
||||
|
||||
if t.Hour() < 12 {
|
||||
b = append(b, en.periodsAbbreviated[0]...)
|
||||
} else {
|
||||
b = append(b, en.periodsAbbreviated[1]...)
|
||||
}
|
||||
|
||||
b = append(b, []byte{0x20}...)
|
||||
|
||||
tz, _ := t.Zone()
|
||||
|
||||
if btz, ok := en.timezones[tz]; ok {
|
||||
b = append(b, btz...)
|
||||
} else {
|
||||
b = append(b, tz...)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
BIN
example-project/vendor/github.com/go-playground/locales/logo.png
generated
vendored
Normal file
BIN
example-project/vendor/github.com/go-playground/locales/logo.png
generated
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 36 KiB |
293
example-project/vendor/github.com/go-playground/locales/rules.go
generated
vendored
Normal file
293
example-project/vendor/github.com/go-playground/locales/rules.go
generated
vendored
Normal file
@ -0,0 +1,293 @@
|
||||
package locales
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-playground/locales/currency"
|
||||
)
|
||||
|
||||
// // ErrBadNumberValue is returned when the number passed for
|
||||
// // plural rule determination cannot be parsed
|
||||
// type ErrBadNumberValue struct {
|
||||
// NumberValue string
|
||||
// InnerError error
|
||||
// }
|
||||
|
||||
// // Error returns ErrBadNumberValue error string
|
||||
// func (e *ErrBadNumberValue) Error() string {
|
||||
// return fmt.Sprintf("Invalid Number Value '%s' %s", e.NumberValue, e.InnerError)
|
||||
// }
|
||||
|
||||
// var _ error = new(ErrBadNumberValue)
|
||||
|
||||
// PluralRule denotes the type of plural rules
|
||||
type PluralRule int
|
||||
|
||||
// PluralRule's
|
||||
const (
|
||||
PluralRuleUnknown PluralRule = iota
|
||||
PluralRuleZero // zero
|
||||
PluralRuleOne // one - singular
|
||||
PluralRuleTwo // two - dual
|
||||
PluralRuleFew // few - paucal
|
||||
PluralRuleMany // many - also used for fractions if they have a separate class
|
||||
PluralRuleOther // other - required—general plural form—also used if the language only has a single form
|
||||
)
|
||||
|
||||
const (
|
||||
pluralsString = "UnknownZeroOneTwoFewManyOther"
|
||||
)
|
||||
|
||||
// Translator encapsulates an instance of a locale
|
||||
// NOTE: some values are returned as a []byte just in case the caller
|
||||
// wishes to add more and can help avoid allocations; otherwise just cast as string
|
||||
type Translator interface {
|
||||
|
||||
// The following Functions are for overriding, debugging or developing
|
||||
// with a Translator Locale
|
||||
|
||||
// Locale returns the string value of the translator
|
||||
Locale() string
|
||||
|
||||
// returns an array of cardinal plural rules associated
|
||||
// with this translator
|
||||
PluralsCardinal() []PluralRule
|
||||
|
||||
// returns an array of ordinal plural rules associated
|
||||
// with this translator
|
||||
PluralsOrdinal() []PluralRule
|
||||
|
||||
// returns an array of range plural rules associated
|
||||
// with this translator
|
||||
PluralsRange() []PluralRule
|
||||
|
||||
// returns the cardinal PluralRule given 'num' and digits/precision of 'v' for locale
|
||||
CardinalPluralRule(num float64, v uint64) PluralRule
|
||||
|
||||
// returns the ordinal PluralRule given 'num' and digits/precision of 'v' for locale
|
||||
OrdinalPluralRule(num float64, v uint64) PluralRule
|
||||
|
||||
// returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for locale
|
||||
RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) PluralRule
|
||||
|
||||
// returns the locales abbreviated month given the 'month' provided
|
||||
MonthAbbreviated(month time.Month) string
|
||||
|
||||
// returns the locales abbreviated months
|
||||
MonthsAbbreviated() []string
|
||||
|
||||
// returns the locales narrow month given the 'month' provided
|
||||
MonthNarrow(month time.Month) string
|
||||
|
||||
// returns the locales narrow months
|
||||
MonthsNarrow() []string
|
||||
|
||||
// returns the locales wide month given the 'month' provided
|
||||
MonthWide(month time.Month) string
|
||||
|
||||
// returns the locales wide months
|
||||
MonthsWide() []string
|
||||
|
||||
// returns the locales abbreviated weekday given the 'weekday' provided
|
||||
WeekdayAbbreviated(weekday time.Weekday) string
|
||||
|
||||
// returns the locales abbreviated weekdays
|
||||
WeekdaysAbbreviated() []string
|
||||
|
||||
// returns the locales narrow weekday given the 'weekday' provided
|
||||
WeekdayNarrow(weekday time.Weekday) string
|
||||
|
||||
// WeekdaysNarrowreturns the locales narrow weekdays
|
||||
WeekdaysNarrow() []string
|
||||
|
||||
// returns the locales short weekday given the 'weekday' provided
|
||||
WeekdayShort(weekday time.Weekday) string
|
||||
|
||||
// returns the locales short weekdays
|
||||
WeekdaysShort() []string
|
||||
|
||||
// returns the locales wide weekday given the 'weekday' provided
|
||||
WeekdayWide(weekday time.Weekday) string
|
||||
|
||||
// returns the locales wide weekdays
|
||||
WeekdaysWide() []string
|
||||
|
||||
// The following Functions are common Formatting functionsfor the Translator's Locale
|
||||
|
||||
// returns 'num' with digits/precision of 'v' for locale and handles both Whole and Real numbers based on 'v'
|
||||
FmtNumber(num float64, v uint64) string
|
||||
|
||||
// returns 'num' with digits/precision of 'v' for locale and handles both Whole and Real numbers based on 'v'
|
||||
// NOTE: 'num' passed into FmtPercent is assumed to be in percent already
|
||||
FmtPercent(num float64, v uint64) string
|
||||
|
||||
// returns the currency representation of 'num' with digits/precision of 'v' for locale
|
||||
FmtCurrency(num float64, v uint64, currency currency.Type) string
|
||||
|
||||
// returns the currency representation of 'num' with digits/precision of 'v' for locale
|
||||
// in accounting notation.
|
||||
FmtAccounting(num float64, v uint64, currency currency.Type) string
|
||||
|
||||
// returns the short date representation of 't' for locale
|
||||
FmtDateShort(t time.Time) string
|
||||
|
||||
// returns the medium date representation of 't' for locale
|
||||
FmtDateMedium(t time.Time) string
|
||||
|
||||
// returns the long date representation of 't' for locale
|
||||
FmtDateLong(t time.Time) string
|
||||
|
||||
// returns the full date representation of 't' for locale
|
||||
FmtDateFull(t time.Time) string
|
||||
|
||||
// returns the short time representation of 't' for locale
|
||||
FmtTimeShort(t time.Time) string
|
||||
|
||||
// returns the medium time representation of 't' for locale
|
||||
FmtTimeMedium(t time.Time) string
|
||||
|
||||
// returns the long time representation of 't' for locale
|
||||
FmtTimeLong(t time.Time) string
|
||||
|
||||
// returns the full time representation of 't' for locale
|
||||
FmtTimeFull(t time.Time) string
|
||||
}
|
||||
|
||||
// String returns the string value of PluralRule
|
||||
func (p PluralRule) String() string {
|
||||
|
||||
switch p {
|
||||
case PluralRuleZero:
|
||||
return pluralsString[7:11]
|
||||
case PluralRuleOne:
|
||||
return pluralsString[11:14]
|
||||
case PluralRuleTwo:
|
||||
return pluralsString[14:17]
|
||||
case PluralRuleFew:
|
||||
return pluralsString[17:20]
|
||||
case PluralRuleMany:
|
||||
return pluralsString[20:24]
|
||||
case PluralRuleOther:
|
||||
return pluralsString[24:]
|
||||
default:
|
||||
return pluralsString[:7]
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Precision Notes:
|
||||
//
|
||||
// must specify a precision >= 0, and here is why https://play.golang.org/p/LyL90U0Vyh
|
||||
//
|
||||
// v := float64(3.141)
|
||||
// i := float64(int64(v))
|
||||
//
|
||||
// fmt.Println(v - i)
|
||||
//
|
||||
// or
|
||||
//
|
||||
// s := strconv.FormatFloat(v-i, 'f', -1, 64)
|
||||
// fmt.Println(s)
|
||||
//
|
||||
// these will not print what you'd expect: 0.14100000000000001
|
||||
// and so this library requires a precision to be specified, or
|
||||
// inaccurate plural rules could be applied.
|
||||
//
|
||||
//
|
||||
//
|
||||
// n - absolute value of the source number (integer and decimals).
|
||||
// i - integer digits of n.
|
||||
// v - number of visible fraction digits in n, with trailing zeros.
|
||||
// w - number of visible fraction digits in n, without trailing zeros.
|
||||
// f - visible fractional digits in n, with trailing zeros.
|
||||
// t - visible fractional digits in n, without trailing zeros.
|
||||
//
|
||||
//
|
||||
// Func(num float64, v uint64) // v = digits/precision and prevents -1 as a special case as this can lead to very unexpected behaviour, see precision note's above.
|
||||
//
|
||||
// n := math.Abs(num)
|
||||
// i := int64(n)
|
||||
// v := v
|
||||
//
|
||||
//
|
||||
// w := strconv.FormatFloat(num-float64(i), 'f', int(v), 64) // then parse backwards on string until no more zero's....
|
||||
// f := strconv.FormatFloat(n, 'f', int(v), 64) // then turn everything after decimal into an int64
|
||||
// t := strconv.FormatFloat(n, 'f', int(v), 64) // then parse backwards on string until no more zero's....
|
||||
//
|
||||
//
|
||||
//
|
||||
// General Inclusion Rules
|
||||
// - v will always be available inherently
|
||||
// - all require n
|
||||
// - w requires i
|
||||
//
|
||||
|
||||
// W returns the number of visible fraction digits in N, without trailing zeros.
|
||||
func W(n float64, v uint64) (w int64) {
|
||||
|
||||
s := strconv.FormatFloat(n-float64(int64(n)), 'f', int(v), 64)
|
||||
|
||||
// with either be '0' or '0.xxxx', so if 1 then w will be zero
|
||||
// otherwise need to parse
|
||||
if len(s) != 1 {
|
||||
|
||||
s = s[2:]
|
||||
end := len(s) + 1
|
||||
|
||||
for i := end; i >= 0; i-- {
|
||||
if s[i] != '0' {
|
||||
end = i + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
w = int64(len(s[:end]))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// F returns the visible fractional digits in N, with trailing zeros.
|
||||
func F(n float64, v uint64) (f int64) {
|
||||
|
||||
s := strconv.FormatFloat(n-float64(int64(n)), 'f', int(v), 64)
|
||||
|
||||
// with either be '0' or '0.xxxx', so if 1 then f will be zero
|
||||
// otherwise need to parse
|
||||
if len(s) != 1 {
|
||||
|
||||
// ignoring error, because it can't fail as we generated
|
||||
// the string internally from a real number
|
||||
f, _ = strconv.ParseInt(s[2:], 10, 64)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// T returns the visible fractional digits in N, without trailing zeros.
|
||||
func T(n float64, v uint64) (t int64) {
|
||||
|
||||
s := strconv.FormatFloat(n-float64(int64(n)), 'f', int(v), 64)
|
||||
|
||||
// with either be '0' or '0.xxxx', so if 1 then t will be zero
|
||||
// otherwise need to parse
|
||||
if len(s) != 1 {
|
||||
|
||||
s = s[2:]
|
||||
end := len(s) + 1
|
||||
|
||||
for i := end; i >= 0; i-- {
|
||||
if s[i] != '0' {
|
||||
end = i + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// ignoring error, because it can't fail as we generated
|
||||
// the string internally from a real number
|
||||
t, _ = strconv.ParseInt(s[:end], 10, 64)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
24
example-project/vendor/github.com/go-playground/universal-translator/.gitignore
generated
vendored
Normal file
24
example-project/vendor/github.com/go-playground/universal-translator/.gitignore
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user