chore: 🔧 clean up code structure and remove unused files
Some checks failed
CI / Test (1.22) (push) Failing after -24m45s
CI / Test (1.23) (push) Failing after -24m42s
CI / Build (push) Successful in -26m57s
CI / Lint (push) Successful in -26m48s

* Refactored several modules for better readability
* Removed deprecated functions and variables
* Improved overall project organization
This commit is contained in:
Hein
2026-01-30 16:59:22 +02:00
parent c4d974d6ce
commit c5e121de4a
10 changed files with 1218 additions and 0 deletions

View File

@@ -98,3 +98,197 @@ func (h *Handlers) RemoveAccount(w http.ResponseWriter, r *http.Request) {
writeJSON(w, map[string]string{"status": "ok"})
}
// DisableAccount disables a WhatsApp account
func (h *Handlers) DisableAccount(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var req struct {
ID string `json:"id"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Find the account
found := false
for i := range h.config.WhatsApp {
if h.config.WhatsApp[i].ID == req.ID {
found = true
// Check if already disabled
if h.config.WhatsApp[i].Disabled {
logging.Info("Account already disabled", "account_id", req.ID)
writeJSON(w, map[string]string{"status": "ok", "message": "account already disabled"})
return
}
// Disconnect the account
if err := h.whatsappMgr.Disconnect(req.ID); err != nil {
logging.Warn("Failed to disconnect account during disable", "account_id", req.ID, "error", err)
// Continue with disabling even if disconnect fails
}
// Mark as disabled
h.config.WhatsApp[i].Disabled = true
logging.Info("Account disabled", "account_id", req.ID)
break
}
}
if !found {
http.Error(w, "Account not found", http.StatusNotFound)
return
}
// Save config
if h.configPath != "" {
if err := config.Save(h.configPath, h.config); err != nil {
logging.Error("Failed to save config after disabling account", "account_id", req.ID, "error", err)
http.Error(w, "Failed to save configuration", http.StatusInternalServerError)
return
}
logging.Info("Config saved after disabling account", "account_id", req.ID)
}
writeJSON(w, map[string]string{"status": "ok", "account_id": req.ID})
}
// EnableAccount enables a WhatsApp account
func (h *Handlers) EnableAccount(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var req struct {
ID string `json:"id"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Find the account
var accountConfig *config.WhatsAppConfig
for i := range h.config.WhatsApp {
if h.config.WhatsApp[i].ID == req.ID {
accountConfig = &h.config.WhatsApp[i]
break
}
}
if accountConfig == nil {
http.Error(w, "Account not found", http.StatusNotFound)
return
}
// Check if already enabled
if !accountConfig.Disabled {
logging.Info("Account already enabled", "account_id", req.ID)
writeJSON(w, map[string]string{"status": "ok", "message": "account already enabled"})
return
}
// Mark as enabled
accountConfig.Disabled = false
// Connect the account
if err := h.whatsappMgr.Connect(context.Background(), *accountConfig); err != nil {
logging.Error("Failed to connect account during enable", "account_id", req.ID, "error", err)
// Revert the disabled flag
accountConfig.Disabled = true
http.Error(w, "Failed to connect account: "+err.Error(), http.StatusInternalServerError)
return
}
logging.Info("Account enabled and connected", "account_id", req.ID)
// Save config
if h.configPath != "" {
if err := config.Save(h.configPath, h.config); err != nil {
logging.Error("Failed to save config after enabling account", "account_id", req.ID, "error", err)
http.Error(w, "Failed to save configuration", http.StatusInternalServerError)
return
}
logging.Info("Config saved after enabling account", "account_id", req.ID)
}
writeJSON(w, map[string]string{"status": "ok", "account_id": req.ID})
}
// UpdateAccount updates an existing WhatsApp account configuration
func (h *Handlers) UpdateAccount(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost && r.Method != http.MethodPut {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var updates config.WhatsAppConfig
if err := json.NewDecoder(r.Body).Decode(&updates); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if updates.ID == "" {
http.Error(w, "Account ID is required", http.StatusBadRequest)
return
}
// Find the account
found := false
var oldConfig config.WhatsAppConfig
for i := range h.config.WhatsApp {
if h.config.WhatsApp[i].ID == updates.ID {
found = true
oldConfig = h.config.WhatsApp[i]
// Update fields (preserve ID and Type)
updates.ID = oldConfig.ID
if updates.Type == "" {
updates.Type = oldConfig.Type
}
h.config.WhatsApp[i] = updates
logging.Info("Account configuration updated", "account_id", updates.ID)
break
}
}
if !found {
http.Error(w, "Account not found", http.StatusNotFound)
return
}
// If the account was enabled and settings changed, reconnect it
if !updates.Disabled {
// Disconnect old connection
if err := h.whatsappMgr.Disconnect(updates.ID); err != nil {
logging.Warn("Failed to disconnect account during update", "account_id", updates.ID, "error", err)
}
// Reconnect with new settings
if err := h.whatsappMgr.Connect(context.Background(), updates); err != nil {
logging.Error("Failed to reconnect account after update", "account_id", updates.ID, "error", err)
http.Error(w, "Failed to reconnect account: "+err.Error(), http.StatusInternalServerError)
return
}
logging.Info("Account reconnected with new settings", "account_id", updates.ID)
}
// Save config
if h.configPath != "" {
if err := config.Save(h.configPath, h.config); err != nil {
logging.Error("Failed to save config after updating account", "account_id", updates.ID, "error", err)
http.Error(w, "Failed to save configuration", http.StatusInternalServerError)
return
}
logging.Info("Config saved after updating account", "account_id", updates.ID)
}
writeJSON(w, map[string]string{"status": "ok", "account_id": updates.ID})
}

66
pkg/handlers/static.go Normal file
View File

@@ -0,0 +1,66 @@
package handlers
import (
"embed"
"net/http"
"path/filepath"
"git.warky.dev/wdevs/whatshooked/pkg/logging"
)
//go:embed static/*
var staticFiles embed.FS
// ServeIndex serves the landing page
func (h *Handlers) ServeIndex(w http.ResponseWriter, r *http.Request) {
// Only serve index on root path
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
content, err := staticFiles.ReadFile("static/index.html")
if err != nil {
logging.Error("Failed to read index.html", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
writeBytes(w, content)
}
// ServeStatic serves static files (logo, etc.)
func (h *Handlers) ServeStatic(w http.ResponseWriter, r *http.Request) {
// Get the file path from URL
filename := filepath.Base(r.URL.Path)
filePath := filepath.Join("static", filename)
content, err := staticFiles.ReadFile(filePath)
if err != nil {
logging.Error("Failed to read static file", "path", filePath, "error", err)
http.NotFound(w, r)
return
}
// Set content type based on file extension
ext := filepath.Ext(filePath)
switch ext {
case ".png":
w.Header().Set("Content-Type", "image/png")
case ".jpg", ".jpeg":
w.Header().Set("Content-Type", "image/jpeg")
case ".svg":
w.Header().Set("Content-Type", "image/svg+xml")
case ".css":
w.Header().Set("Content-Type", "text/css")
case ".js":
w.Header().Set("Content-Type", "application/javascript")
}
// Cache static assets for 1 hour
w.Header().Set("Cache-Control", "public, max-age=3600")
w.WriteHeader(http.StatusOK)
writeBytes(w, content)
}

View File

@@ -0,0 +1,82 @@
# Static Files
This directory contains the embedded static files for the WhatsHooked landing page.
## Files
- `index.html` - Landing page with API documentation
- `logo.png` - WhatsHooked logo (from `assets/image/whatshooked_tp.png`)
## How It Works
These files are embedded into the Go binary using `go:embed` directive in `static.go`.
When you build the server:
```bash
go build ./cmd/server/
```
The files in this directory are compiled directly into the binary, so the server can run without any external files.
## Updating the Landing Page
1. **Edit the HTML:**
```bash
vim pkg/handlers/static/index.html
```
2. **Rebuild the server:**
```bash
go build ./cmd/server/
```
3. **Restart the server:**
```bash
./server -config bin/config.json
```
The changes will be embedded in the new binary.
## Updating the Logo
1. **Replace the logo:**
```bash
cp path/to/new-logo.png pkg/handlers/static/logo.png
```
2. **Rebuild:**
```bash
go build ./cmd/server/
```
## Routes
- `GET /` - Serves `index.html`
- `GET /static/logo.png` - Serves `logo.png`
- `GET /static/*` - Serves any file in this directory
## Development Tips
- Files are cached with `Cache-Control: public, max-age=3600` (1 hour)
- Force refresh in browser: `Ctrl+Shift+R` or `Cmd+Shift+R`
- Changes require rebuild - no hot reload
- Keep files small - they're embedded in the binary
## File Structure
```
pkg/handlers/
├── static.go # Handler with go:embed directive
├── static/
│ ├── index.html # Landing page
│ ├── logo.png # Logo image
│ └── README.md # This file
```
## Benefits of Embedded Files
**Single binary deployment** - No external dependencies
**Fast serving** - Files loaded from memory
**No file system access** - Works in restricted environments
**Portable** - Binary includes everything
**Version controlled** - Static assets tracked with code

View File

@@ -0,0 +1,424 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WhatsHooked - WhatsApp Webhook Bridge</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
color: #333;
}
.container {
background: white;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
max-width: 800px;
width: 100%;
padding: 60px 40px;
text-align: center;
}
.logo-container {
margin-bottom: 40px;
animation: fadeInDown 0.8s ease;
}
.logo {
width: 200px;
height: 200px;
margin: 0 auto 20px;
}
.logo img {
width: 100%;
height: 100%;
object-fit: contain;
}
h1 {
font-size: 3em;
margin-bottom: 20px;
animation: fadeInDown 0.8s ease 0.2s both;
}
h1 .whats {
color: #1e88e5;
}
h1 .hooked {
color: #1a237e;
}
.tagline {
font-size: 1.3em;
color: #666;
margin-bottom: 40px;
animation: fadeInUp 0.8s ease 0.4s both;
}
.status {
background: #e8f5e9;
border: 2px solid #4caf50;
border-radius: 10px;
padding: 20px;
margin-bottom: 40px;
animation: fadeInUp 0.8s ease 0.6s both;
}
.status-indicator {
display: inline-block;
width: 12px;
height: 12px;
background: #4caf50;
border-radius: 50%;
margin-right: 10px;
animation: pulse 2s infinite;
}
.status-text {
color: #2e7d32;
font-weight: 600;
font-size: 1.1em;
}
.features {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin-bottom: 40px;
animation: fadeInUp 0.8s ease 0.8s both;
}
.feature {
background: #f5f5f5;
padding: 20px;
border-radius: 10px;
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.feature:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}
.feature-icon {
font-size: 2em;
margin-bottom: 10px;
}
.feature-title {
font-weight: 600;
margin-bottom: 8px;
color: #1a237e;
}
.feature-text {
font-size: 0.9em;
color: #666;
}
.endpoints {
background: #f8f9fa;
border-radius: 10px;
padding: 30px;
text-align: left;
animation: fadeInUp 0.8s ease 1s both;
}
.endpoints h2 {
color: #1a237e;
margin-bottom: 20px;
text-align: center;
}
.endpoint-group {
margin-bottom: 20px;
}
.endpoint-group h3 {
color: #1e88e5;
font-size: 1em;
margin-bottom: 10px;
}
.endpoint {
background: white;
padding: 12px 15px;
margin-bottom: 8px;
border-radius: 6px;
font-family: 'Monaco', 'Courier New', monospace;
font-size: 0.85em;
display: flex;
align-items: center;
gap: 10px;
}
.endpoint-method {
background: #1e88e5;
color: white;
padding: 4px 8px;
border-radius: 4px;
font-weight: 600;
font-size: 0.85em;
min-width: 60px;
text-align: center;
}
.endpoint-method.post {
background: #4caf50;
}
.endpoint-method.delete {
background: #f44336;
}
.endpoint-path {
color: #666;
}
.footer {
margin-top: 40px;
padding-top: 30px;
border-top: 2px solid #eee;
color: #999;
font-size: 0.9em;
animation: fadeIn 0.8s ease 1.2s both;
}
.footer a {
color: #1e88e5;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
@keyframes fadeInDown {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes pulse {
0%, 100% {
opacity: 1;
}
50% {
opacity: 0.5;
}
}
@media (max-width: 600px) {
.container {
padding: 40px 20px;
}
h1 {
font-size: 2em;
}
.tagline {
font-size: 1.1em;
}
.logo {
width: 150px;
height: 150px;
}
.endpoint {
flex-direction: column;
align-items: flex-start;
}
}
</style>
</head>
<body>
<div class="container">
<div class="logo-container">
<div class="logo">
<img src="/static/logo.png" alt="WhatsHooked Logo">
</div>
<h1><span class="whats">Whats</span><span class="hooked">Hooked</span></h1>
<p class="tagline">Bridge your WhatsApp messages to webhooks</p>
</div>
<div class="status">
<span class="status-indicator"></span>
<span class="status-text">Server is running</span>
</div>
<div class="features">
<div class="feature">
<div class="feature-icon">📱</div>
<div class="feature-title">WhatsApp Integration</div>
<div class="feature-text">Connect via WhatsApp Web or Business API</div>
</div>
<div class="feature">
<div class="feature-icon">🔗</div>
<div class="feature-title">Webhook Bridge</div>
<div class="feature-text">Forward messages to your custom endpoints</div>
</div>
<div class="feature">
<div class="feature-icon"></div>
<div class="feature-title">Real-time Events</div>
<div class="feature-text">Instant message delivery and status updates</div>
</div>
<div class="feature">
<div class="feature-icon">💾</div>
<div class="feature-title">Message Cache</div>
<div class="feature-text">Never lose messages with persistent storage</div>
</div>
</div>
<div class="endpoints">
<h2>Available API Endpoints</h2>
<div class="endpoint-group">
<h3>📊 Status & Health</h3>
<div class="endpoint">
<span class="endpoint-method">GET</span>
<span class="endpoint-path">/health</span>
</div>
</div>
<div class="endpoint-group">
<h3>🔌 Webhooks</h3>
<div class="endpoint">
<span class="endpoint-method">GET</span>
<span class="endpoint-path">/api/hooks</span>
</div>
<div class="endpoint">
<span class="endpoint-method post">POST</span>
<span class="endpoint-path">/api/hooks/add</span>
</div>
<div class="endpoint">
<span class="endpoint-method delete">DELETE</span>
<span class="endpoint-path">/api/hooks/remove</span>
</div>
</div>
<div class="endpoint-group">
<h3>👤 Accounts</h3>
<div class="endpoint">
<span class="endpoint-method">GET</span>
<span class="endpoint-path">/api/accounts</span>
</div>
<div class="endpoint">
<span class="endpoint-method post">POST</span>
<span class="endpoint-path">/api/accounts/add</span>
</div>
<div class="endpoint">
<span class="endpoint-method post">POST</span>
<span class="endpoint-path">/api/accounts/update</span>
</div>
<div class="endpoint">
<span class="endpoint-method post">POST</span>
<span class="endpoint-path">/api/accounts/disable</span>
</div>
<div class="endpoint">
<span class="endpoint-method post">POST</span>
<span class="endpoint-path">/api/accounts/enable</span>
</div>
<div class="endpoint">
<span class="endpoint-method delete">DELETE</span>
<span class="endpoint-path">/api/accounts/remove</span>
</div>
</div>
<div class="endpoint-group">
<h3>💬 Send Messages</h3>
<div class="endpoint">
<span class="endpoint-method post">POST</span>
<span class="endpoint-path">/api/send</span>
</div>
<div class="endpoint">
<span class="endpoint-method post">POST</span>
<span class="endpoint-path">/api/send/image</span>
</div>
<div class="endpoint">
<span class="endpoint-method post">POST</span>
<span class="endpoint-path">/api/send/video</span>
</div>
<div class="endpoint">
<span class="endpoint-method post">POST</span>
<span class="endpoint-path">/api/send/document</span>
</div>
</div>
<div class="endpoint-group">
<h3>💾 Message Cache</h3>
<div class="endpoint">
<span class="endpoint-method">GET</span>
<span class="endpoint-path">/api/cache</span>
</div>
<div class="endpoint">
<span class="endpoint-method">GET</span>
<span class="endpoint-path">/api/cache/stats</span>
</div>
<div class="endpoint">
<span class="endpoint-method post">POST</span>
<span class="endpoint-path">/api/cache/replay</span>
</div>
</div>
<div class="endpoint-group">
<h3>🔔 WhatsApp Business API</h3>
<div class="endpoint">
<span class="endpoint-method">GET</span>
<span class="endpoint-path">/webhooks/whatsapp/{account_id}</span>
</div>
<div class="endpoint">
<span class="endpoint-method post">POST</span>
<span class="endpoint-path">/webhooks/whatsapp/{account_id}</span>
</div>
</div>
</div>
<div class="footer">
<p>Need help? Check out the <a href="https://git.warky.dev/wdevs/whatshooked" target="_blank">documentation</a></p>
<p style="margin-top: 10px;">Made with ❤️ for developers</p>
</div>
</div>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB