示例#1
0
文件: main.go 项目: synbioz/go_api
func main() {
	config.DatabaseInit()
	router := InitializeRouter()

	// Populate database
	models.NewCar(&models.Car{Manufacturer: "citroen", Design: "ds3", Style: "sport", Doors: 4})

	log.Fatal(http.ListenAndServe(":8080", router))
}
示例#2
0
文件: cars.go 项目: synbioz/go_api
func CarsCreate(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-type", "application/json;charset=UTF-8")
	w.WriteHeader(http.StatusOK)

	body, err := ioutil.ReadAll(r.Body)

	if err != nil {
		log.Fatal(err)
	}

	var car models.Car

	err = json.Unmarshal(body, &car)

	if err != nil {
		log.Fatal(err)
	}

	models.NewCar(&car)

	json.NewEncoder(w).Encode(car)
}