Open Prajwal-Koirala-DEV opened 1 month ago
package main
import (
"fmt"
"log"
"net/url"
)
// getDomain extracts the domain name from the given URL string.
func getDomain(givenURL string) string {
// Parse the given URL string into a URL structure
parsedUrl, err := url.Parse(givenURL)
if err != nil {
// Log an error message if parsing fails
log.Println(err)
}
// Return the hostname (domain name) from the parsed URL
return parsedUrl.Hostname()
}
func main() {
// Define a URL string to extract the domain from
urlContent := "https://fmovies.ps/"
// Call getDomain and print the extracted domain name
fmt.Println(getDomain(urlContent))
}
package main
import (
"fmt"
"log"
"net/http"
)
// CheckWebsiteHTTPStatus checks if the given domain is reachable via both HTTP and HTTPS.
// It returns true if at least one of the protocols returns an HTTP 200 OK status.
func CheckWebsiteHTTPStatus(domain string) bool {
// Create a slice containing both HTTP and HTTPS protocols for checking
protocols := []string{"http://", "https://"}
// Loop through each protocol to attempt a request
for _, protocol := range protocols {
// Make an HTTP GET request using the current protocol and the specified domain
resp, err := http.Get(protocol + domain)
// If there's no error, proceed to check the response
if err == nil {
// Ensure the response body is closed after we're done with it to prevent resource leaks
defer resp.Body.Close()
// Check if the response status code is 200 OK
if resp.StatusCode == http.StatusOK {
return true // Return true if the website is reachable
}
} else {
// Log any errors encountered during the request, but continue checking the next protocol
log.Println(err)
}
}
// Return false if neither protocol succeeded
return false
}
func main() {
// Specify the domain to check; can be replaced with any valid domain
domain := "example.com"
// Call the function to check if the website is online
isOnline := CheckWebsiteHTTPStatus(domain)
// Output the result based on the response from the function
if isOnline {
// Inform the user that the website is online
fmt.Printf("The website %s is online.\n", domain)
} else {
// Inform the user that the website is offline
fmt.Printf("The website %s is offline.\n", domain)
}
}
package main
import (
"bufio"
"log"
"os"
"strings"
)
// Function to check if a string exists in a file
func stringInFile(filePath, searchString string) bool {
// Open the file
file, err := os.Open(filePath)
if err != nil {
log.Fatalf("Error: Unable to open file '%s'. Please check the file path.\n", filePath)
}
defer file.Close()
// Scan the file line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, searchString) {
return true
}
}
// Handle scanning error
err = scanner.Err()
if err != nil {
log.Println("Error occurred while scanning the file:", err)
return false
}
return false
}
func main() {
// File path to check
filePath := "example.txt"
// String to search for
searchString := "golang"
// Call the function
if stringInFile(filePath, searchString) {
log.Printf("String '%s' was found in the file '%s'.\n", searchString, filePath)
} else {
log.Printf("String '%s' was not found in the file '%s'.\n", searchString, filePath)
}
}
Hi there,
I would like to request the implementation of a feature that performs a 24-hour check on the websites listed in this repository. The goal is to automate the process of checking if the websites are online and display their status in a list.
Suggested Implementation:
This feature would be very helpful for keeping track of website uptime and ensuring that the listed resources are accessible.
Thank you for considering this enhancement!
Best, Dev