Пример #1
0
func showTask(response http.ResponseWriter, request *http.Request) {
	vars := mux.Vars(request)
	taskID := vars["id"]

	endpoint := Services.Storage.Endpoint + "/tasks/" + taskID

	storageResponse, err := http.Get(endpoint)
	if err != nil {
		log.Errorf("No response from endpoint '%v' (%v)", endpoint, err)
		WriteBasicResponse(http.StatusInternalServerError, response)
		return
	}

	var task Task
	defer storageResponse.Body.Close()
	decoder := json.NewDecoder(storageResponse.Body)
	err = decoder.Decode(&task)
	if err != nil {
		log.Errorf("Couldn't read body as json in response from '%v' (%v)", endpoint, err)
		WriteBasicResponse(http.StatusInternalServerError, response)
		return
	}

	taskTemplate, err := template.ParseFiles("./templates/task.html", "./templates/newtask.html")
	if err != nil {
		log.Errorf("Couldn't read task template")
		WriteBasicResponse(http.StatusInternalServerError, response)
		return
	}

	fmt.Println(task)

	taskTemplate.ExecuteTemplate(response, "task", task)
}
Пример #2
0
func deleteTask(response http.ResponseWriter, request *http.Request) {
	vars := mux.Vars(request)
	taskID := vars["id"]

	tasklist, err := storage.GetTasklist()
	if err != nil {
		log.Errorf("Couldn't get tasklist (%v)", err)
		WriteBasicResponse(http.StatusInternalServerError, response)
		return
	}

	task, ok := tasklist.Registry[taskID]
	if !ok {
		WriteBasicResponse(http.StatusNotFound, response)
		return
	}

	tasklist.Delete(task)

	err = storage.SaveTasklist(tasklist)
	if err != nil {
		log.Errorf("Couldn't save tasklist (%v)", err)
		WriteBasicResponse(http.StatusInternalServerError, response)
		return
	}

	WriteBasicResponse(http.StatusOK, response)
}
Пример #3
0
func postNewTask(response http.ResponseWriter, request *http.Request) {
	if request.Body == nil {
		log.Warn("Empty body in new task POST request")
		WriteBasicResponse(http.StatusBadRequest, response)
		return
	}

	defer request.Body.Close()
	decoder := json.NewDecoder(request.Body)

	var params NewTaskParams
	err := decoder.Decode(&params)
	if err != nil {
		log.Warn("Couldn't decode params")
		WriteBasicResponse(http.StatusBadRequest, response)
		return
	}

	tasklist, err := storage.GetTasklist()
	if err != nil {
		log.Errorf("Could not get tasklist")
		WriteBasicResponse(http.StatusInternalServerError, response)
		return
	}

	var parentTasks []*task.Task
	for _, parentID := range params.ParentIDs {
		parentTask, ok := tasklist.Registry[parentID]
		if !ok {
			log.Warnf("Couldn't find parent task (%v)", parentID)
			WriteBasicResponse(http.StatusBadRequest, response)
			return
		}

		parentTasks = append(parentTasks, parentTask)
	}

	newTask := tasklist.AddTask(params.Name, parentTasks)
	newTask.DueDate = params.DueDate
	newTask.Categories = params.Categories

	if err := storage.SaveTasklist(tasklist); err != nil {
		log.Errorf("Couldn't save tasklist (%v)", err)
		WriteBasicResponse(http.StatusInternalServerError, response)
		return
	}

	WriteJSONResponse(response, newTask, http.StatusOK)
}
Пример #4
0
func deleteFromSliceByID(tasks []*Task, taskID string) []*Task {
	taskIndex := findTaskInSlice(tasks, taskID)
	if taskIndex == -1 {
		log.Errorf("Couldn't find task ('%v') in parent subtask list, should never happen", taskID)
		return tasks
	}

	return deleteFromSliceByIndex(tasks, taskIndex)
}
Пример #5
0
func getTasks(response http.ResponseWriter, request *http.Request) {
	tasklist, err := storage.GetTasklist()
	if err != nil {
		log.Errorf("Couldn't get tasklist (%v)", err)
		WriteBasicResponse(http.StatusInternalServerError, response)
		return
	}

	WriteJSONResponse(response, tasklist.RootTasks, http.StatusOK)
}
Пример #6
0
func getConfigFilename() (configFilename string) {
	flag.Parse()

	if flag.NArg() != 1 {
		log.Errorf("Usage: loadbalancer path/to/config.json")
		os.Exit(exitUsageError)
	}

	return flag.Arg(0)
}
Пример #7
0
func (h Host) ServeHTTP(response http.ResponseWriter, request *http.Request) {
	newURL, err := url.Parse(h.Hostname + request.URL.String())
	if err != nil {
		log.Printf("Couldn't parse url (%v): %v\n", newURL, err)
		return
	}

	newRequest := &http.Request{
		URL:    newURL,
		Header: request.Header,
	}

	intermediateResponse, err := http.DefaultClient.Do(newRequest)
	if err != nil {
		log.Errorf("Had a problem with a response: %v\n", err)
		response.WriteHeader(http.StatusInternalServerError)
		return
	}

	responseBody, err := ioutil.ReadAll(intermediateResponse.Body)
	if err != nil {
		log.Errorf("Had a problem reading response body: %v\n", err)
		response.WriteHeader(http.StatusInternalServerError)
		return
	}

	// There can be multiple values for a given header key. Here I am
	// clearing any values that may pre-exist in the header and replacing
	// them with the values from the response.
	for key, values := range intermediateResponse.Header {
		response.Header().Del(key)

		for _, value := range values {
			response.Header().Add(key, value)
		}
	}

	response.WriteHeader(intermediateResponse.StatusCode)
	response.Write(responseBody)
}
Пример #8
0
func getTasks(response http.ResponseWriter, request *http.Request) {
	endpoint := Services.Storage.Endpoint + "/tasks"

	storageResponse, err := http.Get(endpoint)
	if err != nil {
		log.Errorf("No response from endpoint '%v' (%v)", endpoint, err)
		WriteBasicResponse(http.StatusInternalServerError, response)
		return
	}

	var tasks []Task
	defer storageResponse.Body.Close()
	decoder := json.NewDecoder(storageResponse.Body)
	err = decoder.Decode(&tasks)
	if err != nil {
		log.Errorf("Couldn't read body as json in response from '%v' (%v)", endpoint, err)
		WriteBasicResponse(http.StatusInternalServerError, response)
		return
	}

	WriteJSONResponse(response, tasks, http.StatusOK)
}
Пример #9
0
func main() {
	configFilename := getConfigFilename()

	conf, err := config.NewConfig(configFilename)
	if err != nil {
		log.Errorf("Trouble reading config file '%v' (%v)\n", configFilename, err)
		os.Exit(exitFailure)
	}

	log.Printf("Using config file '%v'\n\n", configFilename)
	conf.Display()

	http.Handle("/", handler.NewBalancer(conf.Hosts))

	http.ListenAndServe(conf.ListenAddress(), nil)
}
Пример #10
0
func putTask(response http.ResponseWriter, request *http.Request) {
	if request.Body == nil {
		WriteBasicResponse(http.StatusBadRequest, response)
		return
	}

	defer request.Body.Close()
	decoder := json.NewDecoder(request.Body)

	var params UpdateTaskParams
	err := decoder.Decode(&params)
	if err != nil {
		WriteBasicResponse(http.StatusBadRequest, response)
		return
	}

	vars := mux.Vars(request)
	taskID := vars["id"]

	tasklist, err := storage.GetTasklist()
	if err != nil {
		log.Errorf("Couldn't get tasklist (%v)", err)
		WriteBasicResponse(http.StatusInternalServerError, response)
		return
	}

	task, ok := tasklist.Registry[taskID]
	if !ok {
		WriteBasicResponse(http.StatusNotFound, response)
		return
	}

	// TODO: Modify the task
	if params.Name != "" {
		task.Name = params.Name
	}

	task.SetComplete(params.Complete)
	if params.Categories != nil {
		task.Categories = params.Categories
	}

	if params.DueDate != 0 {
		task.DueDate = params.DueDate
	}

	for _, subtaskID := range params.SubtaskIDs {
		subtask, ok := tasklist.Registry[subtaskID]
		if !ok {
			log.Warnf("Could not find subtask (%v)", subtaskID)
			WriteBasicResponse(http.StatusBadRequest, response)
			return
		}

		task.AddSubtask(subtask)
	}

	for _, parentID := range params.ParentIDs {
		parent, ok := tasklist.Registry[parentID]
		if !ok {
			log.Warnf("Could not find parent task (%v)", parentID)
			WriteBasicResponse(http.StatusBadRequest, response)
			return
		}

		task.AddParent(parent)
	}

	err = storage.SaveTasklist(tasklist)

	if err != nil {
		log.Errorf("Couldn't save tasklist (%v)", err)
		WriteBasicResponse(http.StatusInternalServerError, response)
		return
	}

	WriteBasicResponse(http.StatusOK, response)
}