From 153dc64f60d626fe6210e249af72d7782b036aa8 Mon Sep 17 00:00:00 2001 From: Aneurin Barker Snook Date: Mon, 30 Mar 2026 17:38:09 +0100 Subject: [PATCH] initial commit with basic http server --- .gitignore | 1 + go.mod | 14 ++++++++++++++ go.sum | 17 +++++++++++++++++ main.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..afed073 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.csv diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6ca2e9b --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module gogs.home.aneur.in/go/ultrashorty + +go 1.25.6 + +require ( + github.com/alecthomas/kong v1.14.0 + github.com/rs/zerolog v1.35.0 +) + +require ( + github.com/mattn/go-colorable v0.1.14 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + golang.org/x/sys v0.29.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6151ae1 --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0= +github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= +github.com/alecthomas/kong v1.14.0 h1:gFgEUZWu2ZmZ+UhyZ1bDhuutbKN1nTtJTwh19Wsn21s= +github.com/alecthomas/kong v1.14.0/go.mod h1:wrlbXem1CWqUV5Vbmss5ISYhsVPkBb1Yo7YKJghju2I= +github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs= +github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= +github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= +github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= +github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= +github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/rs/zerolog v1.35.0 h1:VD0ykx7HMiMJytqINBsKcbLS+BJ4WYjz+05us+LRTdI= +github.com/rs/zerolog v1.35.0/go.mod h1:EjML9kdfa/RMA7h/6z6pYmq1ykOuA8/mjWaEvGI+jcw= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/main.go b/main.go new file mode 100644 index 0000000..9a3a0ee --- /dev/null +++ b/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "net" + "net/http" + + "github.com/alecthomas/kong" + "github.com/rs/zerolog" +) + +type CLI struct { + File string `arg:"" name:"file" short:"f" help:"Path to CSV redirects file." type:"path"` + Host string `name:"host" help:"HTTP listen host." default:"${host}"` + Port string `name:"port" short:"p" help:"HTTP listen port." default:"${port}"` +} + +type HttpServer struct { + CLI *CLI + Log zerolog.Logger +} + +func (srv *HttpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { + statusCode := 404 + srv.Log.Trace().Int("result", statusCode).Msg(req.URL.Path) + w.WriteHeader(statusCode) +} + +func main() { + vars := kong.Vars{ + "host": "localhost", + "port": "4000", + } + + cli := &CLI{} + kong.Parse(cli, vars) + + srv := &HttpServer{ + CLI: cli, + Log: zerolog.New(zerolog.NewConsoleWriter()).With().Timestamp().Logger(), + } + + addr := net.JoinHostPort(cli.Host, cli.Port) + errc := make(chan error) + go func() { + errc <- http.ListenAndServe(addr, srv) + }() + + srv.Log.Info().Msgf("Listening at %s", addr) + err := <-errc + panic(err) +}