Skip to content

Commit

Permalink
Merge pull request #3 from airdb/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
deancn authored May 28, 2022
2 parents f2f0621 + 9ba7c1e commit 9b3bb9b
Show file tree
Hide file tree
Showing 34 changed files with 2,300 additions and 57 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/scf-actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Scf Actions
on: [push]
jobs:
deploy:
runs-on: ubuntu-latest
environment: development
timeout-minutes: 5
strategy:
matrix:
node-version: ["16.x"]
pnpm-version: ["6.0.2"]
go-version: ["1.18.2"]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Use pnpm ${{ matrix.pnpm-version }}
uses: pnpm/[email protected]
with:
version: ${{ matrix.pnpm-version }}
run_install: false
- name: Use Golang ${{ matrix.go-version }}
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: Use Buf
uses: bufbuild/[email protected]
- name: Use Buf Lint
uses: bufbuild/buf-lint-action@v1

- name: Setup pnpm cache
uses: actions/cache@v3
with:
path: |
~/.pnpm-store
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/serverless.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Stepup go cache
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install dependencies
run: pnpm install --global serverless serverless-tencent

- name: Check Go Version
run: go version

- name: Make prepare
run: |
export PATH="$PATH:$(go env GOPATH)/bin"
make plugins
make buf
# - name: Make build
# run: |
# export PATH="$PATH:$(go env GOPATH)/bin"
# make scf-build

# - name: Make and deploy
# run: make
# env:
# TENCENT_APP_ID: ${{ secrets.TENCENT_APP_ID }}
# TENCENT_SECRET_ID: ${{ secrets.TENCENT_SECRET_ID }}
# TENCENT_SECRET_KEY: ${{ secrets.TENCENT_SECRET_KEY }}
# TENCENT_TOKEN: ${{ secrets.TENCENT_TOKEN }}
# STAGE: ${{ secrets.STAGE }}
# SCF_NAMESPACE: ${{ secrets.SCF_NAMESPACE }}
# SERVERLESS_PLATFORM_VENDOR: ${{ secrets.SERVERLESS_PLATFORM_VENDOR }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
__debug_bin
xadmin-api
main
.env

# Test binary, built with `go test -c`
*.test
Expand Down
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@ GIT:='github.com/go-masonry/mortar/mortar.gitCommit=$(shell git rev-parse --shor
BUILD_TAG:='github.com/go-masonry/mortar/mortar.buildTag=42'
BUILD_TS:='github.com/go-masonry/mortar/mortar.buildTimestamp=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")'

LDFLAGS=-ldflags
LDFLAGS+="-X ${VER} -X ${GIT} -X ${BUILD_TAG} -X ${BUILD_TS}"

export JAEGER_AGENT_HOST = localhost
export JAEGER_AGENT_PORT = 6831
export JAEGER_SAMPLER_TYPE = const
export JAEGER_SAMPLER_PARAM = 1

run: buf
@go run -ldflags="-X ${VER} -X ${GIT} -X ${BUILD_TAG} -X ${BUILD_TS}" main.go \
@go run $(LDFLAGS) -o output/main cmd/api-server/main.go \
config config/config.yml

dev-local: buf
@CGO_ENABLED=1 go run -ldflags="-X ${VER} -X ${GIT} -X ${BUILD_TAG} -X ${BUILD_TS}" main.go \
@go run $(LDFLAGS) -o output/main cmd/api-server/main.go \
config config/config.yml \
--additional-files config/config_local.yml

build: buf
@CGO_ENABLED=1 go build -ldflags="-X ${VER} -X ${GIT} -X ${BUILD_TAG} -X ${BUILD_TS}" main.go
go build $(LDFLAGS) -o output/main cmd/api-server/main.go

scf-build:
GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o main cmd/api-scf/main.go

scf-deploy:
sls deploy

