Skip to content

Commit

Permalink
fix: added tailwind/templates
Browse files Browse the repository at this point in the history
  • Loading branch information
henrywhitaker3 committed Apr 19, 2024
1 parent d9016a5 commit 84acfbf
Show file tree
Hide file tree
Showing 12 changed files with 1,364 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ tmp/
local/

prompage.yaml
internal/resources/static/tailwind.css
node_modules/
1 change: 1 addition & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ tasks:
serve:
desc: Run the http server
cmds:
- go generate
- go run main.go serve

docker:build:
Expand Down
10 changes: 10 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ type Service struct {
// Extras []Query `yaml:"extras"`
}

type UI struct {
Title string `yaml:"title"`
}

type Config struct {
Port int `yaml:"port"`
Services []Service `yaml:"services"`
Prometheus string `yaml:"prometheus"`
Refresh time.Duration `yaml:"refresh"`

UI UI `yaml:"ui"`
}

func Load(path string) (*Config, error) {
Expand Down Expand Up @@ -66,6 +72,10 @@ func setDefaults(conf *Config) {
if conf.Refresh == 0 {
conf.Refresh = time.Second * 30
}

if conf.UI.Title == "" {
conf.UI.Title = "PromPage"
}
}

func (c *Config) Validate() error {
Expand Down
6 changes: 6 additions & 0 deletions internal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package http
import (
"context"
"fmt"
"net/http"

"github.com/henrywhitaker3/prompage/internal/app"
"github.com/henrywhitaker3/prompage/internal/resources/static"
"github.com/labstack/echo-contrib/echoprometheus"
"github.com/labstack/echo/v4"
)
Expand All @@ -16,9 +18,13 @@ type Http struct {

func NewHttp(app *app.App) *Http {
e := echo.New()
e.HideBanner = true

e.Use(echoprometheus.NewMiddleware("prompage"))

e.GET("/", NewStatusPageHandler(app))
e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", http.FileServerFS(static.FS))))

return &Http{
e: e,
app: app,
Expand Down
32 changes: 32 additions & 0 deletions internal/http/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package http

import (
"bytes"
"html/template"
"log"
"net/http"

"github.com/henrywhitaker3/prompage/internal/app"
"github.com/henrywhitaker3/prompage/internal/config"
"github.com/henrywhitaker3/prompage/internal/resources/views"
"github.com/labstack/echo/v4"
)

func NewStatusPageHandler(app *app.App) echo.HandlerFunc {
tmpl := template.Must(template.ParseFS(views.Views, "index.html"))

return func(c echo.Context) error {
var buf bytes.Buffer
data := struct {
Config config.Config
}{
Config: *app.Config,
}
if err := tmpl.Execute(&buf, data); err != nil {
log.Printf("ERROR - could not render template: %s", err)
return err
}

return c.HTML(http.StatusOK, buf.String())
}
}
8 changes: 8 additions & 0 deletions internal/resources/static/static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package static

import "embed"

var (
//go:embed *
FS embed.FS
)
12 changes: 12 additions & 0 deletions internal/resources/views/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/tailwind.css" />
<title>{{ .Config.UI.Title }}</title>
</head>
<body>
<h1 class="text-2xl text-red-500">body here...</h1>
</body>
</html>
8 changes: 8 additions & 0 deletions internal/resources/views/views.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package views

import "embed"

var (
//go:embed *
Views embed.FS
)
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var (
configPath string
)

//go:generate npm run build

func main() {
pflag.StringVarP(&configPath, "config", "c", "prompage.yaml", "The location of the config file")

Expand Down
Loading

0 comments on commit 84acfbf

Please sign in to comment.