aboutsummaryrefslogtreecommitdiff
path: root/internal/cmd
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-03-17 17:50:17 +0700
committerShulhan <ms@kilabit.info>2024-03-18 21:55:35 +0700
commit182a79905737010fa135d101be656144b7b0f39a (patch)
tree2074073942e549924d29fd1c9f84571a827c14c5 /internal/cmd
parent97b8266d5a6c95dd70c255845aafbb82cacc97d9 (diff)
downloadhaminer-182a79905737010fa135d101be656144b7b0f39a.tar.xz
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.
Diffstat (limited to 'internal/cmd')
-rw-r--r--internal/cmd/haminer-dummy-backend/main.go89
1 files changed, 89 insertions, 0 deletions
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 <ms@kilabit.info>
+// 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
+}