format:
go fmt ./...
Expand Down
8 changes: 8 additions & 0 deletions api/annotation/v1/extends.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,19 @@ extend google.protobuf.MethodOptions {
optional bool method_option = 50007;
}

message GencodeOption {
repeated string layers = 2;
}

// The message descriptor.
message MessageDescriptor {
// The the FieldMask paths mapping.
map<string, string> mask_map = 1;

// The default feild actions when message's field action is empty.
repeated string default_field_actions = 2;

GencodeOption gencode = 3;
}

// The resource field descriptor.
Expand All @@ -61,6 +67,8 @@ message FieldDescriptor {
// The the field tags.
// Append extra tags on filed, default add yaml with camlecase.
map<string, string> tags = 2;

GencodeOption gencode = 3;
}

message ResourceDescriptor {
Expand Down
4 changes: 4 additions & 0 deletions api/bchm/v1/bchm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ option java_outer_classname = "BchmProto";
option java_package = "com.github.airdb.xadmin-api.bchm.v1";

// 宝贝回家 业务
//
// @Kit
// gencode:
// layers: ["*"]
service BchmService {
// ListLosts 走失信息 列表.
//
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/bchm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ type BchmServiceController interface {
type bchmInfoControllerDeps struct {
fx.In

DB data.LostRepo
LostRepo data.LostRepo
Logger log.Logger
LostRepo data.LostRepo
}

type bchmInfoController struct {
Expand Down
12 changes: 6 additions & 6 deletions app/data/lost.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ type lostRepoDeps struct {
fx.In
}

type lostRepo struct {
*repokit.Repo[LostEntity, uint, bchmv1.ListLostsRequest]

deps lostRepoDeps
}

func NewLostRepo(deps lostRepoDeps) LostRepo {
repo := &lostRepo{
deps: deps,
Expand All @@ -24,9 +30,3 @@ func NewLostRepo(deps lostRepoDeps) LostRepo {

return repo
}

type lostRepo struct {
*repokit.Repo[LostEntity, uint, bchmv1.ListLostsRequest]

deps lostRepoDeps
}
2 changes: 2 additions & 0 deletions app/mortar/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ func (interceptor *AuthInterceptor) Unary() grpc.UnaryServerInterceptor {
claims, err := auth.ParseJwtToken(token)
if err != nil {
interceptor.log.WithError(err).Debug(ctx, "parse oauth token")
return handler(ctx, req)
}

user, err := auth.GetUser(claims.User.Name)
if err != nil {
interceptor.log.WithError(err).Debug(ctx, "get oauth user error")
return handler(ctx, req)
}

return handler(authkit.NewContextUser(ctx, user), req)
Expand Down
154 changes: 154 additions & 0 deletions app/mortar/serverless.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package mortar

import (
"context"
"encoding/json"
"net"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/go-masonry/mortar/constructors/partial"
serverInt "github.com/go-masonry/mortar/interfaces/http/server"
"github.com/go-masonry/mortar/interfaces/log"
chiadapter "github.com/serverless-plus/tencent-serverless-go/chi"
"github.com/serverless-plus/tencent-serverless-go/events"
"github.com/serverless-plus/tencent-serverless-go/faas"
"go.uber.org/fx"
)

func BuildServerlessFxOption() fx.Option {
return fx.Options(
fx.Provide(NewServerless),
// fx.Provide(fx.Annotated{
// Group: partial.FxGroupBuilderCallbacks,
// Target: func(s *Serverless) partial.BuilderCallback {
// return s.BuilderCallback()
// },
// }),
fx.Provide(fx.Annotated{
Group: partial.FxGroupExternalBuilderCallbacks,
Target: func(s *Serverless) partial.RESTBuilderCallback {
return s.ExternalBuilderCallback()
},
}),
// fx.Provide(fx.Annotated{
// Group: partial.FxGroupInternalBuilderCallbacks,
// Target: func(s *Serverless) partial.RESTBuilderCallback {
// return s.InternalBuilderCallback()
// },
// }),
)
}

type serverlessDeps struct {
fx.In

LifeCycle fx.Lifecycle
Logger log.Logger
Serverless *Serverless
}

// InvokeWebServerlessFxOption creates the entire dependency graph
// and registers all provided fx.LifeCycle hooks
func InvokeWebServerlessFxOption() fx.Option {
return fx.Invoke(func(deps serverlessDeps) error {
deps.Serverless.initAdapter()

deps.LifeCycle.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
go faas.Start(deps.Serverless.scfHandler)
return nil
},
OnStop: func(ctx context.Context) error {
return nil
},
})

return nil
})
}

type Serverless struct {
externalMux *http.ServeMux
chiFaas *chiadapter.ChiFaas
}

func NewServerless() *Serverless {
return &Serverless{
externalMux: http.NewServeMux(),
}
}

func (s *Serverless) initAdapter() {
r := chi.NewRouter()
r.Mount("/", s.externalMux)
r.Route("/debug", func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
buf, _ := json.Marshal(map[string]interface{}{
"message": "Hello Serverless Chi",
"query": r.URL.Query().Get("q"),
})
w.Write(buf)
})
})

s.chiFaas = chiadapter.New(r)
}

// Handler serverless faas handler
func (s *Serverless) scfHandler(ctx context.Context, req events.APIGatewayRequest) (events.APIGatewayResponse, error) {
return s.chiFaas.ProxyWithContext(ctx, req)
}

func (s *Serverless) BuilderCallback() partial.BuilderCallback {
return func(builder serverInt.GRPCWebServiceBuilder) serverInt.GRPCWebServiceBuilder {
ln, err := s.listen("./output/grpc.socket")
if err != nil {
panic(err)
}
return builder.SetCustomListener(ln)
}
}

func (s *Serverless) ExternalBuilderCallback() partial.RESTBuilderCallback {
return func(builder serverInt.RESTBuilder) serverInt.RESTBuilder {
// ln, err := s.listen("./output/external.socket")
// if err != nil {
// panic(err)
// }
// return builder.SetCustomListener(ln)
return builder.SetCustomServer(&http.Server{
Handler: s.externalMux,
})
}
}

func (s *Serverless) InternalBuilderCallback() partial.RESTBuilderCallback {
return func(builder serverInt.RESTBuilder) serverInt.RESTBuilder {
ln, err := s.listen("./output/internal.socket")
if err != nil {
panic(err)
}
return builder.SetCustomListener(ln)
}
}

func (s *Serverless) listen(name string) (net.Listener, error) {
ln, err := net.Listen("unix", name)
if err != nil {
return nil, err
}
// if ln, ok := ln.(*net.UnixListener); ok {
// fp, err := ln.File()
// if err != nil {
// return nil, err
// }
// if err = fp.Sync(); err != nil {
// return nil, err
// }
// }

return ln, nil
}
6 changes: 6 additions & 0 deletions buf.gen.kit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ plugins:
- actions=extends
- paths=source_relative
- Meinride/iam/v1/annotations.proto=go.einride.tech/iam/proto/gen/einride/iam/v1
# - name: kit
# out: output
# opt:
# - actions=code
# - paths=source_relative
# - Meinride/iam/v1/annotations.proto=go.einride.tech/iam/proto/gen/einride/iam/v1
6 changes: 4 additions & 2 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ plugins:
- generate_unbound_methods=true
- Meinride/iam/v1/annotations.proto=go.einride.tech/iam/proto/gen/einride/iam/v1
- name: openapiv2
out: genproto
out: ./output
strategy: all
opt:
- allow_repeated_fields_in_body=true
- use_go_templates=true
- output_format=yaml
- allow_merge=true,merge_file_name=apis
- Meinride/iam/v1/annotations.proto=go.einride.tech/iam/proto/gen/einride/iam/v1
Loading

0 comments on commit 9b3bb9b

Please sign in to comment.