Skip to content

Commit

Permalink
feat: http server method
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh-98 committed Jan 24, 2024
1 parent 938aa84 commit 448fbf5
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions utils/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,22 @@ func ServerFromMux(mux http.Handler, host string) {
srv.ListenAndServe()
}()
}

func HTTPServerWriteErr(w http.ResponseWriter, code int, err error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(ToJsonBytes(map[string]string{"message": err.Error()}))
}
func HTTPServerWriteSuccess(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.Write(ToJsonBytes(map[string]interface{}{"data": data}))
}

func HTTPServerCORSMiddleware(fn func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS,PUT")
// w.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type")
fn(w, r)
}
}

0 comments on commit 448fbf5

Please sign in to comment.