Skip to content

Commit

Permalink
feat: switch to chi http router
Browse files Browse the repository at this point in the history
  • Loading branch information
Cat committed Jul 15, 2024
1 parent ac636dc commit 59015b8
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 145 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ on:
types: [ published ]

jobs:

build:
strategy:
matrix:
Expand All @@ -37,7 +36,6 @@ jobs:
goos: linux
goarch: riscv64
fail-fast: false

runs-on: ubuntu-latest
env:
BUILD_TAG: ${{ matrix.build-tag }}
Expand All @@ -48,7 +46,6 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Generate build information
id: get_filename
run: |
Expand All @@ -70,15 +67,12 @@ jobs:
echo "ASSET_NAME=$GOOS-$GOARCH-$BUILD_TAG" >> $GITHUB_ENV
fi
fi
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ^1.22

- name: Get project dependencies
run: go mod download

- name: Build netstatus-api-go
run: |
mkdir build_assets
Expand All @@ -87,13 +81,11 @@ jobs:
else
go build -v -o build_assets/netstatus-api-go -trimpath -ldflags "-s -w -buildid="
fi
- name: Prepare config files
run: |
cp ${GITHUB_WORKSPACE}/README.md ./build_assets/README.md
cp ${GITHUB_WORKSPACE}/LICENSE ./build_assets/LICENSE
cp ${GITHUB_WORKSPACE}/config.json.example ./build_assets/config.json
- name: Create zip archive
run: |
pushd build_assets || exit 1
Expand All @@ -105,14 +97,12 @@ jobs:
openssl dgst -sha256 $FILE | sed 's/([^)]*)//g' >> $DGST
openssl dgst -sha3-256 $FILE | sed 's/([^)]*)//g' >> $DGST
mv build_assets netstatus-api-go-$ASSET_NAME
- name: Upload files to artifacts
uses: actions/upload-artifact@v4
with:
name: netstatus-api-go-${{ steps.get_filename.outputs.ASSET_NAME }}
path: |
./netstatus-api-go-${{ steps.get_filename.outputs.ASSET_NAME }}/*
- name: Upload files to release
uses: svenstaro/upload-release-action@v2
if: ${{ github.event_name == 'release' }}
Expand Down
20 changes: 3 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

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

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

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

# Go workspace file
go.work

vendor
.idea

config.json
43 changes: 31 additions & 12 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,63 @@
package api

import (
"github.com/SSPanel-NeXT/NetStatus-API-Go/config"
"github.com/gin-gonic/gin"
"encoding/json"
"github.com/The-NeXT-Project/NetStatus-API-Go/config"
"net"
"net/http"
"time"
)

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

writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusBadRequest)
_, err := writer.Write(res)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
}

return
}

if c.Query("port") == "" {
c.JSON(http.StatusBadRequest, tcpingRes{
if request.URL.Query().Get("port") == "" {
res, _ := json.Marshal(tcpingRes{
Status: "false",
Message: "Missing port parameter",
})

writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusBadRequest)
_, err := writer.Write(res)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
}

return
}

status := "true"
msg := ""
status, msg = ping(c.Query("ip"), c.Query("port"))
status, msg := ping(request.URL.Query().Get("ip"), request.URL.Query().Get("port"))

c.JSON(http.StatusOK, tcpingRes{
res, _ := json.Marshal(tcpingRes{
Status: status,
Message: msg,
})

writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
_, err := writer.Write(res)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
}
}

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

conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, port), timeout)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion config.json.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"port": 8080,
"timeout": 1000,
"api_timeout": 3000,
"tcping_timeout": 1000,
"rate_limit": 60
}
27 changes: 17 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ package config
import "github.com/spf13/viper"

var (
Config = viper.New()
apiConfig = &ApiConfig{}
Viper = viper.New()
Config = &ApiConfig{}
)

func init() {
Config.SetConfigName("Config")
Config.SetConfigType("json")
Config.AddConfigPath("/etc/netstatus-api-go/")
Config.AddConfigPath(".")
Viper.SetConfigName("config")
Viper.SetConfigType("json")
Viper.AddConfigPath("/etc/netstatus-api-go/")
Viper.AddConfigPath(".")

Config.SetDefault("port", 8080)
Config.SetDefault("timeout", 1000)
Viper.SetDefault("port", 8080)
Viper.SetDefault("api_timeout", 3000)
Viper.SetDefault("tcping_timeout", 1000)
Viper.SetDefault("rate_limit", 60)

err := Config.ReadInConfig()
err := Viper.ReadInConfig()
if err != nil {
return
panic(err)
}

err = Viper.Unmarshal(&Config)
if err != nil {
panic(err)
}
}
6 changes: 4 additions & 2 deletions config/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

type ApiConfig struct {
Port int `mapstructure:"port"`
Timeout int `mapstructure:"timeout"`
Port int `mapstructure:"port"`
ApiTimeout int `mapstructure:"api_timeout"`
TcpingTimeout int `mapstructure:"tcping_timeout"`
RateLimit int `mapstructure:"rate_limit"`
}
28 changes: 4 additions & 24 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,52 +1,32 @@
module github.com/SSPanel-NeXT/NetStatus-API-Go
module github.com/The-NeXT-Project/NetStatus-API-Go

go 1.22

require (
github.com/gin-gonic/gin v1.10.0
github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/httprate v0.9.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
)

require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 59015b8

Please sign in to comment.