-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
seperated different parts of api and gave it some structures
- Loading branch information
parsa
committed
Sep 2, 2023
1 parent
d7a289e
commit 6a5ec9d
Showing
5 changed files
with
143 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func testHandler(c *gin.Context) { | ||
|
||
var r requestLinks | ||
err := c.BindJSON(&r) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
done := make(chan bool) | ||
go func() { | ||
|
||
select { | ||
case <-done: | ||
fmt.Println("test done") | ||
cancel() | ||
|
||
} | ||
|
||
}() | ||
|
||
if err != nil { | ||
c.JSON(http.StatusBadRequest, responseError{Status: http.StatusBadRequest, Message: fmt.Sprint(err)}) | ||
return | ||
} | ||
|
||
if c.GetHeader("Authorization") == os.Getenv("auth") { | ||
res := getTestResultsAsService(&r.Links, &r.Timeout, &r.UpperBoundPingLimit, &r.TestUrl, &ctx) | ||
c.JSON(http.StatusOK, responseLinks{Links: res}) | ||
done <- true | ||
|
||
} else { | ||
c.JSON(http.StatusBadRequest, responseError{Status: http.StatusForbidden, Message: "request unauthorized."}) | ||
|
||
} | ||
|
||
} | ||
|
||
func pingSelf(c *gin.Context) { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "pong", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package api | ||
|
||
type link struct { | ||
ID int `json:"id"` | ||
Link string `json:"link"` | ||
} | ||
type requestLinks struct { | ||
Links []link `json:"links"` | ||
Timeout int32 `json:"timeout"` | ||
UpperBoundPingLimit int32 `json:"upperbound"` | ||
TestUrl string `json:"testurl"` | ||
} | ||
type responseLink struct { | ||
ID int `json:"id"` | ||
Ping int32 `json:"ping"` | ||
Link string `json:"link"` | ||
} | ||
type responseLinks struct { | ||
Links []responseLink `json:"links"` | ||
} | ||
type responseError struct { | ||
Status int `json:"status"` | ||
Message string `json:"message"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/gin-contrib/cors" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func InitRouter() *gin.Engine { | ||
config := cors.DefaultConfig() | ||
config.AllowAllOrigins = true | ||
config.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"} | ||
config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"} | ||
r := gin.New() | ||
|
||
r.Use(cors.New(config)) | ||
|
||
r.Use(gin.Logger()) | ||
r.Use(gin.Recovery()) | ||
|
||
r.POST("/test", testHandler) | ||
r.GET("/ping", pingSelf) | ||
|
||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/Kawaii-Konnections-KK-Limited/Hayasashiken/run" | ||
) | ||
|
||
func getTestResultsAsService(links *[]link, timeout *int32, upperBoundPingLimit *int32, TestUrl *string, ctx *context.Context) []responseLink { | ||
var wg sync.WaitGroup | ||
|
||
var pairs []responseLink | ||
baseBroadcast := "" | ||
|
||
for i, v := range *links { | ||
link := v.Link | ||
id := v.ID | ||
kills := make(chan bool, 1) | ||
wg.Add(1) | ||
port := i + 50000 | ||
go func(link *string, port int, i int) { | ||
defer wg.Done() | ||
|
||
r, _ := run.SingByLink(link, TestUrl, &port, timeout, &baseBroadcast, *ctx, &kills) | ||
if r > 10 && r < *upperBoundPingLimit { | ||
pairs = append(pairs, responseLink{ | ||
Ping: r, | ||
Link: *link, | ||
ID: i, | ||
}) | ||
} else { | ||
kills <- true | ||
|
||
} | ||
|
||
}(&link, port, id) | ||
} | ||
wg.Wait() | ||
return pairs | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters