mirror of
https://github.com/thomiceli/opengist
synced 2024-11-08 12:55:50 +01:00
Add embed Filesystem
This commit is contained in:
parent
c1ad78d984
commit
f9f5b140b8
@ -1,11 +1,14 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var OpengistVersion = "0.0.1"
|
||||
@ -105,6 +108,27 @@ func InitLog() {
|
||||
log.Logger = zerolog.New(multi).Level(level).With().Timestamp().Logger()
|
||||
}
|
||||
|
||||
func CheckGitVersion(version string) (bool, error) {
|
||||
versionParts := strings.Split(version, ".")
|
||||
if len(versionParts) < 2 {
|
||||
return false, fmt.Errorf("invalid version string")
|
||||
}
|
||||
major, err := strconv.Atoi(versionParts[0])
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("invalid major version number")
|
||||
}
|
||||
minor, err := strconv.Atoi(versionParts[1])
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("invalid minor version number")
|
||||
}
|
||||
|
||||
// Check if version is prior to 2.20
|
||||
if major < 2 || (major == 2 && minor < 20) {
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func GetHomeDir() string {
|
||||
absolutePath, _ := filepath.Abs(C.OpengistHome)
|
||||
return filepath.Clean(absolutePath)
|
||||
|
@ -3,6 +3,7 @@ package web
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gorilla/sessions"
|
||||
@ -15,7 +16,6 @@ import (
|
||||
"opengist/internal/config"
|
||||
"opengist/internal/git"
|
||||
"opengist/internal/models"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@ -79,6 +79,8 @@ var fm = template.FuncMap{
|
||||
},
|
||||
}
|
||||
|
||||
var EmbedFS embed.FS
|
||||
|
||||
type Template struct {
|
||||
templates *template.Template
|
||||
}
|
||||
@ -89,7 +91,7 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, _ echo.Con
|
||||
|
||||
func Start() {
|
||||
store = sessions.NewCookieStore([]byte("opengist"))
|
||||
parseManifestEntries()
|
||||
assetsFS := echo.MustSubFS(EmbedFS, "public/assets")
|
||||
|
||||
e := echo.New()
|
||||
e.HideBanner = true
|
||||
@ -113,9 +115,8 @@ func Start() {
|
||||
e.Use(middleware.Secure())
|
||||
|
||||
e.Renderer = &Template{
|
||||
templates: template.Must(template.New("t").Funcs(fm).ParseGlob("templates/*/*.html")),
|
||||
templates: template.Must(template.New("t").Funcs(fm).ParseFS(EmbedFS, "templates/*/*.html")),
|
||||
}
|
||||
|
||||
e.HTTPErrorHandler = func(er error, ctx echo.Context) {
|
||||
if err, ok := er.(*echo.HTTPError); ok {
|
||||
if err.Code >= 500 {
|
||||
@ -135,7 +136,8 @@ func Start() {
|
||||
|
||||
e.Validator = NewValidator()
|
||||
|
||||
e.Static("/assets", "./public/assets")
|
||||
parseManifestEntries()
|
||||
e.GET("/assets/*", cacheControl(echo.WrapHandler(http.StripPrefix("/assets", http.FileServer(http.FS(assetsFS))))))
|
||||
|
||||
// Web based routes
|
||||
g1 := e.Group("")
|
||||
@ -294,6 +296,13 @@ func logged(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func cacheControl(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
c.Response().Header().Set(echo.HeaderCacheControl, "public, max-age=31536000")
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
||||
func noRouteFound(echo.Context) error {
|
||||
return notFound("Page not found")
|
||||
}
|
||||
@ -307,7 +316,7 @@ type Asset struct {
|
||||
var manifestEntries map[string]Asset
|
||||
|
||||
func parseManifestEntries() {
|
||||
file, err := os.Open("public/manifest.json")
|
||||
file, err := EmbedFS.Open("public/manifest.json")
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to open manifest.json")
|
||||
}
|
||||
|
24
opengist.go
24
opengist.go
@ -1,9 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/rs/zerolog/log"
|
||||
"opengist/internal/config"
|
||||
"opengist/internal/git"
|
||||
"opengist/internal/models"
|
||||
"opengist/internal/ssh"
|
||||
"opengist/internal/web"
|
||||
@ -11,6 +14,9 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
//go:embed templates/*/*.html public/manifest.json public/assets/*.js public/assets/*.css public/assets/*.svg
|
||||
var embedFS embed.FS
|
||||
|
||||
func initialize() {
|
||||
configPath := flag.String("config", "config.yml", "Path to a config file in YML format")
|
||||
flag.Parse()
|
||||
@ -25,8 +31,20 @@ func initialize() {
|
||||
|
||||
config.InitLog()
|
||||
|
||||
log.Info().Msg("Opengist v" + config.OpengistVersion)
|
||||
log.Info().Msg("Using config file: " + absolutePath)
|
||||
fmt.Println("Opengist v" + config.OpengistVersion)
|
||||
fmt.Println("Using config file: " + absolutePath)
|
||||
|
||||
gitVersion, err := git.GetGitVersion()
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Send()
|
||||
}
|
||||
|
||||
if ok, err := config.CheckGitVersion(gitVersion); err != nil {
|
||||
log.Fatal().Err(err).Send()
|
||||
} else if !ok {
|
||||
log.Warn().Msg("Git version may be too old, as Opengist has not been tested prior git version 2.20. " +
|
||||
"Current git version: " + gitVersion)
|
||||
}
|
||||
|
||||
homePath := config.GetHomeDir()
|
||||
log.Info().Msg("Data directory: " + homePath)
|
||||
@ -42,6 +60,8 @@ func initialize() {
|
||||
if err := models.Setup(filepath.Join(homePath, config.C.DBFilename)); err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to initialize database")
|
||||
}
|
||||
|
||||
web.EmbedFS = embedFS
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
Loading…
Reference in New Issue
Block a user