Initial clean commit
This commit is contained in:
commit
6784d81c2c
141 changed files with 25219 additions and 0 deletions
27
rss-web-go/Dockerfile
Normal file
27
rss-web-go/Dockerfile
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
FROM golang:1.21-alpine AS builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Install git
|
||||
RUN apk add --no-cache git
|
||||
|
||||
# Copy files
|
||||
COPY . .
|
||||
|
||||
# Download dependencies
|
||||
RUN go mod tidy && go mod download
|
||||
|
||||
# Build
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -o rss-web .
|
||||
|
||||
# Final stage
|
||||
FROM alpine:latest
|
||||
WORKDIR /root/
|
||||
COPY --from=builder /app/rss-web .
|
||||
|
||||
# Copy static assets and templates manually for now (assuming context is root)
|
||||
# In docker-compose we will mount volumes or copy them
|
||||
# COPY templates ./templates
|
||||
# COPY static ./static
|
||||
|
||||
CMD ["./rss-web"]
|
||||
9
rss-web-go/go.mod
Normal file
9
rss-web-go/go.mod
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
module rss-web-go
|
||||
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
github.com/lib/pq v1.10.9
|
||||
github.com/joho/godotenv v1.5.1
|
||||
)
|
||||
108
rss-web-go/main.go
Normal file
108
rss-web-go/main.go
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"html/template"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
func initDB() {
|
||||
connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
|
||||
os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_USER"), os.Getenv("DB_PASS"), os.Getenv("DB_NAME"))
|
||||
|
||||
var err error
|
||||
db, err = sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
db.SetMaxOpenConns(25)
|
||||
db.SetMaxIdleConns(25)
|
||||
db.SetConnMaxLifetime(5 * time.Minute)
|
||||
|
||||
if err = db.Ping(); err != nil {
|
||||
log.Fatalf("Cannot connect to DB: %v", err)
|
||||
}
|
||||
log.Println("Connected to Database")
|
||||
}
|
||||
|
||||
// Template Functions (to replace Jinja filters)
|
||||
var funcMap = template.FuncMap{
|
||||
"safe_html": func(s string) template.HTML {
|
||||
return template.HTML(s)
|
||||
},
|
||||
"format_date": func(t time.Time) string {
|
||||
return t.Format("02/01/2006")
|
||||
},
|
||||
"country_flag": func(code string) string {
|
||||
// Placeholder logic, real logic needs country mapping
|
||||
return "🏳️"
|
||||
},
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Debug mode for now
|
||||
gin.SetMode(gin.DebugMode)
|
||||
|
||||
initDB()
|
||||
|
||||
r := gin.Default()
|
||||
|
||||
// Load Templates with FuncMap
|
||||
// We need to support the template structure.
|
||||
// For now, let's try to load them directly, but likely we need to adapt syntax.
|
||||
// r.SetFuncMap(funcMap)
|
||||
// r.LoadHTMLGlob("templates/*")
|
||||
|
||||
// Static Files
|
||||
r.Static("/static", "./static")
|
||||
|
||||
// Routes
|
||||
r.GET("/", homeHandler)
|
||||
r.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"status": "ok", "engine": "golang"})
|
||||
})
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8001"
|
||||
}
|
||||
|
||||
log.Printf("Starting Server on port %s", port)
|
||||
if err := r.Run("0.0.0.0:" + port); err != nil {
|
||||
log.Fatalf("Server failed to start: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func homeHandler(c *gin.Context) {
|
||||
// Simple query for testing connectivity
|
||||
rows, err := db.Query("SELECT titulo, url FROM noticias ORDER BY fecha DESC LIMIT 10")
|
||||
if err != nil {
|
||||
c.String(500, "DB Error: %v", err)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var news []map[string]string
|
||||
for rows.Next() {
|
||||
var titulo, url string
|
||||
if err := rows.Scan(&titulo, &url); err != nil {
|
||||
continue
|
||||
}
|
||||
news = append(news, map[string]string{"titulo": titulo, "url": url})
|
||||
}
|
||||
|
||||
// For now, return JSON to prove it works before porting the complex HTML
|
||||
c.JSON(200, gin.H{
|
||||
"message": "Welcome to RSS2 Go Web Server",
|
||||
"news": news,
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue