-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
103 lines (89 loc) · 2.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type Template struct {
templates *template.Template
}
type TemplateData struct {
Env map[string]string `json:"env"`
Json string `json:"json"`
EscapedJson string `json:"escapedJson"`
}
func (t *Template) Render(w io.Writer, name string, data any, c echo.Context) error {
fmt.Printf("%+v\n", data)
return t.templates.ExecuteTemplate(w, name, data)
}
func getAppEnv() map[string]string {
prefix := getEnv("CONFIG_PREFIX", "VITE_")
data := make(map[string]string)
for _, env := range os.Environ() {
parts := strings.Split(env, "=")
key := parts[0]
value := strings.Join(parts[1:], "=")
if strings.HasPrefix(key, prefix) {
data[strings.Replace(key, prefix, "", 1)] = value
}
}
return data
}
var appEnv = getAppEnv()
func Index(c echo.Context) error {
jsonString, err := json.Marshal(appEnv)
if err != nil {
return err
}
return c.Render(http.StatusOK, "index.html", &TemplateData{
Env: appEnv,
Json: string(jsonString),
EscapedJson: strings.Replace(string(jsonString), "\"", "\\\"", -1),
})
}
func getEnv(name string, fallback string) string {
value, exists := os.LookupEnv(name)
if !exists {
value = fallback
}
return value
}
func main() {
e := echo.New()
addr := ":" + getEnv("PORT", "80")
filepath.Walk("public", func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
urlPath := strings.Replace(path, "public", "", 1)
if info.Name() == "index.html" {
indexUrlPath := strings.Replace(urlPath, "/index.html", "", 1)
if indexUrlPath == "" {
indexUrlPath = "/"
}
fmt.Println("⇨ adding index", indexUrlPath, "→", path)
e.File(indexUrlPath, path)
}
fmt.Println("⇨ adding route", urlPath, "→", path)
e.File(urlPath, path)
return nil
})
if os.Getenv("SPA_MODE") == "1" {
t := &Template{
templates: template.Must(template.ParseFiles("public/index.html")),
}
e.Renderer = t
e.GET("/*", Index)
e.GET("/", Index)
}
e.Use(middleware.Gzip())
e.Use(middleware.Logger())
e.Logger.Fatal(e.Start(addr))
}