integrate frontend into builds, add build tags

This commit is contained in:
gilex-dev 2023-10-24 22:29:07 +02:00
parent 685bfbd525
commit aecc0c7935
10 changed files with 156 additions and 7 deletions

4
.gitignore vendored
View File

@ -0,0 +1,4 @@
server/dist
YetAnotherToDoList
YetAnotherToDoList.log
YetAnotherToDoList.sqlite3

View File

@ -5,6 +5,11 @@ include_toc: true
# Install YetAnotherToDoList # 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: ## Requirements:
- go 1.21.1 (not tested, but _should_ also work in all versions since 1.16) - 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` ## 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 ```bash
go install somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList@latest 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 git clone https://somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList.git
cd YetAnotherToDoList cd YetAnotherToDoList
go mod tidy # only if you want to edit the GraphQL backend go mod tidy # only if you want to edit the GraphQL backend
make frontend # omit if you want to run headless
make build make build
``` ```
@ -42,6 +49,14 @@ BUILD_FLAGS=<go build flags>
RUN_FLAGS=<cli arguments for YetAnotherToDoList> RUN_FLAGS=<cli arguments for YetAnotherToDoList>
``` ```
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 ## Install from pre-build binary
There are currently no pre-build binaries available There are currently no pre-build binaries available

View File

@ -51,6 +51,10 @@ run:
$(project): build $(project): build
.PHONY: frontend
frontend:
cd frontend && pnpm run build
.PHONY: install .PHONY: install
install: $(project) install: $(project)
install $(project) $(INSTALL_DIR) install $(project) $(INSTALL_DIR)

View File

@ -4,4 +4,14 @@ import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
plugins: [vue()], plugins: [vue()],
server: {
port: 4243,
proxy: {
'^/(api|playground|version)': 'http://localhost:4242/'
}
},
build: {
outDir: '../server/dist',
emptyOutDir: true
}
}) })

30
server/handleDevTools.go Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
package server
import (
"net/http"
"github.com/99designs/gqlgen/graphql/playground"
)
func handleDevTools() {
http.Handle("/playground", playground.Handler("GraphQL playground", "/api"))
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
package server
func handleDevTools() {
}

40
server/handleFrontend.go Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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)
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
package server
func handleFrontend() {
}

View File

@ -22,19 +22,19 @@ import (
"strconv" "strconv"
"github.com/99designs/gqlgen/graphql/handler" "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/globals"
"somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/graph" "somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/graph"
) )
func StartServer(port int) { 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) { http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s %s", globals.Version, globals.CommitHash) fmt.Fprintf(w, "%s %s", globals.Version, globals.CommitHash)
}) })
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
http.Handle("/api", srv) http.Handle("/api", srv)
globals.Logger.Printf("connect to http://localhost:%v/ for GraphQL playground", port) globals.Logger.Printf("connect to http://localhost:%v/ for GraphQL playground", port)
globals.Logger.Fatal(http.ListenAndServe(":"+strconv.FormatInt(int64(port), 10), nil)) globals.Logger.Fatal(http.ListenAndServe(":"+strconv.FormatInt(int64(port), 10), nil))

View File

@ -1,5 +1,5 @@
//go:build tools //go:build tools && !tools
// +build tools // +build tools,!tools
/* /*
YetAnotherToDoList YetAnotherToDoList