Skip to content

Commit

Permalink
Merge pull request #38 from r7wx/fix/root-path
Browse files Browse the repository at this point in the history
v2.0.2
  • Loading branch information
r7wx authored Oct 14, 2024
2 parents 0c56dc5 + cdfca9f commit 48a703f
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 188 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,17 @@ theme:
custom_css: "/path/to/custom.css"
```
<p align="justify">
Keep in mind, even if you provide a custom css file, the background and foreground colors will still be applied as long as you provide the correct templating keys in your custom css file like so:
</p>
```css
```
body {
background: {{.Background}};
color: {{.Foreground}};
}
```


### Groups

<p align="justify">
Expand Down Expand Up @@ -370,3 +368,4 @@ A note entry is used to define a simple text note which has a title and a conten
- **EASY_GATE_CONFIG_PATH:** Easy Gate configuration file path can be provided by this environment variable. The value will have precedence over the configuration file path provided in the command line.
- **EASY_GATE_CONFIG:** Insted of providing a configuration file, it is possible to provide the entire configuration as a JSON or YAML string in this environment variable. The content of this variable will have precedence over the configuration file.
- **EASY_GATE_ROOT_PATH:** This environment variable lets you specify a custom root directory for the Easy Gate application.
6 changes: 0 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import (
"gopkg.in/yaml.v3"
)

// Easy Gate environment variable names
const (
cfgPathEnv = "EASY_GATE_CONFIG_PATH"
cfgEnv = "EASY_GATE_CONFIG"
)

// Service - Easy Gate service configuration struct
type Service struct {
Icon string `json:"icon" yaml:"icon"`
Expand Down
22 changes: 22 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ func TestGetPath(t *testing.T) {
}
}

func TestRootPath(t *testing.T) {
os.Setenv(rootPathEnv, "")
path := GetRootPath()
if path != "/" {
t.Fatal()
}

os.Setenv(rootPathEnv, "aa")
path = GetRootPath()
if path != "/aa" {
t.Fatal()
}

os.Setenv(rootPathEnv, "../../../abc")
path = GetRootPath()
if path != "/abc" {
t.Fatal()
}

os.Unsetenv(rootPathEnv)
}

func TestValidate(t *testing.T) {
cfg := Config{}
err := validateConfig(&cfg)
Expand Down
2 changes: 2 additions & 0 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
)

const cfgEnv = "EASY_GATE_CONFIG"

// Load - Load configuration from environment or file
func Load(filePath string) (*Config, string, error) {
envCfg := os.Getenv(cfgEnv)
Expand Down
22 changes: 22 additions & 0 deletions internal/config/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@ package config

import (
"fmt"
"net/url"
"os"
)

const (
cfgPathEnv = "EASY_GATE_CONFIG_PATH"
rootPathEnv = "EASY_GATE_ROOT_PATH"
)

// GetRootPath - Get root directory path from env
// or return the default value "/"
func GetRootPath() string {
rootPath := os.Getenv(rootPathEnv)
return JoinUrlPath("/", rootPath)
}

// GetConfigPath - Get the path to the configuration file
func GetConfigPath(args []string) (string, error) {
cfgFilePath := os.Getenv(cfgPathEnv)
Expand All @@ -18,3 +31,12 @@ func GetConfigPath(args []string) (string, error) {

return args[1], nil
}

// JoinUrlPath - Wrapper around url.JoinPath
func JoinUrlPath(base string, elem ...string) string {
path, err := url.JoinPath(base, elem...)
if err != nil {
url.JoinPath("/", elem...)
}
return path
}
20 changes: 13 additions & 7 deletions internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/template/html/v2"
"github.com/r7wx/easy-gate/internal/config"
"github.com/r7wx/easy-gate/internal/engine/static"
"github.com/r7wx/easy-gate/internal/engine/template"
"github.com/r7wx/easy-gate/internal/routine"
Expand Down Expand Up @@ -39,7 +40,9 @@ func (e Engine) Serve() {
TimeFormat: "2006/01/02 15:04:05",
}))

app.Get("/favicon.ico", func(c *fiber.Ctx) error {
rootPath := config.GetRootPath()

app.Get(config.JoinUrlPath(rootPath, "favicon.ico"), func(c *fiber.Ctx) error {
data, err := static.StaticFS.ReadFile("public/favicon.ico")
if err != nil {
return c.SendStatus(http.StatusNotFound)
Expand All @@ -49,7 +52,7 @@ func (e Engine) Serve() {
return c.Send(data)
})

app.Get("/roboto-regular.ttf", func(c *fiber.Ctx) error {
app.Get(config.JoinUrlPath(rootPath, "roboto-regular.ttf"), func(c *fiber.Ctx) error {
data, err := static.StaticFS.ReadFile("public/font/roboto-regular.ttf")
if err != nil {
return c.SendStatus(http.StatusNotFound)
Expand All @@ -59,7 +62,7 @@ func (e Engine) Serve() {
return c.Send(data)
})

app.Get("/style.css", func(c *fiber.Ctx) error {
app.Get(config.JoinUrlPath(rootPath, "style.css"), func(c *fiber.Ctx) error {
status, err := e.Routine.GetStatus()
if err != nil {
c.Status(http.StatusInternalServerError)
Expand All @@ -75,10 +78,11 @@ func (e Engine) Serve() {
return tmpl.Execute(c, fiber.Map{
"Background": status.Theme.Background,
"Foreground": status.Theme.Foreground,
"FontURL": config.JoinUrlPath(rootPath, "roboto-regular.ttf"),
})
})

app.Get("/", func(c *fiber.Ctx) error {
app.Get(rootPath, func(c *fiber.Ctx) error {
status, err := e.Routine.GetStatus()
if err != nil {
c.Status(http.StatusInternalServerError)
Expand All @@ -89,13 +93,15 @@ func (e Engine) Serve() {
data := getData(status, addr)

return c.Render("views/index", fiber.Map{
"Title": status.Title,
"Data": data,
"Title": status.Title,
"Data": data,
"FaviconPath": config.JoinUrlPath(rootPath, "favicon.ico"),
"StylePath": config.JoinUrlPath(rootPath, "style.css"),
})
})

app.Use(func(c *fiber.Ctx) error {
return c.Redirect("/")
return c.Redirect(rootPath)
})

if status.UseTLS {
Expand Down
Loading

0 comments on commit 48a703f

Please sign in to comment.