-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
71 lines (61 loc) · 1.76 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
// "bytes"
// "encoding/json"
// "fmt"
"strings"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"net/http"
"regexp"
// "io/ioutil"
)
// https://backend.kamva.academy/api/fsm/articles/?page=1
type RequestData struct {
Data string `json:"data"`
}
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Any("/api/:path*", SendReqGet)
e.Logger.Fatal(e.Start(":8000"))
}
func SendReqGet(c echo.Context) error {
// Retrieve the wildcard parameter
otherURL := ""
wildcardParam := c.Request().URL
wildcardParam1 := wildcardParam.String()
re := regexp.MustCompile(`/api/([^/]+)`)
matches := re.FindStringSubmatch(wildcardParam1)
if len(matches) > 1 {
firstWordAfterAPI := matches[1]
if firstWordAfterAPI == "fsm"{
otherURL = "https://backend.kamva.academy/api"
}
if firstWordAfterAPI == "party-manager"{
otherURL = "https://mps.sepid.org/api"
}
// find service
}
resultString := strings.Replace(wildcardParam1, "api/", "", 1)
// fmt.Println("parameter:", resultString)
// fsm/articles/?page=1
if otherURL == ""{
return c.String(http.StatusOK, "سلام فرمانده!")
}
return c.Redirect(http.StatusMovedPermanently, otherURL + resultString)
// resp, err := http.Get(otherURL)
// if err != nil {
// return c.String(http.StatusInternalServerError, "Error making POST request to other URL")
// }
// defer resp.Body.Close()
// responseBody, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// return c.String(http.StatusInternalServerError, "Error reading response body from other URL")
// }
// responseString := string(responseBody)
// return c.String(http.StatusOK, responseString)
// }
// return c.String(http.StatusOK, "Wildcard parameter: "+"lk")
}