From 1cd303824db538d12e100fb51fd462bd67a58901 Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Mon, 4 Nov 2024 21:41:33 +0100 Subject: [PATCH] Setting up basic application structure --- .air.toml | 52 +++++++++++++++++++ .gitignore | 2 +- docker-compose.yaml | 4 ++ go.mod | 6 +++ go.sum | 32 ++++++++++++ .../js/Pages/Home/Index.vue | 0 {serve => public}/js/app.js | 0 root.html => public/templates/root.html | 0 server.go | 34 ------------ src/middleware/inertia.go | 22 ++++++++ src/middleware/logger.go | 15 ++++++ src/routes/api.go | 15 ++++++ src/routes/web.go | 22 ++++++++ src/server.go | 24 +++++++++ tests/Integration/server_test.go | 11 ++++ 15 files changed, 204 insertions(+), 35 deletions(-) create mode 100644 .air.toml create mode 100644 docker-compose.yaml rename serve/js/Pages/Home/index.vue => public/js/Pages/Home/Index.vue (100%) rename {serve => public}/js/app.js (100%) rename root.html => public/templates/root.html (100%) delete mode 100644 server.go create mode 100644 src/middleware/inertia.go create mode 100644 src/middleware/logger.go create mode 100644 src/routes/api.go create mode 100644 src/routes/web.go create mode 100644 src/server.go create mode 100644 tests/Integration/server_test.go diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..dfaba2c --- /dev/null +++ b/.air.toml @@ -0,0 +1,52 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] + args_bin = [] + bin = "tmp\\main.exe" + cmd = "go build -o ./tmp/main.exe ./src/server.go" + delay = 1000 + exclude_dir = ["assets", "tmp", "vendor", "testdata"] + exclude_file = [] + exclude_regex = ["_test.go"] + exclude_unchanged = false + follow_symlink = false + full_bin = "" + include_dir = [] + include_ext = ["go", "tpl", "tmpl", "html"] + include_file = [] + kill_delay = "0s" + log = "build-errors.log" + poll = false + poll_interval = 0 + post_cmd = [] + pre_cmd = [] + rerun = false + rerun_delay = 500 + send_interrupt = false + stop_on_error = false + +[color] + app = "" + build = "yellow" + main = "magenta" + runner = "green" + watcher = "cyan" + +[log] + main_only = false + silent = false + time = false + +[misc] + clean_on_exit = true + +[proxy] + app_port = 0 + enabled = false + proxy_port = 0 + +[screen] + clear_on_rebuild = false + keep_scroll = true diff --git a/.gitignore b/.gitignore index 2013223..48db264 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ node_modules/ -serve/build +public/build diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..9f206d5 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,4 @@ +version: 3.0 +services: + WebServer: + diff --git a/go.mod b/go.mod index 762302c..8939569 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,12 @@ require ( ) require ( + github.com/0xAX/notificator v0.0.0-20220220101646-ee9b8921e557 // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect + github.com/go-chi/chi/v5 v5.1.0 // indirect github.com/gorilla/mux v1.8.1 // indirect + github.com/mattn/go-shellwords v1.0.12 // indirect github.com/romsar/gonertia v1.3.4 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/urfave/cli v1.22.16 // indirect ) diff --git a/go.sum b/go.sum index 1294356..0650454 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,40 @@ +github.com/0xAX/notificator v0.0.0-20220220101646-ee9b8921e557 h1:l6surSnJ3RP4qA1qmKJ+hQn3UjytosdoG27WGjrDlVs= +github.com/0xAX/notificator v0.0.0-20220220101646-ee9b8921e557/go.mod h1:sTrmvD/TxuypdOERsDOS7SndZg0rzzcCi1b6wQMXUYM= +github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/codegangsta/envy v0.0.0-20141216192214-4b78388c8ce4 h1:ihrIKrLQzm6Q6NJHBMemvaIGTFxgxQUEkn2AjN0Aulw= +github.com/codegangsta/envy v0.0.0-20141216192214-4b78388c8ce4/go.mod h1:X7wHz0C25Lga6CnJ4WAQNbUQ9P/8eWSNv8qIO71YkSM= +github.com/codegangsta/gin v0.0.0-20230218063734-2c98d96c9244 h1:p7q80MWx/lN0rvRiVPvX57b+z7IlQl+l4J31diJpwJw= +github.com/codegangsta/gin v0.0.0-20230218063734-2c98d96c9244/go.mod h1:UBYuwaH3dMw91EZ7tGVaFF6GDj5j46S7zqB9lZPIe58= +github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc= +github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk= +github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= github.com/petaki/inertia-go v1.8.0 h1:Gwt0U7fcSXoALahTYg4ovh6RKb+nJDG6kToMfzBfeTo= github.com/petaki/inertia-go v1.8.0/go.mod h1:roW6skZWJ8jz6RvGHOUyRipi8wb5fLu+4RmQWvV/0JE= github.com/petaki/support-go v1.10.0 h1:dtRkoRyU5n/Nv5LuIztoVk+PE0Bf7LzcaDqHZXAEqN0= github.com/petaki/support-go v1.10.0/go.mod h1:KdaZcqk+BrjYTpv+vEqUToRijzr0GssEsECN8/NSyGo= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/romsar/gonertia v1.3.4 h1:5BTBFNtKBVCRJF9vMBatvcw4CDrHpEtpJqU2aZ3Nw4M= github.com/romsar/gonertia v1.3.4/go.mod h1:aFqeLl9P8/zQ/aMfLz8iDj8gZWiNsooHFajj3b1VsYA= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/urfave/cli v1.22.16 h1:MH0k6uJxdwdeWQTwhSO42Pwr4YLrNLwBtg1MRgTqPdQ= +github.com/urfave/cli v1.22.16/go.mod h1:EeJR6BKodywf4zciqrdw6hpCPk68JO9z5LazXZMn5Po= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/serve/js/Pages/Home/index.vue b/public/js/Pages/Home/Index.vue similarity index 100% rename from serve/js/Pages/Home/index.vue rename to public/js/Pages/Home/Index.vue diff --git a/serve/js/app.js b/public/js/app.js similarity index 100% rename from serve/js/app.js rename to public/js/app.js diff --git a/root.html b/public/templates/root.html similarity index 100% rename from root.html rename to public/templates/root.html diff --git a/server.go b/server.go deleted file mode 100644 index c91df3a..0000000 --- a/server.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - inertia "github.com/romsar/gonertia" - "log" - "net/http" -) - -func main() { - i, err := inertia.NewFromFile("./root.html") - if err != nil { - log.Fatal(err) - } - mux := http.NewServeMux() - - mux.Handle("/", i.Middleware(homeHandler(i))) - - mux.Handle("/build/", http.StripPrefix("/build/", http.FileServer(http.Dir("./serve/build")))) - - http.ListenAndServe(":8080", mux) -} - -func homeHandler(i *inertia.Inertia) http.Handler { - fn := func(w http.ResponseWriter, r *http.Request) { - err := i.Render(w, r, "Home/Index", inertia.Props{ - "text": "From Golang", - }) - if err != nil { - return - } - } - - return http.HandlerFunc(fn) -} diff --git a/src/middleware/inertia.go b/src/middleware/inertia.go new file mode 100644 index 0000000..dac0aa9 --- /dev/null +++ b/src/middleware/inertia.go @@ -0,0 +1,22 @@ +package middleware + +import ( + "fmt" + inertia "github.com/romsar/gonertia" + "net/http" +) + +func InertiaMiddleware(i *inertia.Inertia) http.Handler { + fn := func(w http.ResponseWriter, r *http.Request) { + getContextData := r.Context().Value("data") + fmt.Println(getContextData) + err := i.Render(w, r, "Home/Index", inertia.Props{ + "text": "From Golang", + }) + if err != nil { + return + } + } + + return http.HandlerFunc(fn) +} diff --git a/src/middleware/logger.go b/src/middleware/logger.go new file mode 100644 index 0000000..99afbbf --- /dev/null +++ b/src/middleware/logger.go @@ -0,0 +1,15 @@ +package middleware + +import ( + "context" + "fmt" + "net/http" +) + +func TheLogger(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Println(fmt.Sprintf("LOG: %s %s", r.Method, r.URL.Path)) + ctx := context.WithValue(r.Context(), "data", "data:test") + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} diff --git a/src/routes/api.go b/src/routes/api.go new file mode 100644 index 0000000..ba5461d --- /dev/null +++ b/src/routes/api.go @@ -0,0 +1,15 @@ +package routes + +import ( + "net/http" + + "github.com/go-chi/chi/v5" +) + +func Api() *chi.Mux { + r := chi.NewRouter() + r.Get("/version", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("V0.1")) + }) + return r +} diff --git a/src/routes/web.go b/src/routes/web.go new file mode 100644 index 0000000..9252919 --- /dev/null +++ b/src/routes/web.go @@ -0,0 +1,22 @@ +package routes + +import ( + "github.com/go-chi/chi/v5" + inertia "github.com/romsar/gonertia" + "homeautomation/webserver/src/middleware" + "log" + "net/http" +) + +func Web() *chi.Mux { + r := chi.NewRouter() + + i, err := inertia.NewFromFile("public/templates/root.html") + if err != nil { + log.Fatal(err) + } + r.Handle("/", i.Middleware(middleware.InertiaMiddleware(i))) + r.Handle("/hello", i.Middleware(middleware.InertiaMiddleware(i))) + r.Handle("/build/", http.StripPrefix("/build/", http.FileServer(http.Dir("./serve/build")))) + return r +} diff --git a/src/server.go b/src/server.go new file mode 100644 index 0000000..595cb17 --- /dev/null +++ b/src/server.go @@ -0,0 +1,24 @@ +package main + +import ( + "net/http" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + HAmiddleware "homeautomation/webserver/src/middleware" + routes "homeautomation/webserver/src/routes" +) + +func main() { + mux := chi.NewRouter() + // Setup Middleware + mux.Use(middleware.Compress(5, "text/html", "text/css")) + mux.Use(middleware.Heartbeat("/")) + mux.Use(middleware.Recoverer) + // Setup custom middleware + mux.Use(HAmiddleware.TheLogger) + // Define routes + mux.Mount("/", routes.Web()) + mux.Mount("/api", routes.Api()) + http.ListenAndServe(":8080", mux) +} diff --git a/tests/Integration/server_test.go b/tests/Integration/server_test.go new file mode 100644 index 0000000..f7fd5d9 --- /dev/null +++ b/tests/Integration/server_test.go @@ -0,0 +1,11 @@ +package main + +import ( + "testing" +) + +func TestTruthy(t *testing.T) { + if true != true { + t.Fatalf("truthy not truthy") + } +}