コード例 #1
0
ファイル: main.go プロジェクト: kevin-cantwell/gitbao
func BaoHandler(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	base36Id := vars["id-base36"]
	fmt.Println(base36Id)
	baoId, err := strconv.ParseInt(base36Id, 36, 64)

	var bao model.Bao
	query := model.DB.Find(&bao, baoId)
	if query.Error == gorm.RecordNotFound {
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("404"))
		return
	} else if query.Error != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	if bao.GitPullUrl == "" {

		// go func() {
		err = github.GetGistData(&bao)
		if err != nil {
			fmt.Printf("%#v", bao)
			w.WriteHeader(http.StatusInternalServerError)
			w.Write([]byte(err.Error()))
			return
		}

		bao.Console = "Welcome to gitbao!!\n" +
			"Getting ready to wrap up a tasty new bao.\n"

		bao.Console += "Found some files:\n"

		var isGo bool
		for _, value := range bao.Files {
			bao.Console += "    " + value.Filename + "\n"
			if value.Language == "Go" {
				isGo = true
			}
			if value.Filename == "Baofile" || value.Filename == "baofile" {
				bao.BaoFileUrl = value.RawUrl
			}
		}

		if isGo != true {
			bao.Console += "Whoops!\n" +
				"gitbao only supports Go programs at the moment.\n" +
				"Quitting...."
			bao.IsComplete = true
		} else {
			bao.Console += "Nice, looks like we can deploy your application\n" +
				"Modify your config file if needed and hit deploy!\n"
		}

		if bao.IsComplete != true {
			go func() {
				var server model.Server
				query = model.DB.Where("kind = ?", "xiaolong").Find(&server)
				if query.Error != nil {
					bao.Console += "Uh oh, we've experienced an error. Please try again.\n"
					fmt.Println(query.Error)
					model.DB.Save(&bao)
					return
				}
				bao.ServerId = server.Id
				model.DB.Save(&bao)
				getUrl := fmt.Sprintf("http://%s:8002/ready/%d", server.Ip, bao.Id)
				log.Println(getUrl)
				resp, err := http.Get(getUrl)
				log.Printf("%#v", resp)
				if err != nil || resp.StatusCode != 200 {
					bao.Console += "Uh oh, we've experienced an error. Please try again.\n"
					bao.IsComplete = true
					model.DB.Save(&bao)
					return
				}
			}()
		}
	}

	query = model.DB.Save(&bao)
	if query.Error != nil {
		fmt.Printf("%#v", bao)
		panic(query.Error)
	}
	RenderTemplate(w, "bao", bao)

}