Beispiel #1
0
Datei: web.go Projekt: kevbook/hk
func getHash(w http.ResponseWriter, r *http.Request) {
	q := mux.Vars(r)
	var info jsonsha
	const s = `select sha256 from release where plat=$1 and cmd=$2 and ver=$3`
	if scan(w, r, db.QueryRow(s, q["plat"], q["cmd"], q["ver"]), &info.Sha256) {
		logErr(json.NewEncoder(w).Encode(info))
	}
}
Beispiel #2
0
Datei: web.go Projekt: kevbook/hk
func initial(w http.ResponseWriter, r *http.Request) {
	cmd := mux.Vars(r)["cmd"]
	plat := guessPlat(r.UserAgent())
	if rel, ok := lookupCurRel(w, r, plat, cmd); ok {
		url := s3DistURL + rel.Gzname()
		http.Redirect(w, r, url, http.StatusTemporaryRedirect)
	}
}
Beispiel #3
0
Datei: web.go Projekt: kevbook/hk
func putVer(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	q := mux.Vars(r)
	plat := q["plat"]
	cmd := q["cmd"]
	ver := q["ver"]
	if strings.IndexFunc(plat, badIdentRune) >= 0 ||
		strings.IndexFunc(cmd, badIdentRune) >= 0 ||
		strings.IndexFunc(ver, badVersionRune) >= 0 {
		http.Error(w, "bad character in path", 400)
		return
	}

	var info jsonsha
	if !readReqJSON(w, r, 1000, &info) {
		return
	}
	if len(info.Sha256) != sha256.Size {
		log.Printf("bad hash length %d != %d", len(info.Sha256), sha256.Size)
		http.Error(w, "unprocessable entity", 422)
		return
	}

	_, err := db.Exec(`
		insert into release (plat, cmd, ver, sha256)
		values ($1, $2, $3, $4)
	`, plat, cmd, ver, info.Sha256)
	if pe, ok := err.(pq.PGError); ok && pe.Get('C') == pgUniqueViolation {
		http.Error(w, "conflict", http.StatusConflict)
		return
	} else if err != nil {
		log.Println(err)
		http.Error(w, "internal error", 500)
		return
	}
	if _, err = db.Exec(`update mod set t=now()`); err != nil {
		log.Println(err)
		http.Error(w, "internal error", 500)
		return
	}
	w.WriteHeader(http.StatusCreated)
	io.WriteString(w, "created\n")
}
Beispiel #4
0
Datei: web.go Projekt: kevbook/hk
func setCur(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	q := mux.Vars(r)
	plat := q["plat"]
	cmd := q["cmd"]
	if strings.IndexFunc(plat, badIdentRune) >= 0 ||
		strings.IndexFunc(cmd, badIdentRune) >= 0 {
		http.Error(w, "bad character in path", 400)
		return
	}

	var info struct{ Version string }
	if !readReqJSON(w, r, 1000, &info) {
		return
	}
	_, err := db.Exec(`
		update cur set curver=$1 where plat=$2 and cmd=$3
	`, info.Version, plat, cmd)
	if err != nil {
		log.Println(err)
		http.Error(w, "internal error", 500)
		return
	}
	_, err = db.Exec(`
		insert into cur (plat, cmd, curver)
		select $1, $2, $3
		where not exists (select 1 from cur where plat=$1 and cmd=$2)
	`, plat, cmd, info.Version)
	if err != nil {
		log.Println(err)
		http.Error(w, "internal error", 500)
		return
	}
	if _, err = db.Exec(`update mod set t=now()`); err != nil {
		log.Println(err)
		http.Error(w, "internal error", 500)
		return
	}
	io.WriteString(w, "ok\n")
}
Beispiel #5
0
Datei: web.go Projekt: kevbook/hk
func curInfo(w http.ResponseWriter, r *http.Request) {
	q := mux.Vars(r)
	if rel, ok := lookupCurRel(w, r, q["plat"], q["cmd"]); ok {
		logErr(json.NewEncoder(w).Encode(rel))
	}
}