nitrokite/main.go
Farid Siddiqi 47be003f6d
Some checks failed
Build and Deploy API Server / deploy (push) Has been cancelled
Build and Deploy API Server / build (push) Has been cancelled
Simplify deployment structure and consolidate documentation
- Remove redundant manual setup (setup-vps.sh) - Ansible handles all VPS setup
- Remove config templates directory - Ansible creates .env.production
- Delete verbose DEPLOYMENT_SETUP.md - info consolidated into deployment/README.md
- Rewrite deployment/README.md with clear 3-step workflow
- Simplify deployment/ansible/README.md to quick reference
- Reduce total documentation from 1159 lines to 225 lines

The deployment process is now easier to understand:
1. Run Ansible playbook (one-time setup)
2. Push code to Gitea (auto-builds)
3. SSH and promote to production

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-23 17:47:50 -08:00

127 lines
2.6 KiB
Go

package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/lib/pq"
)
var (
db *sql.DB
Version string = "dev"
BuildTime string = "unknown"
)
func initDB() {
var err error
connStr := os.Getenv("DATABASE_URL")
if connStr == "" {
connStr = "host=localhost port=5432 user=apiserver password=apiserver dbname=apiserver sslmode=disable"
}
db, err = sql.Open("postgres", connStr)
if err != nil {
log.Fatal("Failed to connect to database:", err)
}
if err = db.Ping(); err != nil {
log.Fatal("Failed to ping database:", err)
}
fmt.Println("Database connected successfully!")
}
func runMigrations() {
driver, err := postgres.WithInstance(db, &postgres.Config{})
if err != nil {
log.Fatal("Failed to create migration driver:", err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://migrations",
"postgres", driver)
if err != nil {
log.Fatal("Failed to create migrate instance:", err)
}
if err := m.Up(); err != nil && err != migrate.ErrNoChange {
log.Fatal("Failed to run migrations:", err)
}
fmt.Println("Migrations completed successfully!")
}
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
func helloWorld(c *gin.Context) {
rows, err := db.Query("SELECT id, name, email, created_at, updated_at FROM users")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
defer rows.Close()
var users []User
for rows.Next() {
var user User
err := rows.Scan(&user.ID, &user.Name, &user.Email, &user.CreatedAt, &user.UpdatedAt)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
users = append(users, user)
}
c.JSON(http.StatusOK, gin.H{
"message": "Hello from PostgreSQL!",
"users": users,
})
}
func main() {
initDB()
defer db.Close()
if os.Getenv("AUTO_MIGRATE") == "true" {
runMigrations()
}
r := gin.Default()
r.GET("/hello", helloWorld)
r.GET("/version", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"version": Version,
"build_time": BuildTime,
})
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
fmt.Printf("Server starting on port %s...\n", port)
if err := r.Run(":" + port); err != nil {
log.Fatal("Failed to start server:", err)
}
}