Skip to content

Commit

Permalink
add port option, strip/minimal server, yaml syntax (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
sholdee authored Jul 8, 2024
1 parent 9944459 commit 1dedd79
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ RUN git clone https://git.zx2c4.com/wireguard-tools && \
make install

COPY healthcheck.go /go/src/healthcheck.go
RUN go build -o /go/bin/healthcheck /go/src/healthcheck.go
RUN go build -ldflags="-s -w" -o /go/bin/healthcheck /go/src/healthcheck.go

FROM alpine:${ALPINE_VERSION}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ spec:
- name: LOG_LEVEL
value: info
- name: ENABLE_HEALTHCHECK
value: true
value: "true"
securityContext:
capabilities:
add:
Expand Down
51 changes: 38 additions & 13 deletions healthcheck.go
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)
}
}

0 comments on commit 1dedd79

Please sign in to comment.