-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from airdb/dev
Dev
- Loading branch information
Showing
34 changed files
with
2,300 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
__debug_bin | ||
xadmin-api | ||
main | ||
.env | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.