示例#1
0
// getHandleCRConfigFunc returns a func which handles requests to the CRConfig endpoint,
// returning the encoded CRConfig data for the requested CDN.
func getHandleCRConfigFunc(db *sqlx.DB) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		cdn := vars["cdn"]
		resp, _ := crconfig.GetCRConfig(cdn, db)
		enc := json.NewEncoder(w)
		enc.Encode(resp)
	}
}
// @Title snapshotCrconfig
// @Description create a snapshot for the given CDN
// @Accept  application/json
// @Param   name              path    string     false        "The CDN name"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/snapshot/crconfig/{cdn} [get]
func snapshotCrconfig(cdn string, db *sqlx.DB) (interface{}, error) {
	resp, err := crconfig.GetCRConfig(cdn, db)
	if err != nil {
		return nil, err
	}

	jsonBytes, err := json.Marshal(resp)
	if err != nil {
		return nil, err
	}

	log.Println(cdn)
	log.Println(string(jsonBytes))
	result, err := db.Exec("INSERT INTO crconfig_snapshots (cdn, snapshot) VALUES ($1, $2);", cdn, string(jsonBytes))
	if err != nil {
		log.Println(err)
		return nil, err
	}

	return result, err
}