Add webui to binary package

This commit is contained in:
Kevin Kandlbinder 2022-09-05 18:41:42 +02:00
parent 52b426cb6a
commit e0965b92e2
6 changed files with 141 additions and 4 deletions

View file

@ -18,7 +18,23 @@ jobs:
with:
go-version: 1.17
- name: Build
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: "18.x"
- run: yarn
name: WebUI - Install Dependencies
working-directory: webui
- run: yarn build
name: WebUI - Build
working-directory: webui
- name: Build with WebUI
run: go build -tags withUI -v ./...
- name: Build without WebUI
run: go build -v ./...
- name: Test

View file

@ -20,10 +20,21 @@ jobs:
goos: windows
steps:
- uses: actions/checkout@v2
- uses: wangyoucao577/go-release-action@v1.25
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: "18.x"
- run: yarn
name: WebUI - Install Dependencies
working-directory: webui
- run: yarn build
name: WebUI - Build
working-directory: webui
- uses: wangyoucao577/go-release-action@v1.30
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "https://go.dev/dl/go1.17.7.linux-amd64.tar.gz"
extra_files: LICENSE README.md
extra_files: LICENSE README.md
build_flags: '-tags withUI'

View file

@ -11,3 +11,18 @@ statistical work.
[![Crowdin](https://badges.crowdin.net/matrix-veles/localized.svg)](https://crowdin.com/project/matrix-veles)
If you have time and language skills, consider [helping to translate the docs](https://crwd.in/matrix-veles)!
## Building
### Building without WebUI
Simply run `go build` in the project directory.
### Building with WebUI
Make sure you have NodeJS and Yarn installed.
Then run the following commands:
1. `go generate ./...` - This will build the webui react project.
2. `go build -tags withUI` - This will build the binary with included UI.

View file

@ -2,12 +2,14 @@ package web
import (
"github.com/Unkn0wnCat/matrix-veles/internal/web/api"
"github.com/Unkn0wnCat/matrix-veles/webui"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
"log"
"net/http"
"strings"
"time"
)
@ -23,11 +25,26 @@ func StartServer() {
r.Handle("/metrics", promhttp.Handler())
r.HandleFunc("/", HomeHandler)
//r.HandleFunc("/", HomeHandler)
//r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
r.Mount("/api", api.SetupAPI())
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api") {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(404)
w.Write([]byte("{\"error_code\": 404, \"error\": \"not_found\"}"))
return
}
return
})
ui, err := webui.ServeUI()
if err == nil {
r.Mount("/", ui)
}
srv := &http.Server{
Handler: r,
Addr: viper.GetString("bot.web.listen"),

58
webui/webui.go Normal file
View file

@ -0,0 +1,58 @@
//go:build withUI
// +build withUI
package webui
//go:generate yarn
//go:generate yarn build
import (
"embed"
"io/fs"
"log"
"net/http"
"path"
"strings"
)
//go:embed build/*
var content embed.FS
func ServeUI() (http.Handler, error) {
fSys, err := fs.Sub(content, "build")
if err != nil {
return nil, err
}
staticServer := http.FileServer(http.FS(fSys))
serveIndex, err := ServeIndex()
if err != nil {
return nil, err
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fSys.Open(strings.TrimPrefix(path.Clean(r.URL.Path), "/"))
if err != nil {
log.Println(err)
serveIndex.ServeHTTP(w, r)
return
}
log.Println("serving static")
staticServer.ServeHTTP(w, r)
}), nil
}
func ServeIndex() (http.HandlerFunc, error) {
indexFile, err := content.ReadFile("build/index.html")
if err != nil {
return nil, err
}
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
w.Write(indexFile)
}, nil
}

20
webui/webuiNotIncluded.go Normal file
View file

@ -0,0 +1,20 @@
//go:build !withUI
// +build !withUI
package webui
//go:generate yarn
//go:generate yarn build
import (
"net/http"
)
func ServeUI() (http.Handler, error) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(404)
w.Write([]byte("{\"error_code\": 404, \"error\": \"not_found\", \"note\": \"WebUI not included in build - visit https://veles.1in1.net/docs/tutorial-basics/install\"}"))
return
}), nil
}