Files
NekoFlow/apps/server-go/main.go
2025-10-08 14:23:46 +07:00

81 lines
1.7 KiB
Go

package main
import (
"embed"
"encoding/json"
"io/fs"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
)
//go:embed static/*
var staticFiles embed.FS
type HealthResponse struct {
Status string `json:"status"`
Message string `json:"message"`
Timestamp string `json:"timestamp"`
}
func main() {
r := mux.NewRouter()
// API Routes
api := r.PathPrefix("/api").Subrouter()
api.HandleFunc("/health", healthHandler).Methods("GET")
api.HandleFunc("/cats", catsHandler).Methods("GET")
// Serve static files (Next.js build)
staticFS, err := fs.Sub(staticFiles, "static")
if err != nil {
log.Fatal(err)
}
// Serve static files
r.PathPrefix("/").Handler(http.FileServer(http.FS(staticFS)))
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("🚀 NekoFlow server starting on http://localhost:%s\n", port)
log.Printf("📱 Web UI: http://localhost:%s\n", port)
log.Printf("🔌 API: http://localhost:%s/api/health\n", port)
if err := http.ListenAndServe(":"+port, r); err != nil {
log.Fatal(err)
}
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
response := HealthResponse{
Status: "ok",
Message: "NekoFlow Go backend is running! 🐹",
Timestamp: time.Now().Format(time.RFC3339),
}
json.NewEncoder(w).Encode(response)
}
func catsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
cats := []map[string]interface{}{
{"id": 1, "name": "Whiskers", "breed": "Persian"},
{"id": 2, "name": "Shadow", "breed": "Siamese"},
{"id": 3, "name": "Luna", "breed": "British Shorthair"},
}
json.NewEncoder(w).Encode(map[string]interface{}{
"message": "Hello from Go!",
"cats": cats,
})
}