Skip to content

Commit

Permalink
feat: add config & update go to 1.22
Browse files Browse the repository at this point in the history
  • Loading branch information
AutisticShark committed Mar 7, 2024
1 parent 787e361 commit d3fb4f1
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 57 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "weekly"
132 changes: 132 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
name: Build and Release

on:
workflow_dispatch:
push:
branches:
- main
paths:
- "**/*.go"
- "go.mod"
- "go.sum"
- ".github/workflows/*.yml"
pull_request:
types: [ opened, synchronize, reopened ]
paths:
- "**/*.go"
- "go.mod"
- "go.sum"
- ".github/workflows/*.yml"
release:
types: [ published ]

jobs:

build:
strategy:
matrix:
build-tag: [ 'none' ]
goos: [ linux ]
goarch: [ amd64 ]
goamd64: [ v1, v3 ]
include:
- build-tag: none
goos: linux
goarch: arm64
- build-tag: none
goos: linux
goarch: riscv64
fail-fast: false

runs-on: ubuntu-latest
env:
BUILD_TAG: ${{ matrix.build-tag }}
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
GOAMD64: ${{ matrix.goamd64 }}
CGO_ENABLED: 0
steps:
- name: Checkout codebase
uses: actions/checkout@v4

- name: Configure & show workflow information
id: get_filename
run: |
echo "BUILD_TAG: $BUILD_TAG, GOOS: $GOOS, GOARCH: $GOARCH, GOAMD64: $GOAMD64"
if [ "$GOAMD64" == "v3" ]; then
if [ "$BUILD_TAG" == "none" ]; then
echo "ASSET_NAME=$GOOS-$GOARCH$GOAMD64" >> $GITHUB_OUTPUT
echo "ASSET_NAME=$GOOS-$GOARCH$GOAMD64" >> $GITHUB_ENV
else
echo "ASSET_NAME=$GOOS-$GOARCH$GOAMD64-$BUILD_TAG" >> $GITHUB_OUTPUT
echo "ASSET_NAME=$GOOS-$GOARCH$GOAMD64-$BUILD_TAG" >> $GITHUB_ENV
fi
else
if [ "$BUILD_TAG" == "none" ]; then
echo "ASSET_NAME=$GOOS-$GOARCH" >> $GITHUB_OUTPUT
echo "ASSET_NAME=$GOOS-$GOARCH" >> $GITHUB_ENV
else
echo "ASSET_NAME=$GOOS-$GOARCH-$BUILD_TAG" >> $GITHUB_OUTPUT
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 -p build_assets
if [ $BUILD_TAG != "none" ]; then
go build -v -o build_assets/netstatus-api-go -trimpath -ldflags "-s -w -buildid=" -tags $BUILD_TAG
else
go build -v -o build_assets/netstatus-api-go -trimpath -ldflags "-s -w -buildid="
fi
- name: Prepare to release
uses: nick-fields/retry@v3
with:
timeout_minutes: 60
retry_wait_seconds: 60
max_attempts: 5
command: |
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
shell: bash
run: |
pushd build_assets || exit 1
touch -mt $(date +%Y01010000) *
zip -9vr ../netstatus-api-go-$ASSET_NAME.zip .
popd || exit 1
FILE=./netstatus-api-go-$ASSET_NAME.zip
DGST=$FILE.dgst
openssl dgst -sha256 $FILE | sed 's/([^)]*)//g' >>$DGST
openssl dgst -sha3-256 $FILE | sed 's/([^)]*)//g' >>$DGST
- name: Change the name
run: |
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 binaries to release
uses: svenstaro/upload-release-action@v2
if: github.event_name == 'release'
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ./netstatus-api-go-${{ steps.get_filename.outputs.ASSET_NAME }}.zip*
tag: ${{ github.ref }}
file_glob: true
40 changes: 10 additions & 30 deletions api.go → api/api.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,34 @@
package main
package api

import (
"github.com/gin-gonic/gin"
"github.com/sspanel-uim/NetStatus-API-Go/config"
"net"
"net/http"
"time"
)

type tcpingRes struct {
Status string `json:"status"`
Message string `json:"message"`
}

var port = "8080"
var timeout = 1 * time.Second

func main() {
gin.SetMode(gin.ReleaseMode)
r := gin.Default()

r.Group("v1").GET("/tcping", tcping)

err := r.Run(":" + port)

if err != nil {
return
}
}

func tcping(c *gin.Context) {
func Tcping(c *gin.Context) {
if c.Query("ip") == "" {
c.JSON(http.StatusOK, tcpingRes{
Status: "error",
c.JSON(http.StatusBadRequest, tcpingRes{
Status: "false",
Message: "Missing ip parameter",
})

return
}

if c.Query("port") == "" {
c.JSON(http.StatusOK, tcpingRes{
Status: "error",
Message: "Missing ip parameter",
c.JSON(http.StatusBadRequest, tcpingRes{
Status: "false",
Message: "Missing port parameter",
})

return
}

status := "true"
msg := ""

status, msg = ping(c.Query("ip"), c.Query("port"))

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

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

conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, port), timeout)
if err != nil {
return "false", "TCP connection failed"
}
Expand Down
6 changes: 6 additions & 0 deletions api/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package api

type tcpingRes struct {
Status string `json:"status"`
Message string `json:"message"`
}
4 changes: 4 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"port": 8080,
"tcpping_timeout": 1000,
}
28 changes: 28 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package config

import "github.com/spf13/viper"

var config = viper.New()

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

config.SetDefault("port", 8080)
config.SetDefault("timeout", 1000)

err := config.ReadInConfig()
if err != nil {
return
}
}

func GetPort() int {
return config.GetInt("port")
}

func GetTimeout() int {
return config.GetInt("timeout")
}
1 change: 1 addition & 0 deletions config/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package config
34 changes: 26 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,32 +1,50 @@
module github.com/sspanel-uim/NetStatus-API-Go

go 1.21
go 1.22.1

require github.com/gin-gonic/gin v1.9.1
require (
github.com/gin-gonic/gin v1.9.1
github.com/spf13/viper v1.18.2
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // 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.14.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.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.19 // 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.0.8 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // 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/spf13/pflag v1.0.5 // 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.11 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit d3fb4f1

Please sign in to comment.