Initial commit

This commit is contained in:
pkg
2021-12-31 00:37:45 +08:00
commit a28ff75884
17 changed files with 835 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
# app
所有应用的入口。
## 应用类型
### `/api`——对外提供接口服务的应用
一般指对外提供`http`服务的应用。
### `/service`——仅对内部提供服务的应用
一般指对内提供`grpc`服务的应用
### `/job`——运行短暂的一次性任务
分为一般任务和定时任务
+21
View File
@@ -0,0 +1,21 @@
FROM golang:1.17 as builder
ENV GO111MODULE on
ENV GOPROXY https://goproxy.io,direct
WORKDIR /go/cache
ADD go.mod .
ADD go.sum .
RUN go mod download
WORKDIR /go/src
ADD . .
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o api_example_api ./app/api/example_api
FROM alpine as example_api
WORKDIR /go/src
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /go/src/api_example_api ./
ENV TZ=Asia/Shanghai
RUN chmod +x ./api_example_api
LABEL io.portainer.accesscontrol.teams="admin"
EXPOSE 8080
CMD ["./api_example_api"]
+25
View File
@@ -0,0 +1,25 @@
package main
import (
"context"
"git.icechen.cn/pkg/go-template/pkg/proto/example_service"
"git.icechen.cn/pkg/go-template/pkg/rpc"
"github.com/gofiber/fiber/v2"
"google.golang.org/protobuf/types/known/emptypb"
)
func main() {
app := fiber.New()
app.Get("/test", func(c *fiber.Ctx) error {
userInfo, err := example_service.NewUserClient(rpc.GetServiceConn("example_service")).SayHello(context.TODO(), &emptypb.Empty{})
if err != nil {
return err
}
return c.JSON(userInfo.Name)
})
err := app.Listen(":8080")
if err != nil {
panic(err)
}
}
@@ -0,0 +1 @@
package model
+21
View File
@@ -0,0 +1,21 @@
FROM golang:1.17 as builder
ENV GO111MODULE on
ENV GOPROXY https://goproxy.io,direct
WORKDIR /go/cache
ADD go.mod .
ADD go.sum .
RUN go mod download
WORKDIR /go/src
ADD . .
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o service_example_service ./app/service/example_service
FROM alpine as example_service
WORKDIR /go/src
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /go/src/service_example_service ./
ENV TZ=Asia/Shanghai
RUN chmod +x ./service_example_service
LABEL io.portainer.accesscontrol.teams="admin"
EXPOSE 8080
CMD ["./service_example_service"]
@@ -0,0 +1,9 @@
package main
import (
"git.icechen.cn/pkg/go-template/app/service/example_service/internal/user"
)
func main() {
user.RpcServer()
}
@@ -0,0 +1,35 @@
package user
import (
"context"
"git.icechen.cn/pkg/go-template/pkg/proto/example_service"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/types/known/emptypb"
"log"
"net"
)
func RpcServer() {
lis, err := net.Listen("tcp", ":3000")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
example_service.RegisterUserServer(s, &Server{})
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
type Server struct {
}
func (s *Server) SayHello(ctx context.Context, empty *emptypb.Empty) (*example_service.UserInfo, error) {
return &example_service.UserInfo{
Name: "icechen",
}, nil
}