-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add port option, strip/minimal server, yaml syntax (#37)
- Loading branch information
Showing
3 changed files
with
40 additions
and
15 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
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,35 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"bufio" | ||
"io" | ||
"net" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func isLinkUp(interfaceName string) bool { | ||
content, err := ioutil.ReadFile("/sys/class/net/" + interfaceName + "/carrier") | ||
content, err := os.ReadFile("/sys/class/net/" + interfaceName + "/carrier") | ||
if err != nil { | ||
return false | ||
} | ||
return strings.TrimSpace(string(content)) == "1" | ||
} | ||
|
||
func healthCheckHandler(w http.ResponseWriter, r *http.Request) { | ||
func handleConnection(conn net.Conn) { | ||
defer conn.Close() // Ensure each connection is closed after handling | ||
|
||
// Read the request (we don't need to parse it for this simple healthcheck) | ||
bufio.NewReader(conn).ReadString('\n') | ||
|
||
status := "KO" | ||
statusCode := "503 Service Unavailable" | ||
if isLinkUp("wg0") { | ||
w.WriteHeader(http.StatusOK) | ||
fmt.Fprintf(w, "status: OK\n") | ||
} else { | ||
w.WriteHeader(http.StatusServiceUnavailable) | ||
fmt.Fprintf(w, "status: KO\n") | ||
status = "OK" | ||
statusCode = "200 OK" | ||
} | ||
|
||
// Write HTTP response | ||
io.WriteString(conn, "HTTP/1.1 "+statusCode+"\r\n") | ||
io.WriteString(conn, "Content-Type: text/plain\r\n") | ||
io.WriteString(conn, "\r\n") | ||
io.WriteString(conn, "status: "+status+"\n") | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/", healthCheckHandler) | ||
if err := http.ListenAndServe(":8080", nil); err != nil { | ||
fmt.Printf("Failed to start server: %v\n", err) | ||
port := "8080" // Default port | ||
if envPort := os.Getenv("HEALTHCHECK_PORT"); envPort != "" { | ||
port = envPort | ||
} | ||
|
||
listener, err := net.Listen("tcp", ":"+port) | ||
if err != nil { | ||
os.Stderr.WriteString("Failed to start server: " + err.Error() + "\n") | ||
os.Exit(1) | ||
} | ||
defer listener.Close() | ||
|
||
// Accept new connections and handle each in a separate goroutine | ||
for { | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
continue // If accept fails, continue to next connection | ||
} | ||
go handleConnection(conn) | ||
} | ||
} |