247 lines
5.7 KiB
Go
247 lines
5.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// --- Config types ---
|
|
|
|
type ServerConfig struct {
|
|
Port int `yaml:"port"`
|
|
Host string `yaml:"host"`
|
|
}
|
|
|
|
type AppConfig struct {
|
|
Name string `yaml:"name"`
|
|
Description string `yaml:"description"`
|
|
}
|
|
|
|
type Config struct {
|
|
Server ServerConfig `yaml:"server"`
|
|
App AppConfig `yaml:"app"`
|
|
}
|
|
|
|
type AppEntry struct {
|
|
Name string `yaml:"name"`
|
|
Subtitle string `yaml:"subtitle"`
|
|
URL string `yaml:"url"`
|
|
Icon string `yaml:"icon"`
|
|
}
|
|
|
|
type AppsFile struct {
|
|
Apps []AppEntry `yaml:"apps"`
|
|
}
|
|
|
|
// --- Config loading ---
|
|
|
|
func loadConfig(configDir string) (*Config, error) {
|
|
path := filepath.Join(configDir, "config.yaml")
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("reading config: %w", err)
|
|
}
|
|
var cfg Config
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
return nil, fmt.Errorf("parsing config: %w", err)
|
|
}
|
|
return &cfg, nil
|
|
}
|
|
|
|
func loadApps(configDir string) ([]AppEntry, error) {
|
|
path := filepath.Join(configDir, "apps.yaml")
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return []AppEntry{}, nil
|
|
}
|
|
return nil, fmt.Errorf("reading apps: %w", err)
|
|
}
|
|
var appsFile AppsFile
|
|
if err := yaml.Unmarshal(data, &appsFile); err != nil {
|
|
return nil, fmt.Errorf("parsing apps: %w", err)
|
|
}
|
|
return appsFile.Apps, nil
|
|
}
|
|
|
|
// --- Handlers ---
|
|
|
|
func healthHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
fmt.Fprint(w, "OK")
|
|
}
|
|
|
|
func launcherHandler(cfg *Config, apps []AppEntry) http.HandlerFunc {
|
|
tmpl := template.Must(template.New("launcher").Parse(launcherHTML))
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
user := r.Header.Get("X-Forwarded-User")
|
|
|
|
data := struct {
|
|
AppName string
|
|
Description string
|
|
User string
|
|
LoggedIn bool
|
|
Apps []AppEntry
|
|
}{
|
|
AppName: cfg.App.Name,
|
|
Description: cfg.App.Description,
|
|
User: user,
|
|
LoggedIn: user != "",
|
|
Apps: apps,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
tmpl.Execute(w, data)
|
|
}
|
|
}
|
|
|
|
// --- Main ---
|
|
|
|
func main() {
|
|
configDir := os.Getenv("CONFIG_DIR")
|
|
if configDir == "" {
|
|
configDir = "/opt/nextworkspace/config/nextworkspace"
|
|
}
|
|
|
|
cfg, err := loadConfig(configDir)
|
|
if err != nil {
|
|
log.Fatalf("Failed to load config: %v", err)
|
|
}
|
|
|
|
apps, err := loadApps(configDir)
|
|
if err != nil {
|
|
log.Fatalf("Failed to load apps: %v", err)
|
|
}
|
|
|
|
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
|
|
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/health", healthHandler)
|
|
mux.HandleFunc("/", launcherHandler(cfg, apps))
|
|
|
|
log.Printf("NextWorkspace launcher listening on %s", addr)
|
|
log.Fatal(http.ListenAndServe(addr, mux))
|
|
}
|
|
|
|
const launcherHTML = `<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>{{.AppName}}</title>
|
|
<style>
|
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
background: #f0f2f5;
|
|
color: #1a1a2e;
|
|
min-height: 100vh;
|
|
}
|
|
header {
|
|
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
|
|
color: #fff;
|
|
padding: 2rem;
|
|
text-align: center;
|
|
}
|
|
header h1 { font-size: 2rem; margin-bottom: 0.25rem; }
|
|
header p { color: #a0aec0; font-size: 1rem; }
|
|
.user-banner {
|
|
background: #2d3748;
|
|
color: #e2e8f0;
|
|
padding: 0.75rem 2rem;
|
|
text-align: center;
|
|
font-size: 0.9rem;
|
|
}
|
|
.user-banner a { color: #63b3ed; text-decoration: none; margin-left: 0.5rem; }
|
|
.user-banner a:hover { text-decoration: underline; }
|
|
.grid {
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
|
|
gap: 1.5rem;
|
|
padding: 2rem;
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
}
|
|
.card {
|
|
background: #fff;
|
|
border-radius: 12px;
|
|
padding: 1.5rem;
|
|
text-align: center;
|
|
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
|
|
transition: transform 0.2s, box-shadow 0.2s;
|
|
text-decoration: none;
|
|
color: inherit;
|
|
display: block;
|
|
}
|
|
.card:hover {
|
|
transform: translateY(-4px);
|
|
box-shadow: 0 8px 24px rgba(0,0,0,0.12);
|
|
}
|
|
.icon {
|
|
width: 48px;
|
|
height: 48px;
|
|
margin: 0 auto 1rem;
|
|
background: #edf2f7;
|
|
border-radius: 12px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
font-size: 1.5rem;
|
|
}
|
|
.card h3 { font-size: 1.1rem; margin-bottom: 0.25rem; }
|
|
.card p { color: #718096; font-size: 0.85rem; }
|
|
.login-prompt {
|
|
text-align: center;
|
|
padding: 3rem 2rem;
|
|
}
|
|
.login-prompt h2 { margin-bottom: 1rem; }
|
|
.btn {
|
|
display: inline-block;
|
|
background: #1a1a2e;
|
|
color: #fff;
|
|
padding: 0.75rem 2rem;
|
|
border-radius: 8px;
|
|
text-decoration: none;
|
|
font-weight: 500;
|
|
}
|
|
.btn:hover { background: #2d3748; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<header>
|
|
<h1>{{.AppName}}</h1>
|
|
<p>{{.Description}}</p>
|
|
</header>
|
|
|
|
{{if .LoggedIn}}
|
|
<div class="user-banner">
|
|
Welcome, {{.User}} · <a href="/logout">Logout</a>
|
|
</div>
|
|
{{else}}
|
|
<div class="login-prompt">
|
|
<h2>Welcome to {{.AppName}}</h2>
|
|
<p>Sign in to access your workspace applications.</p>
|
|
<a class="btn" href="/login">Login</a>
|
|
</div>
|
|
{{end}}
|
|
|
|
{{if .LoggedIn}}
|
|
<div class="grid">
|
|
{{range .Apps}}
|
|
<a class="card" href="{{.URL}}" target="_blank" rel="noopener">
|
|
<div class="icon">{{.Icon}}</div>
|
|
<h3>{{.Name}}</h3>
|
|
<p>{{.Subtitle}}</p>
|
|
</a>
|
|
{{end}}
|
|
</div>
|
|
{{end}}
|
|
</body>
|
|
</html>`
|