generated from wesen/wesen-go-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add UI server for rendering YAML UI definitions
- Loading branch information
Showing
8 changed files
with
1,345 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func main() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "ui-server [directory]", | ||
Short: "Start a UI server that renders YAML UI definitions", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := context.Background() | ||
dir := args[0] | ||
port, _ := cmd.Flags().GetInt("port") | ||
|
||
server := NewServer(dir) | ||
return server.Start(ctx, port) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.Flags().IntP("port", "p", 8080, "Port to run the server on") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/fs" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type Server struct { | ||
dir string | ||
pages map[string]UIDefinition | ||
} | ||
|
||
type UIDefinition struct { | ||
Components map[string]interface{} `yaml:",inline"` | ||
} | ||
|
||
func NewServer(dir string) *Server { | ||
return &Server{ | ||
dir: dir, | ||
pages: make(map[string]UIDefinition), | ||
} | ||
} | ||
|
||
func (s *Server) Start(ctx context.Context, port int) error { | ||
if err := s.loadPages(); err != nil { | ||
return fmt.Errorf("failed to load pages: %w", err) | ||
} | ||
|
||
mux := http.NewServeMux() | ||
|
||
// Serve static files | ||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) | ||
|
||
// Index page | ||
mux.HandleFunc("/", s.handleIndex()) | ||
|
||
// Individual pages | ||
for name := range s.pages { | ||
pagePath := "/" + strings.TrimSuffix(name, ".yaml") | ||
mux.HandleFunc(pagePath, s.handlePage(name)) | ||
} | ||
|
||
addr := fmt.Sprintf(":%d", port) | ||
log.Printf("Starting server on %s", addr) | ||
return http.ListenAndServe(addr, mux) | ||
} | ||
|
||
func (s *Server) loadPages() error { | ||
return filepath.WalkDir(s.dir, func(path string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !d.IsDir() && strings.HasSuffix(d.Name(), ".yaml") { | ||
data, err := os.ReadFile(path) | ||
if err != nil { | ||
return fmt.Errorf("failed to read file %s: %w", path, err) | ||
} | ||
|
||
var def UIDefinition | ||
if err := yaml.Unmarshal(data, &def); err != nil { | ||
return fmt.Errorf("failed to parse YAML in %s: %w", path, err) | ||
} | ||
|
||
relPath, err := filepath.Rel(s.dir, path) | ||
if err != nil { | ||
return fmt.Errorf("failed to get relative path for %s: %w", path, err) | ||
} | ||
|
||
s.pages[relPath] = def | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func (s *Server) handleIndex() http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path != "/" { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
component := indexTemplate(s.pages) | ||
if err := component.Render(r.Context(), w); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
} | ||
} | ||
|
||
func (s *Server) handlePage(name string) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
def, ok := s.pages[name] | ||
if !ok { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
component := pageTemplate(name, def) | ||
if err := component.Render(r.Context(), w); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
} | ||
} |
Oops, something went wrong.