Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Ccheers committed Nov 1, 2024
1 parent cd8f7df commit bf47a55
Show file tree
Hide file tree
Showing 35 changed files with 4,838 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .chglog/CHANGELOG.tpl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{ range .Versions }}
<a name="{{ .Tag.Name }}"></a>
## {{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}){{ else }}{{ .Tag.Name }}{{ end }} ({{ datetime "2006-01-02" .Tag.Date }})

{{ range .CommitGroups -}}
### {{ .Title }}

{{ range .Commits -}}
* {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} ({{ .Hash.Short }})
{{ end }}
{{ end -}}

{{- if .RevertCommits -}}
### Reverts

{{ range .RevertCommits -}}
* {{ .Revert.Header }}
{{ end }}
{{ end -}}

{{- if .NoteGroups -}}
{{ range .NoteGroups -}}
### {{ .Title }}

{{ range .Notes }}
{{ .Body }}
{{ end }}
{{ end -}}
{{ end -}}
{{ end -}}
28 changes: 28 additions & 0 deletions .chglog/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
style: gitlab
template: CHANGELOG.tpl.md
info:
title: CHANGELOG
repository_url: https://github.com/opendevops-cn/codo-golang-sdk
options:
commits:
# filters:
# Type:
# - feat
# - fix
# - perf
# - refactor
commit_groups:
# title_maps:
# feat: Features
# fix: Bug Fixes
# perf: Performance Improvements
# refactor: Code Refactoring
header:
pattern: "^(\\w*)(?:\\(([\\S\\s]*)\\))?\\:(.*)$"
pattern_maps:
- Type
- Scope
- Subject
notes:
keywords:
- BREAKING CHANGE
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
### Go template
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

.idea
76 changes: 76 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
run:
timeout: 5m
tests: false
skip-dirs:
- .go
skip-dirs-use-default: false
# issues:
# exclude-use-default: false
linters-settings:
nakedret:
max-func-lines: 0
funlen:
lines: 300
statements: 150
ignore-comments: true
goconst:
min-len: 2
min-occurrences: 3
gocyclo:
min-complexity: 30
revive:
ignore-generated-header: true
severity: error
rules:
- name: var-naming
disabled: true
- name: exported
disabled: true
metadata: disable-name
ignore: ["struct"]
arguments:
- "disableStutteringCheck"
- "sayRepetitiveInsteadOfStutters"
- name: function-result-limit
severity: warning
disabled: false
arguments: [3]
lll:
line-length: 200
misspell:
locale: US
nolintlint:
allow-unused: false # report any unused nolint directives
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
linters: # https://golangci-lint.run/usage/linters/
disable-all: true
enable:
- bodyclose
- dogsled
- errcheck
- exportloopref
- funlen
- goconst
- gocyclo
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- noctx
- nolintlint
- staticcheck
- typecheck
- unconvert
- unparam
- unused
- whitespace
- goimports
# - gomnd
# - contextcheck
- containedctx
- revive
Empty file added CHANGELOG.md
Empty file.
44 changes: 44 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

.PHONY: init
init:
go install mvdan.cc/gofumpt@latest
go install golang.org/x/tools/cmd/goimports@latest
go install github.com/golangci/golangci-lint/cmd/[email protected]

.PHONY: fmt
# 格式化代码
fmt:
gofumpt -w -l .
goimports -w -l .


.PHONY: lint
# 格式化代码 lint
lint: fmt
golangci-lint run -v -c .golangci.yaml --new-from-rev=HEAD~1


.PHONY: changelog
# 变更日志
changelog:
git-chglog -o ./CHANGELOG.md


