fix(projects): fix minor changes in projects APIs Needs further updating but changed API enough to get frontend working
Brijesh ops@brijesh.dev
Fri, 28 Jun 2024 02:28:33 +0530
6 files changed,
35 insertions(+),
25 deletions(-)
D
.env
@@ -1,4 +0,0 @@
-PORT=4000 - -DB_URL=libsql://watchman-wbrijesh.turso.io -DB_TOKEN=eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MTkzMDgzMzMsImlkIjoiZDI2ZWZjOTItNGU0ZC00NDY3LWIxZjQtNTgwZjc2ZjYzOTJjIn0.4XzwBsWhSLxm1hGO307_zlBaIBZ-YhMrtwEiTIaselVFDdbA3N-14ucMjdgoeYkUBeiA3UibXTvjSkiHLaNODg
M
README.md
→
README.md
@@ -39,13 +39,13 @@
#### 4. Update Project ```sh -curl -X PUT http://127.0.0.1:4000/projects -d '{"ID": "1", "Name": "Project 1 Updated"}' +curl -X PUT http://127.0.0.1:4000/project -d '{"ID": "1", "Name": "Project 1 Updated"}' ``` #### 5. Delete Project ```sh -curl -X DELETE http://127.0.0.1:4000/projects -d '{"ID": "1"}' +curl -X DELETE http://127.0.0.1:4000/project -d '{"ID": "1"}' ``` ### Log Management API
M
internal/projects.go
→
internal/projects.go
@@ -105,24 +105,17 @@ }
} func GetProjectByID(w http.ResponseWriter, r *http.Request, db *sql.DB) { - if r.Method != http.MethodPost { + if r.Method != http.MethodGet { w.WriteHeader(http.StatusMethodNotAllowed) fmt.Fprintf(w, "Method %s not allowed", r.Method) return } - var project schema.Project - decoder := json.NewDecoder(r.Body) - err := decoder.Decode(&project) - if err != nil { - w.WriteHeader(http.StatusBadRequest) - fmt.Fprintf(w, "Error decoding project data: %v", err) - return - } + projectID := r.URL.Query().Get("id") - row := db.QueryRow("SELECT * FROM Projects WHERE ID = ?", project.ID) + row := db.QueryRow("SELECT * FROM Projects WHERE ID = ?", projectID) var projectByID schema.Project - err = row.Scan(&projectByID.ID, &projectByID.Name) + err := row.Scan(&projectByID.ID, &projectByID.Name) if err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error querying database: %v", err)
M
main.go
→
main.go
@@ -44,6 +44,10 @@ multiplexer.HandleFunc("/project", func(w http.ResponseWriter, r *http.Request) {
switch r.Method { case http.MethodGet: internal.GetProjectByID(w, r, db_connection) + case http.MethodPut: + internal.UpdateProject(w, r, db_connection) + case http.MethodDelete: + internal.DeleteProject(w, r, db_connection) default: utils.Method_Not_Allowed_Handler(w, r) }@@ -55,10 +59,6 @@ case http.MethodPost:
internal.CreateProject(w, r, db_connection) case http.MethodGet: internal.ListProjects(w, r, db_connection) - case http.MethodPut: - internal.UpdateProject(w, r, db_connection) - case http.MethodDelete: - internal.DeleteProject(w, r, db_connection) default: utils.Method_Not_Allowed_Handler(w, r) }@@ -119,8 +119,9 @@ })
fmt.Println("Starting server on " + port) log.Fatal(http.ListenAndServe(":"+port, - middleware.RequestIDMiddleware( - middleware.Ratelimit( - multiplexer, - )))) + middleware.CorsMiddleware( + middleware.RequestIDMiddleware( + middleware.Ratelimit( + multiplexer, + ))))) }
A
middleware/cors.go
@@ -0,0 +1,19 @@
+package middleware + +import "net/http" + +func CorsMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "http://localhost:3000") + w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") // Adjust as needed + w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") // Adjust as needed + + // Handle preflight requests (OPTIONS method) + if r.Method == "OPTIONS" { + w.WriteHeader(http.StatusOK) + return + } + + next.ServeHTTP(w, r) + }) +}