func main() {
	go mainHub.Run()
	document_hubs = setupDocuments()
	chat_hubs = setupChats()
	// Get hostname if specified
	args := os.Args
	host := "0.0.0.0:8080"
	if len(args) == 2 {
		host = args[1]
	}
	parts := strings.Split(host, ":")
	fmt.Println(parts)
	web.Config.Addr = parts[0]
	web.Config.Port, _ = strconv.Atoi(parts[1])
	web.Config.StaticDir = filepath.Join(os.Getenv("GOPATH"), "src/server/client/public")
	web.Get("/", home)
	web.Get("/ws", serveWs)
	web.Get("/rest/documents", getDocuments)
	web.Put("/rest/documents/(.*)", setDocumentTitle)
	web.Delete("/rest/documents/(.*)", deleteDocument)
	web.Get("/documents/(.*)", documentStream)
	web.Get("/chat/(.*)", chatStream)
	web.Run(host)

}
Example #2
0
func Routes() {

	// "Brain" stuff
	web.Get("/scoreboard", getScoreboard)
	web.Get("/publicUsers", getPublicUsers)
	web.Put("/validateFlag", domainHandler(validateFlag))

	// To be implemented ?
	web.Post("/newuser", domainHandler(newUser))
}
Example #3
0
func (srv *Server) Serve() {
	web.Get("/job/list", jobList)
	web.Get("/task/list", taskList)
	web.Get("/job/(.*)", jobGet)
	web.Post("/job", jobNew)
	web.Post("/job/run/(.*)", jobRun)
	web.Delete("/job/(.*)", jobRemove)
	web.Put("/job/(.*)", jobUpdate)

	addr, _ := globalCfg.ReadString("http_addr", ":9090")
	web.Run(addr)
}
Example #4
0
func generateResourceRoutes(resource controllers.Controllers) {
	resourceString := controllers.FriendlyName(resource)

	models.SetupTable(resourceString)

	log.Printf(" - starting /%s routes generation\n", resourceString)
	web.Get("(?i)/"+resourceString+"/?", resource.Index)
	web.Get("(?i)/"+resourceString+"/(\\d*)", resource.Show)
	web.Get("(?i)/"+resourceString+"/new/?", resource.New)
	web.Get("(?i)/"+resourceString+"/(\\d*)", resource.Edit)

	web.Post("(?i)/"+resourceString+"/?", resource.Create)
	web.Put("(?i)/"+resourceString+"/(\\d*)", resource.Update)
	web.Delete("(?i)/"+resourceString+"/(\\d*)", resource.Delete)
	log.Printf(" - finished /%s routes generation\n", resourceString)
}
Example #5
0
func routes() {
	web.Put("/", paste)  //Defined in paste.go
	web.Post("/", paste) //Defined in paste.go
	web.Get("/([a-zA-Z0-9]+)", getPaste)
	web.Get("/", index)
}
Example #6
0
func daemonLoop() {
	ch := make(chan byte)
	ProcessTaskDaemon(ch, func(t *Task) {
		glog.V(2).Infof("%s %s %sB/s %.2f%%\n", t.Id, fixedLengthName(t.TaskName, 32), t.Speed, t.Progress)
	})
	go GetTasks()

	// GET - ls, info
	// ctx.SetHeader("Access-Control-Allow-Origin", "*", true)
	web.Get("/task/([0-4]|l[cdeis])", func(ctx *web.Context, val string) {
		flusher, _ := ctx.ResponseWriter.(http.Flusher)
		defer flusher.Flush()
		var v []*Task
		var err error
		switch val {
		case "0", "ls":
			v, err = GetTasks()
		case "1", "li":
			v, err = GetIncompletedTasks()
		case "2", "lc":
			v, err = GetCompletedTasks()
		case "3", "ld":
			v, err = GetDeletedTasks()
		case "4", "le":
			v, err = GetExpiredTasks()
		default:
			ctx.WriteHeader(404)
			ctx.Write(errorMsg("INVALID TASK GROUP"))
			return
		}
		if err != nil {
			ctx.WriteHeader(503)
			ctx.Write(errorMsg(err.Error()))
			return
		}
		r, err := json.Marshal(v)
		if err != nil {
			ctx.WriteHeader(503)
			ctx.Write(errorMsg(err.Error()))
			return
		}
		ctx.SetHeader("Content-Type", "application/json", true)
		ctx.Write(r)
	})
	web.Get("/session", func(ctx *web.Context) {
		flusher, _ := ctx.ResponseWriter.(http.Flusher)
		defer flusher.Flush()
		if M.Account == nil {
			ctx.WriteHeader(404)
			ctx.Write(errorMsg("ACCOUNT INFORMATION NOT RETRIEVED"))
			return
		}
		r, err := json.Marshal(M.Account)
		if err != nil {
			ctx.WriteHeader(503)
			ctx.Write(errorMsg(err.Error()))
			return
		}
		ctx.SetHeader("Content-Type", "application/json", true)
		ctx.Write(r)
	})
	web.Get("/task/raw/([0-9]+)", func(ctx *web.Context, val string) {
		flusher, _ := ctx.ResponseWriter.(http.Flusher)
		defer flusher.Flush()
		page, err := strconv.Atoi(val)
		if err != nil {
			ctx.WriteHeader(503)
			ctx.Write(errorMsg("INVALID PAGE NUMBER"))
			return
		}
		b, err := RawTaskList(4, page)
		if err != nil {
			ctx.WriteHeader(503)
			ctx.Write(errorMsg(err.Error()))
			return
		}
		ctx.SetHeader("Content-Type", "application/json", true)
		ctx.Write(b)
	})
	web.Get("/task/bt/([0-9]+)/(.*)", func(ctx *web.Context, taskId string, taskHash string) {
		flusher, _ := ctx.ResponseWriter.(http.Flusher)
		defer flusher.Flush()
		m, err := RawFillBtList(taskId, taskHash, 1)
		if err != nil {
			ctx.WriteHeader(503)
			ctx.Write(errorMsg(err.Error()))
			return
		}
		if bytes.HasPrefix(m, []byte("fill_bt_list")) {
			ctx.SetHeader("Content-Type", "application/javascript", true)
		} else {
			ctx.SetHeader("Content-Type", "text/plain", true)
		}
		ctx.Write(m)
	})
	// POST - relogin, saveconf, loadconf, savesession
	web.Post("/session", func(ctx *web.Context) {
		unpack(ctx, func(v *payload) {
			var err error
			switch v.Action {
			case "relogin":
				if !IsOn() {
					if err = Login(conf.Id, conf.Pass); err != nil {
						ctx.WriteHeader(400)
						ctx.Write(errorMsg(err.Error()))
					} else if err = SaveSession(cookie_file); err != nil {
						ctx.Write(errorMsg(err.Error()))
					} else {
						ctx.Write(makeResponse(false, "SESSION STATUS OK"))
					}
				} else {
					ctx.Write(makeResponse(false, "SESSION STATUS OK"))
				}
			case "saveconf", "save_conf":
				conf.Pass = EncryptPass(conf.Pass)
				if _, err := conf.save(conf_file); err != nil {
					ctx.WriteHeader(400)
					ctx.Write(errorMsg(err.Error()))
					return
				}
				ctx.Write(makeResponse(false, "CONFIGURATION SAVED"))
			case "loadconf", "load_conf":
				if _, err = conf.load(conf_file); err != nil {
					ctx.WriteHeader(400)
					ctx.Write(errorMsg(err.Error()))
					return
				}
				ctx.Write(makeResponse(false, "CONFIGURATION RELOADED"))
			case "savesession", "save_session":
				if err := SaveSession(cookie_file); err != nil {
					ctx.WriteHeader(400)
					ctx.Write(errorMsg(err.Error()))
					return
				}
				ctx.Write(makeResponse(false, "SESSION COOKIE SAVED"))
			default:
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			}
		})
	})
	// POST - add, readd
	web.Post("/task", func(ctx *web.Context) {
		unpack(ctx, func(v *payload) {
			var err error
			switch v.Action {
			case "add":
				switch url := v.Data.(type) {
				case string:
					err = AddTask(url)
				case []string:
					for i, _ := range url {
						if err = AddTask(url[i]); err != nil {
							break
						}
					}
				default:
					err = fmt.Errorf("INVALID PAYLOAD DATA")
				}
				if err != nil {
					ctx.WriteHeader(400)
					ctx.Write(errorMsg(err.Error()))
					return
				}
				ctx.Write(makeResponse(false, "OK"))
			case "readd":
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			default:
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			}
		})
	})
	// PUT - delay(All), pause, resume, rename, dl, dt, ti
	web.Put("/task", func(ctx *web.Context) {
		unpack(ctx, func(v *payload) {
			glog.V(2).Infof("payload: %#v\n", v)
			var err error
			switch v.Action {
			case "delayAll":
				err = DelayAllTasks()
			case "pause":
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			case "resume":
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			case "rename":
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			case "delay":
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			case "dl", "download":
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			case "dt", "download_torent":
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			case "ti", "torrent_info":
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			default:
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			}
			if err != nil {
				ctx.WriteHeader(400)
				ctx.Write(errorMsg(err.Error()))
				return
			}
		})
	})
	// DELETE - rm, purge, GOODBYE
	web.Delete("/task", func(ctx *web.Context) {
		unpack(ctx, func(v *payload) {
			glog.V(2).Infof("payload: %#v\n", v)
			var err error
			switch v.Action {
			case "remove", "delete", "rm":
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			case "purge":
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			default:
				ctx.WriteHeader(501)
				ctx.Write(errorMsg("NOT IMPLEMENTED"))
			}
			if err != nil {
				ctx.WriteHeader(400)
				ctx.Write(errorMsg(err.Error()))
				return
			}
		})
	})
	web.Delete("/session", func(ctx *web.Context) {
		unpack(ctx, func(v *payload) {
			glog.V(2).Infof("payload: %#v\n", v)
			if v.Action == "GOODBYE" {
				ctx.Write(makeResponse(false, "GOODBYE!"))
				time.AfterFunc(time.Second, func() {
					os.Exit(0)
				})
				return
			}
			ctx.WriteHeader(405)
			ctx.Write(errorMsg("ACTION NOT ALLOWED"))
		})
	})
	web.Run("127.0.0.1:8808")
}