mirror of
https://github.com/thomiceli/opengist.git
synced 2025-01-05 17:02:39 +00:00
Add embed Filesystem
This commit is contained in:
parent
c1ad78d984
commit
f9f5b140b8
3 changed files with 61 additions and 8 deletions
|
@ -1,11 +1,14 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var OpengistVersion = "0.0.1"
|
var OpengistVersion = "0.0.1"
|
||||||
|
@ -105,6 +108,27 @@ func InitLog() {
|
||||||
log.Logger = zerolog.New(multi).Level(level).With().Timestamp().Logger()
|
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 {
|
func GetHomeDir() string {
|
||||||
absolutePath, _ := filepath.Abs(C.OpengistHome)
|
absolutePath, _ := filepath.Abs(C.OpengistHome)
|
||||||
return filepath.Clean(absolutePath)
|
return filepath.Clean(absolutePath)
|
||||||
|
|
|
@ -3,6 +3,7 @@ package web
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
|
@ -15,7 +16,6 @@ import (
|
||||||
"opengist/internal/config"
|
"opengist/internal/config"
|
||||||
"opengist/internal/git"
|
"opengist/internal/git"
|
||||||
"opengist/internal/models"
|
"opengist/internal/models"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -79,6 +79,8 @@ var fm = template.FuncMap{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var EmbedFS embed.FS
|
||||||
|
|
||||||
type Template struct {
|
type Template struct {
|
||||||
templates *template.Template
|
templates *template.Template
|
||||||
}
|
}
|
||||||
|
@ -89,7 +91,7 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, _ echo.Con
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
store = sessions.NewCookieStore([]byte("opengist"))
|
store = sessions.NewCookieStore([]byte("opengist"))
|
||||||
parseManifestEntries()
|
assetsFS := echo.MustSubFS(EmbedFS, "public/assets")
|
||||||
|
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
e.HideBanner = true
|
e.HideBanner = true
|
||||||
|
@ -113,9 +115,8 @@ func Start() {
|
||||||
e.Use(middleware.Secure())
|
e.Use(middleware.Secure())
|
||||||
|
|
||||||
e.Renderer = &Template{
|
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) {
|
e.HTTPErrorHandler = func(er error, ctx echo.Context) {
|
||||||
if err, ok := er.(*echo.HTTPError); ok {
|
if err, ok := er.(*echo.HTTPError); ok {
|
||||||
if err.Code >= 500 {
|
if err.Code >= 500 {
|
||||||
|
@ -135,7 +136,8 @@ func Start() {
|
||||||
|
|
||||||
e.Validator = NewValidator()
|
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
|
// Web based routes
|
||||||
g1 := e.Group("")
|
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 {
|
func noRouteFound(echo.Context) error {
|
||||||
return notFound("Page not found")
|
return notFound("Page not found")
|
||||||
}
|
}
|
||||||
|
@ -307,7 +316,7 @@ type Asset struct {
|
||||||
var manifestEntries map[string]Asset
|
var manifestEntries map[string]Asset
|
||||||
|
|
||||||
func parseManifestEntries() {
|
func parseManifestEntries() {
|
||||||
file, err := os.Open("public/manifest.json")
|
file, err := EmbedFS.Open("public/manifest.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Failed to open manifest.json")
|
log.Fatal().Err(err).Msg("Failed to open manifest.json")
|
||||||
}
|
}
|
||||||
|
|
24
opengist.go
24
opengist.go
|
@ -1,9 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"opengist/internal/config"
|
"opengist/internal/config"
|
||||||
|
"opengist/internal/git"
|
||||||
"opengist/internal/models"
|
"opengist/internal/models"
|
||||||
"opengist/internal/ssh"
|
"opengist/internal/ssh"
|
||||||
"opengist/internal/web"
|
"opengist/internal/web"
|
||||||
|
@ -11,6 +14,9 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed templates/*/*.html public/manifest.json public/assets/*.js public/assets/*.css public/assets/*.svg
|
||||||
|
var embedFS embed.FS
|
||||||
|
|
||||||
func initialize() {
|
func initialize() {
|
||||||
configPath := flag.String("config", "config.yml", "Path to a config file in YML format")
|
configPath := flag.String("config", "config.yml", "Path to a config file in YML format")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
@ -25,8 +31,20 @@ func initialize() {
|
||||||
|
|
||||||
config.InitLog()
|
config.InitLog()
|
||||||
|
|
||||||
log.Info().Msg("Opengist v" + config.OpengistVersion)
|
fmt.Println("Opengist v" + config.OpengistVersion)
|
||||||
log.Info().Msg("Using config file: " + absolutePath)
|
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()
|
homePath := config.GetHomeDir()
|
||||||
log.Info().Msg("Data directory: " + homePath)
|
log.Info().Msg("Data directory: " + homePath)
|
||||||
|
@ -42,6 +60,8 @@ func initialize() {
|
||||||
if err := models.Setup(filepath.Join(homePath, config.C.DBFilename)); err != nil {
|
if err := models.Setup(filepath.Join(homePath, config.C.DBFilename)); err != nil {
|
||||||
log.Fatal().Err(err).Msg("Failed to initialize database")
|
log.Fatal().Err(err).Msg("Failed to initialize database")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
web.EmbedFS = embedFS
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
Loading…
Reference in a new issue