port options
This commit is contained in:
BIN
Portifolio
BIN
Portifolio
Binary file not shown.
27
main.go
27
main.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"Portifolio/internal/shell"
|
"Portifolio/internal/shell"
|
||||||
"bufio"
|
"bufio"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -20,15 +21,29 @@ var db *sql.DB
|
|||||||
func corsMiddleware(next http.Handler) http.Handler {
|
func corsMiddleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("Incoming request: %s %s from %s", r.Method, r.URL.Path, r.Header.Get("Origin"))
|
log.Printf("Incoming request: %s %s from %s", r.Method, r.URL.Path, r.Header.Get("Origin"))
|
||||||
|
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8081")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||||
|
|
||||||
if r.Method == http.MethodOptions {
|
if r.Method == http.MethodOptions {
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// --- CLI flags ---
|
||||||
|
port := flag.String("port", "8080", "Port to listen on")
|
||||||
|
host := flag.String("host", "", "Host/IP to listen on (default: all interfaces)")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
addr := fmt.Sprintf("%s:%s", *host, *port)
|
||||||
|
|
||||||
|
// --- Database ---
|
||||||
var err error
|
var err error
|
||||||
db, err = sql.Open("sqlite3", "./app.db?_foreign_keys=on")
|
db, err = sql.Open("sqlite3", "./app.db?_foreign_keys=on")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -43,33 +58,29 @@ func main() {
|
|||||||
database.InitDB(db)
|
database.InitDB(db)
|
||||||
fmt.Println("Connected to SQLite database")
|
fmt.Println("Connected to SQLite database")
|
||||||
|
|
||||||
|
// --- Routes ---
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
mux.HandleFunc("/health", handlers.HealthHandler(db))
|
mux.HandleFunc("/health", handlers.HealthHandler(db))
|
||||||
|
|
||||||
//Trades
|
|
||||||
mux.HandleFunc("POST /trade/add", handlers.AddTradeHandler(db))
|
mux.HandleFunc("POST /trade/add", handlers.AddTradeHandler(db))
|
||||||
mux.HandleFunc("GET /trade/list", handlers.GetTradeListHandler(db))
|
mux.HandleFunc("GET /trade/list", handlers.GetTradeListHandler(db))
|
||||||
mux.HandleFunc("GET /positions/list", handlers.GetPositionListHandler(db))
|
mux.HandleFunc("GET /positions/list", handlers.GetPositionListHandler(db))
|
||||||
|
|
||||||
// Company
|
|
||||||
mux.HandleFunc("POST /company/add", handlers.AddCompanyHandler(db))
|
mux.HandleFunc("POST /company/add", handlers.AddCompanyHandler(db))
|
||||||
mux.HandleFunc("GET /company/list", handlers.GetCompaniesHandler(db))
|
mux.HandleFunc("GET /company/list", handlers.GetCompaniesHandler(db))
|
||||||
mux.HandleFunc("GET /company/revenue/categories", handlers.GetCompanyRevenueCategories(db))
|
mux.HandleFunc("GET /company/revenue/categories", handlers.GetCompanyRevenueCategories(db))
|
||||||
|
|
||||||
// Currency
|
|
||||||
mux.HandleFunc("GET /currency/list", handlers.GetCurrenciesHandler(db))
|
mux.HandleFunc("GET /currency/list", handlers.GetCurrenciesHandler(db))
|
||||||
mux.HandleFunc("POST /currency/add", handlers.AddCurrencyHandler(db))
|
mux.HandleFunc("POST /currency/add", handlers.AddCurrencyHandler(db))
|
||||||
|
|
||||||
// Revenue
|
|
||||||
mux.HandleFunc("POST /add/revenue/entry", handlers.AddRevenueEntryHandler(db))
|
mux.HandleFunc("POST /add/revenue/entry", handlers.AddRevenueEntryHandler(db))
|
||||||
mux.HandleFunc("POST /api/v1/revenue/add", handlers.AddRevenueEntryHandler(db))
|
mux.HandleFunc("POST /api/v1/revenue/add", handlers.AddRevenueEntryHandler(db))
|
||||||
|
|
||||||
//http.HandleFunc("GET /revenue/report", handlers.GetRevenueReportHandler(db))
|
// --- Start server ---
|
||||||
|
fmt.Printf("Server running on %s\n", addr)
|
||||||
fmt.Println("Server running on :8080")
|
|
||||||
go func() {
|
go func() {
|
||||||
log.Fatal(http.ListenAndServe(":8080", corsMiddleware(mux)))
|
log.Fatal(http.ListenAndServe(addr, corsMiddleware(mux)))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
runShell(db)
|
runShell(db)
|
||||||
|
|||||||
Reference in New Issue
Block a user