-
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.
Merge pull request #1 from natrontech/feature-retries
feat: add GET_RETIRES variable
- Loading branch information
Showing
3 changed files
with
84 additions
and
33 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
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 |
---|---|---|
@@ -1,43 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"net/http" | ||
"net/url" | ||
"log" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/natrontech/alertmanager-uptime-kuma-push/internal/util" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/natrontech/alertmanager-uptime-kuma-push/internal/util" | ||
) | ||
|
||
func init() { | ||
|
||
// Log with timestamp | ||
log.SetFlags(log.LstdFlags) | ||
// Log with timestamp | ||
log.SetFlags(log.LstdFlags) | ||
|
||
if err := util.LoadEnv(); err != nil { | ||
if err := util.LoadEnv(); err != nil { | ||
log.Println("Error loading environment variables:", err) | ||
os.Exit(1) | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
app := fiber.New() | ||
|
||
app.All("/push", func(c *fiber.Ctx) error { | ||
|
||
// Only log the host of the URL | ||
url, _ := url.Parse(util.UptimeKumaURL) | ||
log.Println("Sending HTTP Get request to", url.Host) | ||
|
||
_ , err := http.Get(util.UptimeKumaURL) | ||
if err != nil { | ||
log.Println("Error sending HTTP request to uptime kuma url", err) | ||
} | ||
|
||
return c.SendString("OK") | ||
}) | ||
|
||
app.Listen(":8081") | ||
} | ||
app := fiber.New() | ||
|
||
app.All("/push", func(c *fiber.Ctx) error { | ||
|
||
// Set the timeout duration | ||
client := &http.Client{ | ||
Timeout: 3 * time.Second, | ||
} | ||
|
||
// Loop over the number of retries | ||
for i := 0; i < util.Retries; i++ { | ||
|
||
// Log trying again if it's not the first try | ||
if i > 0 { | ||
log.Println("Try " + strconv.Itoa(i) + " of " + strconv.Itoa(util.Retries) + " failed, trying again") | ||
} | ||
|
||
// Only log the host of the URL | ||
url, _ := url.Parse(util.UptimeKumaURL) | ||
log.Println("Sending HTTP GET request to", url.Host) | ||
|
||
_, err := client.Get(util.UptimeKumaURL) | ||
if err != nil { | ||
log.Println("Error sending HTTP request to uptime kuma url", err) | ||
} | ||
|
||
// Exit the loop if there is no error | ||
if err == nil { | ||
break | ||
} | ||
|
||
// Log if it was the last try | ||
if i == util.Retries-1 { | ||
log.Println("Try " + strconv.Itoa(i+1) + " of " + strconv.Itoa(util.Retries) + " failed") | ||
} | ||
|
||
} | ||
return c.SendString("OK") | ||
}) | ||
|
||
app.Listen(":8081") | ||
} |
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