From 182a79905737010fa135d101be656144b7b0f39a Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 17 Mar 2024 17:50:17 +0700 Subject: all: create new dummy backend to test backend in HAProxy The haminer-dummy-backend run in container and serve two ports in HTTP and TCP modes. --- Makefile | 8 +- _bin/.gitignore | 2 + .../etc/systemd/system/haminer-dummy-backend.path | 9 +++ .../systemd/system/haminer-dummy-backend.service | 14 ++++ .../etc/systemd/system/systemctl-restart@.service | 6 ++ _ops/haminer-test/mkosi.finalize.chroot | 2 + go.mod | 5 +- go.sum | 2 + internal/cmd/haminer-dummy-backend/main.go | 89 ++++++++++++++++++++++ 9 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 _bin/.gitignore create mode 100644 _ops/haminer-test/mkosi.extra/etc/systemd/system/haminer-dummy-backend.path create mode 100644 _ops/haminer-test/mkosi.extra/etc/systemd/system/haminer-dummy-backend.service create mode 100644 _ops/haminer-test/mkosi.extra/etc/systemd/system/systemctl-restart@.service create mode 100644 internal/cmd/haminer-dummy-backend/main.go diff --git a/Makefile b/Makefile index 1aa4c4a..dbf01a4 100644 --- a/Makefile +++ b/Makefile @@ -39,11 +39,17 @@ serve-doc: MACHINE_NAME:=haminer-test +.PHONY: haminer-dummy-backend +haminer-dummy-backend: + go build -o ./_bin/ ./internal/cmd/... + .PHONY: init-local-dev -init-local-dev: +init-local-dev: build haminer-dummy-backend @echo ">>> Stopping container ..." -sudo machinectl poweroff $(MACHINE_NAME) + cp -f _bin/* _ops/haminer-test/mkosi.extra/data/haminer/bin/ + @echo ">>> Building container $(MACHINE_NAME) ..." sudo mkosi --directory=_ops/$(MACHINE_NAME)/ --force build diff --git a/_bin/.gitignore b/_bin/.gitignore new file mode 100644 index 0000000..120f485 --- /dev/null +++ b/_bin/.gitignore @@ -0,0 +1,2 @@ +* +!/.gitignore diff --git a/_ops/haminer-test/mkosi.extra/etc/systemd/system/haminer-dummy-backend.path b/_ops/haminer-test/mkosi.extra/etc/systemd/system/haminer-dummy-backend.path new file mode 100644 index 0000000..5261287 --- /dev/null +++ b/_ops/haminer-test/mkosi.extra/etc/systemd/system/haminer-dummy-backend.path @@ -0,0 +1,9 @@ +[Unit] +Description="Watch haminer-dummy-backend" + +[Path] +PathChanged=/data/haminer/bin/haminer-dummy-backend +Unit=systemctl-restart@%p.service + +[Install] +WantedBy=default.target diff --git a/_ops/haminer-test/mkosi.extra/etc/systemd/system/haminer-dummy-backend.service b/_ops/haminer-test/mkosi.extra/etc/systemd/system/haminer-dummy-backend.service new file mode 100644 index 0000000..3f6e41a --- /dev/null +++ b/_ops/haminer-test/mkosi.extra/etc/systemd/system/haminer-dummy-backend.service @@ -0,0 +1,14 @@ +## SPDX-FileCopyrightText: 2024 M. Shulhan +## SPDX-License-Identifier: GPL-3.0-or-later + +[Unit] +Description=haminer-dummy-backend +After=syslog.target network.target + +[Service] +ExecStart=/data/haminer/bin/haminer-dummy-backend +Restart=always +RestartSec=10s + +[Install] +WantedBy=multi-user.target diff --git a/_ops/haminer-test/mkosi.extra/etc/systemd/system/systemctl-restart@.service b/_ops/haminer-test/mkosi.extra/etc/systemd/system/systemctl-restart@.service new file mode 100644 index 0000000..800316e --- /dev/null +++ b/_ops/haminer-test/mkosi.extra/etc/systemd/system/systemctl-restart@.service @@ -0,0 +1,6 @@ +[Unit] +Description=systemctl-restart@%i + +[Service] +Type=oneshot +ExecStart=/bin/systemctl restart %i diff --git a/_ops/haminer-test/mkosi.finalize.chroot b/_ops/haminer-test/mkosi.finalize.chroot index eb19874..200b78f 100755 --- a/_ops/haminer-test/mkosi.finalize.chroot +++ b/_ops/haminer-test/mkosi.finalize.chroot @@ -5,3 +5,5 @@ chown postgres:postgres /var/lib/postgres/data/pg_hba.conf sudo systemctl enable haproxy.service sudo systemctl enable postgresql.service +sudo systemctl enable haminer-dummy-backend.path +sudo systemctl enable haminer-dummy-backend.service diff --git a/go.mod b/go.mod index c359df6..b920d38 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,7 @@ go 1.21 require git.sr.ht/~shulhan/pakakeh.go v0.53.2-0.20240315075343-713d51e4792f -require golang.org/x/sys v0.18.0 // indirect +require ( + golang.org/x/net v0.22.0 // indirect + golang.org/x/sys v0.18.0 // indirect +) diff --git a/go.sum b/go.sum index 9edb8c8..190ab4f 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ git.sr.ht/~shulhan/pakakeh.go v0.53.2-0.20240315075343-713d51e4792f h1:bP4msj5TVm+kQ6GUt6QvwEXOVOzUk2MQc5c8bSto8sc= git.sr.ht/~shulhan/pakakeh.go v0.53.2-0.20240315075343-713d51e4792f/go.mod h1:tTHoHDHuBxj5q1zwpLZGCKrdc6i0I3sP8kPp+JEs16c= +golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= +golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/internal/cmd/haminer-dummy-backend/main.go b/internal/cmd/haminer-dummy-backend/main.go new file mode 100644 index 0000000..5c6517e --- /dev/null +++ b/internal/cmd/haminer-dummy-backend/main.go @@ -0,0 +1,89 @@ +// SPDX-FileCopyrightText: 2024 Shulhan +// SPDX-License-Identifier: GPL-3.0-or-later + +package main + +import ( + "fmt" + "os" + "os/signal" + "syscall" + + libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http" + "git.sr.ht/~shulhan/pakakeh.go/lib/mlog" +) + +func main() { + go runHTTPServer(`127.0.0.1:5001`) + go runHTTPServer(`127.0.0.1:5002`) + + var chSignal = make(chan os.Signal, 1) + signal.Notify(chSignal, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) + <-chSignal + signal.Stop(chSignal) +} + +func runHTTPServer(addr string) { + var ( + logp = `runHTTPServer ` + addr + serverOpts = libhttp.ServerOptions{ + Address: addr, + } + + httpServer *libhttp.Server + err error + ) + + httpServer, err = libhttp.NewServer(serverOpts) + if err != nil { + mlog.Fatalf(`%s: %w`, logp, err) + } + + err = registerEndpoints(httpServer) + if err != nil { + mlog.Fatalf(`%s: %w`, logp, err) + } + + mlog.Outf(`%s: %s`, os.Args[0], logp) + + err = httpServer.Start() + if err != nil { + mlog.Fatalf(`%s: %w`, logp, err) + } +} + +func registerEndpoints(httpServer *libhttp.Server) (err error) { + var logp = `registerEndpoints` + + err = httpServer.RegisterEndpoint(libhttp.Endpoint{ + Method: libhttp.RequestMethodGet, + Path: `/`, + ResponseType: libhttp.ResponseTypePlain, + Call: handleGet, + }) + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + + err = httpServer.RegisterEndpoint(libhttp.Endpoint{ + Method: libhttp.RequestMethodPost, + Path: `/`, + ResponseType: libhttp.ResponseTypePlain, + Call: handlePost, + }) + if err != nil { + return fmt.Errorf(`%s: %w`, logp, err) + } + + return nil +} + +func handleGet(_ *libhttp.EndpointRequest) (resbody []byte, err error) { + resbody = []byte(`Example of plain GET response`) + return resbody, nil +} + +func handlePost(_ *libhttp.EndpointRequest) (resbody []byte, err error) { + resbody = []byte(`Example of plain POST response`) + return resbody, nil +} -- cgit v1.3