embed web files for build

This commit is contained in:
nahakubuilde
2025-08-26 19:55:28 +01:00
parent 4cafd9848f
commit 6fb6054803
4 changed files with 77 additions and 29 deletions

View File

@@ -122,7 +122,8 @@ gobsidian/
To build a standalone binary:
```bash
go build -o gobsidian cmd/main.go
go mod tidy
go build -o gobsidian ./cmd
```
## Image storing trying to follow Obsidian settings
Image storing modes:

View File

@@ -112,8 +112,20 @@ var defaultConfig = map[string]map[string]string{
},
}
// exeDir returns the directory containing the running executable.
func exeDir() string {
exe, err := os.Executable()
if err != nil {
wd, _ := os.Getwd()
return wd
}
return filepath.Dir(exe)
}
func Load() (*Config, error) {
configPath := "settings.ini"
baseDir := exeDir()
// settings.ini lives next to the executable
configPath := filepath.Join(baseDir, "settings.ini")
// Ensure config file exists
if err := ensureConfigFile(configPath); err != nil {
@@ -167,15 +179,23 @@ func Load() (*Config, error) {
config.ImageStoragePath = notesSection.Key("IMAGE_STORAGE_PATH").String()
config.ImageSubfolderName = notesSection.Key("IMAGE_SUBFOLDER_NAME").String()
// Convert relative paths to absolute
// Convert relative paths to be next to the executable
if !filepath.IsAbs(config.NotesDir) {
wd, _ := os.Getwd()
config.NotesDir = filepath.Join(wd, config.NotesDir)
config.NotesDir = filepath.Join(baseDir, config.NotesDir)
}
if !filepath.IsAbs(config.ImageStoragePath) && config.ImageStorageMode == 2 {
wd, _ := os.Getwd()
config.ImageStoragePath = filepath.Join(wd, config.ImageStoragePath)
config.ImageStoragePath = filepath.Join(baseDir, config.ImageStoragePath)
}
// Ensure these directories exist
if err := os.MkdirAll(config.NotesDir, 0o755); err != nil {
return nil, fmt.Errorf("failed to create notes directory: %w", err)
}
if config.ImageStorageMode == 2 {
if err := os.MkdirAll(config.ImageStoragePath, 0o755); err != nil {
return nil, fmt.Errorf("failed to create image storage directory: %w", err)
}
}
// Load DATABASE section
@@ -184,8 +204,7 @@ func Load() (*Config, error) {
config.DBPath = dbSection.Key("PATH").String()
if config.DBType == "sqlite" {
if !filepath.IsAbs(config.DBPath) {
wd, _ := os.Getwd()
config.DBPath = filepath.Join(wd, config.DBPath)
config.DBPath = filepath.Join(baseDir, config.DBPath)
}
// ensure parent dir exists
if err := os.MkdirAll(filepath.Dir(config.DBPath), 0o755); err != nil {
@@ -222,6 +241,10 @@ func Load() (*Config, error) {
func ensureConfigFile(configPath string) error {
// Check if file exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
// ensure parent dir exists
if err := os.MkdirAll(filepath.Dir(configPath), 0o755); err != nil {
return err
}
return createDefaultConfigFile(configPath)
}
@@ -290,7 +313,7 @@ func parseCommaSeparated(value string) []string {
}
func (c *Config) SaveSetting(section, key, value string) error {
configPath := "settings.ini"
configPath := filepath.Join(exeDir(), "settings.ini")
cfg, err := ini.Load(configPath)
if err != nil {
return err

View File

@@ -3,12 +3,15 @@ package server
import (
"fmt"
"html/template"
"io/fs"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/gorilla/sessions"
webassets "gobsidian/web"
"gobsidian/internal/auth"
"gobsidian/internal/config"
"gobsidian/internal/handlers"
@@ -173,8 +176,14 @@ func (s *Server) setupRoutes() {
}
func (s *Server) setupStaticFiles() {
s.router.Static("/static", "./web/static")
s.router.StaticFile("/favicon.ico", "./web/static/favicon.ico")
// Serve /static from embedded web/static
sub, err := fs.Sub(webassets.StaticFS, "static")
if err != nil {
panic(err)
}
s.router.StaticFS("/static", http.FS(sub))
// Favicon from same sub FS
s.router.StaticFileFS("/favicon.ico", "favicon.ico", http.FS(sub))
}
func (s *Server) setupTemplates() {
@@ -244,8 +253,12 @@ func (s *Server) setupTemplates() {
},
}
// Load templates - make sure base.html is loaded with all the other templates
templates := template.Must(template.New("").Funcs(funcMap).ParseGlob("web/templates/*.html"))
// Load templates from embedded FS
tplFS, err := fs.Sub(webassets.TemplatesFS, "templates")
if err != nil {
panic(err)
}
templates := template.Must(template.New("").Funcs(funcMap).ParseFS(tplFS, "*.html"))
s.router.SetHTMLTemplate(templates)
fmt.Printf("DEBUG: Templates loaded successfully\n")

11
web/assets_embed.go Normal file
View File

@@ -0,0 +1,11 @@
package webassets
import "embed"
// TemplatesFS embeds all HTML templates under web/templates.
//go:embed templates/*.html
var TemplatesFS embed.FS
// StaticFS embeds all static assets under web/static.
//go:embed static/*
var StaticFS embed.FS