示例#1
0
func Projects(w http.ResponseWriter, r *http.Request, s *GoautSession) {
	name := r.URL.Path[len("/projects/"):]
	if len(name) != 0 {
		r.ParseForm()
		if len(r.Form["cmd"]) == 1 {
			switch r.Form["cmd"][0] {
			case "read":
				fmt.Fprintf(w, "%s", s.GetProject(name))
			case "tasks":
				fmt.Fprintf(w, "%s", s.GetProjectTasks(name))
			case "run":
				fmt.Fprintf(w, "%s", s.ProjectRun(name))
			default:
				fmt.Fprintf(w, "{ \"Success\": false, \"Message\": \"Unknown command '%s'.\" }", r.Form["cmd"][0])
			}
		} else {
			var list ProjectBriefList
			brief := project.GetProjectBrief(filepath.Join(s.Conf.ProjectDir, name+".json"))
			list.Projects = append(list.Projects, brief)
			list.Success = true
			response, err := json.MarshalIndent(list, "", "  ")
			if err == nil {
				fmt.Fprintf(w, string(response))
			} else {
				fmt.Fprintf(w, "{ \"Success\": false, \"Message\": \"%q\" }", err)
			}
		}
	} else {
		fmt.Fprintf(w, s.GetAllProjects())
	}
}
示例#2
0
func (g *GoautSession) GetAllProjects() string {
	var response []byte
	var err error
	var list ProjectBriefList
	files, _ := ioutil.ReadDir(g.Conf.ProjectDir)
	for _, f := range files {
		if utils.ExtFromFile(f.Name()) == ".json" {
			brief := project.GetProjectBrief(filepath.Join(g.Conf.ProjectDir, f.Name()))
			list.Projects = append(list.Projects, brief)
		}
	}
	if err == nil {
		list.Success = true
		response, err = json.MarshalIndent(list, "", "  ")
	}
	if err == nil {
		return string(response)
	}
	return fmt.Sprintf("{ \"Success\": false, \"Message\": \"%q\" }", err)
}