Skip to content

Commit 2660bc5

Browse files
authored
Merge pull request #3 from CodeShellDev/main
Update Dev
2 parents 35b7ef6 + 5efcfe2 commit 2660bc5

4 files changed

Lines changed: 24 additions & 13 deletions

File tree

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ ARG IMAGE_TAG
55
ENV IMAGE_TAG=$IMAGE_TAG
66
LABEL org.opencontainers.image.version=$IMAGE_TAG
77

8+
ENV PORT=5555
9+
810
ARG TARGETOS
911
ARG TARGETARCH
1012

internals/config/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ var ENV = &structure.ENV{
1616
}
1717

1818
func Load() {
19-
ENV.LOG_LEVEL = os.Getenv("LOG_LEVEL")
19+
logLevel := os.Getenv("LOG_LEVEL")
20+
21+
if logLevel != "" {
22+
ENV.LOG_LEVEL = logLevel
23+
}
2024

2125
ENV.PORT = os.Getenv("PORT")
2226

internals/server/http.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package server
33
import (
44
"encoding/json"
55
"net/http"
6-
"strconv"
76
"time"
87

98
"github.com/codeshelldev/gotl/pkg/logger"
@@ -14,10 +13,10 @@ import (
1413
)
1514

1615
type RequestBody struct {
17-
IP string `json:"ip,omitempty"`
18-
Addr string `json:"addr,omitempty"`
19-
Mac string `json:"mac,omitempty"`
20-
StartupTime string `json:"startupTime,omitempty"`
16+
IP string `json:"ip,omitempty"`
17+
Addr string `json:"addr,omitempty"`
18+
Mac string `json:"mac,omitempty"`
19+
StartupTime *int `json:"startupTime,omitempty"`
2120
}
2221

2322
func httpHandler(w http.ResponseWriter, req *http.Request) {
@@ -92,11 +91,9 @@ func httpHandler(w http.ResponseWriter, req *http.Request) {
9291
return
9392
}
9493

95-
if body.StartupTime != "" {
96-
startupTime, err := strconv.Atoi(body.StartupTime)
97-
94+
if body.StartupTime != nil {
9895
if err == nil {
99-
time.Sleep(time.Duration(startupTime) * time.Second)
96+
time.Sleep(time.Duration(*body.StartupTime) * time.Second)
10097

10198
reachable, err = tryPing(client, body.Addr,
10299
func() (bool, error) {
@@ -142,7 +139,7 @@ func httpHandler(w http.ResponseWriter, req *http.Request) {
142139
}
143140

144141
func tryPingInterval(client *websocket.Conn, interval, retries int, addr string) (bool, error) {
145-
ticker := time.NewTicker(time.Duration(config.ENV.PING_INTERVAL) * time.Second)
142+
ticker := time.NewTicker(time.Duration(interval) * time.Second)
146143
defer ticker.Stop()
147144

148145
count := 0
@@ -164,7 +161,7 @@ func tryPingInterval(client *websocket.Conn, interval, retries int, addr string)
164161
return false, err
165162
}
166163

167-
if count >= config.ENV.PING_RETRIES {
164+
if count >= retries {
168165
break
169166
}
170167
}

internals/server/server.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package server
22

33
import (
44
"net/http"
5+
6+
"github.com/codeshelldev/gotl/pkg/logger"
57
)
68

79
func Handle() http.Handler {
@@ -11,5 +13,11 @@ func Handle() http.Handler {
1113

1214
mux.HandleFunc("/ws", websocketHandler)
1315

14-
return mux
16+
final := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
17+
mux.ServeHTTP(w, r)
18+
19+
logger.Info(r.Method, " ", r.URL.Path, " ", r.URL.RawQuery)
20+
})
21+
22+
return final
1523
}

0 commit comments

Comments
 (0)