Skip to content

Commit 59015b8

Browse files
author
Cat
committed
feat: switch to chi http router
1 parent ac636dc commit 59015b8

File tree

9 files changed

+98
-145
lines changed

9 files changed

+98
-145
lines changed

.github/workflows/release.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ on:
2121
types: [ published ]
2222

2323
jobs:
24-
2524
build:
2625
strategy:
2726
matrix:
@@ -37,7 +36,6 @@ jobs:
3736
goos: linux
3837
goarch: riscv64
3938
fail-fast: false
40-
4139
runs-on: ubuntu-latest
4240
env:
4341
BUILD_TAG: ${{ matrix.build-tag }}
@@ -48,7 +46,6 @@ jobs:
4846
steps:
4947
- name: Checkout
5048
uses: actions/checkout@v4
51-
5249
- name: Generate build information
5350
id: get_filename
5451
run: |
@@ -70,15 +67,12 @@ jobs:
7067
echo "ASSET_NAME=$GOOS-$GOARCH-$BUILD_TAG" >> $GITHUB_ENV
7168
fi
7269
fi
73-
7470
- name: Set up Go
7571
uses: actions/setup-go@v5
7672
with:
7773
go-version: ^1.22
78-
7974
- name: Get project dependencies
8075
run: go mod download
81-
8276
- name: Build netstatus-api-go
8377
run: |
8478
mkdir build_assets
@@ -87,13 +81,11 @@ jobs:
8781
else
8882
go build -v -o build_assets/netstatus-api-go -trimpath -ldflags "-s -w -buildid="
8983
fi
90-
9184
- name: Prepare config files
9285
run: |
9386
cp ${GITHUB_WORKSPACE}/README.md ./build_assets/README.md
9487
cp ${GITHUB_WORKSPACE}/LICENSE ./build_assets/LICENSE
9588
cp ${GITHUB_WORKSPACE}/config.json.example ./build_assets/config.json
96-
9789
- name: Create zip archive
9890
run: |
9991
pushd build_assets || exit 1
@@ -105,14 +97,12 @@ jobs:
10597
openssl dgst -sha256 $FILE | sed 's/([^)]*)//g' >> $DGST
10698
openssl dgst -sha3-256 $FILE | sed 's/([^)]*)//g' >> $DGST
10799
mv build_assets netstatus-api-go-$ASSET_NAME
108-
109100
- name: Upload files to artifacts
110101
uses: actions/upload-artifact@v4
111102
with:
112103
name: netstatus-api-go-${{ steps.get_filename.outputs.ASSET_NAME }}
113104
path: |
114105
./netstatus-api-go-${{ steps.get_filename.outputs.ASSET_NAME }}/*
115-
116106
- name: Upload files to release
117107
uses: svenstaro/upload-release-action@v2
118108
if: ${{ github.event_name == 'release' }}

.gitignore

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
# If you prefer the allow list template instead of the deny list, see community template:
2-
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3-
#
41
# Binaries for programs and plugins
52
*.exe
6-
*.exe~
7-
*.dll
8-
*.so
9-
*.dylib
10-
113
# Test binary, built with `go test -c`
124
*.test
13-
14-
# Output of the go coverage tool, specifically when used with LiteIDE
15-
*.out
16-
175
# Dependency directories (remove the comment below to include it)
18-
# vendor/
19-
20-
# Go workspace file
21-
go.work
22-
6+
vendor
237
.idea
8+
9+
config.json

api/api.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,63 @@
11
package api
22

33
import (
4-
"github.com/SSPanel-NeXT/NetStatus-API-Go/config"
5-
"github.com/gin-gonic/gin"
4+
"encoding/json"
5+
"github.com/The-NeXT-Project/NetStatus-API-Go/config"
66
"net"
77
"net/http"
88
"time"
99
)
1010

11-
func Tcping(c *gin.Context) {
12-
if c.Query("ip") == "" {
13-
c.JSON(http.StatusBadRequest, tcpingRes{
11+
func TcpingV1(writer http.ResponseWriter, request *http.Request) {
12+
if request.URL.Query().Get("ip") == "" {
13+
res, _ := json.Marshal(tcpingRes{
1414
Status: "false",
1515
Message: "Missing ip parameter",
1616
})
1717

18+
writer.Header().Set("Content-Type", "application/json")
19+
writer.WriteHeader(http.StatusBadRequest)
20+
_, err := writer.Write(res)
21+
if err != nil {
22+
writer.WriteHeader(http.StatusInternalServerError)
23+
}
24+
1825
return
1926
}
2027

21-
if c.Query("port") == "" {
22-
c.JSON(http.StatusBadRequest, tcpingRes{
28+
if request.URL.Query().Get("port") == "" {
29+
res, _ := json.Marshal(tcpingRes{
2330
Status: "false",
2431
Message: "Missing port parameter",
2532
})
2633

34+
writer.Header().Set("Content-Type", "application/json")
35+
writer.WriteHeader(http.StatusBadRequest)
36+
_, err := writer.Write(res)
37+
if err != nil {
38+
writer.WriteHeader(http.StatusInternalServerError)
39+
}
40+
2741
return
2842
}
2943

30-
status := "true"
31-
msg := ""
32-
status, msg = ping(c.Query("ip"), c.Query("port"))
44+
status, msg := ping(request.URL.Query().Get("ip"), request.URL.Query().Get("port"))
3345

34-
c.JSON(http.StatusOK, tcpingRes{
46+
res, _ := json.Marshal(tcpingRes{
3547
Status: status,
3648
Message: msg,
3749
})
50+
51+
writer.Header().Set("Content-Type", "application/json")
52+
writer.WriteHeader(http.StatusOK)
53+
_, err := writer.Write(res)
54+
if err != nil {
55+
writer.WriteHeader(http.StatusInternalServerError)
56+
}
3857
}
3958

4059
func ping(ip string, port string) (status string, msg string) {
41-
timeout := time.Duration(int64(config.Config.GetInt("timeout")) * int64(time.Millisecond))
60+
timeout := time.Duration(int64(config.Config.TcpingTimeout) * int64(time.Millisecond))
4261

4362
conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, port), timeout)
4463
if err != nil {

config.json.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
22
"port": 8080,
3-
"timeout": 1000,
3+
"api_timeout": 3000,
4+
"tcping_timeout": 1000,
5+
"rate_limit": 60
46
}

config/config.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@ package config
33
import "github.com/spf13/viper"
44

55
var (
6-
Config = viper.New()
7-
apiConfig = &ApiConfig{}
6+
Viper = viper.New()
7+
Config = &ApiConfig{}
88
)
99

1010
func init() {
11-
Config.SetConfigName("Config")
12-
Config.SetConfigType("json")
13-
Config.AddConfigPath("/etc/netstatus-api-go/")
14-
Config.AddConfigPath(".")
11+
Viper.SetConfigName("config")
12+
Viper.SetConfigType("json")
13+
Viper.AddConfigPath("/etc/netstatus-api-go/")
14+
Viper.AddConfigPath(".")
1515

16-
Config.SetDefault("port", 8080)
17-
Config.SetDefault("timeout", 1000)
16+
Viper.SetDefault("port", 8080)
17+
Viper.SetDefault("api_timeout", 3000)
18+
Viper.SetDefault("tcping_timeout", 1000)
19+
Viper.SetDefault("rate_limit", 60)
1820

19-
err := Config.ReadInConfig()
21+
err := Viper.ReadInConfig()
2022
if err != nil {
21-
return
23+
panic(err)
24+
}
25+
26+
err = Viper.Unmarshal(&Config)
27+
if err != nil {
28+
panic(err)
2229
}
2330
}

config/model.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

33
type ApiConfig struct {
4-
Port int `mapstructure:"port"`
5-
Timeout int `mapstructure:"timeout"`
4+
Port int `mapstructure:"port"`
5+
ApiTimeout int `mapstructure:"api_timeout"`
6+
TcpingTimeout int `mapstructure:"tcping_timeout"`
7+
RateLimit int `mapstructure:"rate_limit"`
68
}

go.mod

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,32 @@
1-
module github.com/SSPanel-NeXT/NetStatus-API-Go
1+
module github.com/The-NeXT-Project/NetStatus-API-Go
22

33
go 1.22
44

55
require (
6-
github.com/gin-gonic/gin v1.10.0
6+
github.com/go-chi/chi/v5 v5.1.0
7+
github.com/go-chi/httprate v0.9.0
78
github.com/spf13/pflag v1.0.5
89
github.com/spf13/viper v1.19.0
910
)
1011

1112
require (
12-
github.com/bytedance/sonic v1.11.6 // indirect
13-
github.com/bytedance/sonic/loader v0.1.1 // indirect
14-
github.com/cloudwego/base64x v0.1.4 // indirect
15-
github.com/cloudwego/iasm v0.2.0 // indirect
13+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
1614
github.com/fsnotify/fsnotify v1.7.0 // indirect
17-
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
18-
github.com/gin-contrib/sse v0.1.0 // indirect
19-
github.com/go-playground/locales v0.14.1 // indirect
20-
github.com/go-playground/universal-translator v0.18.1 // indirect
21-
github.com/go-playground/validator/v10 v10.20.0 // indirect
22-
github.com/goccy/go-json v0.10.2 // indirect
2315
github.com/hashicorp/hcl v1.0.0 // indirect
24-
github.com/json-iterator/go v1.1.12 // indirect
25-
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
26-
github.com/leodido/go-urn v1.4.0 // indirect
2716
github.com/magiconair/properties v1.8.7 // indirect
28-
github.com/mattn/go-isatty v0.0.20 // indirect
2917
github.com/mitchellh/mapstructure v1.5.0 // indirect
30-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
31-
github.com/modern-go/reflect2 v1.0.2 // indirect
3218
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
3319
github.com/sagikazarmark/locafero v0.4.0 // indirect
3420
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
3521
github.com/sourcegraph/conc v0.3.0 // indirect
3622
github.com/spf13/afero v1.11.0 // indirect
3723
github.com/spf13/cast v1.6.0 // indirect
3824
github.com/subosito/gotenv v1.6.0 // indirect
39-
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
40-
github.com/ugorji/go/codec v1.2.12 // indirect
4125
go.uber.org/atomic v1.9.0 // indirect
4226
go.uber.org/multierr v1.9.0 // indirect
43-
golang.org/x/arch v0.8.0 // indirect
44-
golang.org/x/crypto v0.23.0 // indirect
4527
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
46-
golang.org/x/net v0.25.0 // indirect
4728
golang.org/x/sys v0.20.0 // indirect
4829
golang.org/x/text v0.15.0 // indirect
49-
google.golang.org/protobuf v1.34.1 // indirect
5030
gopkg.in/ini.v1 v1.67.0 // indirect
5131
gopkg.in/yaml.v3 v3.0.1 // indirect
5232
)

0 commit comments

Comments
 (0)