Commit bb6dd43e by 岳巧源

init

parents
* linguist-language=GO
\ No newline at end of file
.buildpath
.hgignore.swp
.project
.orig
.swp
.idea/
.settings/
.vscode/
vendor/
composer.lock
gitpush.sh
pkg/
bin/
cbuild
**/.DS_Store
.test/
main
output/
manifest/output/
temp/
\ No newline at end of file
ROOT_DIR = $(shell pwd)
NAMESPACE = "default"
DEPLOY_NAME = "template-single"
DOCKER_NAME = "template-single"
# Install/Update to the latest CLI tool.
.PHONY: cli
cli:
@set -e; \
wget -O gf https://github.com/gogf/gf/releases/latest/download/gf_$(shell go env GOOS)_$(shell go env GOARCH) && \
chmod +x gf && \
./gf install -y && \
rm ./gf
# Check and install CLI tool.
.PHONY: cli.install
cli.install:
@set -e; \
gf -v > /dev/null 2>&1 || if [[ "$?" -ne "0" ]]; then \
echo "GoFame CLI is not installed, start proceeding auto installation..."; \
make cli; \
fi;
# Generate Go files for DAO/DO/Entity.
.PHONY: dao
dao: cli.install
@gf gen dao
# Build image, deploy image and yaml to current kubectl environment and make port forward to local machine.
.PHONY: start
start:
@set -e; \
make image; \
make deploy; \
make port;
# Build docker image.
.PHONY: image
image: cli.install
$(eval _TAG = $(shell git log -1 --format="%cd.%h" --date=format:"%Y%m%d%H%M%S"))
ifneq (, $(shell git status --porcelain 2>/dev/null))
$(eval _TAG = $(_TAG).dirty)
endif
$(eval _TAG = $(if ${TAG}, ${TAG}, $(_TAG)))
$(eval _PUSH = $(if ${PUSH}, ${PUSH}, ))
@gf docker -p -b "-a amd64 -s linux -p temp" -t $(DOCKER_NAME):${_TAG};
# Build docker image and automatically push to docker repo.
.PHONY: image.push
image.push:
@make image PUSH=-p;
# Deploy image and yaml to current kubectl environment.
.PHONY: deploy
deploy:
$(eval _ENV = $(if ${ENV}, ${ENV}, develop))
@set -e; \
mkdir -p $(ROOT_DIR)/temp/kustomize;\
cd $(ROOT_DIR)/manifest/deploy/kustomize/overlays/${_ENV};\
kustomize build > $(ROOT_DIR)/temp/kustomize.yaml;\
kubectl apply -f $(ROOT_DIR)/temp/kustomize.yaml; \
kubectl patch -n $(NAMESPACE) deployment/$(DEPLOY_NAME) -p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"date\":\"$(shell date +%s)\"}}}}}";
# GoFrame Template For SingleRepo
package v1
import "github.com/gogf/gf/v2/frame/g"
type ApiListReq struct {
g.Meta `path:"/api/list" method:"get" summary:"查询api列表"`
Page string `json:"page" d:"1" dc:"请输入第几页,不传默认第1页"`
Limit string `json:"limit" d:"10" dc:"每页限制个数"`
OffsetTime int64 `json:"offset_time" dc:"offset_time为时间戳"`
}
type ApiListRes struct {
}
package v1
import (
"github.com/gogf/gf/v2/frame/g"
)
type HelloReq struct {
g.Meta `path:"/hello" tags:"Hello" method:"get" summary:"You first hello api"`
}
type HelloRes struct {
g.Meta `mime:"text/html" example:"string"`
}
module dev-ops
go 1.15
require github.com/gogf/gf/v2 v2.1.1
This diff is collapsed. Click to expand it.
package cmd
import (
"context"
"dev-ops/utility/response"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcmd"
"dev-ops/internal/controller"
)
var (
Main = gcmd.Command{
Name: "main",
Usage: "main",
Brief: "start http server",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
s := g.Server()
oai := s.GetOpenApi()
s.SetClientMaxBodySize(1024 * 1024 * 50)
s.SetFormParsingMemory(1024 * 1024 * 50)
// OpenApi自定义信息
oai.Info.Title = `API Reference`
oai.Config.CommonResponse = response.JsonRes{}
oai.Config.CommonResponseDataField = `Data`
s.Group("/", func(group *ghttp.RouterGroup) {
//group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Bind(
controller.CApiList,
)
})
port, _ := g.Cfg().Get(ctx, "server.port")
s.SetPort(port.Int())
s.Run()
return nil
},
}
)
package controller
import (
"context"
v1 "dev-ops/api/v1"
"dev-ops/internal/service"
"dev-ops/utility/response"
"github.com/gogf/gf/v2/frame/g"
)
var CApiList = cApiList{}
type cApiList struct{}
func (c *cApiList) ApiList(ctx context.Context, req *v1.ApiListReq) (res *v1.ApiListRes, err error) {
data, code := service.ApiList().GetApiList(ctx, req)
response.JsonExit(g.RequestFromCtx(ctx), code, data)
return
}
package controller
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"dev-ops/api/v1"
)
var (
Hello = cHello{}
)
type cHello struct{}
func (c *cHello) Hello(ctx context.Context, req *v1.HelloReq) (res *v1.HelloRes, err error) {
g.RequestFromCtx(ctx).Response.Writeln("Hello World!")
return
}
File mode changed
package service
import (
"context"
v1 "dev-ops/api/v1"
"dev-ops/utility/response"
"fmt"
"github.com/gogf/gf/v2/errors/gcode"
)
type sApiList struct{}
func ApiList() *sApiList {
return &sApiList{}
}
func (s *sApiList) GetApiList(ctx context.Context, req *v1.ApiListReq) (interface{}, gcode.Code) {
param := map[string]interface{}{
"page": req.Page,
"limit": req.Limit,
"offset_time": req.OffsetTime,
}
fmt.Println(param)
//业务逻辑 先从mongodb中获取全量的openplatform的接口名称,再依次去mysql数据库中查
interfaceName := "brandgetStandardBrandList"
return "ok", response.CodeSuccess
}
package main
import (
_ "dev-ops/internal/packed"
"github.com/gogf/gf/v2/os/gctx"
"dev-ops/internal/cmd"
)
func main() {
cmd.Main.Run(gctx.New())
}
[server]
name = "dev-ops"
port = 12018
address = ":12018"
apiVersion: apps/v1
kind: Deployment
metadata:
name: template-single
labels:
app: template-single
spec:
replicas: 1
selector:
matchLabels:
app: template-single
template:
metadata:
labels:
app: template-single
spec:
containers:
- name : main
image: template-single
imagePullPolicy: Always
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
apiVersion: v1
kind: Service
metadata:
name: template-single
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8000
selector:
app: template-single
apiVersion: v1
kind: ConfigMap
metadata:
name: template-single-configmap
data:
config.yaml: |
server:
address: ":8000"
openapiPath: "/api.json"
swaggerPath: "/swagger"
logger:
level : "all"
stdout: true
apiVersion: apps/v1
kind: Deployment
metadata:
name: template-single
spec:
template:
spec:
containers:
- name : main
image: template-single:develop
\ No newline at end of file
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
- configmap.yaml
patchesStrategicMerge:
- deployment.yaml
namespace: default
FROM loads/alpine:3.8
###############################################################################
# INSTALLATION
###############################################################################
ENV WORKDIR /app
ADD resource $WORKDIR/
ADD ./temp/linux_amd64/main $WORKDIR/main
RUN chmod +x $WORKDIR/main
###############################################################################
# START
###############################################################################
WORKDIR $WORKDIR
CMD ./main
#!/bin/bash
# This shell is executed before docker build.
File mode changed
File mode changed
package response
import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/net/ghttp"
)
type JsonRes struct {
Code int `json:"code"` // 错误码((0:成功, 1:失败, >1: 错误码))
Message string `json:"msg"` // 提示信息
Data interface{} `json:"data"` // 返回数据(业务接口定义具体数据结构)
}
// Json 返回标准JSON数据。
func Json(r *ghttp.Request, code gcode.Code, data interface{}) {
// 针对全局捕获错误情况
if code == nil && data == nil {
code = CodeInternal
} else if code == nil {
code = CodeSuccess
}
r.Response.WriteJson(JsonRes{
Code: code.Code(),
Message: code.Message(),
Data: data,
})
}
// JsonExit 返回标准JSON数据并退出当前HTTP执行函数。
func JsonExit(r *ghttp.Request, code gcode.Code, data interface{}) {
Json(r, code, data)
r.Exit()
}
package response
import (
"fmt"
"github.com/gogf/gf/v2/errors/gcode"
)
type ResCode struct {
code int // Error code, usually an integer.
message string // Brief message for this error code.
detail interface{} // As type of interface, it is mainly designed as an extension field for error code.
}
// Code returns the integer number of current error code.
func (c ResCode) Code() int {
return c.code
}
// Message returns the brief message for current error code.
func (c ResCode) Message() string {
return c.message
}
// Detail returns the detailed information of current error code,
// which is mainly designed as an extension field for error code.
func (c ResCode) Detail() interface{} {
return c.detail
}
// String returns current error code as a string.
func (c ResCode) String() string {
if c.detail != nil {
return fmt.Sprintf(`%d:%s %v`, c.code, c.message, c.detail)
}
if c.message != "" {
return fmt.Sprintf(`%d:%s`, c.code, c.message)
}
return fmt.Sprintf(`%d`, c.code)
}
var (
CodeSuccess = New(0, "Success", "")
CodeFail = New(1, "Fail", "")
CodeNotFound = New(404, "Not Found", "Resource does not exist")
CodeInternal = New(500, "Internal Error", "An error occurred internally")
PramIsError = New(501, "参数有误", "")
)
func New(code int, message string, detail interface{}) gcode.Code {
return ResCode{
code: code,
message: message,
detail: detail,
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment