Implement IP echo

This commit is contained in:
PhatPhuckDave
2024-07-21 19:01:07 +02:00
parent 996e062424
commit b5f3f660f4
3 changed files with 66 additions and 0 deletions

1
deploy.sh Normal file
View File

@@ -0,0 +1 @@
go build main && docker build -t echo-ip .

32
dockerfile Normal file
View File

@@ -0,0 +1,32 @@
FROM golang:1.22.4-bullseye as base
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid 65532 \
small-user
WORKDIR $GOPATH/src/app/
COPY . .
RUN go mod download
RUN go mod verify
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /main .
FROM scratch
COPY --from=base /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=base /etc/passwd /etc/passwd
COPY --from=base /etc/group /etc/group
COPY --from=base /main .
USER small-user:small-user
CMD ["/main"]

33
main.go
View File

@@ -0,0 +1,33 @@
package main
import (
"fmt"
"io"
"log"
"os"
"net/http"
)
var Error *log.Logger
func init() {
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
// logFile, err := os.Create("main.log")
// if err != nil {
// log.Printf("Error creating log file: %v", err)
// os.Exit(1)
// }
logger := io.MultiWriter(os.Stdout)
log.SetOutput(logger)
Error = log.New(io.MultiWriter(os.Stderr, os.Stdout),
fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"),
log.Lmicroseconds|log.Lshortfile)
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, r.RemoteAddr)
})
http.ListenAndServe(":8080", nil)
}