Skip to content

Commit

Permalink
seperated different parts of api and gave it some structures
Browse files Browse the repository at this point in the history
  • Loading branch information
parsa committed Sep 2, 2023
1 parent d7a289e commit 6a5ec9d
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 122 deletions.
51 changes: 51 additions & 0 deletions foreignusage/api/handlers.go
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",
})
}
24 changes: 24 additions & 0 deletions foreignusage/api/models.go
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"`
}
24 changes: 24 additions & 0 deletions foreignusage/api/router.go
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
}
42 changes: 42 additions & 0 deletions foreignusage/api/testInterface.go
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

}
124 changes: 2 additions & 122 deletions foreignusage/service.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package foreignusage

import (
"context"
"errors"
"fmt"
"net/http"
"sync"

"os"

"github.com/Kawaii-Konnections-KK-Limited/Hayasashiken/run"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/Kawaii-Konnections-KK-Limited/Hayasashiken/foreignusage/api"
)

func InitService() {
Expand All @@ -24,124 +18,10 @@ func InitService() {

}

err := initRouter().Run(":" + port)
err := api.InitRouter().Run(":" + port)

if err != nil {
return
}

}
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.GET("/", api.RestrictedEndpoint)
r.Use(gin.Logger())
r.Use(gin.Recovery())

r.POST("/test", testHandler)
r.GET("/ping", pingSelf)

return r
}

func pingSelf(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
}

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"`
}

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 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

}

0 comments on commit 6a5ec9d

Please sign in to comment.