update all the makefiles (#111)

* update all the makefiles

* add back openapi

* update cache package

* fix cache

* update go mod
This commit is contained in:
Asim Aslam
2021-05-19 10:36:07 +01:00
committed by GitHub
parent a94f032acd
commit f3ca93b789
17 changed files with 160 additions and 71 deletions

View File

@@ -43,35 +43,17 @@ jobs:
go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/golang/protobuf/protoc-gen-go
go install go install
- name: Install redoc cli
run: |
# https://github.com/actions/virtual-environments/issues/599
sudo npm install -g redoc-cli
- name: Install openapi plugin - name: Install openapi plugin
working-directory: micro/cmd/protoc-gen-openapi working-directory: micro/cmd/protoc-gen-openapi
run: | run: |
go install go install
# - name: Install hugo
# run: sudo snap install hugo --channel=extended
- name: Generate openapi spec and publish the api - name: Generate openapi spec and publish the api
working-directory: services working-directory: services
run: | run: |
go run cmd/publisher/main.go . go run cmd/publisher/main.go .
env: env:
MICRO_ADMIN_TOKEN: ${{ secrets.MICRO_ADMIN_TOKEN }} MICRO_ADMIN_TOKEN: ${{ secrets.MICRO_ADMIN_TOKEN }}
# - name: Deploy
# if: github.ref == 'refs/heads/master'
# uses: s0/git-publish-subdir-action@develop
# env:
# REPO: self
# BRANCH: gh-pages
# FOLDER: services/docs
# GITHUB_TOKEN: ${{ secrets.GH_PAT }}
- name: Generate package - name: Generate package
working-directory: services working-directory: services
env: env:

View File

@@ -75,12 +75,12 @@ func main() {
} }
fmt.Println("Processing folder", serviceDir) fmt.Println("Processing folder", serviceDir)
makeProto := exec.Command("make", "docs") makeProto := exec.Command("make", "api")
makeProto.Dir = serviceDir makeProto.Dir = serviceDir
fmt.Println(serviceDir) fmt.Println(serviceDir)
outp, err := makeProto.CombinedOutput() outp, err := makeProto.CombinedOutput()
if err != nil { if err != nil {
fmt.Println("Failed to make docs", string(outp)) fmt.Println("Failed to make api", string(outp))
os.Exit(1) os.Exit(1)
} }
serviceName := f.Name() serviceName := f.Name()

View File

@@ -5,14 +5,14 @@ init:
go get -u github.com/golang/protobuf/proto go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: proto .PHONY: proto
proto: proto:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/datastore.proto protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/datastore.proto
.PHONY: docs .PHONY: api
docs: api:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/datastore.proto protoc --openapi_out=. --proto_path=. proto/datastore.proto
@redoc-cli bundle api-datastore.json
.PHONY: build .PHONY: build
build: build:

View File

@@ -5,14 +5,14 @@ init:
go get -u github.com/golang/protobuf/proto go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: proto .PHONY: proto
proto: proto:
protoc --proto_path=. --micro_out=. --go_out=:. proto/files.proto protoc --proto_path=. --micro_out=. --go_out=:. proto/files.proto
.PHONY: docs .PHONY: api
docs: api:
protoc --openapi_out=. --proto_path=. --micro_out=${MODIFY}:. --go_out=${MODIFY}:. proto/files.proto protoc --openapi_out=. --proto_path=. proto/files.proto
@redoc-cli bundle api-files.json
.PHONY: build .PHONY: build
build: build:

View File

@@ -5,14 +5,14 @@ init:
go get -u github.com/golang/protobuf/proto go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: proto .PHONY: proto
proto: proto:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/geocoding.proto protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/geocoding.proto
.PHONY: docs .PHONY: api
docs: api:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/geocoding.proto protoc --openapi_out=. --proto_path=. proto/geocoding.proto
@redoc-cli bundle api-geocoding.json
.PHONY: build .PHONY: build
build: build:

1
go.mod
View File

@@ -10,6 +10,7 @@ require (
github.com/golang/protobuf v1.5.1 github.com/golang/protobuf v1.5.1
github.com/google/uuid v1.1.2 github.com/google/uuid v1.1.2
github.com/hailocab/go-geoindex v0.0.0-20160127134810-64631bfe9711 github.com/hailocab/go-geoindex v0.0.0-20160127134810-64631bfe9711
github.com/hashicorp/golang-lru v0.5.3
github.com/lib/pq v1.9.0 // indirect github.com/lib/pq v1.9.0 // indirect
github.com/micro/dev v0.0.0-20201117163752-d3cfc9788dfa github.com/micro/dev v0.0.0-20201117163752-d3cfc9788dfa
github.com/micro/micro/v3 v3.2.2-0.20210514120220-1ee39904d3dd github.com/micro/micro/v3 v3.2.2-0.20210514120220-1ee39904d3dd

View File

@@ -6,10 +6,9 @@ GOPATH:=$(shell go env GOPATH)
proto: proto:
protoc --openapi_out=. --proto_path=${GOPATH}/src:. --micro_out=. --go_out=. proto/helloworld.proto protoc --openapi_out=. --proto_path=${GOPATH}/src:. --micro_out=. --go_out=. proto/helloworld.proto
.PHONY: docs .PHONY: api
docs: api:
protoc --openapi_out=. --proto_path=${GOPATH}/src:. --micro_out=. --go_out=. proto/helloworld.proto protoc --openapi_out=. --proto_path=${GOPATH}/src:. proto/helloworld.proto
@redoc-cli bundle api-helloworld.json
.PHONY: build .PHONY: build
build: proto build: proto

View File

@@ -1,6 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package helloworld; package helloworld;
option go_package = "./proto;helloworld"; option go_package = "./proto;helloworld";
service Helloworld { service Helloworld {

View File

@@ -5,13 +5,14 @@ init:
go get -u github.com/golang/protobuf/proto go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: proto .PHONY: proto
proto: proto:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=. proto/image.proto protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=. proto/image.proto
docs: .PHONE: api
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=. proto/image.proto api:
@redoc-cli bundle api-image.json protoc --openapi_out=. --proto_path=. proto/image.proto
.PHONY: build .PHONY: build
build: build:

View File

@@ -5,14 +5,14 @@ init:
go get -u github.com/golang/protobuf/proto go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: proto .PHONY: proto
proto: proto:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/location.proto protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/location.proto
.PHONY: docs .PHONY: api
docs: api:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/location.proto protoc --openapi_out=. --proto_path=. proto/location.proto
@redoc-cli bundle api-location.json
.PHONY: build .PHONY: build
build: build:

View File

@@ -5,13 +5,14 @@ init:
go get -u github.com/golang/protobuf/proto go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: proto .PHONY: proto
proto: proto:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=. proto/otp.proto protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=. proto/otp.proto
docs: .PHONY: api
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=. proto/otp.proto api:
@redoc-cli bundle api-otp.json protoc --openapi_out=. --proto_path=. proto/otp.proto
.PHONY: build .PHONY: build
build: build:

View File

@@ -38,7 +38,7 @@ func (e *Otp) Generate(ctx context.Context, req *pb.GenerateRequest, rsp *pb.Gen
secret = key.Secret() secret = key.Secret()
if err := cache.Context(ctx).Put(req.Id, secret, time.Now().Add(time.Minute*5)); err != nil { if err := cache.Context(ctx).Set(req.Id, secret, time.Now().Add(time.Minute*5)); err != nil {
logger.Error("Failed to store secret: %v", err) logger.Error("Failed to store secret: %v", err)
return errors.InternalServerError("otp.generate", "failed to generate code") return errors.InternalServerError("otp.generate", "failed to generate code")
} }

112
pkg/cache/cache.go vendored
View File

@@ -4,9 +4,12 @@ package cache
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"sync"
"time" "time"
"github.com/hashicorp/golang-lru"
"github.com/micro/micro/v3/service/store" "github.com/micro/micro/v3/service/store"
"github.com/micro/services/pkg/tenant" "github.com/micro/services/pkg/tenant"
) )
@@ -15,21 +18,39 @@ type Cache interface {
// Context returns a tenant scoped Cache // Context returns a tenant scoped Cache
Context(ctx context.Context) Cache Context(ctx context.Context) Cache
Get(key string, val interface{}) error Get(key string, val interface{}) error
Put(key string, val interface{}, expires time.Time) error Set(key string, val interface{}, expires time.Time) error
Delete(key string) error Delete(key string) error
Increment(key string, val int64) (int64, error)
Decrement(key string, val int64) (int64, error)
} }
type cache struct { type cache struct {
sync.Mutex
LRU *lru.Cache
Store store.Store Store store.Store
Prefix string Prefix string
} }
type item struct {
key string
val []byte
expires time.Time
}
var ( var (
DefaultCacheSize = 1000
DefaultCache = New(nil) DefaultCache = New(nil)
ErrNotFound = errors.New("not found")
) )
func New(st store.Store) Cache { func New(st store.Store) Cache {
return &cache{Store: st} l, _ := lru.New(DefaultCacheSize)
return &cache{
LRU: l,
Store: st,
}
} }
func (c *cache) Key(k string) string { func (c *cache) Key(k string) string {
@@ -51,6 +72,29 @@ func (c *cache) Context(ctx context.Context) Cache {
} }
func (c *cache) Get(key string, val interface{}) error { func (c *cache) Get(key string, val interface{}) error {
k := c.Key(key)
// try the LRU
v, ok := c.LRU.Get(key)
if ok {
i := v.(*item)
// check if the item expired
if !i.expires.IsZero() && i.expires.Sub(time.Now()).Seconds() < 0 {
// remove it
c.LRU.Remove(k)
return ErrNotFound
}
// otherwise unmarshal and return it
if err := json.Unmarshal(i.val, val); err != nil {
return err
}
return nil
}
// otherwise check the store
if c.Store == nil { if c.Store == nil {
c.Store = store.DefaultStore c.Store = store.DefaultStore
} }
@@ -60,7 +104,7 @@ func (c *cache) Get(key string, val interface{}) error {
return err return err
} }
if len(recs) == 0 { if len(recs) == 0 {
return store.ErrNotFound return ErrNotFound
} }
if err := json.Unmarshal(recs[0].Value, val); err != nil { if err := json.Unmarshal(recs[0].Value, val); err != nil {
return err return err
@@ -68,7 +112,7 @@ func (c *cache) Get(key string, val interface{}) error {
return nil return nil
} }
func (c *cache) Put(key string, val interface{}, expires time.Time) error { func (c *cache) Set(key string, val interface{}, expires time.Time) error {
if c.Store == nil { if c.Store == nil {
c.Store = store.DefaultStore c.Store = store.DefaultStore
} }
@@ -80,18 +124,60 @@ func (c *cache) Put(key string, val interface{}, expires time.Time) error {
if expiry < time.Duration(0) { if expiry < time.Duration(0) {
expiry = time.Duration(0) expiry = time.Duration(0)
} }
return c.Store.Write(&store.Record{ rec := &store.Record{
Key: c.Key(key), Key: c.Key(key),
Value: b, Value: b,
Expiry: expiry, Expiry: expiry,
}) }
if err := c.Store.Write(rec); err != nil {
return err
}
// set in the lru
c.LRU.Add(rec.Key, &item{key: rec.Key, val: rec.Value, expires: expires})
return nil
} }
func (c *cache) Delete(key string) error { func (c *cache) Delete(key string) error {
if c.Store == nil { if c.Store == nil {
c.Store = store.DefaultStore c.Store = store.DefaultStore
} }
return c.Store.Delete(c.Key(key))
k := c.Key(key)
// remove from the lru
c.LRU.Remove(k)
// delete from the store
return c.Store.Delete(k)
}
func (c *cache) Increment(key string, value int64) (int64, error) {
c.Lock()
defer c.Unlock()
var val int64
if err := c.Get(key, &val); err != nil {
return 0, err
}
val += value
if err := c.Set(key, val, time.Time{}); err != nil {
return val, err
}
return val, nil
}
func (c *cache) Decrement(key string, value int64) (int64, error) {
c.Lock()
defer c.Unlock()
var val int64
if err := c.Get(key, &val); err != nil {
return 0, err
}
val -= value
if err := c.Set(key, val, time.Time{}); err != nil {
return val, err
}
return val, nil
} }
func Context(ctx context.Context) Cache { func Context(ctx context.Context) Cache {
@@ -102,10 +188,18 @@ func Get(key string, val interface{}) error {
return DefaultCache.Get(key, val) return DefaultCache.Get(key, val)
} }
func Put(key string, val interface{}, expires time.Time) error { func Set(key string, val interface{}, expires time.Time) error {
return DefaultCache.Put(key, val, expires) return DefaultCache.Set(key, val, expires)
} }
func Delete(key string) error { func Delete(key string) error {
return DefaultCache.Delete(key) return DefaultCache.Delete(key)
} }
func Increment(key string, val int64) (int64, error) {
return DefaultCache.Increment(key, val)
}
func Decrement(key string, val int64) (int64, error) {
return DefaultCache.Decrement(key, val)
}

View File

@@ -5,14 +5,14 @@ init:
go get -u github.com/golang/protobuf/proto go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: proto .PHONY: proto
proto: proto:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/routing.proto protoc --proto_path=. --micro_out=. --go_out=:. proto/routing.proto
.PHONY: docs .PHONY: api
docs: api:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/routing.proto protoc --openapi_out=. --proto_path=. proto/routing.proto
@redoc-cli bundle api-routing.json
.PHONY: build .PHONY: build
build: build:

View File

@@ -25,7 +25,9 @@ type Point struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
// Lat e.g 52.523219
Latitude float64 `protobuf:"fixed64,1,opt,name=latitude,proto3" json:"latitude,omitempty"` Latitude float64 `protobuf:"fixed64,1,opt,name=latitude,proto3" json:"latitude,omitempty"`
// Long e.g 13.428555
Longitude float64 `protobuf:"fixed64,2,opt,name=longitude,proto3" json:"longitude,omitempty"` Longitude float64 `protobuf:"fixed64,2,opt,name=longitude,proto3" json:"longitude,omitempty"`
} }
@@ -368,12 +370,15 @@ func (x *Direction) GetIntersections() []*Intersection {
return nil return nil
} }
// Turn by turn directions from a starting and endpoint including maneuvers and bearings
type DirectionsRequest struct { type DirectionsRequest struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
// The staring point for the journey
Origin *Point `protobuf:"bytes,1,opt,name=origin,proto3" json:"origin,omitempty"` Origin *Point `protobuf:"bytes,1,opt,name=origin,proto3" json:"origin,omitempty"`
// The destinationg of the journey
Destination *Point `protobuf:"bytes,2,opt,name=destination,proto3" json:"destination,omitempty"` Destination *Point `protobuf:"bytes,2,opt,name=destination,proto3" json:"destination,omitempty"`
} }
@@ -498,12 +503,15 @@ func (x *DirectionsResponse) GetDuration() float64 {
return 0 return 0
} }
// Get the eta for a route from origin to destination. The eta is an estimated time based on car routes
type EtaRequest struct { type EtaRequest struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
// The starting point for the eta calculation
Origin *Point `protobuf:"bytes,1,opt,name=origin,proto3" json:"origin,omitempty"` Origin *Point `protobuf:"bytes,1,opt,name=origin,proto3" json:"origin,omitempty"`
// The end point for the eta calculation
Destination *Point `protobuf:"bytes,2,opt,name=destination,proto3" json:"destination,omitempty"` Destination *Point `protobuf:"bytes,2,opt,name=destination,proto3" json:"destination,omitempty"`
// type of transport e.g car, foot, bicycle // type of transport e.g car, foot, bicycle
Type string `protobuf:"bytes,3,opt,name=type,proto3" json:"type,omitempty"` Type string `protobuf:"bytes,3,opt,name=type,proto3" json:"type,omitempty"`
@@ -619,6 +627,7 @@ func (x *EtaResponse) GetDuration() float64 {
return 0 return 0
} }
// Retrieve a route as a simple list of gps points along with total distance and estimated duration
type RouteRequest struct { type RouteRequest struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache

View File

@@ -5,6 +5,7 @@ init:
go get -u github.com/golang/protobuf/proto go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: proto .PHONY: proto
proto: proto:
protoc --proto_path=. --micro_out=. --go_out=:. proto/url.proto protoc --proto_path=. --micro_out=. --go_out=:. proto/url.proto
@@ -13,10 +14,9 @@ proto:
build: build:
go build -o url *.go go build -o url *.go
.PHONY: docs .PHONY: api
docs: api:
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/url.proto protoc --openapi_out=. --proto_path=. proto/url.proto
@redoc-cli bundle api-url.json
.PHONY: test .PHONY: test
test: test:

View File

@@ -5,13 +5,14 @@ init:
go get -u github.com/golang/protobuf/proto go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: proto .PHONY: proto
proto: proto:
protoc --proto_path=. --micro_out=. --go_out=:. proto/users.proto protoc --proto_path=. --micro_out=. --go_out=:. proto/users.proto
docs: .PHONY: api
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=. proto/users.proto api:
@redoc-cli bundle api-users.json protoc --openapi_out=. --proto_path=. proto/users.proto
.PHONY: build .PHONY: build
build: build: