Beispiel #1
0
// API: Shows the harvest schedule as currently configured
func ShowSchedule(w rest.ResponseWriter, r *rest.Request) {
	res := config.NewHypermediaResource()
	res.AddCurie("schedule", "/docs/rels/{rel}", true)

	res.Links["self"] = config.HypermediaLink{
		Href: "/schedule/read",
	}
	res.Links["schedule:add"] = config.HypermediaLink{
		Href: "/schedule/add",
	}
	res.Links["schedule:delete"] = config.HypermediaLink{
		Href:      "/schedule/delete/{id}",
		Templated: true,
	}

	jobs := []map[string]interface{}{}
	for _, item := range socialHarvest.Schedule.Cron.Entries() {
		m := make(map[string]interface{})
		m["id"] = item.Id
		m["name"] = item.Name
		m["next"] = item.Next
		m["prev"] = item.Prev
		m["job"] = getFunctionName(item.Job)
		jobs = append(jobs, m)
	}
	res.Data["totalJobs"] = len(jobs)
	res.Data["jobs"] = jobs

	res.Success()
	w.WriteJson(res.End("There are " + strconv.Itoa(len(jobs)) + " jobs scheduled."))
}
Beispiel #2
0
// Reloads the configuration from the available file on disk
func ReloadSocialHarvestConfig(w rest.ResponseWriter, r *rest.Request) {
	res := config.NewHypermediaResource()
	res.Links["self"] = config.HypermediaLink{
		Href: "/config/reload{?original}",
	}
	res.Links["read"] = config.HypermediaLink{
		Href: "/config/read",
	}
	res.Links["write"] = config.HypermediaLink{
		Href: "/config/write",
	}

	// If the original configuration (passed via --conf flag or the default social-harvest-conf.json) is desired, pass true to setConfig() by looking
	// for an "?original=true" in the route. By default, it will look for an updated config in the "sh-data" directory.
	queryParams := r.URL.Query()
	original := false
	if len(queryParams["original"]) > 0 {
		original = true
		res.Meta.Message = "Original configuration loaded."
	}
	setConfig(original)

	// Return the updated config
	res.Data["config"] = socialHarvest.Config.Harvest
	res.Success()
	w.WriteJson(res.End())
}
Beispiel #3
0
// Writes a new JSON configuration file into "sh-data" (a reload should be called afer this unless there's an error) so that the original is preserved
func WriteSocialHarvestConfig(w rest.ResponseWriter, r *rest.Request) {
	res := config.NewHypermediaResource()
	res.Links["self"] = config.HypermediaLink{
		Href: "/config/write",
	}
	res.Links["read"] = config.HypermediaLink{
		Href: "/config/read",
	}
	res.Links["reload"] = config.HypermediaLink{
		Href: "/config/reload{?original}",
	}

	// TODO: Take JSON from request and create new SocialHarvestConf struct with it.
	// Then save to disk in "sh-data" path.
	// Validate it? Aside from being able to convert it to a struct... Make sure it has a certain number of fields?

	var c = config.SocialHarvestConf{}
	err := r.DecodeJsonPayload(&c)
	if err != nil {
		//rest.Error(w, err.Error(), http.StatusInternalServerError)
		//return
		res.Meta.Message = "Invalid configuration."
		w.WriteJson(res.End())
	}
	if config.SaveConfig(c) {
		res.Success()
	}

	w.WriteJson(res.End())
}
Beispiel #4
0
// API: Shows the current harvester configuration
func ShowSocialHarvestConfig(w rest.ResponseWriter, r *rest.Request) {
	res := config.NewHypermediaResource()
	res.Links["self"] = config.HypermediaLink{
		Href: "/config/read",
	}
	res.Links["reload"] = config.HypermediaLink{
		Href: "/config/reload{?original}",
	}
	res.Links["write"] = config.HypermediaLink{
		Href: "/config/write",
	}
	res.Data["config"] = socialHarvest.Config.Harvest
	res.Success()
	w.WriteJson(res.End())
}
Beispiel #5
0
// Returns information about the currently configured database, if it's reachable, etc.
func DatabaseInfo(w rest.ResponseWriter, r *rest.Request) {
	res := config.NewHypermediaResource()
	res.Links["database:info"] = config.HypermediaLink{
		Href: "/database/info",
	}

	if socialHarvest.Database.Postgres != nil {
		res.Data["type"] = "postgres"
		// SELECT * FROM has_database_privilege('username', 'database', 'connect');
		// var r struct {
		// 	hasAccess string `db:"has_database_privilege" json:"has_database_privilege"`
		// }
		//err := socialHarvest.Database.Postgres.Get(&r, "SELECT * FROM has_database_privilege("+socialHarvest.Config.Database.User+", "+socialHarvest.Config.Database.Database+", 'connect')")
		//res.Data["r"] = r
		//res.Data["err"] = err
		res.Data["hasAccess"] = socialHarvest.Database.HasAccess()
	}

	res.Data["configuredType"] = socialHarvest.Config.Database.Type

	res.Success()
	w.WriteJson(res.End())
}
Beispiel #6
0
func (bamw *BasicAuthMw) unauthorized(writer rest.ResponseWriter) {
	writer.Header().Set("WWW-Authenticate", "Basic realm="+bamw.Realm)
	rest.Error(writer, "Not Authorized", http.StatusUnauthorized)
}
Beispiel #7
0
// API: Territory list returns all currently configured territories and their settings
func TerritoryList(w rest.ResponseWriter, r *rest.Request) {
	res := setTerritoryLinks("territory:list")
	res.Data["territories"] = socialHarvest.Config.Harvest.Territories
	res.Success()
	w.WriteJson(res.End())
}