add support for embedding templates and other static resources
This feature is disabled by default and can be enabled using the "bundle" build tag Fixes #823 Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
parent
c8158e14e0
commit
81de7d271e
8 changed files with 268 additions and 58 deletions
|
@ -13,6 +13,7 @@ The following build tags are available:
|
|||
- `nosqlite`, disable SQLite data provider, default enabled
|
||||
- `noportable`, disable portable mode, default enabled
|
||||
- `nometrics`, disable Prometheus metrics, default enabled
|
||||
- `bundle`, embed static files and templates. Before building with this tag enabled you have to copy `openapi`, `static` and `templates` dirs to `internal/bundle` directory. Default disabled
|
||||
|
||||
If no build tag is specified the build will include the default features.
|
||||
|
||||
|
@ -36,5 +37,5 @@ You should get a version that includes git commit, build date and available feat
|
|||
|
||||
```bash
|
||||
$ ./sftpgo -v
|
||||
SFTPGo 0.9.6-dev-b30614e-dirty-2020-06-19T11:04:56Z +metrics -gcs -s3 +bolt +mysql +pgsql -sqlite +portable
|
||||
SFTPGo 2.3.1-dev-c8158e1-2022-07-24T17:25:45Z +metrics +azblob +gcs +s3 +bolt +mysql +pgsql +sqlite +portable
|
||||
```
|
||||
|
|
65
internal/bundle/bundle.go
Normal file
65
internal/bundle/bundle.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
// Copyright (C) 2019-2022 Nicola Murino
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//go:build bundle
|
||||
// +build bundle
|
||||
|
||||
package bundle
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
|
||||
"github.com/drakkan/sftpgo/v2/internal/version"
|
||||
)
|
||||
|
||||
func init() {
|
||||
version.AddFeature("+bundle")
|
||||
}
|
||||
|
||||
//go:embed templates/*
|
||||
var templatesFs embed.FS
|
||||
|
||||
//go:embed static/*
|
||||
var staticFs embed.FS
|
||||
|
||||
//go:embed openapi/*
|
||||
var openapiFs embed.FS
|
||||
|
||||
// GetTemplatesFs returns the embedded filesystem with the SFTPGo templates
|
||||
func GetTemplatesFs() embed.FS {
|
||||
return templatesFs
|
||||
}
|
||||
|
||||
// GetStaticFs return the http Filesystem with the embedded static files
|
||||
func GetStaticFs() http.FileSystem {
|
||||
fsys, err := fs.Sub(staticFs, "static")
|
||||
if err != nil {
|
||||
err = fmt.Errorf("unable to get embedded filesystem for static files: %w", err)
|
||||
panic(err)
|
||||
}
|
||||
return http.FS(fsys)
|
||||
}
|
||||
|
||||
// GetOpenAPIFs return the http Filesystem with the embedded static files
|
||||
func GetOpenAPIFs() http.FileSystem {
|
||||
fsys, err := fs.Sub(openapiFs, "openapi")
|
||||
if err != nil {
|
||||
err = fmt.Errorf("unable to get embedded filesystem for OpenAPI files: %w", err)
|
||||
panic(err)
|
||||
}
|
||||
return http.FS(fsys)
|
||||
}
|
28
internal/httpd/resources.go
Normal file
28
internal/httpd/resources.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright (C) 2019-2022 Nicola Murino
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//go:build !bundle
|
||||
// +build !bundle
|
||||
|
||||
package httpd
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func serveStaticDir(router chi.Router, path, fsDirPath string) {
|
||||
fileServer(router, path, http.Dir(fsDirPath))
|
||||
}
|
33
internal/httpd/resources_embedded.go
Normal file
33
internal/httpd/resources_embedded.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Copyright (C) 2019-2022 Nicola Murino
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//go:build bundle
|
||||
// +build bundle
|
||||
|
||||
package httpd
|
||||
|
||||
import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/drakkan/sftpgo/v2/internal/bundle"
|
||||
)
|
||||
|
||||
func serveStaticDir(router chi.Router, path, _ string) {
|
||||
switch path {
|
||||
case webStaticFilesPath:
|
||||
fileServer(router, path, bundle.GetStaticFs())
|
||||
case webOpenAPIPath:
|
||||
fileServer(router, path, bundle.GetOpenAPIFs())
|
||||
}
|
||||
}
|
|
@ -1352,14 +1352,14 @@ func (s *httpdServer) initializeRouter() {
|
|||
if s.renderOpenAPI {
|
||||
s.router.Group(func(router chi.Router) {
|
||||
router.Use(compressor.Handler)
|
||||
fileServer(router, webOpenAPIPath, http.Dir(s.openAPIPath))
|
||||
serveStaticDir(router, webOpenAPIPath, s.openAPIPath)
|
||||
})
|
||||
}
|
||||
|
||||
if s.enableWebAdmin || s.enableWebClient {
|
||||
s.router.Group(func(router chi.Router) {
|
||||
router.Use(compressor.Handler)
|
||||
fileServer(router, webStaticFilesPath, http.Dir(s.staticFilesPath))
|
||||
serveStaticDir(router, webStaticFilesPath, s.staticFilesPath)
|
||||
})
|
||||
if s.binding.OIDC.isEnabled() {
|
||||
s.router.Get(webOIDCRedirectPath, s.handleOIDCRedirect)
|
||||
|
|
81
internal/util/resources.go
Normal file
81
internal/util/resources.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
// Copyright (C) 2019-2022 Nicola Murino
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//go:build !bundle
|
||||
// +build !bundle
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/drakkan/sftpgo/v2/internal/logger"
|
||||
)
|
||||
|
||||
// FindSharedDataPath searches for the specified directory name in searchDir
|
||||
// and in system-wide shared data directories.
|
||||
// If name is an absolute path it is returned unmodified.
|
||||
func FindSharedDataPath(name, searchDir string) string {
|
||||
if !IsFileInputValid(name) {
|
||||
return ""
|
||||
}
|
||||
if name != "" && !filepath.IsAbs(name) {
|
||||
searchList := []string{searchDir}
|
||||
if additionalSharedDataSearchPath != "" {
|
||||
searchList = append(searchList, additionalSharedDataSearchPath)
|
||||
}
|
||||
if runtime.GOOS != osWindows {
|
||||
searchList = append(searchList, "/usr/share/sftpgo")
|
||||
searchList = append(searchList, "/usr/local/share/sftpgo")
|
||||
}
|
||||
searchList = RemoveDuplicates(searchList, false)
|
||||
for _, basePath := range searchList {
|
||||
res := filepath.Join(basePath, name)
|
||||
_, err := os.Stat(res)
|
||||
if err == nil {
|
||||
logger.Debug(logSender, "", "found share data path for name %#v: %#v", name, res)
|
||||
return res
|
||||
}
|
||||
}
|
||||
return filepath.Join(searchDir, name)
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// LoadTemplate parses the given template paths.
|
||||
// It behaves like template.Must but it writes a log before exiting.
|
||||
// You can optionally provide a base template (e.g. to define some custom functions)
|
||||
func LoadTemplate(base *template.Template, paths ...string) *template.Template {
|
||||
var t *template.Template
|
||||
var err error
|
||||
|
||||
if base != nil {
|
||||
base, err = base.Clone()
|
||||
if err == nil {
|
||||
t, err = base.ParseFiles(paths...)
|
||||
}
|
||||
} else {
|
||||
t, err = template.ParseFiles(paths...)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
logger.ErrorToConsole("error loading required template: %v", err)
|
||||
logger.Error(logSender, "", "error loading required template: %v", err)
|
||||
panic(err)
|
||||
}
|
||||
return t
|
||||
}
|
57
internal/util/resources_embedded.go
Normal file
57
internal/util/resources_embedded.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
// Copyright (C) 2019-2022 Nicola Murino
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//go:build bundle
|
||||
// +build bundle
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
|
||||
"github.com/drakkan/sftpgo/v2/internal/bundle"
|
||||
"github.com/drakkan/sftpgo/v2/internal/logger"
|
||||
)
|
||||
|
||||
// FindSharedDataPath searches for the specified directory name in searchDir
|
||||
// and in system-wide shared data directories.
|
||||
// If name is an absolute path it is returned unmodified.
|
||||
func FindSharedDataPath(name, _ string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
// LoadTemplate parses the given template paths.
|
||||
// It behaves like template.Must but it writes a log before exiting.
|
||||
// You can optionally provide a base template (e.g. to define some custom functions)
|
||||
func LoadTemplate(base *template.Template, paths ...string) *template.Template {
|
||||
var t *template.Template
|
||||
var err error
|
||||
|
||||
templateFs := bundle.GetTemplatesFs()
|
||||
if base != nil {
|
||||
base, err = base.Clone()
|
||||
if err == nil {
|
||||
t, err = base.ParseFS(templateFs, paths...)
|
||||
}
|
||||
} else {
|
||||
t, err = template.ParseFS(templateFs, paths...)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
logger.ErrorToConsole("error loading required template: %v", err)
|
||||
logger.Error(logSender, "", "error loading required template: %v", err)
|
||||
panic(err)
|
||||
}
|
||||
return t
|
||||
}
|
|
@ -27,7 +27,6 @@ import (
|
|||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net"
|
||||
|
@ -335,30 +334,6 @@ func CleanPathWithBase(base, p string) string {
|
|||
return path.Clean(p)
|
||||
}
|
||||
|
||||
// LoadTemplate parses the given template paths.
|
||||
// It behaves like template.Must but it writes a log before exiting.
|
||||
// You can optionally provide a base template (e.g. to define some custom functions)
|
||||
func LoadTemplate(base *template.Template, paths ...string) *template.Template {
|
||||
var t *template.Template
|
||||
var err error
|
||||
|
||||
if base != nil {
|
||||
base, err = base.Clone()
|
||||
if err == nil {
|
||||
t, err = base.ParseFiles(paths...)
|
||||
}
|
||||
} else {
|
||||
t, err = template.ParseFiles(paths...)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
logger.ErrorToConsole("error loading required template: %v", err)
|
||||
logger.Error(logSender, "", "error loading required template: %v", err)
|
||||
panic(err)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// IsFileInputValid returns true this is a valid file name.
|
||||
// This method must be used before joining a file name, generally provided as
|
||||
// user input, with a directory
|
||||
|
@ -370,36 +345,6 @@ func IsFileInputValid(fileInput string) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
// FindSharedDataPath searches for the specified directory name in searchDir
|
||||
// and in system-wide shared data directories.
|
||||
// If name is an absolute path it is returned unmodified.
|
||||
func FindSharedDataPath(name, searchDir string) string {
|
||||
if !IsFileInputValid(name) {
|
||||
return ""
|
||||
}
|
||||
if name != "" && !filepath.IsAbs(name) {
|
||||
searchList := []string{searchDir}
|
||||
if additionalSharedDataSearchPath != "" {
|
||||
searchList = append(searchList, additionalSharedDataSearchPath)
|
||||
}
|
||||
if runtime.GOOS != osWindows {
|
||||
searchList = append(searchList, "/usr/share/sftpgo")
|
||||
searchList = append(searchList, "/usr/local/share/sftpgo")
|
||||
}
|
||||
searchList = RemoveDuplicates(searchList, false)
|
||||
for _, basePath := range searchList {
|
||||
res := filepath.Join(basePath, name)
|
||||
_, err := os.Stat(res)
|
||||
if err == nil {
|
||||
logger.Debug(logSender, "", "found share data path for name %#v: %#v", name, res)
|
||||
return res
|
||||
}
|
||||
}
|
||||
return filepath.Join(searchDir, name)
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// CleanDirInput sanitizes user input for directories.
|
||||
// On Windows it removes any trailing `"`.
|
||||
// We try to help windows users that set an invalid path such as "C:\ProgramData\SFTPGO\".
|
||||
|
|
Loading…
Reference in a new issue