-
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.
- Loading branch information
Showing
6 changed files
with
222 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name: Docker Image CI | ||
|
||
on: | ||
release: | ||
types: [created] | ||
|
||
jobs: | ||
|
||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Save version | ||
run: basename $GITHUB_REF >version.txt | ||
|
||
- name: Docker meta | ||
id: meta | ||
uses: docker/metadata-action@v4 | ||
with: | ||
images: | | ||
lindex/melp | ||
tags: | | ||
type=semver,pattern=v{{version}} | ||
type=semver,pattern=v{{major}}.{{minor}} | ||
labels: | | ||
org.opencontainers.image.title=melp | ||
org.opencontainers.image.description=Simple service to test TCP-connections | ||
org.opencontainers.image.source=https://github.com/AB-Lindex/tcpecho | ||
org.opencontainers.image.url=https://github.com/AB-Lindex/tcpecho | ||
org.opencontainers.image.licenses=apache2.0 | ||
- name: Login to DockerHub | ||
uses: docker/login-action@v2 | ||
with: | ||
username: ${{ secrets.DOCKERHUB_USER }} | ||
password: ${{ secrets.DOCKERHUB_SECRET }} | ||
|
||
- name: Build and push Docker images | ||
uses: docker/build-push-action@v3 | ||
with: | ||
context: . | ||
push: true | ||
tags: ${{ steps.meta.outputs.tags }} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
version.txt |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# syntax=docker/dockerfile:1 | ||
|
||
## | ||
## STEP 1 - BUILD | ||
## | ||
|
||
# specify the base image to be used for the application, alpine or ubuntu | ||
FROM golang:1.21-alpine AS build | ||
|
||
# create a working directory inside the image | ||
WORKDIR /app | ||
|
||
# copy Go modules and dependencies to image | ||
COPY *go* version.txt ./ | ||
|
||
# compile application (static linked) | ||
RUN CGO_ENABLED=0 go build -ldflags="-extldflags=-static" -o /tcpecho | ||
|
||
## | ||
## STEP 2 - DEPLOY | ||
## | ||
FROM scratch | ||
|
||
ENV PATH=/ | ||
|
||
WORKDIR / | ||
|
||
COPY --from=build /tcpecho / | ||
|
||
CMD ["/tcpecho"] |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
SOURCES=*.go | ||
|
||
build: bin/tcpecho | ||
|
||
bin/tcpecho: $(SOURCES) Makefile go.mod | ||
@mkdir -p bin | ||
go build -o bin/tcpecho . | ||
|
||
run: bin/tcpecho | ||
bin/tcpecho | ||
|
||
runc: | ||
docker run --rm -it tcpecho:docker | ||
|
||
docker: | ||
-docker rmi tcpecho:docker | ||
docker build -t tcpecho:docker . | ||
|
||
nerdctl: | ||
-docker rmi tcpecho:nerdctl | ||
nerdctl build -t tcpecho:nerdctl . | ||
|
||
check: | ||
@echo "Checking...\n" | ||
gocyclo -over 15 . || echo -n "" | ||
@echo "" | ||
golint -min_confidence 0.21 -set_exit_status ./... | ||
@echo "" | ||
go mod verify | ||
@echo "\nAll ok!" | ||
|
||
check2: | ||
@echo "" | ||
golangci-lint run -E misspell -E depguard -E dupl -E goconst -E gocyclo -E ifshort -E predeclared -E tagliatelle -E errorlint -E godox -D structcheck | ||
|
||
release: | ||
gh release create `cat version.txt` |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/AB-Lindex/tcpecho | ||
|
||
go 1.21.4 |
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
_ "embed" | ||
"fmt" | ||
"log" | ||
"net" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
//go:embed version.txt | ||
var version string | ||
|
||
var ( | ||
port int = 43210 | ||
workerID int | ||
hostname string | ||
) | ||
|
||
func main() { | ||
if len(os.Args) > 1 { | ||
v, err := strconv.Atoi(os.Args[1]) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
port = v | ||
} | ||
|
||
hostname, _ = os.Hostname() | ||
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Printf("%s Listening on port %d...", version, port) | ||
defer l.Close() | ||
for { | ||
conn, err := l.Accept() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
workerID++ | ||
go handle(conn, workerID) | ||
} | ||
} | ||
|
||
func handle(c net.Conn, id int) { | ||
log.Printf("%d: starting connection with %v", id, c.RemoteAddr().String()) | ||
defer func() { | ||
log.Printf("%d: closing connection with %v", id, c.RemoteAddr().String()) | ||
c.Close() | ||
}() | ||
fmt.Fprintf(c, "Welcome to tcpecho on %s:%d\nThis is handler #%d, type '.' to quit\n", hostname, port, id) | ||
rd := bufio.NewScanner(c) | ||
for { | ||
if rd.Scan() { | ||
txt := rd.Text() | ||
if txt == "." { | ||
fmt.Fprintln(c, "Goodbye.") | ||
return | ||
} | ||
do(id, c, txt) | ||
} | ||
} | ||
} | ||
|
||
type dooer func(id int, c net.Conn, cmd, args string) | ||
|
||
var commands = map[string]dooer{ | ||
"date": dateCmd, | ||
"time": timeCmd, | ||
"info": infoCmd, | ||
} | ||
|
||
func do(id int, c net.Conn, txt string) { | ||
parts := strings.SplitN(txt, " ", 2) | ||
var args string | ||
if len(parts) > 1 { | ||
args = strings.TrimSpace(parts[1]) | ||
} | ||
for cmd, fn := range commands { | ||
if strings.EqualFold(cmd, parts[0]) { | ||
log.Printf("%d: doing '%s'", id, cmd) | ||
fn(id, c, cmd, args) | ||
return | ||
} | ||
} | ||
fmt.Fprintf(c, "unknown command: '%s'\n", parts[0]) | ||
} | ||
|
||
func dateCmd(id int, c net.Conn, cmd, args string) { | ||
fmt.Fprintf(c, "The date is %s\n", time.Now().Format(time.DateOnly)) | ||
} | ||
|
||
func timeCmd(id int, c net.Conn, cmd, args string) { | ||
fmt.Fprintf(c, "The time is %s\n", time.Now().Format(time.TimeOnly)) | ||
} | ||
|
||
func infoCmd(id int, c net.Conn, cmd, args string) { | ||
fmt.Fprintf(c, "You are connecting from %s\n", c.RemoteAddr().String()) | ||
} |