feat: initial commit of mini-paas engine and documentation
This commit is contained in:
167
examples/page_teste/main.go
Normal file
167
examples/page_teste/main.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
var requestCount uint64
|
||||
|
||||
func main() {
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
appName := os.Getenv("APP_NAME")
|
||||
if appName == "" {
|
||||
appName = "page_teste"
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
count := atomic.AddUint64(&requestCount, 1)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
fmt.Fprintf(w, `
|
||||
<!DOCTYPE html>
|
||||
<html lang="pt-BR">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>App: %s</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: system-ui, -apple-system, sans-serif;
|
||||
background: #0f172a;
|
||||
color: #f8fafc;
|
||||
padding: 2rem;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
line-height: 1.6;
|
||||
}
|
||||
.card {
|
||||
background: #1e293b;
|
||||
border: 1px solid #334155;
|
||||
border-radius: 12px;
|
||||
padding: 2rem;
|
||||
margin-top: 1.5rem;
|
||||
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
|
||||
}
|
||||
h1 { color: #38bdf8; margin-bottom: 0.5rem; }
|
||||
.badge {
|
||||
background: #0284c7;
|
||||
color: #fff;
|
||||
padding: 0.2rem 0.6rem;
|
||||
border-radius: 6px;
|
||||
font-size: 0.85rem;
|
||||
font-weight: bold;
|
||||
}
|
||||
pre {
|
||||
background: #020617;
|
||||
padding: 1rem;
|
||||
border-radius: 8px;
|
||||
color: #a5f3fc;
|
||||
overflow-x: auto;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
.nav {
|
||||
margin-top: 1.5rem;
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
}
|
||||
.nav a {
|
||||
color: #38bdf8;
|
||||
text-decoration: none;
|
||||
background: #0f172a;
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 6px;
|
||||
border: 1px solid #334155;
|
||||
}
|
||||
.nav a:hover {
|
||||
background: #38bdf8;
|
||||
color: #0f172a;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<h1>🚀 Olá! Eu sou o app '%s'</h1>
|
||||
<p>Esta página está sendo servida por um binário nativo em Go gerenciado pelo <strong>Mini PaaS</strong>.</p>
|
||||
|
||||
<p style="margin-top: 1rem;">
|
||||
Status: <span class="badge">Ativo & Saudável</span>
|
||||
• Requisições atendidas: <strong>%d</strong>
|
||||
</p>
|
||||
|
||||
<h3 style="margin-top: 1.5rem; margin-bottom: 0.5rem;">Informações do Ambiente:</h3>
|
||||
<pre>
|
||||
Porta interna alocada: :%s
|
||||
Hora atual do servidor: %s
|
||||
Host da requisição: %s
|
||||
X-Forwarded-Prefix: %s
|
||||
X-Forwarded-Host: %s
|
||||
</pre>
|
||||
|
||||
<div class="nav">
|
||||
<a href="./sobre">Ir para Página Sobre (Link Relativo)</a>
|
||||
<a href="./api/health">API Healthcheck (JSON)</a>
|
||||
<a href="/">Dashboard do PaaS</a>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`, appName, appName, count, port, time.Now().Format(time.RFC3339), r.Host, r.Header.Get("X-Forwarded-Prefix"), r.Header.Get("X-Forwarded-Host"))
|
||||
})
|
||||
|
||||
mux.HandleFunc("/sobre", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
fmt.Fprintf(w, `
|
||||
<!DOCTYPE html>
|
||||
<html lang="pt-BR">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Sobre - %s</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; background: #0f172a; color: #fff; padding: 2rem; max-width: 600px; margin: 0 auto; }
|
||||
.card { background: #1e293b; padding: 2rem; border-radius: 8px; border: 1px solid #334155; }
|
||||
a { color: #38bdf8; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<h2>Página "Sobre" de Exemplo</h2>
|
||||
<p>Perceba que você navegou para <code>/sobre</code> usando um link relativo e continuou dentro do subcaminho do seu app!</p>
|
||||
<p style="margin-top: 1.5rem;"><a href="./">← Voltar para a página inicial do app</a></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`, appName)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"status": "healthy",
|
||||
"app": appName,
|
||||
"port": port,
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
})
|
||||
|
||||
log.Printf("[App %s] Iniciando servidor HTTP na porta :%s...", appName, port)
|
||||
if err := http.ListenAndServe(":"+port, mux); err != nil {
|
||||
log.Fatalf("[App %s] Erro no servidor: %v", appName, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user