-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
160 lines (138 loc) · 3.66 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
)
func main() {
// Prepare the index file
if err := prepareIndex(); err != nil {
panic(err)
}
build := http.FileServer(http.Dir("build"))
handler := func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/plugins") {
// Get the file path by replacing "/plugins" with "plugins" and removing any trailing slashes
filePath := strings.TrimPrefix(r.URL.Path, "/plugins")
filePath = strings.TrimPrefix(filePath, "/")
filePath = filepath.Join("plugins", filePath)
// Check if the file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
fmt.Println("File not found: " + filePath)
http.NotFound(w, r)
return
}
http.ServeFile(w, r, filePath)
} else {
build.ServeHTTP(w, r)
}
}
// Register the request handler function
http.HandleFunc("/", handler)
// Start the server
err := http.ListenAndServe(":3000", nil)
if err != nil {
panic(err)
}
fmt.Println("Server started on port 3000")
}
type PluginsModule struct {
Id string `json:"id"`
Path string `json:"modulePath"`
}
type IndexData struct {
Plugins []PluginsModule `json:"Plugins"`
}
func prepareIndex() error {
// Read the template file
templateData, err := os.ReadFile("build/index-template.html")
if err != nil {
fmt.Errorf("Error reading template file: %s", err)
}
// Parse the template
tmpl, err := template.New("index").Parse(string(templateData))
if err != nil {
return fmt.Errorf("Error parsing template: %s", err)
}
// Create the data for the page
plugins, _ := preparePluginList()
// Create a new file to write the output to
outputFile, err := os.Create("build/index.html")
if err != nil {
return fmt.Errorf("Error creating output file: %s", err)
}
defer outputFile.Close()
// Execute the template with the data and write the output to the file
err = tmpl.Execute(outputFile, IndexData{
Plugins: plugins,
})
if err != nil {
return fmt.Errorf("Error executing template: %s", err)
}
return nil
}
func preparePluginList() ([]PluginsModule, error) {
pluginsDir := "plugins"
topLevelDirs := make(map[string]bool)
walkFunc := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
if path == pluginsDir {
return nil
}
dir := filepath.Base(path)
if _, ok := topLevelDirs[dir]; !ok {
topLevelDirs[dir] = true
}
return filepath.SkipDir
}
if err := filepath.Walk(pluginsDir, walkFunc); err != nil {
return nil, fmt.Errorf("Error walking directory: %v\n", err)
}
plugins := make([]PluginsModule, 0)
// Print the top-level directories
for dir := range topLevelDirs {
plugin, err := getPluginMeta(dir)
if err != nil {
fmt.Println(err)
continue
}
plugins = append(plugins, PluginsModule{
Id: plugin.Id,
Path: plugin.Path,
})
}
return plugins, nil
}
type Plugin struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Version string `json:"version"`
Path string `json:"-"`
}
func getPluginMeta(folderName string) (*Plugin, error) {
// Read the contents of the file
pluginPath := filepath.Join("plugins", folderName, "dist", "plugin.json")
fileBytes, err := ioutil.ReadFile(pluginPath)
if err != nil {
return nil, fmt.Errorf("Error reading file: %s", err)
}
// Unmarshal the JSON data into a Plugin struct
var plugin Plugin
err = json.Unmarshal(fileBytes, &plugin)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling JSON: %s", err)
}
plugin.Path = filepath.Join("plugins", folderName)
return &plugin, nil
}