Setting up basic application structure
Some checks failed
My Actions / Explore (push) Has been cancelled

This commit is contained in:
Nigel Barink 2024-11-04 21:41:33 +01:00
parent 1a5a4f3ec9
commit 1cd303824d
Signed by: Nigel
GPG Key ID: 6893A31C2D84A9D2
15 changed files with 204 additions and 35 deletions

52
.air.toml Normal file
View File

@ -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

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
node_modules/
serve/build
public/build

4
docker-compose.yaml Normal file
View File

@ -0,0 +1,4 @@
version: 3.0
services:
WebServer:

6
go.mod
View File

@ -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
)

32
go.sum
View File

@ -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=

View File

@ -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)
}

22
src/middleware/inertia.go Normal file
View File

@ -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)
}

15
src/middleware/logger.go Normal file
View File

@ -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))
})
}

15
src/routes/api.go Normal file
View File

@ -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
}

22
src/routes/web.go Normal file
View File

@ -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
}

24
src/server.go Normal file
View File

@ -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)
}

View File

@ -0,0 +1,11 @@
package main
import (
"testing"
)
func TestTruthy(t *testing.T) {
if true != true {
t.Fatalf("truthy not truthy")
}
}