-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
243 lines (216 loc) · 6.31 KB
/
main.go
File metadata and controls
243 lines (216 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// main.go
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/joho/godotenv"
)
// possible status values
type StatusType string
const (
StatusHealthy StatusType = "healthy"
StatusSuccess StatusType = "success"
StatusError StatusType = "error"
StatusOnline StatusType = "online"
)
// API response structure
// Standard API response structure
type APIResponse struct {
Success bool `json:"success"`
Data interface{} `json:"data,omitempty"`
Error interface{} `json:"error,omitempty"`
}
// /health (health check) endpoint
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
response := APIResponse{
Success: true,
Data: map[string]interface{}{
"message": "Server is running successfully!",
"timestamp": time.Now(),
},
}
json.NewEncoder(w).Encode(response)
}
// Handles root and unknown endpoints
func rootHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
response := APIResponse{
Success: false,
Error: map[string]interface{}{
"message": fmt.Sprintf("Endpoint '%s' does not exist", r.URL.Path),
"timestamp": time.Now(),
},
}
json.NewEncoder(w).Encode(response)
}
// /analyze/article endpoint handler
func analyzeArticleHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(APIResponse{
Success: false,
Error: "Method not allowed",
})
return
}
w.Header().Set("Content-Type", "application/json")
var req AnalyzeArticleRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(APIResponse{
Success: false,
Error: "Invalid request body",
})
return
}
result, err := AiAnalyzeArticle(req.Content, req.Title, req.URL, req.LastEdited, selectedModel)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(APIResponse{
Success: false,
Error: map[string]interface{}{"message": "AI analysis failed", "error": err.Error()},
})
return
}
json.NewEncoder(w).Encode(APIResponse{
Success: true,
Data: result,
})
}
// /analyze/text/long endpoint handler
func analyzeLongTextHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(APIResponse{
Success: false,
Error: "Method not allowed",
})
return
}
w.Header().Set("Content-Type", "application/json")
var req AnalyzeTextRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(APIResponse{
Success: false,
Error: "Invalid request body",
})
return
}
result, err := AiAnalyzeTextLong(req.Content, selectedModel)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(APIResponse{
Success: false,
Error: map[string]interface{}{"message": "AI analysis failed", "error": err.Error()},
})
return
}
json.NewEncoder(w).Encode(APIResponse{
Success: true,
Data: result,
})
}
// /analyze/text/short endpoint handler
func analyzeShortTextHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(APIResponse{
Success: false,
Error: "Method not allowed",
})
return
}
w.Header().Set("Content-Type", "application/json")
var req AnalyzeTextRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(APIResponse{
Success: false,
Error: "Invalid request body",
})
return
}
result, err := AiAnalyzeTextShort(req.Content, selectedModel)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(APIResponse{
Success: false,
Error: map[string]interface{}{"message": "AI analysis failed", "error": err.Error()},
})
return
}
json.NewEncoder(w).Encode(APIResponse{
Success: true,
Data: result,
})
}
var selectedModel Model
// CORS middleware
func withCORS(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
next(w, r)
}
}
func main() {
flag.BoolVar(&verbose, "verbose", false, "Enable verbose debug output")
flag.Parse()
http.HandleFunc("/", withCORS(rootHandler))
http.HandleFunc("/health", withCORS(healthHandler))
http.HandleFunc("/analyze/article", withCORS(analyzeArticleHandler))
http.HandleFunc("/analyze/text/short", withCORS(analyzeShortTextHandler))
http.HandleFunc("/analyze/text/long", withCORS(analyzeLongTextHandler))
err := godotenv.Load()
if err != nil {
log.Fatal("No .env file found or failed to load .env")
}
// Model selection via env file
modelEnv := strings.ToLower(os.Getenv("MODEL"))
switch modelEnv {
case "pollinations":
selectedModel = Pollinations
fmt.Println("[main] Using model: Pollinations")
case "gemini":
selectedModel = Gemini
fmt.Println("[main] Using model: Gemini")
if os.Getenv("GEMINI_API_KEY") == "" {
log.Fatal("GEMINI_API_KEY is not set in the environment. Please set it to use the Gemini model.")
}
case "":
log.Fatal("No MODEL set in env file, please set MODEL to 'Pollinations' or 'Gemini'")
default:
log.Fatal(fmt.Sprintf("[main] Unknown MODEL '%s'", modelEnv))
}
port := os.Getenv("PORT")
if port == "" {
log.Fatal("No PORT variable set in env file\n")
}
port = ":" + port
fmt.Printf("🚀 Server starting on port %s\n", port)
fmt.Printf("📡 API endpoints:\n")
fmt.Printf(" - POST /analyze/article\n")
fmt.Printf(" - POST /analyze/text/short\n")
fmt.Printf(" - POST /analyze/text/long\n")
fmt.Printf(" - GET /health\n")
fmt.Printf("\n💡 Access your server at: http://localhost%s\n", port)
if verbose {
fmt.Printf("[main] Verbose mode enabled\n")
}
// Start the server
log.Fatal(http.ListenAndServe(port, nil))
}