Skip to content

Commit

Permalink
adding drop to my github
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Adeniyi authored and Stephen Adeniyi committed Apr 22, 2021
0 parents commit fea91c5
Show file tree
Hide file tree
Showing 888 changed files with 187,077 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
scripts/compose/
scripts/docker/
scripts/k8s/
.dockerignore
.gitgnore
docker-compose.yml
LICENSE
makefile
README.md
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
23 changes: 23 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This is the official list of people who can contribute
# (and typically have contributed) code to the drop 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.

Adeniyi Stephen <[email protected]>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# drop
An application to find studios near you and review your favorite studio's
10 changes: 10 additions & 0 deletions app/drop-admin/commands/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Package commands contains the functionality for the set of commands
// currently supported by the CLI tooling.
package commands

import (
"github.com/pkg/errors"
)

// ErrHelp provides context that help was given.
var ErrHelp = errors.New("provided help")
67 changes: 67 additions & 0 deletions app/drop-admin/commands/genkey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package commands

import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"

"github.com/pkg/errors"
)

// GenKey creates an x509 private/public key for auth tokens.
func GenKey() error {

// Generate a new private key.
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return ErrHelp
}

// Create a file for the private key information in PEM form.
privateFile, err := os.Create("private.pem")
if err != nil {
return errors.Wrap(err, "creating private file")
}
defer privateFile.Close()

// Construct a PEM block for the private key.
privateBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}

// Write the private key to the private key file.
if err := pem.Encode(privateFile, &privateBlock); err != nil {
return errors.Wrap(err, "encoding to private file")
}

// Marshal the public key from the private key to PKIX.
asn1Bytes, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey)
if err != nil {
return errors.Wrap(err, "marshaling public key")
}

// Create a file for the public key information in PEM form.
publicFile, err := os.Create("public.pem")
if err != nil {
return errors.Wrap(err, "creating public file")
}
defer publicFile.Close()

// Construct a PEM block for the public key.
publicBlock := pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: asn1Bytes,
}

// Write the public key to the private key file.
if err := pem.Encode(publicFile, &publicBlock); err != nil {
return errors.Wrap(err, "encoding to public file")
}

fmt.Println("private and public key files generated")
return nil
}
80 changes: 80 additions & 0 deletions app/drop-admin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"fmt"
"log"
"os"

"github.com/ardanlabs/conf"
"github.com/nextwavedevs/drop/app/drop-admin/commands"
"github.com/pkg/errors"
)

// build is the git version of this program. It is set using build flags in the makefile.
var build = "develop"

func main() {
log := log.New(os.Stdout, "ADMIN : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)

if err := run(log); err != nil {
if errors.Cause(err) != commands.ErrHelp {
log.Printf("error: %s", err)
}
os.Exit(1)
}
}

func run(log *log.Logger) error {

// =========================================================================
// Configuration

var cfg struct {
conf.Version
Args conf.Args
}
cfg.Version.SVN = build
cfg.Version.Desc = "copyright information here"

const prefix = "SALES"
if err := conf.Parse(os.Args[1:], prefix, &cfg); err != nil {
switch err {
case conf.ErrHelpWanted:
usage, err := conf.Usage(prefix, &cfg)
if err != nil {
return errors.Wrap(err, "generating config usage")
}
fmt.Println(usage)
return nil
case conf.ErrVersionWanted:
version, err := conf.VersionString(prefix, &cfg)
if err != nil {
return errors.Wrap(err, "generating config version")
}
fmt.Println(version)
return nil
}
return errors.Wrap(err, "parsing config")
}

out, err := conf.String(&cfg)
if err != nil {
return errors.Wrap(err, "generating config for output")
}
log.Printf("main: Config :\n%v\n", out)

// ========================================================
// Commands

switch cfg.Args.Num(0) {
case "genkey":
if err := commands.GenKey(); err != nil {
return errors.Wrap(err, "key generation")
}

fmt.Println("genkey: generate a set of private/public key files")
return commands.ErrHelp
}

return nil
}
67 changes: 67 additions & 0 deletions app/drop-api/handlers/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package handlers

import (
"context"
"net/http"
"os"
"time"

"github.com/nextwavedevs/drop/foundation/web"
"go.mongodb.org/mongo-driver/mongo"
"go.opentelemetry.io/otel/trace"
)

type checkGroup struct {
build string
db *mongo.Client
}

func (cg checkGroup)readiness(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := trace.SpanFromContext(ctx).Tracer().Start(ctx, "handlers.check.readiness")
defer span.End()

ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()

status := "ok"
statusCode := http.StatusOK

health := struct {
Status string `json:"status"`
}{
Status: status,
}

return web.Respond(ctx, w, health, statusCode)
}

// liveness returns simple status info if the service is alive.
func (cg checkGroup) liveness(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := trace.SpanFromContext(ctx).Tracer().Start(ctx, "handlers.check.liveness")
defer span.End()

host, err := os.Hostname()
if err != nil {
host = "unavailable"
}

info := struct {
Status string `json:"status,omitempty"`
Build string `json:"build,omitempty"`
Host string `json:"host,omitempty"`
Pod string `json:"pod,omitempty"`
PodIP string `json:"podIP,omitempty"`
Node string `json:"node,omitempty"`
Namespace string `json:"namespace,omitempty"`
}{
Status: "up",
Build: cg.build,
Host: host,
Pod: os.Getenv("KUBERNETES_PODNAME"),
PodIP: os.Getenv("KUBERNETES_NAMESPACE_POD_IP"),
Node: os.Getenv("KUBERNETES_NODENAME"),
Namespace: os.Getenv("KUBERNETES_NAMESPACE"),
}

return web.Respond(ctx, w, info, http.StatusOK)
}
87 changes: 87 additions & 0 deletions app/drop-api/handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Package handlers contains the full set of handler functions and routes
// supported by the web api.
package handlers

import (
"context"
"log"
"net/http"
"os"

"github.com/nextwavedevs/drop/business/auth"
"github.com/nextwavedevs/drop/business/data/studio"
"github.com/nextwavedevs/drop/business/data/user"
"github.com/nextwavedevs/drop/business/mid"
"github.com/nextwavedevs/drop/foundation/web"
"go.mongodb.org/mongo-driver/mongo"
)

// Options represent optional parameters.
type Options struct {
corsOrigin string
}

// WithCORS provides configuration options for CORS.
func WithCORS(origin string) func(opts *Options) {
return func(opts *Options) {
opts.corsOrigin = origin
}
}

// API constructs an http.Handler with all application routes defined.
func API(build string, shutdown chan os.Signal, log *log.Logger, a *auth.Auth, db *mongo.Client, options ...func(opts *Options)) http.Handler {

var opts Options
for _, option := range options {
option(&opts)
}

// Construct the web.App which holds all routes as well as common Middleware.
app := web.NewApp(shutdown, mid.Logger(log), mid.Errors(log), mid.Metrics(), mid.Panics(log))

//Register check group
cg := checkGroup{
build: build,
db: db,
}

app.HandleDebug(http.MethodGet, "/readiness", cg.readiness)
app.HandleDebug(http.MethodGet, "/liveness", cg.liveness)

// Register user management and authentication endpoints.
ug := userGroup{
user: user.New(log, db),
auth: a,
}

app.Handle(http.MethodGet, "/v1/users/:page/:rows", ug.query, mid.Authenticate(a), mid.Authorize(auth.RoleAdmin)) //<== you can't do this if you are not an admin and are not yet authenticated. so he used the get token with his id as kid to generate token
app.Handle(http.MethodGet, "/v1/users/token/:kid", ug.token)
app.Handle(http.MethodGet, "/v1/users/:id", ug.queryByID, mid.Authenticate(a))
app.Handle(http.MethodPost, "/v1/users", ug.create)
app.Handle(http.MethodPut, "/v1/users/:id", ug.update, mid.Authenticate(a), mid.Authorize(auth.RoleAdmin))
app.Handle(http.MethodDelete, "/v1/users/:id", ug.delete, mid.Authenticate(a), mid.Authorize(auth.RoleAdmin))

// Register studio endpoints.
sg := studioGroup{
studio: studio.New(log, db),
}

app.Handle(http.MethodGet, "/v1/studio/:page/:rows", sg.query)
app.Handle(http.MethodGet, "/v1/studio/:page/:rows/:city", sg.queryByLocation)
app.Handle(http.MethodGet, "/v1/studio/:id", sg.queryByID)
app.Handle(http.MethodPost, "/v1/studio", sg.create)
app.Handle(http.MethodPut, "/v1/studio/:id", sg.update)
app.Handle(http.MethodDelete, "/v1/studio/:id", sg.delete, mid.Authenticate(a), mid.Authorize(auth.RoleAdmin))

// Accept CORS 'OPTIONS' preflight requests if config has been provided.
// Don't forget to apply the CORS middleware to the routes that need it.
// Example Config: `conf:"default:https://MY_DOMAIN.COM"`
if opts.corsOrigin != "" {
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return nil
}
app.Handle(http.MethodOptions, "/*", h)
}

return app
}
Loading

0 comments on commit fea91c5

Please sign in to comment.