frontend
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user