# show help
help:
@echo ''
@echo 'Usage:'
@echo ' make [target]'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-_0-9]+:/ { \
helpMessage = match(lastLine, /^# (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# codo golang SDK

## 背景
许多的业务需求其实有大量的重复工作, 可以使用通用的能力完成

## 目标
- [ ] 基础架构
- [ ] 通用的 mysql 客户端支持 otel
- [ ] 通用的 redis 客户端支持 otel
- [ ] 通用的 http 客户端支持 otel
- [ ] 通用的 消息队列 otel
- [ ] 通用的 协程池 支持 otel
- [ ] 通用的 配置 组件
- [ ] 通用的 日志 组件
- [ ] 通用的 otel propagator
- [ ] 工具链
- [ ] 自动生成 HTTP 代码(proto 转 http)
- [ ] 自动生成 MYSQL 代码(数据库转结构体)
- [ ] LINT 检查

## 目录结构
```
.
├── CHANGELOG.md 变更日志
├── Makefile 快捷工具
├── README.md
├── adapter 适配器
│ └── kratos kratos 适配
├── app 应用层
├── client 客户端
│ └── xhttp http 客户端
├── config 配置统一处理
│ ├── config.go
│ ├── config_test.go
│ └── testdata
├── consts 常量定义
│ ├── bytes.go
│ └── consts.go
├── go.mod
├── go.sum
├── internal # 私有包
│ └── meta # lib元数据
├── logger # 日志组件
│ ├── global.go
│ ├── helper.go
│ ├── level.go
│ ├── logger.go
│ └── std.go
├── middleware # 通用中间件
│ └── xsign.middleware.go
├── mq
├── mysql # mysql 客户端 wrapper
│ └── mysql.go
├── redis # redis 客户端 wrapper
│ └── redis.go
├── tools # 小工具
│ ├── cascmd # cas
│ └── xsgin # sign 签名
└── xnet # 网络工具
├── xip # ip 工具
└── xtls # tls 工具
```
21 changes: 21 additions & 0 deletions adapter/kratos/transport/websocket/replier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package websocket

import (
"context"
"net/http"
)

type IReplier[T any, R any] interface {
Apply(ctx context.Context, req R) error
Reply(ctx context.Context) (T, error)
Close(ctx context.Context) error
}

type Handler interface {
handleConnections(ctx context.Context, w http.ResponseWriter, r *http.Request)
}

type IWSHandlerBuilder interface {
Build(ctx context.Context) (Handler, error)
Path() string
}
90 changes: 90 additions & 0 deletions adapter/kratos/transport/websocket/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package websocket

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

type ServerOptions struct {
handlerBuilders []IWSHandlerBuilder

errorEncodeFunc func(w http.ResponseWriter, err error)
}

func defaultServerOptions() ServerOptions {
return ServerOptions{
errorEncodeFunc: func(w http.ResponseWriter, err error) {
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"code": 500,
"msg": err.Error(),
})
},
}
}

type IServerOption interface {
apply(*ServerOptions)
}

type ServerOptionFunc func(*ServerOptions)

func WithHandlerBuilders(handlerBuilders ...IWSHandlerBuilder) IServerOption {
return ServerOptionFunc(func(options *ServerOptions) {
options.handlerBuilders = append(options.handlerBuilders, handlerBuilders...)
})
}

func WithServerOptionErrorEncodeFunc(fn func(w http.ResponseWriter, err error)) ServerOptionFunc {
return func(options *ServerOptions) {
options.errorEncodeFunc = fn
}
}

func (f ServerOptionFunc) apply(options *ServerOptions) {
f(options)
}

type Server struct {
listener net.Listener
mux *http.ServeMux

options ServerOptions
}

func NewServer(addr string, opts ...IServerOption) (*Server, error) {
listener, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
options := defaultServerOptions()
for _, opt := range opts {
opt.apply(&options)
}

mux := http.NewServeMux()
for _, builder := range options.handlerBuilders {
mux.HandleFunc(builder.Path(), func(writer http.ResponseWriter, request *http.Request) {
handler, err := builder.Build(request.Context())
if err != nil {
options.errorEncodeFunc(writer, err)
return
}
handler.handleConnections(request.Context(), writer, request)
})
}
return &Server{
listener: listener,
mux: mux,
options: options,
}, nil
}

func (x *Server) Start(ctx context.Context) error {
return http.Serve(x.listener, x.mux)
}

func (x *Server) Stop(ctx context.Context) error {
return x.listener.Close()
}
Loading

0 comments on commit bf47a55

Please sign in to comment.