Skip to content

Commit

Permalink
MVP: renamed to helmes
Browse files Browse the repository at this point in the history
  • Loading branch information
rugwirobaker committed Sep 16, 2020
1 parent 338e439 commit a6d23c4
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 54 deletions.
8 changes: 4 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (

"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/rugwirobaker/sam"
helmes "github.com/rugwirobaker/helmes"
)

// Server ...
type Server struct {
Service sam.Service
Service helmes.Service
}

// New api Server instance
func New(svc sam.Service) *Server {
func New(svc helmes.Service) *Server {
return &Server{Service: svc}
}

Expand All @@ -28,7 +28,7 @@ func (s Server) Handler() http.Handler {
r.Use(middleware.Recoverer)

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Welcome to sam"))
w.Write([]byte("Welcome to helmes"))
})

r.Get("/version", VersionHandler(s.Service))
Expand Down
33 changes: 27 additions & 6 deletions api/handlers.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
package api

import (
"fmt"
"encoding/json"
"log"
"net/http"

"github.com/rugwirobaker/sam"
helmes "github.com/rugwirobaker/helmes"
)

// SMSHandler ...
func SMSHandler(svc sam.Service) http.HandlerFunc {
func SMSHandler(svc helmes.Service) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, cost, err := svc.Send(r.Context(), sam.SMS{})

in := new(helmes.SMS)

if err := json.NewDecoder(r.Body).Decode(in); err != nil {
log.Printf("failed to send sms %v", err)
http.Error(w, err.Error(), 500)
return
}
out, err := svc.Send(r.Context(), in)
if err != nil {
log.Printf("failed to send sms %v", err)
http.Error(w, err.Error(), 500)
return
}
w.Write([]byte(fmt.Sprintf("%s:%d", id, cost)))

log.Printf("sent sms to '%s'", in.Recipient)
JSON(w, out, 200)
}
}

// VersionHandler ...
func VersionHandler(svc sam.Service) http.HandlerFunc {
func VersionHandler(svc helmes.Service) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ver, err := svc.Version(r.Context())
if err != nil {
Expand All @@ -28,3 +41,11 @@ func VersionHandler(svc sam.Service) http.HandlerFunc {
w.Write([]byte(ver))
}
}

// JSON responds with json
func JSON(w http.ResponseWriter, v interface{}, status int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
enc := json.NewEncoder(w)
enc.Encode(v)
}
Empty file removed cloudbuild.yaml
Empty file.
21 changes: 21 additions & 0 deletions cmd/helmes/inject_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"net/http"

"github.com/quarksgroup/sms-client/sms"
"github.com/quarksgroup/sms-client/sms/driver/fdi"
"github.com/quarksgroup/sms-client/sms/transport/oauth2"
)

func provideClient() *sms.Client {
client := fdi.NewDefault()
client.Client = &http.Client{
Transport: &oauth2.Transport{
Scheme: oauth2.SchemeBearer,
Source: oauth2.ContextTokenSource(),
Base: http.DefaultTransport,
},
}
return client
}
35 changes: 28 additions & 7 deletions cmd/sam/main.go → cmd/helmes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import (

"github.com/go-chi/chi"
"github.com/joho/godotenv"
"github.com/rugwirobaker/sam"
"github.com/rugwirobaker/sam/api"
"github.com/rugwirobaker/helmes"
"github.com/rugwirobaker/helmes/api"
)

