This commit is contained in:
2026-04-20 20:40:48 +02:00
parent 463c391d14
commit 53dd49612d
19 changed files with 2435 additions and 4 deletions
+27 -4
View File
@@ -34,19 +34,42 @@ func New(database *db.DB, port string) (*Server, error) {
}
func (s *Server) setupRoutes() {
s.router.Use(corsMiddleware)
api := s.router.PathPrefix("/api").Subrouter()
api.HandleFunc("/health", s.handleHealth).Methods("GET")
api.HandleFunc("/health", s.handleHealth).Methods("GET", "OPTIONS")
// Settings
api.HandleFunc("/settings", s.handleGetSettings).Methods("GET")
api.HandleFunc("/settings", s.handleSaveSettings).Methods("POST")
api.HandleFunc("/settings/test/{provider}", s.handleTestKey).Methods("GET")
api.HandleFunc("/settings", s.handleGetSettings).Methods("GET", "OPTIONS")
api.HandleFunc("/settings", s.handleSaveSettings).Methods("POST", "OPTIONS")
api.HandleFunc("/settings/test/{provider}", s.handleTestKey).Methods("GET", "OPTIONS")
// Watchlist
api.HandleFunc("/watchlist", s.handleGetWatchlist).Methods("GET", "OPTIONS")
api.HandleFunc("/watchlist", s.handleAddWatchlist).Methods("POST", "OPTIONS")
api.HandleFunc("/watchlist/{ticker}", s.handleRemoveWatchlist).Methods("DELETE", "OPTIONS")
// News
api.HandleFunc("/news", s.handleGetNews).Methods("GET", "OPTIONS")
s.router.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "StockRadar API running")
})
}
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
})
}
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"status":"ok","port":"%s"}`, s.port)