diff --git a/.gitignore b/.gitignore index e69de29..3b8be1c 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,4 @@ +server/dist +YetAnotherToDoList +YetAnotherToDoList.log +YetAnotherToDoList.sqlite3 diff --git a/INSTALL.md b/INSTALL.md index b5aa567..9c66d62 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -5,6 +5,11 @@ include_toc: true # Install YetAnotherToDoList +It is recommended to use this behind a proxy (e.g. +[Apache/httpd](https://httpd.apache.org/) or [NGINX](https://nginx.org/)) to add +TLS and [Gzip](https://www.gnu.org/software/gzip/)/[brotli](https://brotli.org/) +compression. + ## Requirements: - go 1.21.1 (not tested, but _should_ also work in all versions since 1.16) @@ -13,7 +18,8 @@ include_toc: true ## Install via `go install` -When installing via this method, no commit & version information is provided. +When installing via this method, no commit & version information is provided and +there will be no frontend. ```bash go install somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList@latest @@ -31,6 +37,7 @@ In addition to the [requirements](#requirements), you will need `git`, git clone https://somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList.git cd YetAnotherToDoList go mod tidy # only if you want to edit the GraphQL backend +make frontend # omit if you want to run headless make build ``` @@ -42,6 +49,14 @@ BUILD_FLAGS= RUN_FLAGS= ``` +You also pass `build tags` to `BUILD_FLAGS`, e.g. +`BUILD_FLAGS="-tags headless,dev"` to disable the frontend and enable +`GraphiQL`. + +While there is a `tools` build constraint/tag, building with it has no effect. +The `tools` package only exists to install the dependencies for the +`gqlgen generate` command when you run `go mod tidy`. + ## Install from pre-build binary There are currently no pre-build binaries available diff --git a/Makefile b/Makefile index 204db16..a6fe15f 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,10 @@ run: $(project): build +.PHONY: frontend +frontend: + cd frontend && pnpm run build + .PHONY: install install: $(project) install $(project) $(INSTALL_DIR) diff --git a/frontend/vite.config.js b/frontend/vite.config.js index 05c1740..1d7aae1 100644 --- a/frontend/vite.config.js +++ b/frontend/vite.config.js @@ -3,5 +3,15 @@ import vue from '@vitejs/plugin-vue' // https://vitejs.dev/config/ export default defineConfig({ - plugins: [vue()], + plugins: [vue()], + server: { + port: 4243, + proxy: { + '^/(api|playground|version)': 'http://localhost:4242/' + } + }, + build: { + outDir: '../server/dist', + emptyOutDir: true + } }) diff --git a/server/handleDevTools.go b/server/handleDevTools.go new file mode 100644 index 0000000..ee00bf1 --- /dev/null +++ b/server/handleDevTools.go @@ -0,0 +1,30 @@ +//go:build dev +// +build dev + +/* +YetAnotherToDoList +Copyright © 2023 gilex-dev gilex-dev@proton.me + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +package server + +import ( + "net/http" + + "github.com/99designs/gqlgen/graphql/playground" +) + +func handleDevTools() { + http.Handle("/playground", playground.Handler("GraphQL playground", "/api")) +} diff --git a/server/handleDevTools.mock.go b/server/handleDevTools.mock.go new file mode 100644 index 0000000..c31422d --- /dev/null +++ b/server/handleDevTools.mock.go @@ -0,0 +1,23 @@ +//go:build !dev +// +build !dev + +/* +YetAnotherToDoList +Copyright © 2023 gilex-dev gilex-dev@proton.me + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +package server + +func handleDevTools() { +} diff --git a/server/handleFrontend.go b/server/handleFrontend.go new file mode 100644 index 0000000..8f1efc6 --- /dev/null +++ b/server/handleFrontend.go @@ -0,0 +1,40 @@ +//go:build !headless +// +build !headless + +/* +YetAnotherToDoList +Copyright © 2023 gilex-dev gilex-dev@proton.me + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +package server + +import ( + "embed" + "io/fs" + "log" + "net/http" +) + +//go:embed dist/* +var frontend embed.FS + +func handleFrontend() { + stripped, err := fs.Sub(frontend, "dist") + if err != nil { + log.Fatalln(err) + } + + frontendFS := http.FileServer(http.FS(stripped)) + http.Handle("/", frontendFS) +} diff --git a/server/handleFrontend.mock.go b/server/handleFrontend.mock.go new file mode 100644 index 0000000..fdbc189 --- /dev/null +++ b/server/handleFrontend.mock.go @@ -0,0 +1,23 @@ +//go:build headless +// +build headless + +/* +YetAnotherToDoList +Copyright © 2023 gilex-dev gilex-dev@proton.me + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +package server + +func handleFrontend() { +} diff --git a/server/main.go b/server/main.go index 32cc1b6..d7c096a 100644 --- a/server/main.go +++ b/server/main.go @@ -22,19 +22,19 @@ import ( "strconv" "github.com/99designs/gqlgen/graphql/handler" - "github.com/99designs/gqlgen/graphql/playground" "somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/globals" "somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/graph" ) func StartServer(port int) { - srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}})) + handleDevTools() // controlled by 'dev' tag + handleFrontend() // controlled by 'headless' tag - http.Handle("/", playground.Handler("GraphQL playground", "/api")) http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s %s", globals.Version, globals.CommitHash) }) + srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}})) http.Handle("/api", srv) globals.Logger.Printf("connect to http://localhost:%v/ for GraphQL playground", port) globals.Logger.Fatal(http.ListenAndServe(":"+strconv.FormatInt(int64(port), 10), nil)) diff --git a/tools.go b/tools.go index 9bc5135..cbcba5d 100644 --- a/tools.go +++ b/tools.go @@ -1,5 +1,5 @@ -//go:build tools -// +build tools +//go:build tools && !tools +// +build tools,!tools /* YetAnotherToDoList