func main() {
var envfile string

ctx := context.Background()

flag.StringVar(&envfile, "env-file", ".env", "Read in a file of environment variables")
flag.Parse()

Expand All @@ -27,10 +30,28 @@ func main() {
}

port := os.Getenv("PORT")
// id := os.Getenv("SAM_SMS_APP_ID")
// secret := os.Getenv("SAM_SMS_APP_SECRET")
id := os.Getenv("HELMES_SMS_APP_ID")
secret := os.Getenv("HELMES_SMS_APP_SECRET")
sender := os.Getenv("HELMES_SENDER_IDENTITY")
callback := os.Getenv("HELMES_CALLBACK_URL")
// log.Printf("env: port-->%s", port)
// log.Printf("env: helmes id-->%s", id)
// log.Printf("env: helmes secret-->%s", secret)
// log.Printf("env: helmes sender identity-->%s", sender)
// log.Printf("env: helmes callback url-->%s", callback)

cli := provideClient()
if err != nil {
log.Fatalf("could not initialize client: %v", err)
}

service, err := helmes.New(cli, id, secret, sender, callback)
if err != nil {
log.Fatalf("could not initialize sms service: %v", err)
}

log.Println("initialized sms client...")

service := sam.New()
api := api.New(service)
mux := chi.NewMux()
mux.Mount("/api", api.Handler())
Expand All @@ -47,11 +68,11 @@ func main() {
log.Fatalf("listen: %s\n", err)
}
}()
log.Printf("Started Sam Server at port %s", port)
log.Printf("Started helmes Server at port %s", port)
<-done
log.Print("Server Stopped")

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer func() {
// extra handling here
cancel()
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module github.com/rugwirobaker/sam
module github.com/rugwirobaker/helmes

go 1.14

require (
github.com/go-chi/chi v4.1.2+incompatible
github.com/google/uuid v1.1.2
github.com/joho/godotenv v1.3.0
github.com/quarksgroup/sms-client v0.0.0-20200916024156-cec3c804e5f8
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
)
30 changes: 29 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
github.com/go-chi/chi v1.0.0 h1:s/kv1cTXfivYjdKJdyUzNGyAWZ/2t7duW1gKn5ivu+c=
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/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec=
github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
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/quarksgroup/sms-client v0.0.0-20200911144732-8cbf6e85ed3a h1:Prr/Ac/a0Xnu6DMB/Wpnci1YKqKtpcGjPRQiweKUiKY=
github.com/quarksgroup/sms-client v0.0.0-20200911144732-8cbf6e85ed3a/go.mod h1:XcPDoSKQvB0PT2pXePS3pqVd7uCHTBhZ/zBdnSFBYOw=
github.com/quarksgroup/sms-client v0.0.0-20200916024156-cec3c804e5f8 h1:AehGo7q3SYg9btSTVFPAUx0ToP3vWu1k6qccpHs+vzk=
github.com/quarksgroup/sms-client v0.0.0-20200916024156-cec3c804e5f8/go.mod h1:XcPDoSKQvB0PT2pXePS3pqVd7uCHTBhZ/zBdnSFBYOw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200904194848-62affa334b73 h1:MXfv8rhZWmFeqX3GNZRsd6vOLoaCHjYEX3qkRo3YBUA=
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/h2non/gock.v1 v1.0.15 h1:SzLqcIlb/fDfg7UvukMpNcWsu7sI5tWwL+KCATZqks0=
gopkg.in/h2non/gock.v1 v1.0.15/go.mod h1:sX4zAkdYX1TRGJ2JY156cFspQn4yRWn6p9EMdODlynE=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
86 changes: 86 additions & 0 deletions helmes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package helmes

import (
"context"

"github.com/google/uuid"
"github.com/quarksgroup/sms-client/sms"
)

// SMS ...
type SMS struct {
Payload string `json:"payload"`
Recipient string `json:"recipient"`
}

// Report message queueing status
type Report struct {
ID string `json:"id"`
Cost int64 `json:"cost"`
}

// Service defines the capabilties of helmes
type Service interface {
// Send an sms message and return it's
Send(context.Context, *SMS) (*Report, error)

//Version returns helmes's current running version
Version(context.Context) (string, error)
}

type service struct {
sender string
callback string
client *sms.Client
token *sms.Token
}

// New instance of service
func New(cli *sms.Client, id, secret, sender, callback string) (Service, error) {
token, _, err := cli.Auth.Login(context.Background(), id, secret)
if err != nil {
return nil, err
}
return &service{
sender: sender,
callback: callback,
client: cli,
token: token,
}, nil
}

func (s *service) Send(ctx context.Context, message *SMS) (*Report, error) {
token, _, err := s.client.Auth.Refresh(ctx, s.token, false)
if err != nil {
return nil, err
}
ctx = context.WithValue(ctx, sms.TokenKey{}, &sms.Token{
Token: token.Token,
Refresh: token.Refresh,
})

in := sms.Message{
ID: uuid.New().String(),
Body: message.Payload,
Recipients: []string{message.Recipient},
Sender: s.sender,
Report: s.callback,
}

report, _, err := s.client.Message.Send(ctx, in)
if err != nil {
return nil, err
}
return convertReport(report), nil
}

func (s *service) Version(ctx context.Context) (string, error) {
return "v0.1.0", nil
}

func convertReport(report *sms.Report) *Report {
return &Report{
ID: report.ID,
Cost: report.Cost,
}
}
35 changes: 0 additions & 35 deletions sam.go

This file was deleted.

0 comments on commit a6d23c4

Please sign in to comment.