Compare commits

...

24 Commits

Author SHA1 Message Date
Xinwei Xiong(cubxxw-openim) da1316af2a fix: fix golang release file path
Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
2023-08-08 18:00:23 +08:00
Xinwei Xiong(cubxxw-openim) b759f8b5cc fix: fix golang release file path
Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
2023-08-08 17:59:45 +08:00
Xinwei Xiong(cubxxw-openim) c1f6fc4e63 fix: fix bin tools path
Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
2023-08-08 17:34:06 +08:00
Xinwei Xiong 73effdf644 fix: fix bin tools path (#814)
Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
2023-08-08 03:42:36 +00:00
dependabot[bot] c8971de833 feat(deps): bump the gomod-deps group with 1 update (#813)
Bumps the gomod-deps group with 1 update: [google.golang.org/api](https://github.com/googleapis/google-api-go-client).

- [Release notes](https://github.com/googleapis/google-api-go-client/releases)
- [Changelog](https://github.com/googleapis/google-api-go-client/blob/main/CHANGES.md)
- [Commits](https://github.com/googleapis/google-api-go-client/compare/v0.134.0...v0.135.0)

---
updated-dependencies:
- dependency-name: google.golang.org/api
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: gomod-deps
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-08-08 03:33:03 +00:00
Xinwei Xiong b26a979378 feat: add scripts format (#811)
* feat: add scripts format

Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>

* feat: add go work

Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>

---------

Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
2023-08-08 03:24:15 +00:00
Xinwei Xiong 3836bf19d6 Update docker-compose.yaml (#810) 2023-08-07 10:04:34 +00:00
dependabot[bot] f2f2448fe2 feat(deps): bump the gomod-deps group with 3 updates (#808)
Bumps the gomod-deps group with 3 updates: [github.com/go-playground/validator/v10](https://github.com/go-playground/validator), [google.golang.org/api](https://github.com/googleapis/google-api-go-client) and [google.golang.org/grpc](https://github.com/grpc/grpc-go).


Updates `github.com/go-playground/validator/v10` from 10.14.1 to 10.15.0
- [Release notes](https://github.com/go-playground/validator/releases)
- [Commits](https://github.com/go-playground/validator/compare/v10.14.1...v10.15.0)

Updates `google.golang.org/api` from 0.114.0 to 0.134.0
- [Release notes](https://github.com/googleapis/google-api-go-client/releases)
- [Changelog](https://github.com/googleapis/google-api-go-client/blob/main/CHANGES.md)
- [Commits](https://github.com/googleapis/google-api-go-client/compare/v0.114.0...v0.134.0)

Updates `google.golang.org/grpc` from 1.56.2 to 1.57.0
- [Release notes](https://github.com/grpc/grpc-go/releases)
- [Commits](https://github.com/grpc/grpc-go/compare/v1.56.2...v1.57.0)

---
updated-dependencies:
- dependency-name: github.com/go-playground/validator/v10
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: gomod-deps
- dependency-name: google.golang.org/api
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: gomod-deps
- dependency-name: google.golang.org/grpc
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: gomod-deps
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-08-07 09:42:49 +00:00
Xinwei Xiong 969a9d8782 Update README.md (#807) 2023-08-07 07:36:18 +00:00
Xinwei Xiong 6f64b0a236 Update README.md (#806) 2023-08-07 07:32:42 +00:00
Xinwei Xiong 4d04c76cd7 docs: add png multi-terminal-synchronization (#805) 2023-08-07 07:26:23 +00:00
Xinwei Xiong 7a448b35d4 feat: fix scripts and build speed (#790)
* feat: fix scripts and build speed

Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>

* feat: add docker compose file

Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>

* fix: fix image

Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>

* feat: add chat scripts design

Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>

---------

Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
2023-08-07 06:21:31 +00:00
Xinwei Xiong 594b16374b Update README.md (#803) 2023-08-07 02:22:48 +00:00
Xinwei Xiong 39275c0110 Update README.md (#802) 2023-08-07 02:07:33 +00:00
Xinwei Xiong 4ad81c363c Update README.md (#801) 2023-08-07 02:07:22 +00:00
Xinwei Xiong 10b56142d5 Update README.md (#800) 2023-08-07 02:05:22 +00:00
Xinwei Xiong 5ee54c9c2a docs: add png file (#799) 2023-08-07 02:04:27 +00:00
Xinwei Xiong 0b75c52a6e Create README.md (#798) 2023-08-07 02:01:55 +00:00
Xinwei Xiong 375105997f Update README.md (#797) 2023-08-06 07:39:06 +00:00
Xinwei Xiong 4e9179f309 Update README.md (#796) 2023-08-06 07:33:48 +00:00
Xinwei Xiong 863e925b30 Update README.md (#795) 2023-08-06 07:33:42 +00:00
Xinwei Xiong 8cb05d50c9 Update go_code.md (#794) 2023-08-06 07:13:13 +00:00
Xinwei Xiong 5d34e3f081 Update README.md (#793) 2023-08-06 07:08:11 +00:00
Xinwei Xiong d8fd4bfcf1 Update README.md (#792) 2023-08-06 05:37:16 +00:00
79 changed files with 2093 additions and 576 deletions
+2 -1
View File
@@ -43,7 +43,8 @@ jobs:
# either 'goreleaser' (default) or 'goreleaser-pro': # either 'goreleaser' (default) or 'goreleaser-pro':
distribution: goreleaser distribution: goreleaser
version: latest version: latest
args: release --clean workdir: .
args: release -f ./build/goreleaser.yaml --rm-dist --clean
env: env:
GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro' # Your GoReleaser Pro key, if you are using the 'goreleaser-pro'
+5 -2
View File
@@ -40,10 +40,13 @@ jobs:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Download Docker Compose
run: |
curl -o docker-compose.yaml https://gist.githubusercontent.com/cubxxw/b1d5cbd2edfa23fee911118aa3e8249e/raw/openim-server.sh
shell: bash
- name: Start Docker Compose - name: Start Docker Compose
run: | run: |
sudo docker compose stop
sudo sleep 30
sudo docker compose up -d sudo docker compose up -d
sudo sleep 60 sudo sleep 60
continue-on-error: true continue-on-error: true
+1 -1
View File
@@ -156,7 +156,7 @@ flycheck_*.el
# vendor/ # vendor/
# Go workspace file # Go workspace file
go.work # go.work
### JetBrains ### ### JetBrains ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
+2 -4
View File
@@ -16,8 +16,8 @@ RUN go mod download
# Copy all files to the container # Copy all files to the container
ADD . . ADD . .
RUN /bin/sh -c "make clean" RUN make clean
RUN /bin/sh -c "make build" RUN make build
FROM ghcr.io/openim-sigs/openim-bash-image:latest FROM ghcr.io/openim-sigs/openim-bash-image:latest
@@ -28,6 +28,4 @@ COPY --from=builder ${OPENIM_SERVER_CMDDIR} /openim/openim-server/scripts
COPY --from=builder ${SERVER_WORKDIR}/config /openim/openim-server/config COPY --from=builder ${SERVER_WORKDIR}/config /openim/openim-server/config
COPY --from=builder ${SERVER_WORKDIR}/_output/bin/platforms /openim/openim-server/_output/bin/platforms COPY --from=builder ${SERVER_WORKDIR}/_output/bin/platforms /openim/openim-server/_output/bin/platforms
VOLUME ["/openim/openim-server/logs","/openim/openim-server/config","/openim/openim-server/scripts"]
CMD ["bash","-c","${OPENIM_SERVER_CMDDIR}/docker_start_all.sh"] CMD ["bash","-c","${OPENIM_SERVER_CMDDIR}/docker_start_all.sh"]
+1 -1
View File
@@ -12,7 +12,7 @@ all: tidy gen add-copyright format lint cover build
# Build set # Build set
ROOT_PACKAGE=github.com/OpenIMSDK/Open-IM-Server ROOT_PACKAGE=github.com/OpenIMSDK/Open-IM-Server
# TODO: This is version control for the future # TODO: This is version control for the future https://github.com/OpenIMSDK/Open-IM-Server/issues/574
VERSION_PACKAGE=github.com/OpenIMSDK/Open-IM-Server/pkg/version VERSION_PACKAGE=github.com/OpenIMSDK/Open-IM-Server/pkg/version
# ============================================================================== # ==============================================================================
+157 -50
View File
@@ -22,42 +22,109 @@
<p align="center"> <p align="center">
<a href="./README.md"><b> English </b></a> • <a href="./README.md"><b> English </b></a> •
<a href="./README-zh_CN.md"><b> 中文 </b></a> <a href="./README-zh_CN.md"><b> 简体中文 </b></a>
<a href="https://www.openim.online/en"><b> Docs </b></a>
</p> </p>
</p> </p>
## What is Open-IM-Server ## ✨ About OpenIM
Open-IM-Server is an instant messaging server developed using pure Golang, adopting JSON over WebSocket as the communication protocol. In Open-IM-Server, everything is a message, so you can easily extend custom messages without modifying the server code. With a microservice architecture, Open-IM-Server can be deployed using clusters. By deploying Open-IM-Server on a server, developers can quickly integrate instant messaging and real-time networking features into their applications, ensuring the security and privacy of business data. Open-IM-Server, crafted meticulously using pure Golang, stands as a robust instant messaging server. Its unique approach to communication, via JSON over WebSocket, treats every interaction as a message. This simplifies customization and eliminates the need for tinkering with server code. Harnessing the power of microservice architecture, the server can be deployed via clusters, promising impressive performance and scalability.
Open-IM-Server is not a standalone product and does not include account registration and login services. For your convenience, we have open-sourced the [chat repository](https://github.com/OpenIMSDK/chat) which includes login and registration functionality. By deploying the chat business server alongside Open-IM-Server, a chat product can be set up. Open-IM-Server is more than an instant messaging server; it's a powerful tool for incorporating real-time networking into your applications, positioning itself as your premier choice for integration! 🚀
## Features Please be aware that Open-IM-Server does not function as a standalone product and does not offer built-in account registration or login services. To ease your implementation process, we've open-sourced the [chat repository](https://github.com/OpenIMSDK/chat), which comprises these features. Deploying this chat business server in conjunction with Open-IM-Server expedites the setup of a comprehensive chat product. 👥
- Open source Further enhancing your experience, we also provide an SDK client, wherein most complex logics are implemented. The [SDK repository](https://github.com/OpenIMSDK/openim-sdk-core) can be found at [this link](https://github.com/OpenIMSDK/openim-sdk-core). The [chat repository](https://github.com/OpenIMSDK/chat) is our business server while the 'core' represents the high-level encapsulation of the SDK, synergistically working together to deliver superior results. ✨
- Easy to integrate
- Excellent scalability
- High performance
- Lightweight
- Supports multiple protocols
## Community ## :star2: Why OpenIM
- Visit the official website: [OpenIM Developer Documentation](https://www.openim.online/)
## Quick Start **🔍 Function screenshot display**
### Deploying with docker-compose <div align="center">
| 💻🔄📱 Multi Terminal Synchronization 🔄🖥️ | 📅⚡ Efficient Meetings 🚀💼 |
| :----------------------------------------------------------: | :---------------------------------------------------------: |
| ![multiple-message](./assets/demo/multi-terminal-synchronization.png) | ![efficient-meetings](./assets/demo/efficient-meetings.png) |
| 📲🔄 **One-to-one and Group Chats** 👥🗣️ | 🎁💻 **Special Features - Custom Messages** ✉️🎨|
| ![group-chat](./assets/demo/group-chat.png) | ![special-function](./assets/demo/special-function.png) |
</div>
1. **Comprehensive Message Type Support :speech_balloon:**
✅ Supports almost all types of messages, including text, images, emojis, voice, video, geographical location, files, quotes, business cards, system notifications, custom messages and more
✅ Supports one-on-one and multi-person audio and video calls
✅ Provides terminal support for multiple platforms such as iOS, Android, Flutter, uni-app, ReactNative, Electron, Web, H5
2. **Efficient Meetings Anytime, Anywhere :earth_americas:**
✅ Based on IM (Instant Messaging) with 100% reliable forced signaling capabilities, it paves the way for IM systems, deeply integrated with chat applications
✅ Supports hundreds of people in a single meeting, with subscription numbers reaching thousands, and server-side audio and video recording
3. **One-on-one and Group Chats for Various Social Scenarios :busts_in_silhouette:**
✅ OpenIM has four roles: application administrator, group owner, group administrator, and regular member
✅ Powerful group features such as muting, group announcements, group validation, unlimited group members, and loading group messages as needed
4. **Unique Features :star2:**
✅ Supports read-and-burn private chats, customizable duration
✅ Message editing function broadens social scenarios, making instant communication more diverse and interesting
5. **Open Source :open_hands:**
✅ The code of OpenIM is open source, self-controlled data, aimed at building a globally leading IM open source community, including client SDK and server
✅ Based on open source Server, many excellent open source projects have been developed, such as [OpenKF](https://github.com/OpenIMSDK/OpenKF) (Open source AI customer service system)
6. **Easy to Expand :wrench:**
✅ The OpenIM server is implemented in Golang, introducing an innovative "everything is a message" communication model, simplifying the implementation of custom messages and extended features
7. **High Performance :racing_car:**
✅ OpenIM supports a hierarchical governance architecture in the cluster, tested by a large number of users, and abstracts the storage model of online messages, offline messages, and historical messages
8. **Full Platform Support :tv:**
✅ Supports native iOS, Android; cross-platform Flutter, uni-app, ReactNative; major web front-end frameworks such as React, Vue; applets; and PC platforms supported by Electron
9. **The ultimate deployment experience 🤖**
✅ Supports [cluster deployment](https://github.com/OpenIMSDK/Open-IM-Server/edit/main/deployments/README.md)
✅ Supports multi-architecture mirroring, our Docker images are hosted not only on GitHub but also on Alibaba Cloud and Docker Hub supporting multiple architectures. Visit [our GitHub packages](https://github.com/orgs/OpenIMSDK/packages?repo_name=Open-IM-Server) and read our [version management document](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/version.md) for more information.
11. **A large ecosystem of open source communities 🤲**
✅ We have tens of thousands of users and many solutions to problems.
✅ We have a large open source community called [OpenIMSDK](https://github.com/OpenIMSDK) that runs the core modules, we have an open source community called [openim-sigs](https://github.com/openim-sigs) to explore more IM-based infrastructure products.
## :rocket: Quick Start
<details> <summary>Deploying with Docker Compose</summary>
1. Clone the project 1. Clone the project
```bash ```bash
# choose what you need # choose what you need
BRANCH=release-v3.0 BRANCH=release-v3.1
git clone -b $BRANCH https://github.com/OpenIMSDK/Open-IM-Server openim && export openim=$(pwd)/openim && cd $openim && make build git clone -b $BRANCH https://github.com/OpenIMSDK/Open-IM-Server openim && export openim=$(pwd)/openim && cd $openim && make build
``` ```
2. Modify .env > **Note**
> Read our release policy: https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/version.md
2. Modify `.env`
```bash ```bash
USER=root #no need to modify USER=root #no need to modify
@@ -70,9 +137,9 @@ DATA_DIR=./ #designate large disk directory
3. Deploy and start 3. Deploy and start
> **Note** > **Note**
> This command can only be executed once. It will modify the component passwords in docker-compose based on the PASSWORD variable in .env, and modify the component passwords in config/config.yaml. If the password in .env changes, you need to first execute docker-compose down; rm components -rf and then execute this command. > This command can only be executed once. It will modify the component passwords in docker-compose based on the `PASSWORD` variable in `.env`, and modify the component passwords in `config/config.yaml`. If the password in `.env` changes, you need to first execute `docker-compose down`; `rm components -rf` and then execute this command.
``` ```bash
make install make install
``` ```
@@ -84,9 +151,9 @@ make check
![https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/images/docker_build.png](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/images/docker_build.png) ![https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/images/docker_build.png](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/images/docker_build.png)
</details>
<details> <summary>Compile from Source</summary>
### Compile from source
Ur need `Go 1.18` or higher version, and `make`. Ur need `Go 1.18` or higher version, and `make`.
@@ -97,6 +164,7 @@ Version Details: https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conv
BRANCH=release-v3.1 BRANCH=release-v3.1
git clone -b $BRANCH https://github.com/OpenIMSDK/Open-IM-Server openim && export openim=$(pwd)/openim && cd $openim && make build git clone -b $BRANCH https://github.com/OpenIMSDK/Open-IM-Server openim && export openim=$(pwd)/openim && cd $openim && make build
``` ```
Read about the [OpenIM Version Policy](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/version.md) Read about the [OpenIM Version Policy](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/version.md)
`make help` to help you see the instructions supported by OpenIM. `make help` to help you see the instructions supported by OpenIM.
@@ -105,7 +173,9 @@ All services have been successfully built as shown in the figure
![Successful Compilation](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/images/build.png) ![Successful Compilation](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/images/build.png)
### Component Configuration Instructions </details>
<details> <summary>Component Configuration Instructions</summary>
The config/config.yaml file has detailed configuration instructions for the storage components. The config/config.yaml file has detailed configuration instructions for the storage components.
@@ -113,7 +183,7 @@ The config/config.yaml file has detailed configuration instructions for the stor
- Used for RPC service discovery and registration, cluster support. - Used for RPC service discovery and registration, cluster support.
``` ```bash
zookeeper: zookeeper:
schema: openim #Not recommended to modify schema: openim #Not recommended to modify
address: [ 127.0.0.1:2181 ] #address address: [ 127.0.0.1:2181 ] #address
@@ -125,7 +195,7 @@ The config/config.yaml file has detailed configuration instructions for the stor
- Used for storing users, relationships, and groups, supports master-slave database. - Used for storing users, relationships, and groups, supports master-slave database.
``` ```bash
mysql: mysql:
address: [ 127.0.0.1:13306 ] #address address: [ 127.0.0.1:13306 ] #address
username: root #username username: root #username
@@ -142,7 +212,7 @@ The config/config.yaml file has detailed configuration instructions for the stor
- Used for storing offline messages, supports mongo sharded clusters. - Used for storing offline messages, supports mongo sharded clusters.
``` ```bash
mongo: mongo:
uri: #Use this value directly if not empty uri: #Use this value directly if not empty
address: [ 127.0.0.1:37017 ] #address address: [ 127.0.0.1:37017 ] #address
@@ -156,7 +226,7 @@ The config/config.yaml file has detailed configuration instructions for the stor
- Used for storing message sequence numbers, latest messages, user tokens, and mysql cache, supports cluster deployment. - Used for storing message sequence numbers, latest messages, user tokens, and mysql cache, supports cluster deployment.
``` ```bash
redis: redis:
address: [ 127.0.0.1:16379 ] #address address: [ 127.0.0.1:16379 ] #address
username: #username username: #username
@@ -167,7 +237,7 @@ The config/config.yaml file has detailed configuration instructions for the stor
- Used for message queues, for message decoupling, supports cluster deployment. - Used for message queues, for message decoupling, supports cluster deployment.
``` ```bash
kafka: kafka:
username: #username username: #username
password: #password password: #password
@@ -188,27 +258,31 @@ The config/config.yaml file has detailed configuration instructions for the stor
msgToModify: modify msgToModify: modify
``` ```
### Start and Stop Services </details>
<details> <summary>Start and Stop Services</summary>
Start services Start services
``` ```
./start_all.sh; ./scripts/start_all.sh;
``` ```
Check services Check services
``` ```
./check_all.sh ./scripts/check_all.sh
``` ```
Stop services Stop services
``` ```
./stop_all.sh ./scripts/stop_all.sh
``` ```
### Open IM Ports </details>
<details> <summary>Open IM Ports</summary>
| TCP Port | Description | Operation | | TCP Port | Description | Operation |
| --------- | ------------------------------------------------------------ | ----------------------------------------------------- | | --------- | ------------------------------------------------------------ | ----------------------------------------------------- |
@@ -216,42 +290,75 @@ Stop services
| TCP:10002 | api port, such as user, friend, group, message interfaces. | Port release or nginx reverse proxy, and firewall off | | TCP:10002 | api port, such as user, friend, group, message interfaces. | Port release or nginx reverse proxy, and firewall off |
| TCP:10005 | Required when choosing minio storage (openIM uses minio storage by default) | Port release or nginx reverse proxy, and firewall off | | TCP:10005 | Required when choosing minio storage (openIM uses minio storage by default) | Port release or nginx reverse proxy, and firewall off |
### Open Chat Ports </details>
<details> <summary>Open Chat Ports</summary>
+ chat warehouse: https://github.com/OpenIMSDK/chat
| TCP Port | Description | Operation | | TCP Port | Description | Operation |
| --------- | --------------------------------------------------- | ----------------------------------------------------- | | --------- | --------------------------------------------------- | ----------------------------------------------------- |
| TCP:10008 | Business system, such as registration, login etc | Port release or nginx reverse proxy, and firewall off | | TCP:10008 | Business system, such as registration, login etc | Port release or nginx reverse proxy, and firewall off |
| TCP:10009 | Management backend, such as statistics, banning etc | Port release or nginx reverse proxy, and firewall off | | TCP:10009 | Management backend, such as statistics, banning etc | Port release or nginx reverse proxy, and firewall off |
## Relationship Between APP and OpenIM </details>
OpenIM is an open source instant messaging component, it is not an independent product. This image shows the relationship between AppServer, AppClient, Open-IM-Server and Open-IM-SDK. ## :link: Relationship Between APP and OpenIM
![https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/open-im-server.png](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/images/open-im-server.png) OpenIM isn't just an open-source instant messaging component, it's an integral part of your application ecosystem. Check out this diagram to understand how AppServer, AppClient, Open-IM-Server, and Open-IM-SDK interact.
## Overall Architecture ![App-OpenIM Relationship](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/images/open-im-server.png)
![https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/Architecture.jpg](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/images/Architecture.jpg) ## :building_construction: Overall Architecture
## To start developing OpenIM Delve into the heart of Open-IM-Server's functionality with our architecture diagram.
The [community repository](https://github.com/OpenIMSDK/community) hosts all information about building Kubernetes from source, how to contribute code and documentation, who to contact about what, etc.
![Overall Architecture](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/images/Architecture.jpg)
## Contributing ## :hammer_and_wrench: To start developing OpenIM
Contributions to this project are welcome! Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for details. OpenIM Our goal is to build a top-level open source community. We have a set of standards, in the [Community repository](https://github.com/OpenIMSDK/community).
## Community Meetings If you'd like to contribute to this Open-IM-Server repository, please read our [contributor documentation](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/CONTRIBUTING.md).
We want anyone to get involved in our community, we offer gifts and rewards, and we welcome you to join us every Thursday night.
We take notes of each [biweekly meeting](https://github.com/OpenIMSDK/Open-IM-Server/issues/381) in [GitHub discussions](https://github.com/OpenIMSDK/Open-IM-Server/discussions/categories/meeting), and our minutes are written in [Google Docs](https://docs.google.com/document/d/1nx8MDpuG74NASx081JcCpxPgDITNTpIIos0DS6Vr9GU/edit?usp=sharing). Before you start, please make sure your changes are in demand. The best for that is to create a [new discussion](https://github.com/OpenIMSDK/Open-IM-Server/discussions/new/choose) OR [Slack Communication](https://join.slack.com/t/openimsdk/shared_invite/zt-1tmoj26uf-_FDy3dowVHBiGvLk9e5Xkg), or if you find an issue, [report it](https://github.com/OpenIMSDK/Open-IM-Server/issues/new/choose) first.
- [Code Standards](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/go_code.md)
- [Docker Images Standards](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/images.md)
- [Directory Standards](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/directory.md)
- [Commit Standards](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/commit.md)
- [Versioning Standards](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/version.md)
- [Interface Standards](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/api.md)
- [Log Standards](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/log.md)
- [Error Code Standards](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/error_code.md)
## Who are using Open-IM-Server ## :busts_in_silhouette: Community
The [user case studies](https://github.com/OpenIMSDK/community/blob/main/ADOPTERS.md) page includes the user list of the project. You can leave a [📝comment](https://github.com/OpenIMSDK/Open-IM-Server/issues/379) to let us know your use case.
![avatar](https://openim-1253691595.cos.ap-nanjing.myqcloud.com/WechatIMG20.jpeg) + 📚 [OpenIM Community](https://github.com/OpenIMSDK/community)
+ 💕 [OpenIM Interest Group](https://github.com/Openim-sigs)
+ 🚀 [Join our Slack community](https://join.slack.com/t/openimsdk/shared_invite/zt-1tmoj26uf-_FDy3dowVHBiGvLk9e5Xkg)
+ :eyes: [Join our wechat (微信群)](https://openim-1253691595.cos.ap-nanjing.myqcloud.com/WechatIMG20.jpeg)
## License ## :calendar: Community Meetings
Open-IM-Server is under the Apache 2.0 license. See the [LICENSE](./LICENSE) file for details We want anyone to get involved in our community and contributing code, we offer gifts and rewards, and we welcome you to join us every Thursday night.
Our conference is in the [OpenIM Slack](https://join.slack.com/t/openimsdk/shared_invite/zt-1tmoj26uf-_FDy3dowVHBiGvLk9e5Xkg) 🎯, then you can search the Open-IM-Server pipeline to join
We take notes of each [biweekly meeting](https://github.com/orgs/OpenIMSDK/discussions/categories/meeting) in [GitHub discussions](https://github.com/OpenIMSDK/Open-IM-Server/discussions/categories/meeting), Our historical meeting notes, as well as replays of the meetings are available at [Google Docs :bookmark_tabs:](https://docs.google.com/document/d/1nx8MDpuG74NASx081JcCpxPgDITNTpIIos0DS6Vr9GU/edit?usp=sharing).
## :eyes: Who are using OpenIM
Check out our [user case studies](https://github.com/OpenIMSDK/community/blob/main/ADOPTERS.md) page for a list of the project users. Don't hesitate to leave a [📝comment](https://github.com/OpenIMSDK/Open-IM-Server/issues/379) and share your use case.
## :page_facing_up: License
OpenIM is licensed under the Apache 2.0 license. See [LICENSE](https://github.com/OpenIMSDK/Open-IM-Server/tree/main/LICENSE) for the full license text.
The OpenIM logo, including its variations and animated versions, displayed in this repository [OpenIM](https://github.com/OpenIMSDK/Open-IM-Server) under the [assets/logo](./assets/logo) and [assets/logo-gif](assets/logo-gif) directories, are protected by copyright laws.
## 🔮 Thanks to our contributors!
<a href="https://github.com/OpenIMSDK/Open-IM-Server/graphs/contributors">
<img src="https://contrib.rocks/image?repo=OpenIMSDK/Open-IM-Server" />
</a>
+14
View File
@@ -0,0 +1,14 @@
## :star2: Why OpenIM
**🔍 Function screenshot display**
<div align="center">
| multiple message | Efficient meetings |
| :---------------------------------------: | :---------------------------------------------: |
| ![multiple-message](./multiple-message.png) | ![efficient-meetings](./efficient-meetings.png) |
| **One-to-one and group chats** | **Special features - Custom messages** |
| ![group-chat](./group-chat.png) | ![special-function](./special-function.png) |
</div>
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 418 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 931 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 318 KiB

+1 -1
View File
@@ -43,4 +43,4 @@ COPY --from=builder /openim/openim-server/config /openim/openim-server/config
EXPOSE ${10002} EXPOSE ${10002}
CMD ["sh","-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-api --port 10002 -c ${SERVER_WORKDIR}/config"] CMD ["bash", "-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-api --port 10002 -c ${SERVER_WORKDIR}/config"]
+1 -1
View File
@@ -42,4 +42,4 @@ WORKDIR /openim/openim-server
COPY --from=builder ${SERVER_WORKDIR}/_output/bin/platforms /openim/openim-server/_output/bin/platforms COPY --from=builder ${SERVER_WORKDIR}/_output/bin/platforms /openim/openim-server/_output/bin/platforms
COPY --from=builder ${SERVER_WORKDIR}/config /openim/openim-server/config COPY --from=builder ${SERVER_WORKDIR}/config /openim/openim-server/config
CMD ["sh","-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-cmdutils"] CMD ["bash", "-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-cmdutils"]
+1 -1
View File
@@ -41,4 +41,4 @@ WORKDIR /openim/openim-server
COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms COPY --from=builder /openim/openim-server/_output/bin/platforms /openim/openim-server/_output/bin/platforms
COPY --from=builder /openim/openim-server/config /openim/openim-server/config COPY --from=builder /openim/openim-server/config /openim/openim-server/config
CMD ["sh","-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-crontask"] CMD ["bash", "-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-crontask"]
+1 -1
View File
@@ -49,4 +49,4 @@ COPY --from=builder /openim/openim-server/config /openim/openim-server/config
EXPOSE 10160 EXPOSE 10160
CMD ["sh","-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-auth --port 10160 -c ${SERVER_WORKDIR}/config"] CMD ["bash", "-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-auth --port 10160 -c ${SERVER_WORKDIR}/config"]
@@ -56,4 +56,4 @@ COPY --from=builder /openim/openim-server/config /openim/openim-server/config
EXPOSE 10230 EXPOSE 10230
EXPOSE 20230 EXPOSE 20230
CMD ["sh","-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-conversation --port 10230 --prometheus_port 20230 -c ${SERVER_WORKDIR}/config"] CMD ["bash", "-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-conversation --port 10230 --prometheus_port 20230 -c ${SERVER_WORKDIR}/config"]
+1 -1
View File
@@ -56,4 +56,4 @@ COPY --from=builder /openim/openim-server/config /openim/openim-server/config
EXPOSE 10120 EXPOSE 10120
EXPOSE 20120 EXPOSE 20120
CMD ["sh","-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-friend --port 10120 --prometheus_port 20120 -c ${SERVER_WORKDIR}/config"] CMD ["bash", "-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-friend --port 10120 --prometheus_port 20120 -c ${SERVER_WORKDIR}/config"]
+1 -1
View File
@@ -56,4 +56,4 @@ COPY --from=builder /openim/openim-server/config /openim/openim-server/config
EXPOSE 10150 EXPOSE 10150
EXPOSE 20150 EXPOSE 20150
CMD ["sh","-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-group --port 10150 --prometheus_port 20150 -c ${SERVER_WORKDIR}/config"] CMD ["bash", "-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-group --port 10150 --prometheus_port 20150 -c ${SERVER_WORKDIR}/config"]
+1 -1
View File
@@ -56,4 +56,4 @@ COPY --from=builder /openim/openim-server/config /openim/openim-server/config
EXPOSE 10130 EXPOSE 10130
EXPOSE 20130 EXPOSE 20130
CMD ["sh","-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-msg --port 10130 --prometheus_port 20130 -c ${SERVER_WORKDIR}/config"] CMD ["bash", "-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-msg --port 10130 --prometheus_port 20130 -c ${SERVER_WORKDIR}/config"]
+1 -1
View File
@@ -55,4 +55,4 @@ COPY --from=builder /openim/openim-server/config /openim/openim-server/config
EXPOSE 10200 EXPOSE 10200
CMD ["sh","-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-third --port 10200 -c ${SERVER_WORKDIR}/config"] CMD ["bash", "-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-third --port 10200 -c ${SERVER_WORKDIR}/config"]
+1 -1
View File
@@ -55,4 +55,4 @@ COPY --from=builder /openim/openim-server/config /openim/openim-server/config
EXPOSE 10110 EXPOSE 10110
CMD ["sh","-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-user --port 10110 -c ${SERVER_WORKDIR}/config"] CMD ["bash", "-c","${OPENIM_SERVER_BINDIR}/platforms/${OS}/${ARCH}/openim-rpc-user --port 10110 -c ${SERVER_WORKDIR}/config"]
+11
View File
@@ -2,6 +2,17 @@
OpenIM 支持很多种集群化部署方式,包括但不限于 helm, sealos, kubeam, kubesphere, kubeflow, kuboard, kubespray, k3s, k3d, k3c, k3sup, k3v, k3x OpenIM 支持很多种集群化部署方式,包括但不限于 helm, sealos, kubeam, kubesphere, kubeflow, kuboard, kubespray, k3s, k3d, k3c, k3sup, k3v, k3x
**目前还在开发这个模块,预计 v3.2.0 之前会有一个集群方案。**
目前各个贡献者,以及之前的官方有出过一些可以参考的方案:
- https://github.com/OpenIMSDK/k8s-jenkins
- https://github.com/OpenIMSDK/Open-IM-Server-k8s-deploy
- https://github.com/OpenIMSDK/openim-charts
- https://github.com/showurl/deploy-openim
### 依赖检查 ### 依赖检查
```bash ```bash
+5 -8
View File
@@ -52,7 +52,6 @@ services:
net.core.somaxconn: 1024 net.core.somaxconn: 1024
command: redis-server --requirepass ${PASSWORD} --appendonly yes command: redis-server --requirepass ${PASSWORD} --appendonly yes
zookeeper: zookeeper:
image: wurstmeister/zookeeper image: wurstmeister/zookeeper
ports: ports:
@@ -64,7 +63,6 @@ services:
TZ: Asia/Shanghai TZ: Asia/Shanghai
restart: always restart: always
kafka: kafka:
image: wurstmeister/kafka image: wurstmeister/kafka
container_name: kafka container_name: kafka
@@ -98,9 +96,8 @@ services:
restart: always restart: always
command: minio server /data --console-address ':9090' command: minio server /data --console-address ':9090'
openim-server:
openim_server: image: ghcr.io/openimsdk/openim-server:latest
image: ghcr.io/openim-sigs/openim-server:v1.0.0-debug.11 #ghcr.io/openimsdk/openim-server:main
container_name: openim-server container_name: openim-server
volumes: volumes:
- ./logs:/openim/openim-server/logs - ./logs:/openim/openim-server/logs
@@ -122,7 +119,7 @@ services:
max-file: "2" max-file: "2"
openim-chat: openim-chat:
image: ghcr.io/openim-sigs/openim-chat:v1.0.0-debug.11 # ghcr.io/openimsdk/openim-chat:main image: ghcr.io/openimsdk/openim-chat:latest
container_name: openim-chat container_name: openim-chat
volumes: volumes:
- ./_output/openim/openim-chat/logs:/openim/openim-chat/logs - ./_output/openim/openim-chat/logs:/openim/openim-chat/logs
@@ -134,7 +131,7 @@ services:
- mongodb - mongodb
- redis - redis
- minio - minio
- openim_server - openim-server
network_mode: "host" network_mode: "host"
logging: logging:
driver: json-file driver: json-file
@@ -150,7 +147,7 @@ services:
# ports: # ports:
# - 9091:9091 # - 9091:9091
depends_on: depends_on:
- openim_server - openim-server
command: --web.listen-address=:9091 --config.file="/etc/prometheus/prometheus.yml" command: --web.listen-address=:9091 --config.file="/etc/prometheus/prometheus.yml"
network_mode: "host" network_mode: "host"
+1 -4
View File
@@ -1,8 +1,5 @@
docs/.generated_docs docs/.generated_docs
docs/guide/en-US/cmd/iam-apiserver.md
docs/guide/en-US/cmd/iam-authz-server.md
docs/guide/en-US/cmd/iam-pump.md
docs/guide/en-US/cmd/iam-watcher.md
docs/guide/en-US/cmd/openim/openim.md docs/guide/en-US/cmd/openim/openim.md
docs/guide/en-US/cmd/openim/openim_color.md docs/guide/en-US/cmd/openim/openim_color.md
docs/guide/en-US/cmd/openim/openim_completion.md docs/guide/en-US/cmd/openim/openim_completion.md
+201 -209
View File
@@ -1,23 +1,19 @@
## Go 代码开发规范 ## OpenIM development specification
在Go 项目开发中,一个好的编码规范可以极大的提高代码质量。为了帮你节省时间和精力,这里我整理了一份清晰、可直接套用的 Go 编码规范,供你参考。 We have very high standards for code style and specification, and we want our products to be polished and perfect
这份规范,是我参考了 Go 官方提供的编码规范,以及 Go 社区沉淀的一些比较合理的规范之后,加入自己的理解总结出的,它比很多公司内部的规范更全面,你掌握了,以后在面试大厂的时候,或者在大厂里写代码的时候,都会让人高看你一眼,觉得你code很专业。 ## 1. Code style
这份编码规范中包含代码风格、命名规范、注释规范、类型、控制结构、函数、GOPATH 设置规范、依赖管理和最佳实践九类规范。如果你觉得这些规范内容太多了,看完一遍也记不住,这完全没关系。你可以多看几遍,也可以在用到时把它翻出来,在实际应用中掌握。这篇特别放送的内容,更多是作为写代码时候的一个参考手册。 ### 1.1 Code format
## 1. 代码风格 - Code must be formatted with `gofmt`.
- Leave spaces between operators and operands.
### 1.1 代码格式 - It is recommended that a line of code does not exceed 120 characters. If the part exceeds, please use an appropriate line break method. But there are also some exception scenarios, such as import lines, code automatically generated by tools, and struct fields with tags.
- The file length cannot exceed 800 lines.
- 代码都必须用 `gofmt` 进行格式化。 - Function length cannot exceed 80 lines.
- 运算符和操作数之间要留空格。 - import specification
- 建议一行代码不超过120个字符,超过部分,请采用合适的换行方式换行。但也有些例外场景,例如import行、工具自动生成的代码、带tag的struct字段。 - All code must be formatted with `goimports` (it is recommended to set the code Go code editor to: run `goimports` on save).
- 文件长度不能超过800行。 - Do not use relative paths to import packages, such as `import ../util/net`.
- 函数长度不能超过80行。 - Import aliases must be used when the package name does not match the last directory name of the import path, or when multiple identical package names conflict.
- import规范
- 代码都必须用`goimports`进行格式化(建议将代码Go代码编辑器设置为:保存时运行 `goimports`)。
- 不要使用相对路径引入包,例如 `import ../util/net`
- 包名称与导入路径的最后一个目录名不匹配时,或者多个相同包名冲突时,则必须使用导入别名。
```go ```go
// bad // bad
@@ -26,32 +22,32 @@
//good //good
jwt "github.com/dgrijalva/jwt-go/v4" jwt "github.com/dgrijalva/jwt-go/v4"
``` ```
- 导入的包建议进行分组,匿名包的引用使用一个新的分组,并对匿名包引用进行说明。 - Imported packages are suggested to be grouped, and anonymous package references use a new group, and anonymous package references are explained.
```go ```go
import ( import (
// go 标准包 // go standard package
"fmt" "fmt"
// 第三方包 // third party package
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
// 匿名包单独分组,并对匿名包引用进行说明 // Anonymous packages are grouped separately, and anonymous package references are explained
// import mysql driver // import mysql driver
_ "github.com/jinzhu/gorm/dialects/mysql" _ "github.com/jinzhu/gorm/dialects/mysql"
// 内部包 // inner package
v1 "github.com/marmotedu/api/apiserver/v1" v1 "github.com/marmotedu/api/apiserver/v1"
metav1 "github.com/marmotedu/apimachinery/pkg/meta/v1" metav1 "github.com/marmotedu/apimachinery/pkg/meta/v1"
"github.com/marmotedu/iam/pkg/cli/genericclioptions" "github.com/marmotedu/iam/pkg/cli/genericclioptions"
) )
``` ```
### 1.2 声明、初始化和定义 ### 1.2 Declaration, initialization and definition
当函数中需要使用到多个变量时,可以在函数开始处使用`var`声明。在函数外部声明必须使用 `var` ,不要采用 `:=` ,容易踩到变量的作用域的问题。 When multiple variables need to be used in a function, the `var` declaration can be used at the beginning of the function. Declaration outside the function must use `var`, do not use `:=`, it is easy to step on the scope of the variable.
```go ```go
var ( var (
@@ -60,7 +56,7 @@ var (
) )
``` ```
- 在初始化结构引用时,请使用`&T{}`代替`new(T)`,以使其与结构体初始化一致。 - When initializing a structure reference, please use `&T{}` instead of `new(T)` to make it consistent with structure initialization.
```go ```go
// bad // bad
@@ -71,7 +67,7 @@ sptr.Name = "bar"
sptr := &T{Name: "bar"} sptr := &T{Name: "bar"}
``` ```
- struct 声明和初始化格式采用多行,定义如下。 - The struct declaration and initialization format takes multiple lines and is defined as follows.
```go ```go
type User struct{ type User struct{
@@ -85,7 +81,7 @@ user := User{
} }
``` ```
- 相似的声明放在一组,同样适用于常量、变量和类型声明。 - Similar declarations are grouped together, and the same applies to constant, variable, and type declarations.
```go ```go
// bad // bad
@@ -99,14 +95,14 @@ import (
) )
``` ```
- 尽可能指定容器容量,以便为容器预先分配内存,例如: - Specify container capacity where possible to pre-allocate memory for the container, for example:
```go ```go
v := make(map[int]string, 4) v := make(map[int]string, 4)
v := make([]string, 0, 4) v := make([]string, 0, 4)
``` ```
- 在顶层,使用标准var关键字。请勿指定类型,除非它与表达式的类型不同。 - At the top level, use the standard var keyword. Do not specify a type unless it is different from the type of the expression.
```go ```go
// bad // bad
@@ -116,13 +112,13 @@ func F() string { return "A" }
// good // good
var_s = F() var_s = F()
// 由于 F 已经明确了返回一个字符串类型,因此我们没有必要显式指定_s 的类型 // Since F already explicitly returns a string type, we don't need to explicitly specify the type of _s
// 还是那种类型 // still of that type
func F() string { return "A" } func F() string { return "A" }
``` ```
- 对于未导出的顶层常量和变量,使用`_`作为前缀。 - Use `_` as a prefix for unexported top-level constants and variables.
```go ```go
// bad // bad
@@ -138,7 +134,7 @@ const (
) )
``` ```
- 嵌入式类型(例如 mutex)应位于结构体内的字段列表的顶部,并且必须有一个空行将嵌入式字段与常规字段分隔开。 - Embedded types (such as mutexes) should be at the top of the field list within the struct, and there must be a blank line separating embedded fields from regular fields.
```go ```go
// bad // bad
@@ -155,9 +151,9 @@ type Client struct {
} }
``` ```
### 1.3 错误处理 ### 1.3 Error Handling
- `error`作为函数的值返回,必须对`error`进行处理,或将返回值赋值给明确忽略。对于`defer xx.Close()`可以不用显式处理。 - `error` is returned as the value of the function, `error` must be handled, or the return value assigned to explicitly ignore. For `defer xx.Close()`, there is no need to explicitly handle it.
```go ```go
func load() error { func load() error {
@@ -171,7 +167,7 @@ load()
_ = load() _ = load()
``` ```
- `error`作为函数的值返回且有多个返回值的时候,`error`必须是最后一个参数。 - When `error` is returned as the value of a function and there are multiple return values, `error` must be the last parameter.
```go ```go
// bad // bad
@@ -185,7 +181,7 @@ func load() (int, error) {
} }
``` ```
- 尽早进行错误处理,并尽早返回,减少嵌套。 - Perform error handling as early as possible and return as early as possible to reduce nesting.
```go ```go
// bad // bad
@@ -203,7 +199,7 @@ if err != nil {
// normal code // normal code
``` ```
- 如果需要在 if 之外使用函数调用的结果,则应采用下面的方式。 - If you need to use the result of the function call outside if, you should use the following method.
```go ```go
// bad // bad
@@ -218,7 +214,7 @@ if err != nil {
} }
``` ```
- 错误要单独判断,不与其他逻辑组合判断。 - Errors should be judged independently, not combined with other logic.
```go ```go
// bad // bad
@@ -241,7 +237,7 @@ if v == nil {
} }
``` ```
- 如果返回值需要初始化,则采用下面的方式。 - If the return value needs to be initialized, use the following method.
```go ```go
v, err := f() v, err := f()
@@ -251,8 +247,8 @@ if err != nil {
} }
``` ```
- 错误描述建议 - Bug description suggestions
- 错误描述用小写字母开头,结尾不要加标点符号,例如: - Error descriptions start with a lowercase letter and do not end with punctuation, for example:
```go ```go
// bad // bad
errors.New("Redis connection failed") errors.New("Redis connection failed")
@@ -261,34 +257,34 @@ if err != nil {
// good // good
errors.New("redis connection failed") errors.New("redis connection failed")
``` ```
- 告诉用户他们可以做什么,而不是告诉他们不能做什么。 - Tell users what they can do, not what they can't.
- 当声明一个需求时,用must 而不是should。例如,`must be greater than 0must match regex '[a-z]+'` - When declaring a requirement, use must instead of should. For example, `must be greater than 0, must match regex '[a-z]+'`.
- 当声明一个格式不对时,用must not。例如,`must not contain` - When declaring that a format is incorrect, use must not. For example, `must not contain`.
- 当声明一个动作时用may not。例如,`may not be specified when otherField is emptyonly name may be specified` - Use may not when declaring an action. For example, `may not be specified when otherField is empty, only name may be specified`.
- 引用文字字符串值时,请在单引号中指示文字。例如,`ust not contain '..'` - When quoting a literal string value, indicate the literal in single quotes. For example, `ust not contain '..'`.
- 当引用另一个字段名称时,请在反引号中指定该名称。例如,must be greater than `request` - When referencing another field name, specify that name in backticks. For example, must be greater than `request`.
- 指定不等时,请使用单词而不是符号。例如,`must be less than 256must be greater than or equal to 0 (不要用 larger thanbigger thanmore thanhigher than)` - When specifying unequal, use words instead of symbols. For example, `must be less than 256, must be greater than or equal to 0 (do not use larger than, bigger than, more than, higher than)`.
- 指定数字范围时,请尽可能使用包含范围。 - When specifying ranges of numbers, use inclusive ranges whenever possible.
- 建议 Go 1.13 以上,error 生成方式为 `fmt.Errorf("module xxx: %w", err)` - Go 1.13 or above is recommended, and the error generation method is `fmt.Errorf("module xxx: %w", err)`.
### 1.4 panic处理 ### 1.4 panic processing
- 在业务逻辑处理中禁止使用panic。 - Panic is prohibited in business logic processing.
- 在main包中,只有当程序完全不可运行时使用panic,例如无法打开文件、无法连接数据库导致程序无法正常运行。 - In the main package, panic is only used when the program is completely inoperable, for example, the file cannot be opened, the database cannot be connected, and the program cannot run normally.
- 在main包中,使用 `log.Fatal` 来记录错误,这样就可以由log来结束程序,或者将panic抛出的异常记录到日志文件中,方便排查问题。 - In the main package, use `log.Fatal` to record errors, so that the program can be terminated by the log, or the exception thrown by the panic can be recorded in the log file, which is convenient for troubleshooting.
- 可导出的接口一定不能有panic - An exportable interface must not panic.
- 包内建议采用error而不是panic来传递错误。 - It is recommended to use error instead of panic to convey errors in the package.
### 1.5 单元测试 ### 1.5 Unit Tests
- 单元测试文件名命名规范为 `example_test.go` - The unit test filename naming convention is `example_test.go`.
- 每个重要的可导出函数都要编写测试用例。 - Write a test case for every important exportable function.
- 因为单元测试文件内的函数都是不对外的,所以可导出的结构体、函数等可以不带注释。 - Because the functions in the unit test file are not external, the exportable structures, functions, etc. can be uncommented.
- 如果存在 `func (b *Bar) Foo` ,单测函数可以为 `func TestBar_Foo` - If `func (b *Bar) Foo` exists, the single test function can be `func TestBar_Foo`.
### 1.6 类型断言失败处理 ### 1.6 Type assertion failure handling
- type assertion 的单个返回值针对不正确的类型将产生 panic。请始终使用 “comma ok”的惯用法。 - A single return value from a type assertion will panic for an incorrect type. Always use the "comma ok" idiom.
```go ```go
// bad // bad
@@ -301,85 +297,85 @@ if !ok {
} }
``` ```
## 2. 命名规范 ## 2. Naming convention
命名规范是代码规范中非常重要的一部分,一个统一的、短小的、精确的命名规范可以大大提高代码的可读性,也可以借此规避一些不必要的Bug。 The naming convention is a very important part of the code specification. A uniform, short, and precise naming convention can greatly improve the readability of the code and avoid unnecessary bugs.
### 2.1 包命名 ### 2.1 Package Naming
- 包名必须和目录名一致,尽量采取有意义、简短的包名,不要和标准库冲突。 - The package name must be consistent with the directory name, try to use a meaningful and short package name, and do not conflict with the standard library.
- 包名全部小写,没有大写或下划线,使用多级目录来划分层级。 - Package names are all lowercase, without uppercase or underscores, and use multi-level directories to divide the hierarchy.
- 项目名可以通过中划线来连接多个单词。 - Item names can connect multiple words with dashes.
- 包名以及包所在的目录名,不要使用复数,例如,是`net/url`,而不是`net/urls` - Do not use plurals for the package name and the directory name where the package is located, for example, `net/url` instead of `net/urls`.
- 不要用 commonutilshared 或者 lib 这类宽泛的、无意义的包名。 - Don't use broad, meaningless package names like common, util, shared or lib.
- 包名要简单明了,例如 nettimelog - The package name should be simple and clear, such as net, time, log.
### 2.2 函数命名 ### 2.2 Function Naming
- 函数名采用驼峰式,首字母根据访问控制决定使用大写或小写,例如:`MixedCaps`或者`mixedCaps` - The function name is in camel case, and the first letter is uppercase or lowercase according to the access control decision,For example: `MixedCaps` or `mixedCaps`.
- 代码生成工具自动生成的代码(如`xxxx.pb.go`)和为了对相关测试用例进行分组,而采用的下划线(如`TestMyFunction_WhatIsBeingTested`)排除此规则。 - Code automatically generated by code generation tools (such as `xxxx.pb.go`) and underscores used to group related test cases (such as `TestMyFunction_WhatIsBeingTested`) exclude this rule.
### 2.3 文件命名 ### 2.3 File Naming
- 文件名要简短有意义。 - Keep the filename short and meaningful.
- 文件名应小写,并使用下划线分割单词。 - Filenames should be lowercase and use underscores to separate words.
### 2.4 结构体命名 ### 2.4 Structure Naming
- 采用驼峰命名方式,首字母根据访问控制决定使用大写或小写,例如`MixedCaps`或者`mixedCaps` - The camel case is adopted, and the first letter is uppercase or lowercase according to the access control, such as `MixedCaps` or `mixedCaps`.
- 结构体名不应该是动词,应该是名词,比如 `Node``NodeSpec` - Struct names should not be verbs, but should be nouns, such as `Node`, `NodeSpec`.
- 避免使用Data、Info这类无意义的结构体名。 - Avoid using meaningless structure names such as Data and Info.
- 结构体的声明和初始化应采用多行,例如: - The declaration and initialization of the structure should take multiple lines, for example:
```go ```go
// User 多行声明 // User multi-line declaration
type User struct { type User struct {
Name string name string
Email string Email string
} }
// 多行初始化 // multi-line initialization
u := User{ u := User{
UserName: "belm", UserName: "belm",
Email: "nosbelm@qq.com", Email: "nosbelm@qq.com",
} }
``` ```
### 2.5 接口命名 ### 2.5 Interface Naming
- 接口命名的规则,基本和结构体命名规则保持一致: - The interface naming rules are basically consistent with the structure naming rules:
- 单个函数的接口名以 “er"”作为后缀(例如ReaderWriter),有时候可能导致蹩脚的英文,但是没关系。 - Interface names of individual functions suffixed with "er"" (e.g. Reader, Writer) can sometimes lead to broken English, but that's okay.
- 两个函数的接口名以两个函数名命名,例如ReadWriter - The interface name of the two functions is named after the two function names, eg ReadWriter.
- 三个以上函数的接口名,类似于结构体名。 - An interface name for more than three functions, similar to a structure name.
例如: For example:
``` ```
// Seeking to an offset before the start of the file is an error. // Seeking to an offset before the start of the file is an error.
// Seeking to any positive offset is legal, but the behavior of subsequent // Seeking to any positive offset is legal, but the behavior of subsequent
// I/O operations on the underlying object is implementation-dependent. // I/O operations on the underlying object are implementation-dependent.
type Seeker interface { type Seeker interface {
Seek(offset int64, whence int) (int64, error) Seek(offset int64, whence int) (int64, error)
} }
// ReadWriter is the interface that groups the basic Read and Write methods. // ReadWriter is the interface that groups the basic Read and Write methods.
type ReadWriter interface { type ReadWriter interface {
Reader reader
Writer Writer
} }
``` ```
### 2.6 变量命名 ### 2.6 Variable Naming
- 变量名必须遵循驼峰式,首字母根据访问控制决定使用大写或小写。 - Variable names must follow camel case, and the initial letter is uppercase or lowercase according to the access control decision.
- 在相对简单(对象数量少、针对性强)的环境中,可以将一些名称由完整单词简写为单个字母,例如: - In relatively simple (few objects, highly targeted) environments, some names can be abbreviated from full words to single letters, for example:
- user 可以简写为 u - user can be abbreviated as u;
- userID 可以简写 uid - userID can be abbreviated as uid.
- 特有名词时,需要遵循以下规则: - When using proper nouns, the following rules need to be followed:
- 如果变量为私有,且特有名词为首个单词,则使用小写,如 apiClient - If the variable is private and the proper noun is the first word, use lowercase, such as apiClient.
- 其他情况都应当使用该名词原有的写法,如 APIClientrepoIDUserID - In other cases, the original wording of the noun should be used, such as APIClient, repoID, UserID.
下面列举了一些常见的特有名词。 Some common nouns are listed below.
``` ```
// A GonicMapper that contains a list of common initialisms taken from golang/lint // A GonicMapper that contains a list of common initialisms taken from golang/lint
@@ -420,7 +416,7 @@ var LintGonicMapper = GonicMapper{
} }
``` ```
- 若变量类型为bool类型,则名称应以Has,Is,Can或Allow开头,例如: - If the variable type is bool, the name should start with Has, Is, Can or Allow, for example:
```go ```go
var has Conflict bool var has Conflict bool
@@ -429,13 +425,13 @@ var canManage bool
var allowGitHook bool var allowGitHook bool
``` ```
- 局部变量应当尽可能短小,比如使用buf指代buffer,使用i指代index - Local variables should be as short as possible, for example, use buf to refer to buffer, and use i to refer to index.
- 代码生成工具自动生成的代码可排除此规则(如`xxx.pb.go`里面的Id) - The code automatically generated by the code generation tool can exclude this rule (such as the Id in `xxx.pb.go`)
### 2.7 常量命名 ### 2.7 Constant naming
- 常量名必须遵循驼峰式,首字母根据访问控制决定使用大写或小写。 - The constant name must follow the camel case, and the initial letter is uppercase or lowercase according to the access control decision.
- 如果是枚举类型的常量,需要先创建相应类型: - If it is a constant of enumeration type, you need to create the corresponding type first:
```go ```go
// Code defines an error code type. // Code defines an error code type.
@@ -450,9 +446,9 @@ const (
) )
``` ```
### 2.8 Error的命名 ### 2.8 Error naming
- Error类型应该写成FooError的形式。 - The Error type should be written in the form of FooError.
```go ```go
type ExitError struct { type ExitError struct {
@@ -460,18 +456,18 @@ type ExitError struct {
} }
``` ```
- Error变量写成ErrFoo的形式。 - The Error variable is written in the form of ErrFoo.
```go ```go
var ErrFormat = errors. New("unknown format") var ErrFormat = errors. New("unknown format")
``` ```
## 3. 注释规范 ## 3. Comment specification
- 每个可导出的名字都要有注释,该注释对导出的变量、函数、结构体、接口等进行简要介绍。 - Each exportable name must have a comment, which briefly introduces the exported variables, functions, structures, interfaces, etc.
- 全部使用单行注释,禁止使用多行注释。 - All single-line comments are used, and multi-line comments are prohibited.
- 和代码的规范一样,单行注释不要过长,禁止超过 120 字符,超过的请使用换行展示,尽量保持格式优雅。 - Same as the code specification, single-line comments should not be too long, and no more than 120 characters are allowed. If it exceeds, please use a new line to display, and try to keep the format elegant.
- 注释必须是完整的句子,以需要注释的内容作为开头,句点作为结尾,`格式为 // 名称 描述.`。例如: - A comment must be a complete sentence, starting with the content to be commented and ending with a period, `the format is // name description.`. For example:
```go ```go
// bad // bad
@@ -487,9 +483,9 @@ func PrintFlags(flags *pflag.FlagSet) {
} }
``` ```
- 所有注释掉的代码在提交code review前都应该被删除,否则应该说明为什么不删除,并给出后续处理建议。 - All commented out code should be deleted before submitting code review, otherwise, it should explain why it is not deleted, and give follow-up processing suggestions.
- 在多段注释之间可以使用空行分隔加以区分,如下所示: - Multiple comments can be separated by blank lines, as follows:
```go ```go
// Package superman implements methods for saving the world. // Package superman implements methods for saving the world.
@@ -499,10 +495,10 @@ func PrintFlags(flags *pflag.FlagSet) {
package superman package superman
``` ```
### 3.1 包注释 ### 3.1 Package Notes
- 每个包都有且仅有一个包级别的注释。 - Each package has one and only one package-level annotation.
- 包注释统一用 // 进行注释,格式为 `// Package 包名 包描述`,例如: - Package comments are uniformly commented with // in the format of `// Package package name package description`, for example:
```go ```go
// Package genericclioptions contains flags which can be added to you command, bound, completed, and produce // Package genericclioptions contains flags which can be added to you command, bound, completed, and produce
@@ -510,15 +506,15 @@ package superman
package genericclioptions package genericclioptions
``` ```
### 3.2 变量/常量注释 ### 3.2 Variable/Constant Comments
- 每个可导出的变量/常量都必须有注释说明,`格式为// 变量名 变量描述`,例如: - Each variable/constant that can be exported must have a comment description, `the format is // variable name variable description`, for example:
```go ```go
// ErrSigningMethod defines invalid signing method error. // ErrSigningMethod defines invalid signing method error.
var ErrSigningMethod = errors. New("Invalid signing method") var ErrSigningMethod = errors. New("Invalid signing method")
``` ```
- 出现大块常量或变量定义时,可在前面注释一个总的说明,然后在每一行常量的前一行或末尾详细注释该常量的定义,例如: - When there is a large block of constant or variable definition, you can comment a general description in front, and then comment the definition of the constant in detail before or at the end of each line of constant, for example:
```go ```go
// Code must start with 1xxxxx. // Code must start with 1xxxxx.
const ( const (
@@ -535,10 +531,10 @@ const (
ErrValidation ErrValidation
) )
``` ```
### 3.3 结构体注释 ### 3.3 Structure Annotation
- 每个需要导出的结构体或者接口都必须有注释说明,格式为 `// 结构体名 结构体描述.` - Each structure or interface that needs to be exported must have a comment description, the format is `// structure name structure description.`.
- 结构体内的可导出成员变量名,如果意义不明确,必须要给出注释,放在成员变量的前一行或同一行的末尾。例如: - The name of the exportable member variable in the structure, if the meaning is not clear, a comment must be given and placed before the member variable or at the end of the same line. For example:
```go ```go
// User represents a user restful resource. It is also used as gorm model. // User represents a user restful resource. It is also used as gorm model.
@@ -554,9 +550,9 @@ type User struct {
} }
``` ```
### 3.4 方法注释 ### 3.4 Method Notes
每个需要导出的函数或者方法都必须有注释,格式为// 函数名 函数描述.,例如: Each function or method that needs to be exported must have a comment, the format is // function name function description., for examplelike:
```go ```go
// BeforeUpdate run before update database record. // BeforeUpdate run before update database record.
@@ -566,20 +562,20 @@ func (p *Policy) BeforeUpdate() (err error) {
} }
``` ```
### 3.5 类型注释 ### 3.5 Type annotations
- 每个需要导出的类型定义和类型别名都必须有注释说明,格式为 `// 类型名 类型描述.`,例如: - Each type definition and type alias that needs to be exported must have a comment description, the format is `// type name type description.`, for example:
```go ```go
// Code defines an error code type. // Code defines an error code type.
type Code int type Code int
``` ```
## 4. 类型 ## 4. Type
### 4.1 字符串 ### 4.1 Strings
- 空字符串判断。 - Empty string judgment.
```go ```go
// bad // bad
@@ -593,7 +589,7 @@ if len(s) == 0 {
} }
``` ```
- `[]byte`/`string`相等比较。 - `[]byte`/`string` equality comparison.
```go ```go
// bad // bad
@@ -611,7 +607,7 @@ bytes.Compare(s1, s2) == 0
bytes. Compare(s1, s2) != 0 bytes. Compare(s1, s2) != 0
``` ```
- 复杂字符串使用raw字符串避免字符转义。 - Complex strings use raw strings to avoid character escaping.
```go ```go
// bad // bad
@@ -621,9 +617,9 @@ regexp.MustCompile("\\.")
regexp.MustCompile(`\.`) regexp.MustCompile(`\.`)
``` ```
### 4.2 切片 ### 4.2 Slicing
- 空slice判断。 - Empty slice judgment.
```go ```go
// bad // bad
@@ -637,9 +633,9 @@ if slice != nil && len(slice) == 0 {
} }
``` ```
上面判断同样适用于map、channel The above judgment also applies to map and channel.
- 声明slice - Declare a slice.
```go ```go
// bad // bad
@@ -650,7 +646,7 @@ s := make([]string, 0)
var s[]string var s[]string
``` ```
- slice复制。 - slice copy.
```go ```go
// bad // bad
@@ -666,7 +662,7 @@ for i := range b1 {
copy(b2, b1) copy(b2, b1)
``` ```
- slice新增。 - slice added.
```go ```go
// bad // bad
@@ -680,16 +676,16 @@ var a, b []int
b = append(b, a...) b = append(b, a...)
``` ```
### 4.3 结构体 ### 4.3 Structure
- struct初始化。 - struct initialization.
struct以多行格式初始化。 The struct is initialized in multi-line format.
```go ```go
type user struct { type user struct {
Id int64 Id int64
Name string name string
} }
u1 := user{100, "Colin"} u1 := user{100, "Colin"}
@@ -700,11 +696,11 @@ u2 := user{
} }
``` ```
## 5. 控制结构 ## 5. Control Structure
### 5.1 if ### 5.1 if
- if 接受初始化语句,约定如下方式建立局部变量。 - if accepts the initialization statement, the convention is to create local variables in the following way.
```go ```go
if err := loadConfig(); err != nil { if err := loadConfig(); err != nil {
@@ -713,7 +709,7 @@ if err := loadConfig(); err != nil {
} }
``` ```
- if 对于bool类型的变量,应直接进行真假判断。 - if For variables of bool type, true and false judgments should be made directly.
```go ```go
var isAllow bool var isAllow bool
@@ -724,7 +720,7 @@ if isAllow {
### 5.2 for ### 5.2 for
- 采用短声明建立局部变量。 - Create local variables using short declarations.
```go ```go
sum := 0 sum := 0
@@ -733,7 +729,7 @@ for i := 0; i < 10; i++ {
} }
``` ```
- 不要在 for 循环里面使用 defer,defer只有在函数退出时才会执行。 - Don't use defer in for loop, defer will only be executed when the function exits.
```go ```go
// bad // bad
@@ -761,7 +757,7 @@ for file := range files {
### 5.3 range ### 5.3 range
- 如果只需要第一项(key),就丢弃第二个。 - If only the first item (key) is needed, discard the second.
```go ```go
for key := range keys { for key := range keys {
@@ -769,7 +765,7 @@ for key := range keys {
} }
``` ```
- 如果只需要第二项,则把第一项置为下划线。 - If only the second item is required, underline the first item.
```go ```go
sum := 0 sum := 0
@@ -780,7 +776,7 @@ for _, value := range array {
### 5.4 switch ### 5.4 switch
- 必须要有default - must have default.
```go ```go
switch os := runtime.GOOS; os { switch os := runtime.GOOS; os {
@@ -794,37 +790,37 @@ switch os := runtime.GOOS; os {
``` ```
### 5.5 goto ### 5.5 goto
- 业务代码禁止使用 goto - Business code prohibits the use of goto.
- 框架或其他底层源码尽量不用。 - Try not to use frameworks or other low-level source code.
## 6. 函数 ## 6. Functions
- 传入变量和返回变量以小写字母开头。 - Incoming variables and return variables start with a lowercase letter.
- 函数参数个数不能超过5个。 - The number of function parameters cannot exceed 5.
- 函数分组与顺序 - Function grouping and ordering
- 函数应按粗略的调用顺序排序。 - Functions should be sorted in rough calling order.
- 同一文件中的函数应按接收者分组。 - Functions in the same file should be grouped by receiver.
- 尽量采用值传递,而非指针传递。 - Try to use value transfer instead of pointer transfer.
- 传入参数是 mapslicechaninterface ,不要传递指针。 - The incoming parameters are map, slice, chan, interface, do not pass pointers.
### 6.1 函数参数 ### 6.1 Function parameters
- 如果函数返回相同类型的两个或三个参数,或者如果从上下文中不清楚结果的含义,使用命名返回,其他情况不建议使用命名返回,例如: - If the function returns two or three arguments of the same type, or if the meaning of the result is not clear from the context, use named returns, otherwise it is not recommended to use named returns, for example:
```go ```go
func coordinate() (x, y float64, err error) { func coordinate() (x, y float64, err error) {
// normal code // normal code
} }
``` ```
- 传入变量和返回变量都以小写字母开头。 - Both incoming and returned variables start with a lowercase letter.
- 尽量用值传递,非指针传递。 - Try to pass by value instead of pointer.
- 参数数量均不能超过5个。 - The number of parameters cannot exceed 5.
- 多返回值最多返回三个,超过三个请使用 struct - Multiple return values can return up to three, and if there are more than three, please use struct.
### 6.2 defer ### 6.2 defer
- 当存在资源创建时,应紧跟defer释放资源(可以大胆使用deferdefer在Go1.14版本中,性能大幅提升,defer的性能损耗即使在性能敏感型的业务中,也可以忽略)。 - When resources are created, resources should be released immediately after defer (defer can be used boldly, the performance of defer is greatly improved in Go1.14 version, and the performance loss of defer can be ignored even in performance-sensitive businesses).
- 先判断是否错误,再defer释放资源,例如: - First judge whether there is an error, and then defer to release resources, for example:
```go ```go
rep, err := http. Get(url) rep, err := http. Get(url)
@@ -835,46 +831,46 @@ if err != nil {
defer resp.Body.Close() defer resp.Body.Close()
``` ```
### 6.3 方法的接收器 ### 6.3 Method Receiver
- 推荐以类名第一个英文首字母的小写作为接收器的命名。 - It is recommended to use the lowercase of the first English letter of the class name as the name of the receiver.
- 接收器的命名在函数超过20行的时候不要用单字符。 - Don't use a single character in the name of the receiver when the function exceeds 20 lines.
- 接收器的命名不能采用me、this、self这类易混淆名称。 - The name of the receiver cannot use confusing names such as me, this, and self.
### 6.4 嵌套 ### 6.4 Nesting
- 嵌套深度不能超过4层。 - The nesting depth cannot exceed 4 levels.
### 6.5 变量命名 ### 6.5 Variable Naming
- 变量声明尽量放在变量第一次使用的前面,遵循就近原则。 - The variable declaration should be placed before the first use of the variable as far as possible, following the principle of proximity.
- 如果魔法数字出现超过两次,则禁止使用,改用一个常量代替,例如: - If the magic number appears more than twice, it is forbidden to use it and use a constant instead, for example:
```go ```go
// PI... // PI...
const Prise = 3.14 const Price = 3.14
func getAppleCost(n float64) float64 { func getAppleCost(n float64) float64 {
return Prise * n return Price * n
} }
func getOrangeCost(n float64) float64 { func getOrangeCost(n float64) float64 {
return Prise * n return Price * n
} }
``` ```
## 7. GOPATH 设置规范 ## 7. GOPATH setting specification
- Go 1.11 之后,弱化了 GOPATH 规则,已有代码(很多库肯定是在1.11之前建立的)肯定符合这个规则,建议保留 GOPATH 规则,便于维护代码。 - After Go 1.11, the GOPATH rule has been weakened. Existing code (many libraries must have been created before 1.11) must conform to this rule. It is recommended to keep the GOPATH rule to facilitate code maintenance.
- 建议只使用一个 GOPATH,不建议使用多个 GOPATH。如果使用多个GOPATH,编译生效的 bin 目录是在第一个 GOPATH 下。 - Only one GOPATH is recommended, multiple GOPATHs are not recommended. If multiple GOPATHs are used, the bin directory where compilation takes effect is under the first GOPATH.
## 8. 依赖管理 ## 8. Dependency Management
- Go 1.11 以上必须使用 Go Modules - Go 1.11 and above must use Go Modules.
- 使用Go Modules作为依赖管理的项目时,不建议提交vendor目录。 - When using Go Modules as a dependency management project, it is not recommended to submit the vendor directory.
- 使用Go Modules作为依赖管理的项目时,必须提交go.sum文件。 - When using Go Modules as a dependency management project, the go.sum file must be submitted.
### 9. 最佳实践 ### 9. Best Practices
- 尽量少用全局变量,而是通过参数传递,使每个函数都是“无状态”的。这样可以减少耦合,也方便分工和单元测试。 - Minimize the use of global variables, but pass parameters, so that each function is "stateless". This reduces coupling and facilitates division of labor and unit testing.
- 在编译时验证接口的符合性,例如: - Verify interface compliance at compile time, for example:
```go ```go
type LogHandler struct { type LogHandler struct {
@@ -883,19 +879,15 @@ type LogHandler struct {
} }
var_http.Handler = LogHandler{} var_http.Handler = LogHandler{}
``` ```
- 服务器处理请求时,应该创建一个context,保存该请求的相关信息(如requestID),并在函数调用链中传递。 - When the server processes a request, it should create a context, save the relevant information of the request (such as requestID), and pass it in the function call chain.
### 9.1 性能 ### 9.1 Performance
- string 表示的是不可变的字符串变量,对 string 的修改是比较重的操作,基本上都需要重新申请内存。所以,如果没有特殊需要,需要修改时多使用 []byte。 - string represents an immutable string variable, modifying string is a relatively heavy operation, and basically needs to re-apply for memory. Therefore, if there is no special need, use []byte more when you need to modify.
- 优先使用 strconv 而不是 fmt - Prefer strconv over fmt.
### 9.2 注意事项 ### 9.2 Precautions
- append 要小心自动分配内存,append 返回的可能是新分配的地址。 - append Be careful about automatically allocating memory, append may return a newly allocated address.
- 如果要直接修改 map 的 value 值,则 value 只能是指针,否则要覆盖原来的值。 - If you want to directly modify the value of the map, the value can only be a pointer, otherwise the original value must be overwritten.
- map 在并发中需要加锁。 - map needs to be locked during concurrency.
- 编译过程无法检查 interface{} 的转换,只能在运行时检查,小心引起 panic。 - The conversion of interface{} cannot be checked during compilation, it can only be checked at runtime, be careful to cause panic.
## 总结
这里向你介绍了九类常用的编码规范。但今天的最后,我要在这里提醒你一句:规范是人定的,你也可以根据需要,制定符合你项目的规范,但同时我也建议你采纳这些业界沉淀下来的规范,并通过工具来确保规范的执行。
+5 -5
View File
@@ -8,7 +8,7 @@ require (
github.com/bwmarrin/snowflake v0.3.0 // indirect github.com/bwmarrin/snowflake v0.3.0 // indirect
github.com/dtm-labs/rockscache v0.1.1 github.com/dtm-labs/rockscache v0.1.1
github.com/gin-gonic/gin v1.9.1 github.com/gin-gonic/gin v1.9.1
github.com/go-playground/validator/v10 v10.14.1 github.com/go-playground/validator/v10 v10.15.0
github.com/gogo/protobuf v1.3.2 github.com/gogo/protobuf v1.3.2
github.com/golang-jwt/jwt/v4 v4.5.0 github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang/protobuf v1.5.3 github.com/golang/protobuf v1.5.3
@@ -26,7 +26,7 @@ require (
github.com/stretchr/testify v1.8.4 github.com/stretchr/testify v1.8.4
go.mongodb.org/mongo-driver v1.12.1 go.mongodb.org/mongo-driver v1.12.1
golang.org/x/image v0.9.0 // indirect golang.org/x/image v0.9.0 // indirect
google.golang.org/api v0.134.0 google.golang.org/api v0.135.0
google.golang.org/grpc v1.57.0 google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0 google.golang.org/protobuf v1.31.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
@@ -37,7 +37,7 @@ require (
require github.com/google/uuid v1.3.0 require github.com/google/uuid v1.3.0
require ( require (
github.com/OpenIMSDK/protocol v0.0.4 github.com/OpenIMSDK/protocol v0.0.6
github.com/OpenIMSDK/tools v0.0.13 github.com/OpenIMSDK/tools v0.0.13
github.com/aliyun/aliyun-oss-go-sdk v2.2.8+incompatible github.com/aliyun/aliyun-oss-go-sdk v2.2.8+incompatible
github.com/go-redis/redis v6.15.9+incompatible github.com/go-redis/redis v6.15.9+incompatible
@@ -125,8 +125,9 @@ require (
golang.org/x/time v0.3.0 // indirect golang.org/x/time v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230720185612-659f7aaaa771 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230726155614-23370e0ffb3e // indirect
) )
require ( require (
@@ -138,6 +139,5 @@ require (
github.com/ugorji/go/codec v1.2.11 // indirect github.com/ugorji/go/codec v1.2.11 // indirect
go.uber.org/zap v1.24.0 // indirect go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.11.0 // indirect golang.org/x/crypto v0.11.0 // indirect
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect
) )
+8 -8
View File
@@ -17,8 +17,8 @@ cloud.google.com/go/storage v1.30.1/go.mod h1:NfxhC0UJE1aXSx7CIIbCf7y9HKT7Biccwk
firebase.google.com/go v3.13.0+incompatible h1:3TdYC3DDi6aHn20qoRkxwGqNgdjtblwVAyRLQwGn/+4= firebase.google.com/go v3.13.0+incompatible h1:3TdYC3DDi6aHn20qoRkxwGqNgdjtblwVAyRLQwGn/+4=
firebase.google.com/go v3.13.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIwjt8toICdV5Wh9ptHs= firebase.google.com/go v3.13.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIwjt8toICdV5Wh9ptHs=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/OpenIMSDK/protocol v0.0.4 h1:zEEAi677nog+k4u3e5h36nvYeb1XAwcKQ3Uc2tzxHYs= github.com/OpenIMSDK/protocol v0.0.6 h1:KjaItOEww7vjrhwyxHnVzhw80pnjcNukpskadqW6gnA=
github.com/OpenIMSDK/protocol v0.0.4/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= github.com/OpenIMSDK/protocol v0.0.6/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y=
github.com/OpenIMSDK/tools v0.0.13 h1:rcw4HS8S2DPZR9UOBxD8/ol9UBMzXBypzOVEytDRIMo= github.com/OpenIMSDK/tools v0.0.13 h1:rcw4HS8S2DPZR9UOBxD8/ol9UBMzXBypzOVEytDRIMo=
github.com/OpenIMSDK/tools v0.0.13/go.mod h1:eg+q4A34Qmu73xkY0mt37FHGMCMfC6CtmOnm0kFEGFI= github.com/OpenIMSDK/tools v0.0.13/go.mod h1:eg+q4A34Qmu73xkY0mt37FHGMCMfC6CtmOnm0kFEGFI=
github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM= github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM=
@@ -100,8 +100,8 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.14.1 h1:9c50NUPC30zyuKprjL3vNZ0m5oG+jU0zvx4AqHGnv4k= github.com/go-playground/validator/v10 v10.15.0 h1:nDU5XeOKtB3GEa+uB7GNYwhVKsgjAR7VgKoNB6ryXfw=
github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-playground/validator/v10 v10.15.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
@@ -463,8 +463,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
google.golang.org/api v0.134.0 h1:ktL4Goua+UBgoP1eL1/60LwZJqa1sIzkLmvoR3hR6Gw= google.golang.org/api v0.135.0 h1:6Vgfj6uPMXcyy66waYWBwmkeNB+9GmUlJDOzkukPQYQ=
google.golang.org/api v0.134.0/go.mod h1:sjRL3UnjTx5UqNQS9EWr9N8p7xbHpy1k0XGRLCf3Spk= google.golang.org/api v0.135.0/go.mod h1:Bp77uRFgwsSKI0BWH573F5Q6wSlznwI2NFayLOp/7mQ=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
@@ -477,8 +477,8 @@ google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 h1:Au6te5hbKUV8pIY
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130/go.mod h1:O9kGHb51iE/nOGvQaDUuadVYqovW56s5emA88lQnj6Y= google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130/go.mod h1:O9kGHb51iE/nOGvQaDUuadVYqovW56s5emA88lQnj6Y=
google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130 h1:XVeBY8d/FaK4848myy41HBqnDwvxeV3zMZhwN1TvAMU= google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130 h1:XVeBY8d/FaK4848myy41HBqnDwvxeV3zMZhwN1TvAMU=
google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130/go.mod h1:mPBs5jNgx2GuQGvFwUvVKqtn6HsUw9nP64BedgvqEsQ= google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130/go.mod h1:mPBs5jNgx2GuQGvFwUvVKqtn6HsUw9nP64BedgvqEsQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230720185612-659f7aaaa771 h1:Z8qdAF9GFsmcUuWQ5KVYIpP3PCKydn/YKORnghIalu4= google.golang.org/genproto/googleapis/rpc v0.0.0-20230726155614-23370e0ffb3e h1:S83+ibolgyZ0bqz7KEsUOPErxcv4VzlszxY+31OfB/E=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230720185612-659f7aaaa771/go.mod h1:TUfxEVdsvPg18p6AslUXFoLdpED4oBnGwyqk3dV1XzM= google.golang.org/genproto/googleapis/rpc v0.0.0-20230726155614-23370e0ffb3e/go.mod h1:TUfxEVdsvPg18p6AslUXFoLdpED4oBnGwyqk3dV1XzM=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
+8
View File
@@ -0,0 +1,8 @@
go 1.20
use (
.
./tools/changelog
./tools/infra
./tools/ncpu
)
+9
View File
@@ -0,0 +1,9 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
+2 -2
View File
@@ -16,10 +16,10 @@ package controller
import ( import (
"context" "context"
unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" "time"
"github.com/OpenIMSDK/protocol/user"
unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/protocol/user"
"github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/errs"
"github.com/OpenIMSDK/tools/tx" "github.com/OpenIMSDK/tools/tx"
+3 -51
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,21 +19,8 @@ SCRIPTS_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
#Include shell font styles and some basic information #Include shell font styles and some basic information
source $SCRIPTS_ROOT/style_info.sh
source $SCRIPTS_ROOT/path_info.sh source $SCRIPTS_ROOT/path_info.sh
source $SCRIPTS_ROOT/function.sh source $SCRIPTS_ROOT/lib/init.sh
echo -e "${YELLOW_PREFIX}=======>SCRIPTS_ROOT=$SCRIPTS_ROOT${COLOR_SUFFIX}"
echo -e "${YELLOW_PREFIX}=======>OPENIM_ROOT=$OPENIM_ROOT${COLOR_SUFFIX}"
echo -e "${YELLOW_PREFIX}=======>pwd=$PWD${COLOR_SUFFIX}"
echo -e ""
echo -e "${BACKGROUND_BLUE}===============> Building all using make build binary files ${COLOR_SUFFIX}"
echo -e ""
echo -e "${BOLD_PREFIX}____________________________________________________________ ${COLOR_SUFFIX}"
bin_dir="$BIN_DIR" bin_dir="$BIN_DIR"
logs_dir="$OPENIM_ROOT/logs" logs_dir="$OPENIM_ROOT/logs"
@@ -67,43 +55,7 @@ if [ $? -ne 0 ]; then
exit 1 exit 1
fi fi
# Get the current operating system and architecture gen_os_arch
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
# Select the repository home directory based on the operating system and architecture
if [[ "$OS" == "darwin" ]]; then
if [[ "$ARCH" == "x86_64" ]]; then
REPO_DIR="darwin/amd64"
else
REPO_DIR="darwin/386"
fi
elif [[ "$OS" == "linux" ]]; then
if [[ "$ARCH" == "x86_64" ]]; then
REPO_DIR="linux/amd64"
elif [[ "$ARCH" == "arm64" ]]; then
REPO_DIR="linux/arm64"
elif [[ "$ARCH" == "mips64" ]]; then
REPO_DIR="linux/mips64"
elif [[ "$ARCH" == "mips64le" ]]; then
REPO_DIR="linux/mips64le"
elif [[ "$ARCH" == "ppc64le" ]]; then
REPO_DIR="linux/ppc64le"
elif [[ "$ARCH" == "s390x" ]]; then
REPO_DIR="linux/s390x"
else
REPO_DIR="linux/386"
fi
elif [[ "$OS" == "windows" ]]; then
if [[ "$ARCH" == "x86_64" ]]; then
REPO_DIR="windows/amd64"
else
REPO_DIR="windows/386"
fi
else
echo -e "${RED_PREFIX}Unsupported OS: $OS${COLOR_SUFFIX}"
exit 1
fi
# Determine if all scripts were successfully built # Determine if all scripts were successfully built
BUILD_SUCCESS=true BUILD_SUCCESS=true
+2 -1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,7 +19,7 @@
# Common utilities, variables and checks for all build scripts. # Common utilities, variables and checks for all build scripts.
set -o errexit set -o errexit
set -o nounset set +o nounset
set -o pipefail set -o pipefail
# Unset CDPATH, having it set messes up with script import paths # Unset CDPATH, having it set messes up with script import paths
+1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
+1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
+1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
+1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
+91
View File
@@ -0,0 +1,91 @@
#!/usr/bin/env bash
# Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
# Use of this source code is governed by a MIT style
# license that can be found in the LICENSE file.
# OPENIM 项目源码根目录
IAM_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
# 生成文件存放目录
LOCAL_OUTPUT_ROOT="${IAM_ROOT}/${OUT_DIR:-_output}"
# 设置统一的密码,方便记忆
readonly PASSWORD=${PASSWORD:-'iam59!z$'}
# Linux系统 going 用户
readonly LINUX_USERNAME=${LINUX_USERNAME:-going}
# Linux root & going 用户密码
readonly LINUX_PASSWORD=${LINUX_PASSWORD:-${PASSWORD}}
# 设置安装目录
readonly INSTALL_DIR=${INSTALL_DIR:-/tmp/installation}
mkdir -p ${INSTALL_DIR}
readonly ENV_FILE=${IAM_ROOT}/scripts/install/environment.sh
# MariaDB 配置信息
readonly MARIADB_ADMIN_USERNAME=${MARIADB_ADMIN_USERNAME:-root} # MariaDB root 用户
readonly MARIADB_ADMIN_PASSWORD=${MARIADB_ADMIN_PASSWORD:-${PASSWORD}} # MariaDB root 用户密码
readonly MARIADB_HOST=${MARIADB_HOST:-127.0.0.1:3306} # MariaDB 主机地址
readonly MARIADB_DATABASE=${MARIADB_DATABASE:-openim} # MariaDB openim 应用使用的数据库名
readonly MARIADB_USERNAME=${MARIADB_USERNAME:-openim} # openim 数据库用户名
readonly MARIADB_PASSWORD=${MARIADB_PASSWORD:-${PASSWORD}} # openim 数据库密码
# Redis 配置信息
readonly REDIS_HOST=${REDIS_HOST:-127.0.0.1} # Redis 主机地址
readonly REDIS_PORT=${REDIS_PORT:-6379} # Redis 监听端口
readonly REDIS_USERNAME=${REDIS_USERNAME:-''} # Redis 用户名
readonly REDIS_PASSWORD=${REDIS_PASSWORD:-${PASSWORD}} # Redis 密码
# MongoDB 配置
readonly MONGO_ADMIN_USERNAME=${MONGO_ADMIN_USERNAME:-root} # MongoDB root 用户
readonly MONGO_ADMIN_PASSWORD=${MONGO_ADMIN_PASSWORD:-${PASSWORD}} # MongoDB root 用户密码
readonly MONGO_HOST=${MONGO_HOST:-127.0.0.1} # MongoDB 地址
readonly MONGO_PORT=${MONGO_PORT:-27017} # MongoDB 端口
readonly MONGO_USERNAME=${MONGO_USERNAME:-openim} # MongoDB 用户名
readonly MONGO_PASSWORD=${MONGO_PASSWORD:-${PASSWORD}} # MongoDB 密码
# openim 配置
readonly IAM_DATA_DIR=${IAM_DATA_DIR:-/data/openim} # openim 各组件数据目录
readonly IAM_INSTALL_DIR=${IAM_INSTALL_DIR:-/opt/openim} # openim 安装文件存放目录
readonly IAM_CONFIG_DIR=${IAM_CONFIG_DIR:-/etc/openim} # openim 配置文件存放目录
readonly IAM_LOG_DIR=${IAM_LOG_DIR:-/var/log/openim} # openim 日志文件存放目录
readonly CA_FILE=${CA_FILE:-${IAM_CONFIG_DIR}/cert/ca.pem} # CA
# openim-apiserver 配置
readonly IAM_APISERVER_HOST=${IAM_APISERVER_HOST:-127.0.0.1} # openim-apiserver 部署机器 IP 地址
readonly IAM_APISERVER_GRPC_BIND_ADDRESS=${IAM_APISERVER_GRPC_BIND_ADDRESS:-0.0.0.0}
readonly IAM_APISERVER_GRPC_BIND_PORT=${IAM_APISERVER_GRPC_BIND_PORT:-8081}
readonly IAM_APISERVER_INSECURE_BIND_ADDRESS=${IAM_APISERVER_INSECURE_BIND_ADDRESS:-127.0.0.1}
readonly IAM_APISERVER_INSECURE_BIND_PORT=${IAM_APISERVER_INSECURE_BIND_PORT:-8080}
readonly IAM_APISERVER_SECURE_BIND_ADDRESS=${IAM_APISERVER_SECURE_BIND_ADDRESS:-0.0.0.0}
readonly IAM_APISERVER_SECURE_BIND_PORT=${IAM_APISERVER_SECURE_BIND_PORT:-8443}
readonly IAM_APISERVER_SECURE_TLS_CERT_KEY_CERT_FILE=${IAM_APISERVER_SECURE_TLS_CERT_KEY_CERT_FILE:-${IAM_CONFIG_DIR}/cert/openim-apiserver.pem}
readonly IAM_APISERVER_SECURE_TLS_CERT_KEY_PRIVATE_KEY_FILE=${IAM_APISERVER_SECURE_TLS_CERT_KEY_PRIVATE_KEY_FILE:-${IAM_CONFIG_DIR}/cert/openim-apiserver-key.pem}
# openim-authz-server 配置
readonly IAM_AUTHZ_SERVER_HOST=${IAM_AUTHZ_SERVER_HOST:-127.0.0.1} # openim-authz-server 部署机器 IP 地址
readonly IAM_AUTHZ_SERVER_INSECURE_BIND_ADDRESS=${IAM_AUTHZ_SERVER_INSECURE_BIND_ADDRESS:-127.0.0.1}
readonly IAM_AUTHZ_SERVER_INSECURE_BIND_PORT=${IAM_AUTHZ_SERVER_INSECURE_BIND_PORT:-9090}
readonly IAM_AUTHZ_SERVER_SECURE_BIND_ADDRESS=${IAM_AUTHZ_SERVER_SECURE_BIND_ADDRESS:-0.0.0.0}
readonly IAM_AUTHZ_SERVER_SECURE_BIND_PORT=${IAM_AUTHZ_SERVER_SECURE_BIND_PORT:-9443}
readonly IAM_AUTHZ_SERVER_SECURE_TLS_CERT_KEY_CERT_FILE=${IAM_AUTHZ_SERVER_SECURE_TLS_CERT_KEY_CERT_FILE:-${IAM_CONFIG_DIR}/cert/openim-authz-server.pem}
readonly IAM_AUTHZ_SERVER_SECURE_TLS_CERT_KEY_PRIVATE_KEY_FILE=${IAM_AUTHZ_SERVER_SECURE_TLS_CERT_KEY_PRIVATE_KEY_FILE:-${IAM_CONFIG_DIR}/cert/openim-authz-server-key.pem}
readonly IAM_AUTHZ_SERVER_CLIENT_CA_FILE=${IAM_AUTHZ_SERVER_CLIENT_CA_FILE:-${CA_FILE}}
readonly IAM_AUTHZ_SERVER_RPCSERVER=${IAM_AUTHZ_SERVER_RPCSERVER:-${IAM_APISERVER_HOST}:${IAM_APISERVER_GRPC_BIND_PORT}}
# openim-pump 配置
readonly IAM_PUMP_HOST=${IAM_PUMP_HOST:-127.0.0.1} # openim-pump 部署机器 IP 地址
readonly IAM_PUMP_COLLECTION_NAME=${IAM_PUMP_COLLECTION_NAME:-iam_analytics}
readonly IAM_PUMP_MONGO_URL=${IAM_PUMP_MONGO_URL:-mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}:${MONGO_PORT}/${IAM_PUMP_COLLECTION_NAME}?authSource=${IAM_PUMP_COLLECTION_NAME}}
# openim-watcher配置
readonly IAM_WATCHER_HOST=${IAM_WATCHER_HOST:-127.0.0.1} # openim-watcher 部署机器 IP 地址
# iamctl 配置
readonly CONFIG_USER_USERNAME=${CONFIG_USER_USERNAME:-admin}
readonly CONFIG_USER_PASSWORD=${CONFIG_USER_PASSWORD:-Admin@2021}
readonly CONFIG_USER_CLIENT_CERTIFICATE=${CONFIG_USER_CLIENT_CERTIFICATE:-${HOME}/.openim/cert/admin.pem}
readonly CONFIG_USER_CLIENT_KEY=${CONFIG_USER_CLIENT_KEY:-${HOME}/.openim/cert/admin-key.pem}
readonly CONFIG_SERVER_ADDRESS=${CONFIG_SERVER_ADDRESS:-${IAM_APISERVER_HOST}:${IAM_APISERVER_SECURE_BIND_PORT}}
readonly CONFIG_SERVER_CERTIFICATE_AUTHORITY=${CONFIG_SERVER_CERTIFICATE_AUTHORITY:-${CA_FILE}}
+1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
+42
View File
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
# Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
# Use of this source code is governed by a MIT style
# license that can be found in the LICENSE file.
# 本脚本功能:根据 scripts/environment.sh 配置,生成 OPENIM 组件 YAML 配置文件。
# 示例:genconfig.sh scripts/environment.sh configs/openim-apiserver.yaml
env_file="$1"
template_file="$2"
OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${OPENIM_ROOT}/scripts/lib/init.sh"
if [ $# -ne 2 ];then
openim::log::error "Usage: genconfig.sh scripts/environment.sh configs/openim-apiserver.yaml"
exit 1
fi
source "${env_file}"
declare -A envs
set +u
for env in $(sed -n 's/^[^#].*${\(.*\)}.*/\1/p' ${template_file})
do
if [ -z "$(eval echo \$${env})" ];then
openim::log::error "environment variable '${env}' not set"
missing=true
fi
done
if [ "${missing}" ];then
openim::log::error 'You may run `source scripts/environment.sh` to set these environment'
exit 1
fi
eval "cat << EOF
$(cat ${template_file})
EOF"
+1 -1
View File
@@ -26,7 +26,7 @@
LC_ALL=C LC_ALL=C
local_branch="$(git rev-parse --abbrev-ref HEAD)" local_branch="$(git rev-parse --abbrev-ref HEAD)"
valid_branch_regex="^(main|master|develop|release(-[a-zA-Z0-9._-]+)?)$|(feature|feat|openim|hotfix|test|bug|bot|ci|cicd|style|)\/[a-z0-9._-]+$|^HEAD$" valid_branch_regex="^(main|master|develop|release(-[a-zA-Z0-9._-]+)?)$|(feature|feat|openim|hotfix|test|bug|bot|refactor|revert|ci|cicd|style|)\/[a-z0-9._-]+$|^HEAD$"
YELLOW="\e[93m" YELLOW="\e[93m"
GREEN="\e[32m" GREEN="\e[32m"
+165
View File
@@ -0,0 +1,165 @@
# A set of helpers for starting/running chat for tests
CHAT_VERSION=${CHAT_VERSION:-1.1.0}
CHAT_HOST=${CHAT_HOST:-127.0.0.1}
CHAT_PORT=${CHAT_PORT:-2379}
# This is intentionally not called CHAT_LOG_LEVEL:
# chat checks that and compains when it is set in addition
# to the command line argument, even when both have the same value.
CHAT_LOGLEVEL=${CHAT_LOGLEVEL:-warn}
export OPENIM_INTEGRATION_CHAT_URL="http://${CHAT_HOST}:${CHAT_PORT}"
openim::chat::validate() {
# validate if in path
command -v chat >/dev/null || {
openim::log::usage "chat must be in your PATH"
openim::log::info "You can use 'hack/install-chat.sh' to install a copy in third_party/."
exit 1
}
# validate chat port is free
local port_check_command
if command -v ss &> /dev/null && ss -Version | grep 'iproute2' &> /dev/null; then
port_check_command="ss"
elif command -v netstat &>/dev/null; then
port_check_command="netstat"
else
openim::log::usage "unable to identify if chat is bound to port ${CHAT_PORT}. unable to find ss or netstat utilities."
exit 1
fi
if ${port_check_command} -nat | grep "LISTEN" | grep "[\.:]${CHAT_PORT:?}" >/dev/null 2>&1; then
openim::log::usage "unable to start chat as port ${CHAT_PORT} is in use. please stop the process listening on this port and retry."
openim::log::usage "$(${port_check_command} -nat | grep "LISTEN" | grep "[\.:]${CHAT_PORT:?}")"
exit 1
fi
# need set the env of "CHAT_UNSUPPORTED_ARCH" on unstable arch.
arch=$(uname -m)
if [[ $arch =~ arm* ]]; then
export CHAT_UNSUPPORTED_ARCH=arm
fi
# validate installed version is at least equal to minimum
version=$(chat --version | grep Version | head -n 1 | cut -d " " -f 3)
if [[ $(openim::chat::version "${CHAT_VERSION}") -gt $(openim::chat::version "${version}") ]]; then
export PATH=${OPENIM_ROOT}/third_party/chat:${PATH}
hash chat
echo "${PATH}"
version=$(chat --version | grep Version | head -n 1 | cut -d " " -f 3)
if [[ $(openim::chat::version "${CHAT_VERSION}") -gt $(openim::chat::version "${version}") ]]; then
openim::log::usage "chat version ${CHAT_VERSION} or greater required."
openim::log::info "You can use 'hack/install-chat.sh' to install a copy in third_party/."
exit 1
fi
fi
}
openim::chat::version() {
printf '%s\n' "${@}" | awk -F . '{ printf("%d%03d%03d\n", $1, $2, $3) }'
}
openim::chat::start() {
# validate before running
openim::chat::validate
# Start chat
CHAT_DIR=${CHAT_DIR:-$(mktemp -d 2>/dev/null || mktemp -d -t test-chat.XXXXXX)}
if [[ -d "${ARTIFACTS:-}" ]]; then
CHAT_LOGFILE="${ARTIFACTS}/chat.$(uname -n).$(id -un).log.DEBUG.$(date +%Y%m%d-%H%M%S).$$"
else
CHAT_LOGFILE=${CHAT_LOGFILE:-"/dev/null"}
fi
openim::log::info "chat --advertise-client-urls ${OPENIM_INTEGRATION_CHAT_URL} --data-dir ${CHAT_DIR} --listen-client-urls http://${CHAT_HOST}:${CHAT_PORT} --log-level=${CHAT_LOGLEVEL} 2> \"${CHAT_LOGFILE}\" >/dev/null"
chat --advertise-client-urls "${OPENIM_INTEGRATION_CHAT_URL}" --data-dir "${CHAT_DIR}" --listen-client-urls "${OPENIM_INTEGRATION_CHAT_URL}" --log-level="${CHAT_LOGLEVEL}" 2> "${CHAT_LOGFILE}" >/dev/null &
CHAT_PID=$!
echo "Waiting for chat to come up."
openim::util::wait_for_url "${OPENIM_INTEGRATION_CHAT_URL}/health" "chat: " 0.25 80
curl -fs -X POST "${OPENIM_INTEGRATION_CHAT_URL}/v3/kv/put" -d '{"key": "X3Rlc3Q=", "value": ""}'
}
openim::chat::start_scraping() {
if [[ -d "${ARTIFACTS:-}" ]]; then
CHAT_SCRAPE_DIR="${ARTIFACTS}/chat-scrapes"
else
CHAT_SCRAPE_DIR=$(mktemp -d -t test.XXXXXX)/chat-scrapes
fi
openim::log::info "Periodically scraping chat to ${CHAT_SCRAPE_DIR} ."
mkdir -p "${CHAT_SCRAPE_DIR}"
(
while sleep 30; do
openim::chat::scrape
done
) &
CHAT_SCRAPE_PID=$!
}
openim::chat::scrape() {
curl -s -S "${OPENIM_INTEGRATION_CHAT_URL}/metrics" > "${CHAT_SCRAPE_DIR}/next" && mv "${CHAT_SCRAPE_DIR}/next" "${CHAT_SCRAPE_DIR}/$(date +%s).scrape"
}
openim::chat::stop() {
if [[ -n "${CHAT_SCRAPE_PID:-}" ]] && [[ -n "${CHAT_SCRAPE_DIR:-}" ]] ; then
kill "${CHAT_SCRAPE_PID}" &>/dev/null || :
wait "${CHAT_SCRAPE_PID}" &>/dev/null || :
openim::chat::scrape || :
(
# shellcheck disable=SC2015
cd "${CHAT_SCRAPE_DIR}"/.. && \
tar czf chat-scrapes.tgz chat-scrapes && \
rm -rf chat-scrapes || :
)
fi
if [[ -n "${CHAT_PID-}" ]]; then
kill "${CHAT_PID}" &>/dev/null || :
wait "${CHAT_PID}" &>/dev/null || :
fi
}
openim::chat::clean_chat_dir() {
if [[ -n "${CHAT_DIR-}" ]]; then
rm -rf "${CHAT_DIR}"
fi
}
openim::chat::cleanup() {
openim::chat::stop
openim::chat::clean_chat_dir
}
openim::chat::install() {
(
local os
local arch
os=$(openim::util::host_os)
arch=$(openim::util::host_arch)
cd "${OPENIM_ROOT}/third_party" || return 1
if [[ $(readlink chat) == chat-v${CHAT_VERSION}-${os}-* ]]; then
openim::log::info "chat v${CHAT_VERSION} already installed. To use:"
openim::log::info "export PATH=\"$(pwd)/chat:\${PATH}\""
return #already installed
fi
if [[ ${os} == "darwin" ]]; then
download_file="chat-v${CHAT_VERSION}-${os}-${arch}.zip"
url="https://github.com/chat-io/chat/releases/download/v${CHAT_VERSION}/${download_file}"
openim::util::download_file "${url}" "${download_file}"
unzip -o "${download_file}"
ln -fns "chat-v${CHAT_VERSION}-${os}-${arch}" chat
rm "${download_file}"
elif [[ ${os} == "linux" ]]; then
url="https://github.com/coreos/chat/releases/download/v${CHAT_VERSION}/chat-v${CHAT_VERSION}-${os}-${arch}.tar.gz"
download_file="chat-v${CHAT_VERSION}-${os}-${arch}.tar.gz"
openim::util::download_file "${url}" "${download_file}"
tar xzf "${download_file}"
ln -fns "chat-v${CHAT_VERSION}-${os}-${arch}" chat
rm "${download_file}"
else
openim::log::info "${os} is NOT supported."
fi
openim::log::info "chat v${CHAT_VERSION} installed. To use:"
openim::log::info "export PATH=\"$(pwd)/chat:\${PATH}\""
)
}
+39
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
@@ -31,6 +32,44 @@ COLOR_BGREEN='\033[42m';COLOR_BYELLOW='\033[43m';
COLOR_BBLUE='\033[44m';COLOR_BMAGENTA='\033[45m'; COLOR_BBLUE='\033[44m';COLOR_BMAGENTA='\033[45m';
COLOR_BCYAN='\033[46m';COLOR_BWHITE='\033[47m'; COLOR_BCYAN='\033[46m';COLOR_BWHITE='\033[47m';
# Color definitions
COLOR_SUFFIX="\033[0m" # End all colors and special effects
BLACK_PREFIX="\033[30m" # Black prefix
RED_PREFIX="\033[31m" # Red prefix
GREEN_PREFIX="\033[32m" # Green prefix
YELLOW_PREFIX="\033[33m" # Yellow prefix
BLUE_PREFIX="\033[34m" # Blue prefix
SKY_BLUE_PREFIX="\033[36m" # Sky blue prefix
WHITE_PREFIX="\033[37m" # White prefix
BOLD_PREFIX="\033[1m" # Bold prefix
UNDERLINE_PREFIX="\033[4m" # Underline prefix
ITALIC_PREFIX="\033[3m" # Italic prefix
BRIGHT_GREEN_PREFIX='\033[1;32m' # Bright green prefix
CYAN_PREFIX="\033[0;36m" # Cyan prefix
# --- helper functions for logs ---
info()
{
echo -e "[${GREEN_PREFIX}INFO${COLOR_SUFFIX}] " "$@"
}
warn()
{
echo -e "[${YELLOW_PREFIX}WARN${COLOR_SUFFIX}] " "$@" >&2
}
fatal()
{
echo -e "[${RED_PREFIX}ERROR${COLOR_SUFFIX}] " "$@" >&2
exit 1
}
debug()
{
echo -e "[${BLUE_PREFIX}DEBUG${COLOR_SUFFIX}]===> " "$@"
}
success()
{
echo -e "${BRIGHT_GREEN_PREFIX}===> [SUCCESS] <===${COLOR_SUFFIX}\n=> " "$@"
}
# Print colors you can use # Print colors you can use
openim::color::print_color() openim::color::print_color()
{ {
+60
View File
@@ -709,3 +709,63 @@ if [[ -z "${color_start-}" ]]; then
fi fi
# ex: ts=2 sw=2 et filetype=sh # ex: ts=2 sw=2 et filetype=sh
# input: [10023, 2323, 3434]
# output: 10023 2323 3434
# Function function: Converts a list to a string, removing Spaces and parentheses
function list_to_string() {
ports_list=$* # 获取传入的参数列表
sub_s1=$(echo $ports_list | sed 's/ //g') # 去除空格
sub_s2=${sub_s1//,/ } # 将逗号替换为空格
sub_s3=${sub_s2#*[} # 去除左括号及其之前的内容
sub_s4=${sub_s3%]*} # 去除右括号及其之后的内容
ports_array=$sub_s4 # 将处理后的字符串赋值给变量 ports_array
}
# Function Function: Remove Spaces in the string
function remove_space() {
value=$* # 获取传入的参数
result=$(echo $value | sed 's/ //g') # 去除空格
}
function gen_os_arch() {
# Get the current operating system and architecture
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
# Select the repository home directory based on the operating system and architecture
if [[ "$OS" == "darwin" ]]; then
if [[ "$ARCH" == "x86_64" ]]; then
REPO_DIR="darwin/amd64"
else
REPO_DIR="darwin/386"
fi
elif [[ "$OS" == "linux" ]]; then
if [[ "$ARCH" == "x86_64" ]]; then
REPO_DIR="linux/amd64"
elif [[ "$ARCH" == "arm64" ]]; then
REPO_DIR="linux/arm64"
elif [[ "$ARCH" == "mips64" ]]; then
REPO_DIR="linux/mips64"
elif [[ "$ARCH" == "mips64le" ]]; then
REPO_DIR="linux/mips64le"
elif [[ "$ARCH" == "ppc64le" ]]; then
REPO_DIR="linux/ppc64le"
elif [[ "$ARCH" == "s390x" ]]; then
REPO_DIR="linux/s390x"
else
REPO_DIR="linux/386"
fi
elif [[ "$OS" == "windows" ]]; then
if [[ "$ARCH" == "x86_64" ]]; then
REPO_DIR="windows/amd64"
else
REPO_DIR="windows/386"
fi
else
echo -e "${RED_PREFIX}Unsupported OS: $OS${COLOR_SUFFIX}"
exit 1
fi
}
+7 -1
View File
@@ -46,6 +46,12 @@ BIN_DIR := $(OUTPUT_DIR)/bin
$(shell mkdir -p $(BIN_DIR)) $(shell mkdir -p $(BIN_DIR))
endif endif
# BIN_TOOLS_DIR: Directory where executable files are stored.
ifeq ($(origin BIN_TOOLS_DIR),undefined)
BIN_TOOLS_DIR := $(OUTPUT_DIR)/bin-tools
$(shell mkdir -p $(BIN_TOOLS_DIR))
endif
# TOOLS_DIR: The directory where tools are stored for build and testing. # TOOLS_DIR: The directory where tools are stored for build and testing.
ifeq ($(origin TOOLS_DIR),undefined) ifeq ($(origin TOOLS_DIR),undefined)
TOOLS_DIR := $(OUTPUT_DIR)/tools TOOLS_DIR := $(OUTPUT_DIR)/tools
@@ -120,7 +126,7 @@ FIND := find . ! -path './utils/*' ! -path './vendor/*' ! -path './third_party/*
XARGS := xargs -r --no-run-if-empty XARGS := xargs -r --no-run-if-empty
# Linux command settings-CODE DIRS Copyright # Linux command settings-CODE DIRS Copyright
CODE_DIRS := $(ROOT_DIR)/pkg $(ROOT_DIR)/cmd $(ROOT_DIR)/config $(ROOT_DIR)/db $(ROOT_DIR)/.docker-compose_cfg $(ROOT_DIR)/internal $(ROOT_DIR)/scripts $(ROOT_DIR)/test $(ROOT_DIR)/.github $(ROOT_DIR)/build $(ROOT_DIR)/build $(ROOT_DIR)/deployments CODE_DIRS := $(ROOT_DIR)/pkg $(ROOT_DIR)/cmd $(ROOT_DIR)/config $(ROOT_DIR)/db $(ROOT_DIR)/.docker-compose_cfg $(ROOT_DIR)/internal $(ROOT_DIR)/scripts $(ROOT_DIR)/test $(ROOT_DIR)/.github $(ROOT_DIR)/build $(ROOT_DIR)/tools $(ROOT_DIR)/deployments
FINDS := find $(CODE_DIRS) FINDS := find $(CODE_DIRS)
# Makefile settings: Select different behaviors by determining whether V option is set # Makefile settings: Select different behaviors by determining whether V option is set
+9 -4
View File
@@ -44,8 +44,8 @@ ifeq ($(origin GOBIN), undefined)
GOBIN := $(GOPATH)/bin GOBIN := $(GOPATH)/bin
endif endif
# COMMANDS is Specify all files under ${ROOT_DIR}/cmd/ except those ending in.md # COMMANDS is Specify all files under ${ROOT_DIR}/cmd/ and ${ROOT_DIR}/tools/ except those ending in.md
COMMANDS ?= $(filter-out %.md, $(wildcard ${ROOT_DIR}/cmd/*)) COMMANDS ?= $(filter-out %.md, $(wildcard ${ROOT_DIR}/cmd/* ${ROOT_DIR}/tools/*))
ifeq (${COMMANDS},) ifeq (${COMMANDS},)
$(error Could not determine COMMANDS, set ROOT_DIR or run in source dir) $(error Could not determine COMMANDS, set ROOT_DIR or run in source dir)
endif endif
@@ -127,8 +127,13 @@ go.build.%:
$(BIN_DIR)/platforms/$(OS)/$(ARCH)/$$(basename $${d})$(GO_OUT_EXT) $${d}/main.go; \ $(BIN_DIR)/platforms/$(OS)/$(ARCH)/$$(basename $${d})$(GO_OUT_EXT) $${d}/main.go; \
done; \ done; \
else \ else \
if [ -f $(ROOT_DIR)/cmd/$(COMMAND)/main.go ]; then \
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o \ CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o \
$(BIN_DIR)/platforms/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) $(ROOT_DIR)/cmd/$(COMMAND)/main.go; \ $(BIN_DIR)/platforms/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) $(ROOT_DIR)/cmd/$(COMMAND)/main.go; \
elif [ -f $(ROOT_DIR)/tools/$(COMMAND)/main.go ]; then \
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o \
$(BIN_TOOLS_DIR)/platforms/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) $(ROOT_DIR)/tools/$(COMMAND)/main.go; \
fi \
fi fi
## go.install: Install deployment openim ## go.install: Install deployment openim
@@ -197,8 +202,8 @@ go.updates: tools.verify.go-mod-outdated
## go.clean: Clean all builds directories and files ## go.clean: Clean all builds directories and files
.PHONY: go.clean .PHONY: go.clean
go.clean: go.clean:
@echo "===========> Cleaning all builds TMP_DIR($(TMP_DIR)) AND BIN_DIR($(BIN_DIR))" @echo "===========> Cleaning all builds TMP_DIR($(TMP_DIR)) AND BIN_DIR($(BIN_DIR)) AND BIN_TOOLS_DIR($(BIN_TOOLS_DIR))"
@-rm -vrf $(TMP_DIR) $(BIN_DIR) @-rm -vrf $(TMP_DIR) $(BIN_DIR) $(BIN_TOOLS_DIR)
@echo "===========> End clean..." @echo "===========> End clean..."
## copyright.help: Show copyright help ## copyright.help: Show copyright help
-1
View File
@@ -30,7 +30,6 @@ echo -e "${YELLOW_PREFIX}=======>pwd=$PWD${COLOR_SUFFIX}"
bin_dir="$BIN_DIR" bin_dir="$BIN_DIR"
logs_dir="$OPENIM_ROOT/logs" logs_dir="$OPENIM_ROOT/logs"
sdk_db_dir="$OPENIM_ROOT/sdk/db/"
ulimit -n 200000 ulimit -n 200000
-1
View File
@@ -28,7 +28,6 @@ echo -e "${YELLOW_PREFIX}=======>pwd=$PWD${COLOR_SUFFIX}"
bin_dir="$BIN_DIR" bin_dir="$BIN_DIR"
logs_dir="$OPENIM_ROOT/logs" logs_dir="$OPENIM_ROOT/logs"
sdk_db_dir="$OPENIM_ROOT/sdk/db/"
cd $OPENIM_ROOT cd $OPENIM_ROOT
+1 -1
View File
@@ -22,7 +22,7 @@ SCRIPTS_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
#Include shell font styles and some basic information #Include shell font styles and some basic information
source $SCRIPTS_ROOT/style_info.sh source $SCRIPTS_ROOT/lib/color.sh
cd $SCRIPTS_ROOT cd $SCRIPTS_ROOT
-1
View File
@@ -32,7 +32,6 @@ echo -e "${YELLOW_PREFIX}=======>pwd=$PWD${COLOR_SUFFIX}"
bin_dir="$BIN_DIR" bin_dir="$BIN_DIR"
logs_dir="$OPENIM_ROOT/logs" logs_dir="$OPENIM_ROOT/logs"
sdk_db_dir="$OPENIM_ROOT/sdk/db/"
cd "$OPENIM_ROOT/scripts/" cd "$OPENIM_ROOT/scripts/"
-1
View File
@@ -38,7 +38,6 @@ fi
bin_dir="$OPENIM_ROOT/_output/bin" bin_dir="$OPENIM_ROOT/_output/bin"
logs_dir="$OPENIM_ROOT/logs" logs_dir="$OPENIM_ROOT/logs"
sdk_db_dir="$OPENIM_ROOT/sdk/db/"
if [ ! -d "$bin_dir" ]; then if [ ! -d "$bin_dir" ]; then
mkdir -p "$bin_dir" mkdir -p "$bin_dir"
+1 -1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
@@ -30,7 +31,6 @@ echo -e "${YELLOW_PREFIX}=======>pwd=$PWD${COLOR_SUFFIX}"
bin_dir="$BIN_DIR" bin_dir="$BIN_DIR"
logs_dir="$OPENIM_ROOT/logs" logs_dir="$OPENIM_ROOT/logs"
sdk_db_dir="$OPENIM_ROOT/sdk/db/"
#Check if the service exists #Check if the service exists
#If it is exists,kill this process #If it is exists,kill this process
+1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
+2 -2
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,13 +24,12 @@ source $OPENIM_ROOT/scripts/path_info.sh
bin_dir="$BIN_DIR" bin_dir="$BIN_DIR"
logs_dir="$OPENIM_ROOT/logs" logs_dir="$OPENIM_ROOT/logs"
sdk_db_dir="$OPENIM_ROOT/sdk/db/"
cd "$SCRIPTS_ROOT" cd "$SCRIPTS_ROOT"
for i in ${service_names[*]}; do for i in ${service_names[*]}; do
#Check whether the service exists #Check whether the service exists
name="ps |grep -w $i |grep -v grep" name="ps aux |grep -w $i |grep -v grep"
count="${name}| wc -l" count="${name}| wc -l"
if [ $(eval ${count}) -gt 0 ]; then if [ $(eval ${count}) -gt 0 ]; then
pid="${name}| awk '{print \$2}'" pid="${name}| awk '{print \$2}'"
+1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright © 2023 OpenIM. All rights reserved. # Copyright © 2023 OpenIM. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
+186
View File
@@ -0,0 +1,186 @@
#!/usr/bin/env bash
# Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
# Use of this source code is governed by a MIT style
# license that can be found in the LICENSE file.
# Use this script to test if a given TCP host/port are available
WAITFORIT_cmdname=${0##*/}
openim_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
source "${openim_root}/scripts/lib/color.sh"
echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then info "$@" 1>&2; fi }
usage()
{
cat << USAGE >&2
Usage:
$WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port
-s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit 1
}
function wait_for()
{
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
else
echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout"
fi
WAITFORIT_start_ts=$(date +%s)
while :
do
if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
nc -z $WAITFORIT_HOST $WAITFORIT_PORT
WAITFORIT_result=$?
else
(echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
WAITFORIT_result=$?
fi
if [[ $WAITFORIT_result -eq 0 ]]; then
WAITFORIT_end_ts=$(date +%s)
echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds"
break
fi
sleep 1
done
return $WAITFORIT_result
}
function wait_for_wrapper()
{
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
if [[ $WAITFORIT_QUIET -eq 1 ]]; then
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
else
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
fi
WAITFORIT_PID=$!
trap "kill -INT -$WAITFORIT_PID" INT
wait $WAITFORIT_PID
WAITFORIT_RESULT=$?
if [[ $WAITFORIT_RESULT -ne 0 ]]; then
echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
fi
return $WAITFORIT_RESULT
}
# process arguments
while [[ $# -gt 0 ]]
do
case "$1" in
*:* )
WAITFORIT_hostport=(${1//:/ })
WAITFORIT_HOST=${WAITFORIT_hostport[0]}
WAITFORIT_PORT=${WAITFORIT_hostport[1]}
shift 1
;;
--child)
WAITFORIT_CHILD=1
shift 1
;;
-q | --quiet)
WAITFORIT_QUIET=1
shift 1
;;
-s | --strict)
WAITFORIT_STRICT=1
shift 1
;;
-h)
WAITFORIT_HOST="$2"
if [[ $WAITFORIT_HOST == "" ]]; then break; fi
shift 2
;;
--host=*)
WAITFORIT_HOST="${1#*=}"
shift 1
;;
-p)
WAITFORIT_PORT="$2"
if [[ $WAITFORIT_PORT == "" ]]; then break; fi
shift 2
;;
--port=*)
WAITFORIT_PORT="${1#*=}"
shift 1
;;
-t)
WAITFORIT_TIMEOUT="$2"
if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi
shift 2
;;
--timeout=*)
WAITFORIT_TIMEOUT="${1#*=}"
shift 1
;;
--)
shift
WAITFORIT_CLI=("$@")
break
;;
--help)
usage
;;
*)
warn "Unknown argument: $1"
usage
;;
esac
done
if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
echoerr "Error: you need to provide a host and port to test."
usage
fi
WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15}
WAITFORIT_STRICT=${WAITFORIT_STRICT:-0}
WAITFORIT_CHILD=${WAITFORIT_CHILD:-0}
WAITFORIT_QUIET=${WAITFORIT_QUIET:-0}
# check to see if timeout is from busybox?
WAITFORIT_TIMEOUT_PATH=$(type -p timeout)
WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH)
if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then
WAITFORIT_ISBUSY=1
WAITFORIT_BUSYTIMEFLAG="-t"
else
WAITFORIT_ISBUSY=0
WAITFORIT_BUSYTIMEFLAG=""
fi
if [[ $WAITFORIT_CHILD -gt 0 ]]; then
wait_for
WAITFORIT_RESULT=$?
exit $WAITFORIT_RESULT
else
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
wait_for_wrapper
WAITFORIT_RESULT=$?
else
wait_for
WAITFORIT_RESULT=$?
fi
fi
if [[ $WAITFORIT_CLI != "" ]]; then
if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then
echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess"
exit $WAITFORIT_RESULT
fi
exec "${WAITFORIT_CLI[@]}"
else
exit $WAITFORIT_RESULT
fi
+15
View File
@@ -0,0 +1,15 @@
## Run the Tests
To run a single test or set of tests, you'll need the [Ginkgo](https://github.com/onsi/ginkgo) tool installed on your
machine:
```console
go install github.com/onsi/ginkgo/ginkgo@latest
```
```shell
ginkgo --help
--focus value
If set, ginkgo will only run specs that match this regular expression. Can be specified multiple times, values are ORed.
```
+38
View File
@@ -0,0 +1,38 @@
// Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/golang-jwt/jwt/v4"
)
func main() {
rawJWT := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJpYW0uYXV0aHoubWFybW90ZWR1LmNvbSIsImV4cCI6MTYwNDEyODQwMywiaWF0IjoxNjA0MTI4NDAyLCJpc3MiOiJpYW1jdGwiLCJraWQiOiJpZDEifQ.Itr5u4C-nTeA01qbjjl7RzuPD-aSQazsJZY_Z25aGnI`
// Verify the token
claims := &jwt.MapClaims{}
parsedT, err := jwt.ParseWithClaims(rawJWT, claims, func(token *jwt.Token) (interface{}, error) {
// Validate the alg is HMAC signature
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
if kid, ok := token.Header["kid"].(string); ok {
fmt.Println("kid", kid)
}
return []byte("key1"), nil
})
if err != nil || !parsedT.Valid {
fmt.Println("token valid failed", err)
return
}
fmt.Println("ok")
}
+64
View File
@@ -0,0 +1,64 @@
# Test Data for OpenIM Server
This directory (`testdata`) contains various JSON formatted data files that are used for testing the OpenIM Server.
## Structure
```bash
testdata/
├── README.md # 描述该目录下各子目录和文件的作用
├── db/ # 存储模拟的数据库数据
│ ├── users.json # 用户的模拟数据
│ └── messages.json # 消息的模拟数据
├── requests/ # 存储模拟的请求数据
│ ├── login.json # 模拟登陆请求
│ ├── register.json # 模拟注册请求
│ └── sendMessage.json # 模拟发送消息请求
└── responses/ # 存储模拟的响应数据
├── login.json # 模拟登陆响应
├── register.json # 模拟注册响应
└── sendMessage.json # 模拟发送消息响应
```
Here is an overview of what each subdirectory or file represents:
- `db/` - This directory contains mock data mimicking the actual database contents.
- `users.json` - Represents a list of users in the system. Each entry contains user-specific information such as user ID, username, password hash, etc.
- `messages.json` - Contains a list of messages exchanged between users. Each message entry includes the sender's and receiver's user IDs, message content, timestamp, etc.
- `requests/` - This directory contains mock requests that a client might send to the server.
- `login.json` - Represents a user login request. It includes fields such as username and password.
- `register.json` - Mimics a user registration request. Contains details such as username, password, email, etc.
- `sendMessage.json` - Simulates a message sending request from a user to another user.
- `responses/` - This directory holds the expected server responses for the respective requests.
- `login.json` - Represents a successful login response from the server. It typically includes a session token and user-specific information.
- `register.json` - Simulates a successful registration response from the server, usually containing the new user's ID, username, etc.
- `sendMessage.json` - Depicts a successful message sending response from the server, confirming the delivery of the message.
## JSON Format
All the data files in this directory are in JSON format. JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate.
Here is a simple example of what a JSON file might look like:
```bash
"users": [
{
"id": 1,
"username": "user1",
"password": "password1"
},
{
"id": 2,
"username": "user2",
"password": "password2"
}
]
```
In this example, "users" is an array of user objects. Each user object has an "id", "username", and "password".
View File
View File
View File
View File
View File
View File
View File
View File
+274
View File
@@ -0,0 +1,274 @@
#!/bin/bash
# Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
# Use of this source code is governed by a MIT style
# license that can be found in the LICENSE file.
: << EOF
The API performance test script automatically executes wrk commands, collects data, analyzes it, and calls gnuplot to plot it
Usage (to test API performance) :
Start the openim-api(port 10002)
Execute the test script: ./wrktest.sh
The script will generate the data file.dat, each column meaning: concurrency QPS average response time success rate
Usage (Compare the results of 2 tests)
1. The performance test:. / wrktest. Sh openim apiserver - http://127.0.0.1:10002/healthz
Execute the command:./wrktest.sh diff apiserver.dat http.dat
&gt; Note: Make sure you have wrk and gnuplot installed on your system
EOF
openim_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
wrkdir="${openim_root}/_output/wrk"
jobname="openim-api"
duration="300s"
threads=$((3 * `grep -c processor /proc/cpuinfo`))
source "${openim_root}/scripts/lib/color.sh"
# Set wrk options
openim::wrk::setup() {
#concurrent="200 500 1000 3000 5000 10000 15000 20000 25000 50000 100000 200000 500000 1000000"
concurrent="200 500 1000 3000 5000 10000 15000 20000 25000 50000"
cmd="wrk -t${threads} -d${duration} -T30s --latency"
}
# Print usage infomation
openim::wrk::usage()
{
cat << EOF
Usage: $0 [OPTION] [diff] URL
Performance automation test script.
URL HTTP request url, like: http://127.0.0.1:10002/healthz
diff Compare two performance test results
OPTIONS:
-h Usage information
-n Performance test task name, default: apiserver
-d Directory used to store performance data and gnuplot graphic, default: _output/wrk
Reprot bugs to <3293172751nss@gmail.com>.
EOF
}
# Convert plot data to useable data
function openim::wrk::convert_plot_data()
{
echo "$1" | awk -v datfile="${wrkdir}/${datfile}" ' {
if ($0 ~ "Running") {
common_time=$2
}
if ($0 ~ "connections") {
connections=$4
common_threads=$1
}
if ($0 ~ "Latency ") {
avg_latency=convertLatency($2)
}
if ($0 ~ "50%") {
p50=convertLatency($2)
}
if ($0 ~ "75%") {
p75=convertLatency($2)
}
if ($0 ~ "90%") {
p90=convertLatency($2)
}
if ($0 ~ "99%") {
p99=convertLatency($2)
}
if ($0 ~ "Requests/sec") {
qps=$2
}
if ($0 ~ "requests in") {
allrequest=$1
}
if ($0 ~ "Socket errors") {
err=$4+$6+$8+$10
}
}
END {
rate=sprintf("%.2f", (allrequest-err)*100/allrequest)
print connections,qps,avg_latency,rate >> datfile
}
function convertLatency(s) {
if (s ~ "us") {
sub("us", "", s)
return s/1000
}
if (s ~ "ms") {
sub("ms", "", s)
return s
}
if (s ~ "s") {
sub("s", "", s)
return s * 1000
}
}'
}
# Remove existing data file
function openim::wrk::prepare()
{
rm -f ${wrkdir}/${datfile}
}
# Plot according to gunplot data file
function openim::wrk::plot() {
gnuplot << EOF
set terminal png enhanced #输出格式为png文件
set ylabel 'QPS'
set xlabel 'Concurrent'
set y2label 'Average Latency (ms)'
set key top left vertical noreverse spacing 1.2 box
set tics out nomirror
set border 3 front
set style line 1 linecolor rgb '#00ff00' linewidth 2 linetype 3 pointtype 2
set style line 2 linecolor rgb '#ff0000' linewidth 1 linetype 3 pointtype 2
set style data linespoints
set grid #显示网格
set xtics nomirror rotate #by 90#只需要一个x轴
set mxtics 5
set mytics 5 #可以增加分刻度
set ytics nomirror
set y2tics
set autoscale y
set autoscale y2
set output "${wrkdir}/${qpsttlb}" #指定数据文件名称
set title "QPS & TTLB\nRunning: ${duration}\nThreads: ${threads}"
plot "${wrkdir}/${datfile}" using 2:xticlabels(1) w lp pt 7 ps 1 lc rgbcolor "#EE0000" axis x1y1 t "QPS","${wrkdir}/${datfile}" using 3:xticlabels(1) w lp pt 5 ps 1 lc rgbcolor "#0000CD" axis x2y2 t "Avg Latency (ms)"
unset y2tics
unset y2label
set ytics nomirror
set yrange[0:100]
set output "${wrkdir}/${successrate}" #指定数据文件名称
set title "Success Rate\nRunning: ${duration}\nThreads: ${threads}"
plot "${wrkdir}/${datfile}" using 4:xticlabels(1) w lp pt 7 ps 1 lc rgbcolor "#F62817" t "Success Rate"
EOF
}
# Plot diff graphic
function openim::wrk::plot_diff()
{
gnuplot << EOF
set terminal png enhanced #输出格式为png文件
set xlabel 'Concurrent'
set ylabel 'QPS'
set y2label 'Average Latency (ms)'
set key below left vertical noreverse spacing 1.2 box autotitle columnheader
set tics out nomirror
set border 3 front
set style line 1 linecolor rgb '#00ff00' linewidth 2 linetype 3 pointtype 2
set style line 2 linecolor rgb '#ff0000' linewidth 1 linetype 3 pointtype 2
set style data linespoints
#set border 3 lt 3 lw 2 #这会让你的坐标图的border更好看
set grid #显示网格
set xtics nomirror rotate #by 90#只需要一个x轴
set mxtics 5
set mytics 5 #可以增加分刻度
set ytics nomirror
set y2tics
#set pointsize 0.4 #点的像素大小
#set datafile separator '\t' #数据文件的字段用\t分开
set autoscale y
set autoscale y2
#设置图像的大小 为标准大小的2倍
#set size 2.3,2
set output "${wrkdir}/${t1}_${t2}.qps.ttlb.diff.png" #指定数据文件名称
set title "QPS & TTLB\nRunning: ${duration}\nThreads: ${threads}"
plot "/tmp/plot_diff.dat" using 2:xticlabels(1) w lp pt 7 ps 1 lc rgbcolor "#EE0000" axis x1y1 t "${t1} QPS","/tmp/plot_diff.dat" using 5:xticlabels(1) w lp pt 7 ps 1 lc rgbcolor "#EE82EE" axis x1y1 t "${t2} QPS","/tmp/plot_diff.dat" using 3:xticlabels(1) w lp pt 5 ps 1 lc rgbcolor "#0000CD" axis x2y2 t "${t1} Avg Latency (ms)", "/tmp/plot_diff.dat" using 6:xticlabels(1) w lp pt 5 ps 1 lc rgbcolor "#6495ED" axis x2y2 t "${t2} Avg Latency (ms)"
unset y2tics
unset y2label
set ytics nomirror
set yrange[0:100]
set output "${wrkdir}/${t1}_${t2}.successrate.diff.png" #指定数据文件名称
set title "Success Rate\nRunning: ${duration}\nThreads: ${threads}"
plot "/tmp/plot_diff.dat" using 4:xticlabels(1) w lp pt 7 ps 1 lc rgbcolor "#EE0000" t "${t1} Success Rate","/tmp/plot_diff.dat" using 7:xticlabels(1) w lp pt 7 ps 1 lc rgbcolor "#EE82EE" t "${t2} Success Rate"
EOF
}
# Start API performance testing
openim::wrk::start_performance_test() {
openim::wrk::prepare
for c in ${concurrent}
do
wrkcmd="${cmd} -c ${c} $1"
echo "Running wrk command: ${wrkcmd}"
result=`eval ${wrkcmd}`
openim::wrk::convert_plot_data "${result}"
done
echo -e "\nNow plot according to ${COLOR_MAGENTA}${wrkdir}/${datfile}${COLOR_NORMAL}"
openim::wrk::plot &> /dev/null
echo -e "QPS graphic file is: ${COLOR_MAGENTA}${wrkdir}/${qpsttlb}${COLOR_NORMAL}
Success rate graphic file is: ${COLOR_MAGENTA}${wrkdir}/${successrate}${COLOR_NORMAL}"
}
while getopts "hd:n:" opt;do
case ${opt} in
d)
wrkdir=${OPTARG}
;;
n)
jobname=${OPTARG}
;;
?)
openim::wrk::usage
exit 0
;;
esac
done
shift $(($OPTIND-1))
mkdir -p ${wrkdir}
case $1 in
"diff")
if [ "$#" -lt 3 ];then
openim::wrk::usage
exit 0
fi
t1=$(basename $2|sed 's/.dat//g') # 对比图中红色线条名称
t2=$(basename $3|sed 's/.dat//g') # 对比图中粉色线条名称
join $2 $3 > /tmp/plot_diff.dat
openim::wrk::plot_diff `basename $2` `basename $3`
exit 0
;;
*)
if [ "$#" -lt 1 ];then
openim::wrk::usage
exit 0
fi
url="$1"
qpsttlb="${jobname}_qps_ttlb.png"
successrate="${jobname}_successrate.png"
datfile="${jobname}.dat"
openim::wrk::setup
openim::wrk::start_performance_test "${url}"
;;
esac
+45
View File
@@ -0,0 +1,45 @@
# Notes about go workspace
As openim is using go1.18's [workspace feature](https://go.dev/doc/tutorial/workspaces), once you add a new module, you need to run `go work use -r .` at root directory to update the workspace synced.
### Create a new extensions
1. Create your tools_name directory in pkg `/tools` first and cd into it.
2. Init the project.
3. Then `go work use -r .` at current directory to update the workspace.
4. Create your tools
You can execute the following commands to do things above:
```bash
# 4dd91a700d3f:/openim# tree
# .
# ├── LICENSE
# ├── README.md
# ├── openim-chat
# │ ├── bin
# │ ├── config
# │ ├── logs
# │ └── scripts
# ├── openim-server
# │ ├── bin
# │ ├── config
# │ ├── logs
# │ └── scripts
# ├── openkf
# │ ├── bin
# │ ├── config
# │ ├── logs
# │ └── scripts
# cd tools_code_dir
# edit the CRD_NAME and CRD_GROUP to your own
export OPENIM_TOOLS_NAME=<Changeme>
# copy and paste to create a new CRD and Controller
mkdir tools/${OPENIM_TOOLS_NAME}
cd tools/${OPENIM_TOOLS_NAME}
go mod init github.com/OpenIMSDK/Open-IM-Server/tools/${OPENIM_TOOLS_NAME}
go mod tidy
go work use -r .
cd ../..
```
+308
View File
@@ -0,0 +1,308 @@
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"log"
"os"
"os/exec"
"regexp"
"sort"
"strings"
)
var (
mergeRequest = regexp.MustCompile(`Merge pull request #([\d]+)`)
webconsoleBump = regexp.MustCompile(regexp.QuoteMeta("bump(github.com/openshift/origin-web-console): ") + `([\w]+)`)
upstreamKube = regexp.MustCompile(`^UPSTREAM: (\d+)+:(.+)`)
upstreamRepo = regexp.MustCompile(`^UPSTREAM: ([\w/-]+): (\d+)+:(.+)`)
prefix = regexp.MustCompile(`^[\w-]: `)
assignments = []prefixAssignment{
{"cluster up", "cluster"},
{" pv ", "storage"},
{"haproxy", "router"},
{"router", "router"},
{"route", "route"},
{"authoriz", "auth"},
{"rbac", "auth"},
{"authent", "auth"},
{"reconcil", "auth"},
{"auth", "auth"},
{"role", "auth"},
{" dc ", "deploy"},
{"deployment", "deploy"},
{"rolling", "deploy"},
{"security context constr", "security"},
{"scc", "security"},
{"pipeline", "build"},
{"build", "build"},
{"registry", "registry"},
{"registries", "image"},
{"image", "image"},
{" arp ", "network"},
{" cni ", "network"},
{"egress", "network"},
{"network", "network"},
{"oc ", "cli"},
{"template", "template"},
{"etcd", "server"},
{"pod", "node"},
{"hack/", "hack"},
{"e2e", "test"},
{"integration", "test"},
{"cluster", "cluster"},
{"master", "server"},
{"packages", "hack"},
{"api", "server"},
}
)
type prefixAssignment struct {
term string
prefix string
}
type commit struct {
short string
parents []string
message string
}
func contains(arr []string, value string) bool {
for _, s := range arr {
if s == value {
return true
}
}
return false
}
func main() {
log.SetFlags(0)
if len(os.Args) != 3 {
log.Fatalf("Must specify two arguments, FROM and TO")
}
from := os.Args[1]
to := os.Args[2]
out, err := exec.Command("git", "log", "--topo-order", "--pretty=tformat:%h %p|%s", "--reverse", fmt.Sprintf("%s..%s", from, to)).CombinedOutput()
if err != nil {
log.Fatal(err)
}
hide := make(map[string]struct{})
var apiChanges []string
var webconsole []string
var commits []commit
var upstreams []commit
var bumps []commit
for _, line := range strings.Split(string(out), "\n") {
if len(strings.TrimSpace(line)) == 0 {
continue
}
parts := strings.SplitN(line, "|", 2)
hashes := strings.Split(parts[0], " ")
c := commit{short: hashes[0], parents: hashes[1:], message: parts[1]}
if strings.HasPrefix(c.message, "UPSTREAM: ") {
hide[c.short] = struct{}{}
upstreams = append(upstreams, c)
}
if strings.HasPrefix(c.message, "bump(") {
hide[c.short] = struct{}{}
bumps = append(bumps, c)
}
if len(c.parents) == 1 {
commits = append(commits, c)
continue
}
matches := mergeRequest.FindStringSubmatch(line)
if len(matches) == 0 {
// this may have been a human pressing the merge button, we'll just record this as a direct push
continue
}
// split the accumulated commits into any that are force merges (assumed to be the initial set due
// to --topo-order) from the PR commits as soon as we see any of our merge parents. Then print
// any of the force merges
var first int
for i := range commits {
first = i
if contains(c.parents, commits[i].short) {
first++
break
}
}
individual := commits[:first]
merged := commits[first:]
for _, commit := range individual {
if len(commit.parents) > 1 {
continue
}
if _, ok := hide[commit.short]; ok {
continue
}
fmt.Printf("force-merge: %s %s\n", commit.message, commit.short)
}
// try to find either the PR title or the first commit title from the merge commit
out, err := exec.Command("git", "show", "--pretty=tformat:%b", c.short).CombinedOutput()
if err != nil {
log.Fatal(err)
}
var message string
para := strings.Split(string(out), "\n\n")
if len(para) > 0 && strings.HasPrefix(para[0], "Automatic merge from submit-queue") {
para = para[1:]
}
// this is no longer necessary with the submit queue in place
if len(para) > 0 && strings.HasPrefix(para[0], "Merged by ") {
para = para[1:]
}
// post submit-queue, the merge bot will add the PR title, which is usually pretty good
if len(para) > 0 {
message = strings.Split(para[0], "\n")[0]
}
if len(message) == 0 && len(merged) > 0 {
message = merged[0].message
}
if len(message) > 0 && len(merged) == 1 && message == merged[0].message {
merged = nil
}
// try to calculate a prefix based on the diff
if len(message) > 0 && !prefix.MatchString(message) {
prefix, ok := findPrefixFor(message, merged)
if ok {
message = prefix + ": " + message
}
}
// github merge
// has api changes
display := fmt.Sprintf("%s [\\#%s](https://github.com/openimsdk/Open-IM-Server/pull/%s)", message, matches[1], matches[1])
if hasFileChanges(c.short, "pkg/apistruct/") {
apiChanges = append(apiChanges, display)
}
var filtered []commit
for _, commit := range merged {
if _, ok := hide[commit.short]; ok {
continue
}
filtered = append(filtered, commit)
}
if len(filtered) > 0 {
fmt.Printf("- %s\n", display)
for _, commit := range filtered {
fmt.Printf(" - %s (%s)\n", commit.message, commit.short)
}
}
// stick the merge commit in at the beginning of the next list so we can anchor the previous parent
commits = []commit{c}
}
// chunk the bumps
var lines []string
for _, commit := range bumps {
if m := webconsoleBump.FindStringSubmatch(commit.message); len(m) > 0 {
webconsole = append(webconsole, m[1])
continue
}
lines = append(lines, commit.message)
}
lines = sortAndUniq(lines)
for _, line := range lines {
fmt.Printf("- %s\n", line)
}
// chunk the upstreams
lines = nil
for _, commit := range upstreams {
lines = append(lines, commit.message)
}
lines = sortAndUniq(lines)
for _, line := range lines {
fmt.Printf("- %s\n", upstreamLinkify(line))
}
if len(webconsole) > 0 {
fmt.Printf("- web: from %s^..%s\n", webconsole[0], webconsole[len(webconsole)-1])
}
for _, apiChange := range apiChanges {
fmt.Printf(" - %s\n", apiChange)
}
}
func findPrefixFor(message string, commits []commit) (string, bool) {
message = strings.ToLower(message)
for _, m := range assignments {
if strings.Contains(message, m.term) {
return m.prefix, true
}
}
for _, c := range commits {
if prefix, ok := findPrefixFor(c.message, nil); ok {
return prefix, ok
}
}
return "", false
}
func hasFileChanges(commit string, prefixes ...string) bool {
out, err := exec.Command("git", "diff", "--name-only", fmt.Sprintf("%s^..%s", commit, commit)).CombinedOutput()
if err != nil {
log.Fatal(err)
}
for _, file := range strings.Split(string(out), "\n") {
for _, prefix := range prefixes {
if strings.HasPrefix(file, prefix) {
return true
}
}
}
return false
}
func sortAndUniq(lines []string) []string {
sort.Strings(lines)
out := make([]string, 0, len(lines))
last := ""
for _, s := range lines {
if last == s {
continue
}
last = s
out = append(out, s)
}
return out
}
func upstreamLinkify(line string) string {
if m := upstreamKube.FindStringSubmatch(line); len(m) > 0 {
return fmt.Sprintf("UPSTREAM: [#%s](https://github.com/OpenIMSDK/Open-IM-Server/pull/%s):%s", m[1], m[1], m[2])
}
if m := upstreamRepo.FindStringSubmatch(line); len(m) > 0 {
return fmt.Sprintf("UPSTREAM: [%s#%s](https://github.com/%s/pull/%s):%s", m[1], m[2], m[1], m[2], m[3])
}
return line
}
+3
View File
@@ -0,0 +1,3 @@
module github.com/OpenIMSDK/Open-IM-Server/tools/changelog
go 1.20
+3
View File
@@ -0,0 +1,3 @@
module github.com/OpenIMSDK/Open-IM-Server/tools/infra
go 1.20
+40
View File
@@ -0,0 +1,40 @@
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"log"
)
func main() {
log.Println("Current module is still under development.")
message := `
Current module is still under development.
____ _____ __ __
/ __ \ |_ _|| \/ |
| | | | _ __ ___ _ __ | | | \ / |
| | | || '_ \ / _ \| '_ \ | | | |\/| |
| |__| || |_) || __/| | | | _| |_ | | | |
\____/ | .__/ \___||_| |_||_____||_| |_|
| |
|_|
Keep checking for updates!
`
fmt.Println(message)
}
+5
View File
@@ -0,0 +1,5 @@
module github.com/OpenIMSDK/Open-IM-Server/tools/ncpu
go 1.20
require go.uber.org/automaxprocs v1.5.3
+7
View File
@@ -0,0 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+27
View File
@@ -0,0 +1,27 @@
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"runtime"
"go.uber.org/automaxprocs/maxprocs"
)
func main() {
maxprocs.Set()
fmt.Print(runtime.GOMAXPROCS(0))
}