mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-04 09:05:59 +08:00
feat: add more test project
Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
# OpenIM Web Service
|
||||
|
||||
- [OpenIM Web Service](#openim-web-service)
|
||||
- [Overview](#overview)
|
||||
- [User](#user)
|
||||
- [Docker Deployment](#docker-deployment)
|
||||
- [Build the Docker Image](#build-the-docker-image)
|
||||
- [Run the Docker Container](#run-the-docker-container)
|
||||
- [Configuration](#configuration)
|
||||
- [Contributions](#contributions)
|
||||
|
||||
|
||||
OpenIM Web Service is a lightweight containerized service built with Go. The service serves static files and allows customization via environment variables.
|
||||
|
||||
## Overview
|
||||
|
||||
- Built using Go.
|
||||
- Deployed as a Docker container.
|
||||
- Serves static files from a directory which can be set via an environment variable.
|
||||
- The default port for the service is `20001`, but it can be customized using an environment variable.
|
||||
|
||||
## User
|
||||
|
||||
example:
|
||||
|
||||
```bash
|
||||
# ./web -h
|
||||
Usage of ./web:
|
||||
-distPath string
|
||||
Path to the distribution (default "/app/dist")
|
||||
-port string
|
||||
Port to run the server on (default "20001")
|
||||
```
|
||||
|
||||
## Docker Deployment
|
||||
|
||||
### Build the Docker Image
|
||||
|
||||
Even though we've implemented automation, it's to make the developer experience easier:
|
||||
|
||||
To build the Docker image for OpenIM Web Service:
|
||||
|
||||
```
|
||||
docker build -t openim-web .
|
||||
```
|
||||
|
||||
### Run the Docker Container
|
||||
|
||||
To run the service:
|
||||
|
||||
```
|
||||
docker run -e DIST_PATH=/app/dist -e PORT=20001 -p 20001:20001 openim-web
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
You can configure the OpenIM Web Service using the following environment variables:
|
||||
|
||||
- **DIST_PATH**: The path to the directory containing the static files. Default: `/app/dist`.
|
||||
- **PORT**: The port on which the service listens. Default: `11001`.
|
||||
|
||||
## Contributions
|
||||
|
||||
We welcome contributions from the community. If you find any bugs or have feature suggestions, please create an issue or send a pull request.
|
||||
@@ -0,0 +1,5 @@
|
||||
module github.com/OpenIMSDK/Open-IM-Server/tools/web
|
||||
|
||||
go 1.18
|
||||
|
||||
require gopkg.in/yaml.v2 v2.4.0
|
||||
@@ -0,0 +1,4 @@
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
distPathFlag string
|
||||
portFlag string
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&distPathFlag, "distPath", "/app/dist", "Path to the distribution")
|
||||
flag.StringVar(&portFlag, "port", "11001", "Port to run the server on")
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
distPath := getConfigValue("DIST_PATH", distPathFlag, "/app/dist")
|
||||
fs := http.FileServer(http.Dir(distPath))
|
||||
http.Handle("/", fs)
|
||||
|
||||
port := getConfigValue("PORT", portFlag, "11001")
|
||||
log.Printf("Server listening on port %s in %s...", port, distPath)
|
||||
err := http.ListenAndServe(":"+port, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getConfigValue(envKey, flagValue, fallback string) string {
|
||||
envVal := os.Getenv(envKey)
|
||||
if envVal != "" {
|
||||
return envVal
|
||||
}
|
||||
if flagValue != "" {
|
||||
return flagValue
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetConfigValue(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
envKey string
|
||||
envValue string
|
||||
flagValue string
|
||||
fallback string
|
||||
wantResult string
|
||||
}{
|
||||
{
|
||||
name: "environment variable set",
|
||||
envKey: "TEST_KEY",
|
||||
envValue: "envValue",
|
||||
flagValue: "",
|
||||
fallback: "default",
|
||||
wantResult: "envValue",
|
||||
},
|
||||
{
|
||||
name: "flag set and environment variable not set",
|
||||
envKey: "TEST_KEY",
|
||||
envValue: "",
|
||||
flagValue: "flagValue",
|
||||
fallback: "default",
|
||||
wantResult: "flagValue",
|
||||
},
|
||||
{
|
||||
name: "nothing set, use fallback",
|
||||
envKey: "TEST_KEY",
|
||||
envValue: "",
|
||||
flagValue: "",
|
||||
fallback: "default",
|
||||
wantResult: "default",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.envValue != "" {
|
||||
os.Setenv(tt.envKey, tt.envValue)
|
||||
defer os.Unsetenv(tt.envKey)
|
||||
}
|
||||
|
||||
got := getConfigValue(tt.envKey, tt.flagValue, tt.fallback)
|
||||
|
||||
if got != tt.wantResult {
|
||||
t.Errorf("getConfigValue(%s, %s, %s) = %s; want %s", tt.envKey, tt.flagValue, tt.fallback, got, tt.wantResult)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user