init project structer
This commit is contained in:
80
apps/server-go/main.go
Normal file
80
apps/server-go/main.go
Normal file
@@ -0,0 +1,80 @@
|
||||
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("🚀 CatSave 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: "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,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user