oioss
58 строк · 1.3 Кб
1package middlewares
2
3import (
4// "log"
5"fmt"
6"net"
7"net/http"
8"strings"
9
10"github.com/gofiber/fiber/v2"
11)
12
13func AllRequests(c *fiber.Ctx) error {
14
15// log.Println("Параметры запроса: ", c.AllParams())
16//Эта ф-ция вызывается при обработке каждого запроса пути /startups/*
17return c.Next()
18}
19
20func IPMiddleware(allowedDomains []string) func(*fiber.Ctx) error {
21
22return func(c *fiber.Ctx) error {
23remoteIP := c.IP()
24names, err := net.LookupAddr(remoteIP)
25if err != nil {
26fmt.Println("Error looking up the host:", err)
27return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
28"error": "Failed to look up the host",
29})
30}
31
32for _, name := range names {
33fmt.Println(name)
34for _, domain := range allowedDomains {
35if strings.TrimSpace(name) == strings.TrimSpace(domain) {
36return c.Next()
37}
38}
39}
40
41return c.Status(http.StatusForbidden).JSON(fiber.Map{
42"error": "Access denied",
43})
44}
45}
46
47func GitMiddleware() func(*fiber.Ctx) error {
48return func(c *fiber.Ctx) error {
49// fmt.Println(c.Get("X-GitHub-Event")) //debug
50if ok := c.Get("X-GitHub-Event", ""); ok != "" {
51return c.Next()
52}
53
54return c.Status(http.StatusForbidden).JSON(fiber.Map{
55"error": "Access denied",
56})
57}
58}
59