I am building out a simple web server in Go. My goal is to expand on this framework in a different application I am building.
Here is my github repo https://github.com/jmacnc/gowebserver
Is what I have here using best practices?
I am specifically looking for feedback on the tests, and how I am connecting to different databases (testing and main application). As seen below...
//Handler for posting a new user
func (d *MongoDB) PostUser(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
//Retrieving request body
var user User
_ = json.NewDecoder(r.Body).Decode(&user)
//Posting Company
err := d.PostUserToDB(user)
//Error Handling
if err != nil {
fmt.Println(err)
w.WriteHeader(500)
}
}
//Populating a new user in the database
func (d *MongoDB) PostUserToDB(user User) error {
_, err := d.db.Collection("users").InsertOne(context.Background(), user)
if err != nil {
return err
}
return nil
}
func main() {
//Connects to the database
d , _ := ConnectToDB()
//Creates mux router/multiplexer
r := mux.NewRouter()
//Handle Functions
r.HandleFunc("/user", d.PostUser).Methods("POST")
//Starts web server
//Prints a string saying that the app is running...
fmt.Println("Application Running...")
log.Fatal(http.ListenAndServe(LOCALHOST, r))
}
ConnectToDB
logic tho? \$\endgroup\$