initial import

This commit is contained in:
kevin
2020-07-26 17:09:05 +08:00
commit 7e3a369a8f
647 changed files with 54754 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
FROM golang:1.13 AS builder
ENV CGO_ENABLED 0
ENV GOOS linux
RUN apt-get update
RUN apt-get install -y apt-utils upx
WORKDIR $GOPATH/src/zero
COPY . .
RUN go build -ldflags="-s -w" -o /app/graceful example/graceful/dns/api/graceful.go
RUN upx /app/graceful
FROM alpine
RUN apk update --no-cache
RUN apk add --no-cache ca-certificates
RUN apk add --no-cache tzdata
ENV TZ Asia/Shanghai
WORKDIR /app
COPY --from=builder /app/graceful /app/graceful
COPY example/graceful/dns/api/etc/graceful-api.json /app/etc/config.json
CMD ["./graceful", "-f", "etc/config.json"]

View File

@@ -0,0 +1,11 @@
version := v$(shell /bin/date "+%y%m%d%H%M%S")
build:
docker pull alpine
cd $(GOPATH)/src/zero && docker build -t registry.cn-hangzhou.aliyuncs.com/xapp/graceful:$(version) . -f example/graceful/dns/api/Dockerfile
push: build
docker push registry.cn-hangzhou.aliyuncs.com/xapp/graceful:$(version)
deploy: push
kubectl -n kevin set image deployment/graceful-deployment graceful=registry-vpc.cn-hangzhou.aliyuncs.com/xapp/graceful:$(version)

View File

@@ -0,0 +1,11 @@
package config
import (
"zero/ngin"
"zero/rpcx"
)
type Config struct {
ngin.NgConf
Rpc rpcx.RpcClientConf
}

View File

@@ -0,0 +1,9 @@
{
"Name": "graceful-api",
"Host": "0.0.0.0",
"Port": 8888,
"MaxConns": 1000000,
"Rpc": {
"Server": "dns:///gracefulrpc:3456"
}
}

View File

@@ -0,0 +1,11 @@
type Response {
Host string `json:"host"`
Time int64 `json:"time"`
}
service graceful-api {
@server(
handler: GracefulHandler
)
get /api/graceful() returns(Response)
}

View File

@@ -0,0 +1,32 @@
package main
import (
"flag"
"zero/core/conf"
"zero/example/graceful/dns/api/config"
"zero/example/graceful/dns/api/handler"
"zero/example/graceful/dns/api/svc"
"zero/ngin"
"zero/rpcx"
)
var configFile = flag.String("f", "etc/graceful-api.json", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
client := rpcx.MustNewClient(c.Rpc)
ctx := &svc.ServiceContext{
Client: client,
}
engine := ngin.MustNewEngine(c.NgConf)
defer engine.Stop()
handler.RegisterHandlers(engine, ctx)
engine.Start()
}

View File

@@ -0,0 +1,42 @@
apiVersion: v1
kind: Service
metadata:
name: graceful
namespace: kevin
spec:
selector:
app: graceful
type: ClusterIP
ports:
- name: graceful-port
port: 3333
targetPort: 8888
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: graceful-deployment
namespace: kevin
labels:
app: graceful
spec:
replicas: 3
selector:
matchLabels:
app: graceful
template:
metadata:
labels:
app: graceful
spec:
containers:
- name: graceful
image: registry-vpc.cn-hangzhou.aliyuncs.com/xapp/graceful:v191022133857
imagePullPolicy: Always
ports:
- containerPort: 8888
imagePullSecrets:
- name: aliyun

View File

@@ -0,0 +1,49 @@
package handler
import (
"context"
"fmt"
"net/http"
"os"
"time"
"zero/core/executors"
"zero/core/httpx"
"zero/core/logx"
"zero/example/graceful/dns/api/svc"
"zero/example/graceful/dns/api/types"
"zero/example/graceful/dns/rpc/graceful"
)
func gracefulHandler(ctx *svc.ServiceContext) http.HandlerFunc {
logger := executors.NewLessExecutor(time.Second)
return func(w http.ResponseWriter, r *http.Request) {
var resp types.Response
conn, ok := ctx.Client.Next()
if !ok {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
host, err := os.Hostname()
if err != nil {
http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
return
}
client := graceful.NewGraceServiceClient(conn)
rp, err := client.Grace(context.Background(), &graceful.Request{From: host})
if err != nil {
logx.Error(err)
http.Error(w, http.StatusText(http.StatusBadGateway), http.StatusBadGateway)
return
}
resp.Host = rp.Host
logger.DoOrDiscard(func() {
fmt.Printf("%s from host: %s\n", time.Now().Format("15:04:05"), rp.Host)
})
httpx.OkJson(w, resp)
}
}

View File

@@ -0,0 +1,19 @@
// DO NOT EDIT, generated by goctl
package handler
import (
"net/http"
"zero/example/graceful/dns/api/svc"
"zero/ngin"
)
func RegisterHandlers(engine *ngin.Engine, ctx *svc.ServiceContext) {
engine.AddRoutes([]ngin.Route{
{
Method: http.MethodGet,
Path: "/api/graceful",
Handler: gracefulHandler(ctx),
},
})
}

View File

@@ -0,0 +1,7 @@
package svc
import "zero/rpcx"
type ServiceContext struct {
Client *rpcx.RpcClient
}

View File

@@ -0,0 +1,7 @@
// DO NOT EDIT, generated by goctl
package types
type Response struct {
Host string `json:"host"`
Time int64 `json:"time"`
}