Skip to content

Commit d3fb4f1

Browse files
M1ScrewM1Screw
authored andcommitted
feat: add config & update go to 1.22
1 parent 787e361 commit d3fb4f1

File tree

10 files changed

+295
-57
lines changed

10 files changed

+295
-57
lines changed

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "gomod"
4+
directory: "/"
5+
schedule:
6+
interval: "weekly"

.github/workflows/release.yml

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
name: Build and Release
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
paths:
9+
- "**/*.go"
10+
- "go.mod"
11+
- "go.sum"
12+
- ".github/workflows/*.yml"
13+
pull_request:
14+
types: [ opened, synchronize, reopened ]
15+
paths:
16+
- "**/*.go"
17+
- "go.mod"
18+
- "go.sum"
19+
- ".github/workflows/*.yml"
20+
release:
21+
types: [ published ]
22+
23+
jobs:
24+
25+
build:
26+
strategy:
27+
matrix:
28+
build-tag: [ 'none' ]
29+
goos: [ linux ]
30+
goarch: [ amd64 ]
31+
goamd64: [ v1, v3 ]
32+
include:
33+
- build-tag: none
34+
goos: linux
35+
goarch: arm64
36+
- build-tag: none
37+
goos: linux
38+
goarch: riscv64
39+
fail-fast: false
40+
41+
runs-on: ubuntu-latest
42+
env:
43+
BUILD_TAG: ${{ matrix.build-tag }}
44+
GOOS: ${{ matrix.goos }}
45+
GOARCH: ${{ matrix.goarch }}
46+
GOAMD64: ${{ matrix.goamd64 }}
47+
CGO_ENABLED: 0
48+
steps:
49+
- name: Checkout codebase
50+
uses: actions/checkout@v4
51+
52+
- name: Configure & show workflow information
53+
id: get_filename
54+
run: |
55+
echo "BUILD_TAG: $BUILD_TAG, GOOS: $GOOS, GOARCH: $GOARCH, GOAMD64: $GOAMD64"
56+
if [ "$GOAMD64" == "v3" ]; then
57+
if [ "$BUILD_TAG" == "none" ]; then
58+
echo "ASSET_NAME=$GOOS-$GOARCH$GOAMD64" >> $GITHUB_OUTPUT
59+
echo "ASSET_NAME=$GOOS-$GOARCH$GOAMD64" >> $GITHUB_ENV
60+
else
61+
echo "ASSET_NAME=$GOOS-$GOARCH$GOAMD64-$BUILD_TAG" >> $GITHUB_OUTPUT
62+
echo "ASSET_NAME=$GOOS-$GOARCH$GOAMD64-$BUILD_TAG" >> $GITHUB_ENV
63+
fi
64+
else
65+
if [ "$BUILD_TAG" == "none" ]; then
66+
echo "ASSET_NAME=$GOOS-$GOARCH" >> $GITHUB_OUTPUT
67+
echo "ASSET_NAME=$GOOS-$GOARCH" >> $GITHUB_ENV
68+
else
69+
echo "ASSET_NAME=$GOOS-$GOARCH-$BUILD_TAG" >> $GITHUB_OUTPUT
70+
echo "ASSET_NAME=$GOOS-$GOARCH-$BUILD_TAG" >> $GITHUB_ENV
71+
fi
72+
fi
73+
74+
- name: Set up Go
75+
uses: actions/setup-go@v5
76+
with:
77+
go-version: ^1.22
78+
79+
- name: Get project dependencies
80+
run: go mod download
81+
82+
- name: Build netstatus-api-go
83+
run: |
84+
mkdir -p build_assets
85+
if [ $BUILD_TAG != "none" ]; then
86+
go build -v -o build_assets/netstatus-api-go -trimpath -ldflags "-s -w -buildid=" -tags $BUILD_TAG
87+
else
88+
go build -v -o build_assets/netstatus-api-go -trimpath -ldflags "-s -w -buildid="
89+
fi
90+
91+
- name: Prepare to release
92+
uses: nick-fields/retry@v3
93+
with:
94+
timeout_minutes: 60
95+
retry_wait_seconds: 60
96+
max_attempts: 5
97+
command: |
98+
cp ${GITHUB_WORKSPACE}/README.md ./build_assets/README.md
99+
cp ${GITHUB_WORKSPACE}/LICENSE ./build_assets/LICENSE
100+
cp ${GITHUB_WORKSPACE}/config.json.example ./build_assets/config.json
101+
102+
- name: Create ZIP archive
103+
shell: bash
104+
run: |
105+
pushd build_assets || exit 1
106+
touch -mt $(date +%Y01010000) *
107+
zip -9vr ../netstatus-api-go-$ASSET_NAME.zip .
108+
popd || exit 1
109+
FILE=./netstatus-api-go-$ASSET_NAME.zip
110+
DGST=$FILE.dgst
111+
openssl dgst -sha256 $FILE | sed 's/([^)]*)//g' >>$DGST
112+
openssl dgst -sha3-256 $FILE | sed 's/([^)]*)//g' >>$DGST
113+
114+
- name: Change the name
115+
run: |
116+
mv build_assets netstatus-api-go-$ASSET_NAME
117+
118+
- name: Upload files to Artifacts
119+
uses: actions/upload-artifact@v4
120+
with:
121+
name: netstatus-api-go-${{ steps.get_filename.outputs.ASSET_NAME }}
122+
path: |
123+
./netstatus-api-go-${{ steps.get_filename.outputs.ASSET_NAME }}/*
124+
125+
- name: Upload binaries to release
126+
uses: svenstaro/upload-release-action@v2
127+
if: github.event_name == 'release'
128+
with:
129+
repo_token: ${{ secrets.GITHUB_TOKEN }}
130+
file: ./netstatus-api-go-${{ steps.get_filename.outputs.ASSET_NAME }}.zip*
131+
tag: ${{ github.ref }}
132+
file_glob: true

api.go renamed to api/api.go

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,34 @@
1-
package main
1+
package api
22

33
import (
44
"github.com/gin-gonic/gin"
5+
"github.com/sspanel-uim/NetStatus-API-Go/config"
56
"net"
67
"net/http"
78
"time"
89
)
910

10-
type tcpingRes struct {
11-
Status string `json:"status"`
12-
Message string `json:"message"`
13-
}
14-
15-
var port = "8080"
16-
var timeout = 1 * time.Second
17-
18-
func main() {
19-
gin.SetMode(gin.ReleaseMode)
20-
r := gin.Default()
21-
22-
r.Group("v1").GET("/tcping", tcping)
23-
24-
err := r.Run(":" + port)
25-
26-
if err != nil {
27-
return
28-
}
29-
}
30-
31-
func tcping(c *gin.Context) {
11+
func Tcping(c *gin.Context) {
3212
if c.Query("ip") == "" {
33-
c.JSON(http.StatusOK, tcpingRes{
34-
Status: "error",
13+
c.JSON(http.StatusBadRequest, tcpingRes{
14+
Status: "false",
3515
Message: "Missing ip parameter",
3616
})
3717

3818
return
3919
}
4020

4121
if c.Query("port") == "" {
42-
c.JSON(http.StatusOK, tcpingRes{
43-
Status: "error",
44-
Message: "Missing ip parameter",
22+
c.JSON(http.StatusBadRequest, tcpingRes{
23+
Status: "false",
24+
Message: "Missing port parameter",
4525
})
4626

4727
return
4828
}
4929

5030
status := "true"
5131
msg := ""
52-
5332
status, msg = ping(c.Query("ip"), c.Query("port"))
5433

5534
c.JSON(http.StatusOK, tcpingRes{
@@ -59,8 +38,9 @@ func tcping(c *gin.Context) {
5938
}
6039

6140
func ping(ip string, port string) (status string, msg string) {
62-
conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, port), timeout)
41+
timeout := time.Duration(int64(config.GetTimeout()) * int64(time.Millisecond))
6342

43+
conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, port), timeout)
6444
if err != nil {
6545
return "false", "TCP connection failed"
6646
}

api/model.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package api
2+
3+
type tcpingRes struct {
4+
Status string `json:"status"`
5+
Message string `json:"message"`
6+
}

config.json.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"port": 8080,
3+
"tcpping_timeout": 1000,
4+
}

config/config.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package config
2+
3+
import "github.com/spf13/viper"
4+
5+
var config = viper.New()
6+
7+
func init() {
8+
config.SetConfigName("config")
9+
config.SetConfigType("json")
10+
config.AddConfigPath("/etc/netstatus-api-go/")
11+
config.AddConfigPath(".")
12+
13+
config.SetDefault("port", 8080)
14+
config.SetDefault("timeout", 1000)
15+
16+
err := config.ReadInConfig()
17+
if err != nil {
18+
return
19+
}
20+
}
21+
22+
func GetPort() int {
23+
return config.GetInt("port")
24+
}
25+
26+
func GetTimeout() int {
27+
return config.GetInt("timeout")
28+
}

config/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package config

go.mod

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,50 @@
11
module github.com/sspanel-uim/NetStatus-API-Go
22

3-
go 1.21
3+
go 1.22.1
44

5-
require github.com/gin-gonic/gin v1.9.1
5+
require (
6+
github.com/gin-gonic/gin v1.9.1
7+
github.com/spf13/viper v1.18.2
8+
)
69

710
require (
811
github.com/bytedance/sonic v1.9.1 // indirect
912
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
13+
github.com/fsnotify/fsnotify v1.7.0 // indirect
1014
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
1115
github.com/gin-contrib/sse v0.1.0 // indirect
1216
github.com/go-playground/locales v0.14.1 // indirect
1317
github.com/go-playground/universal-translator v0.18.1 // indirect
1418
github.com/go-playground/validator/v10 v10.14.0 // indirect
1519
github.com/goccy/go-json v0.10.2 // indirect
20+
github.com/hashicorp/hcl v1.0.0 // indirect
1621
github.com/json-iterator/go v1.1.12 // indirect
1722
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
1823
github.com/leodido/go-urn v1.2.4 // indirect
24+
github.com/magiconair/properties v1.8.7 // indirect
1925
github.com/mattn/go-isatty v0.0.19 // indirect
26+
github.com/mitchellh/mapstructure v1.5.0 // indirect
2027
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2128
github.com/modern-go/reflect2 v1.0.2 // indirect
22-
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
29+
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
30+
github.com/sagikazarmark/locafero v0.4.0 // indirect
31+
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
32+
github.com/sourcegraph/conc v0.3.0 // indirect
33+
github.com/spf13/afero v1.11.0 // indirect
34+
github.com/spf13/cast v1.6.0 // indirect
35+
github.com/spf13/pflag v1.0.5 // indirect
36+
github.com/subosito/gotenv v1.6.0 // indirect
2337
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
2438
github.com/ugorji/go/codec v1.2.11 // indirect
39+
go.uber.org/atomic v1.9.0 // indirect
40+
go.uber.org/multierr v1.9.0 // indirect
2541
golang.org/x/arch v0.3.0 // indirect
26-
golang.org/x/crypto v0.9.0 // indirect
27-
golang.org/x/net v0.10.0 // indirect
28-
golang.org/x/sys v0.8.0 // indirect
29-
golang.org/x/text v0.9.0 // indirect
30-
google.golang.org/protobuf v1.30.0 // indirect
42+
golang.org/x/crypto v0.16.0 // indirect
43+
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
44+
golang.org/x/net v0.19.0 // indirect
45+
golang.org/x/sys v0.15.0 // indirect
46+
golang.org/x/text v0.14.0 // indirect
47+
google.golang.org/protobuf v1.31.0 // indirect
48+
gopkg.in/ini.v1 v1.67.0 // indirect
3149
gopkg.in/yaml.v3 v3.0.1 // indirect
3250
)

0 commit comments

Comments
 (0)