Example #1
0
/**
 * attach all api endpoints
 */
func SetupApiEndpoints(m *martini.ClassicMartini) {
	m.Get("/api/:project", GetProject)
	m.Put("/api/:project", PutProject)
	m.Delete("/api/:project", DeleteProject)
	m.Get("/api/:project/translation/:key", GetTranslation)
	m.Put("/api/:project/translation/:key/:locale", PutLocale)
	m.Delete("/api/:project/translation/:key/:locale", DeleteLocale)
	m.Get("/api/:project/file/:locale", GetLanguageFile)
	m.Put("/api/:project/file/:locale", PutLanguageFile)
}
Example #2
0
func RegisterWebService(server *martini.ClassicMartini) {
	path := "/api/v1/keys"

	server.Get(path, Get)
	//server.Get(path+"/:email", Get)

	server.Post(path, Post)

	server.Delete(path, Delete)
	server.Delete(path+"/:id", Delete)
}
Example #3
0
File: user.go Project: xrstf/raziel
func setupUsersCtrl(app *martini.ClassicMartini) {
	app.Group("/users", func(r martini.Router) {
		app.Get("", usersIndexAction)
		app.Get("/add", usersAddAction)
		app.Post("", sessions.RequireCsrfToken, usersCreateAction)
		app.Get("/:id", usersEditAction)
		app.Put("/:id", sessions.RequireCsrfToken, usersUpdateAction)
		app.Delete("/:id", sessions.RequireCsrfToken, usersDeleteAction)
		app.Get("/:id/delete", usersDeleteConfirmAction)
	}, sessions.RequireLogin)
}
Example #4
0
func registerRoutes(server *martini.ClassicMartini) {
	// Box
	server.Post("/boxes", binding.Bind(request.CreateRequest{}), boxCreate)
	server.Put("/boxes/:name", binding.Bind(request.DeployRequest{}), boxDeploy)
	server.Get("/boxes", boxList)
	server.Get("/boxes/:name", boxInspect)
	server.Delete("/boxes/:name", boxDelete)

	// Domain
	server.Post("/boxes/:name/domain", binding.Bind(request.Domain{}), domainAdd)
	server.Delete("/boxes/:name/domain/:domain", domainDelete)
}
Example #5
0
func RegisterRESTFunction(f RESTFunction, cm *martini.ClassicMartini) {
	path := f.GetPath()

	cm.Get(path, f.RGet)
	cm.Get(path+"/:id", f.RGet)

	cm.Post(path, f.RPost)
	cm.Post(path+"/:id", f.RPost)

	cm.Delete(path, f.RDelete)
	cm.Delete(path+"/:id", f.RDelete)
}
// RegisterWebService adds martini routes to the relevant webservice methods
// based on the path returned by GetPath. Each method is registered once for
// the collection and once for each id in the collection.
func RegisterWebService(webService WebService,
	classicMartini *martini.ClassicMartini) {
	path := webService.GetPath()

	classicMartini.Get(path, webService.WebGet)
	classicMartini.Get(path+"/:id", webService.WebGet)

	classicMartini.Post(path, webService.WebPost)
	classicMartini.Post(path+"/:id", webService.WebPost)

	classicMartini.Delete(path, webService.WebDelete)
	classicMartini.Delete(path+"/:id", webService.WebDelete)
}
Example #7
0
func addLatestNewsRoutes(m *martini.ClassicMartini) {
	m.Get("/latest_news", ShowLatestNewsCollection)

	m.Get("/latest_news/:id", EditLatestNews)

	m.Post("/latest_news", CreateLatestNews)
	m.Post("/latest_news/new", CreateLatestNews)

	m.Put("/latest_news/:id", UpdateLatestNews)
	m.Post("/latest_news/:id", UpdateLatestNews)

	m.Delete("/latest_news/:id", DeleteLatestNews)
	m.Get("/latest_news/del/:id", DeleteLatestNews)
}
Example #8
0
func addComponentRoutes(m *martini.ClassicMartini) {
	m.Get("/component", ShowComponents)

	m.Get("/component/:id", EditComponent)

	m.Post("/component", CreateComponent)
	m.Post("/component/new", CreateComponent)

	m.Put("/component/:id", UpdateComponent)
	m.Post("/component/:id", UpdateComponent)

	m.Delete("/component/:id", DeleteComponent)
	m.Get("/component/del/:id", DeleteComponent)
}
Example #9
0
func addInvertebrateRoutes(m *martini.ClassicMartini) {
	m.Get("/invertebrate", ShowInvertebrates)

	m.Get("/invertebrate/:id", EditInvertebrate)

	m.Post("/invertebrate", CreateInvertebrate)
	m.Post("/invertebrate/new", CreateInvertebrate)

	m.Put("/invertebrate/:id", UpdateInvertebrate)
	m.Post("/invertebrate/:id", UpdateInvertebrate)

	m.Delete("/invertebrate/:id", DeleteInvertebrate)
	m.Get("/invertebrate/del/:id", DeleteInvertebrate)
}
Example #10
0
func addCoralRoutes(m *martini.ClassicMartini) {
	m.Get("/coral", ShowCorals)

	m.Get("/coral/:id", EditCoral)

	m.Post("/coral", CreateCoral)
	m.Post("/coral/new", CreateCoral)

	m.Put("/coral/:id", UpdateCoral)
	m.Post("/coral/:id", UpdateCoral)

	m.Delete("/coral/:id", DeleteCoral)
	m.Get("/coral/del/:id", DeleteCoral)
}
Example #11
0
func addPlantRoutes(m *martini.ClassicMartini) {
	m.Get("/plant", ShowPlants)

	m.Get("/plant/:id", EditPlant)

	m.Post("/plant", CreatePlant)
	m.Post("/plant/new", CreatePlant)

	m.Put("/plant/:id", UpdatePlant)
	m.Post("/plant/:id", UpdatePlant)

	m.Delete("/plant/:id", DeletePlant)
	m.Get("/plant/del/:id", DeletePlant)
}
Example #12
0
func addInhabitantRoutes(m *martini.ClassicMartini) {
	m.Get("/inhabitant", ShowInhabitants)

	m.Get("/inhabitant/:id", EditInhabitant)

	m.Post("/inhabitant", CreateInhabitant)
	m.Post("/inhabitant/new", CreateInhabitant)

	m.Put("/inhabitant/:id", UpdateInhabitant)
	m.Post("/inhabitant/:id", UpdateInhabitant)

	m.Delete("/inhabitant/:id", DeleteInhabitant)
	m.Get("/inhabitant/del/:id", DeleteInhabitant)
}
Example #13
0
func setupConsumersCtrl(app *martini.ClassicMartini) {
	app.Group("/consumers", func(r martini.Router) {
		app.Get("", consumersIndexAction)
		app.Get("/add", consumersAddAction)
		app.Post("", sessions.RequireCsrfToken, consumersCreateAction)
		app.Get("/:id", consumersEditAction)
		app.Put("/:id", sessions.RequireCsrfToken, consumersUpdateAction)
		app.Delete("/:id", sessions.RequireCsrfToken, consumersDeleteAction)
		app.Get("/:id/delete", consumersDeleteConfirmAction)
		app.Get("/:id/urls", consumersUrlsAction)
	}, sessions.RequireLogin)

	// public
	app.Get("/info/:consumer/:token", consumerInfoAction)
}
Example #14
0
func SetupAuth(martini *martini.ClassicMartini) {
	os.Mkdir(backendfile, 0755)
	backend_, err := httpauth.NewLeveldbAuthBackend(backendfile)
	if err != nil {
		log.Fatal(err.Error())
	}
	backend = backend_

	roles = make(map[string]httpauth.Role)
	roles["user"] = 30
	roles["admin"] = 80

	aaa, err = httpauth.NewAuthorizer(backend, []byte("cookie-encryption-key"), "user", roles)
	if err != nil {
		log.Fatal(err.Error())
	}

	users, err := backend.Users()
	if err != nil || len(users) == 0 {
		// create a default user
		hash, err := bcrypt.GenerateFromPassword([]byte("toor"), bcrypt.DefaultCost)
		//hash, err := bcrypt.GenerateFromPassword([]byte("l4ngu4g3"), bcrypt.DefaultCost)
		if err != nil {
			panic(err)
		}
		defaultUser := httpauth.UserData{Username: "******", Email: "", Hash: hash, Role: "admin"}
		//defaultUser := httpauth.UserData{Username: "******", Email: "", Hash: hash, Role: "admin"}
		err = backend.SaveUser(defaultUser)
		if err != nil {
			panic(err)
		}
	}

	martini.Post("/auth/login", doLogin)
	martini.Get("/auth/logout", doLogout)
	martini.Get("/auth/token", doGetToken)

	martini.Post("/auth/user", AssertRole("admin"), doAddUser)

	martini.Get("/auth/currentuser", doGetCurrentUser)
	martini.Get("/auth/user", AssertRole("admin"), doGetUsers)
	martini.Get("/auth/user/:username", AssertRole("admin"), doGetUser)

	martini.Put("/auth/user/:username", AssertRole("admin"), doUpdateUser)
	martini.Delete("/auth/user/:username", AssertRole("admin"), doDeleteUser)
	martini.Post("/auth/user/:username/allow", AssertRole("admin"), doAllow)
	martini.Post("/auth/user/:username/forbid", AssertRole("admin"), doForbid)

}
Example #15
0
func api(mart *martini.ClassicMartini) {
	var engine = backend.GetEngine()
	mart.Post(API+"/datasets/?", binding.Bind(DatasetForm{}), func(form DatasetForm, r render.Render) {
		var err error
		var file *backend.File
		var tag *backend.Tag
		var dataset *backend.Dataset

		if form.FileId > 0 {
			file = &backend.File{Id: form.FileId}
			if has, _ := engine.Get(file); !has {
				r.JSON(http.StatusOK, map[string]interface{}{"err": "file not exists."})
				return
			}
		} else {
			if file, err = backend.UploadFile(form.File); err != nil {
				r.JSON(http.StatusInternalServerError, map[string]interface{}{"err": err.Error()})
				return
			}
		}

		if tag, err = backend.SaveTag(form.Tag); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]interface{}{"err": err.Error()})
			return
		}

		if dataset, err = backend.SaveDataset(file, tag, form.DataType, form.Description); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]interface{}{"err": err.Error()})
			return
		}

		r.JSON(http.StatusOK, map[string]*backend.Dataset{"dataset": dataset})
	})

	mart.Post(API+"/datasets/(?P<dataset_id>\\d+)/?", func(req *http.Request, params martini.Params, r render.Render) {
		datasetId, _ := strconv.Atoi(params["dataset_id"])
		var dataset = new(backend.Dataset)
		if has, err := engine.Id(datasetId).Get(dataset); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		} else if has {
			dataset.FillObject()
			dataset.Description = req.Form.Get("description")
			if _, err := engine.Id(datasetId).Update(dataset); err != nil {
				r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
				return
			}
			r.JSON(http.StatusOK, map[string]*backend.Dataset{"dataset": dataset})
		} else {
			r.JSON(http.StatusNotFound, map[string]string{"err": "Dataset not exists."})
		}
	})

	mart.Get(API+"/datasets/(?P<dataset_id>\\d+)/?", func(params martini.Params, r render.Render) {
		datasetId, _ := strconv.Atoi(params["dataset_id"])
		var dataset = new(backend.Dataset)
		if has, err := engine.Id(datasetId).Get(dataset); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		} else if has {
			dataset.FillObject()
			r.JSON(http.StatusOK, map[string]*backend.Dataset{"dataset": dataset})
		} else {
			r.JSON(http.StatusNotFound, map[string]string{"err": "Dataset not exists."})
		}
	})

	mart.Delete(API+"/datasets/(?P<dataset_id>\\d+)/?", func(params martini.Params, r render.Render) {
		datasetId, _ := strconv.Atoi(params["dataset_id"])
		var dataset = new(backend.Dataset)
		if has, err := engine.Id(datasetId).Get(dataset); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		} else if has {
			if _, err := engine.Id(datasetId).Delete(dataset); err != nil {
				r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			} else {
				var sql string
				if dataset.DataType == backend.TRAIN {
					sql = "update `tag` set `train_count` = `train_count` - 1 where `id` = ?"
				} else if dataset.DataType == backend.VAL {
					sql = "update `tag` set `test_count` = `test_count` - 1 where `id` = ?"
				}
				engine.Exec(sql, dataset.TagId)
				r.JSON(http.StatusOK, map[string]string{})
			}
		} else {
			r.JSON(http.StatusNotFound, map[string]string{"err": "Dataset not exists."})
		}

	})

	mart.Get(API+"/datasets/?", func(req *http.Request, r render.Render) {
		var qs = req.URL.Query()
		var err error
		var max, limit int
		if max, err = strconv.Atoi(qs.Get("max")); err != nil {
			max = -1
		}

		if limit, err = strconv.Atoi(qs.Get("limit")); err != nil {
			limit = 10
		}

		if limit > 100 {
			limit = 100
		}

		var dataType = qs.Get("data_type")

		var tagName = qs.Get("tag")

		var datasets = make([]backend.Dataset, 0)
		var q = engine.Desc("id")
		if max > -1 {
			q = q.Where("id < ?", max)
		}

		if tagName != "" {
			tag := &backend.Tag{Name: tagName}
			has, _ := engine.Get(tag)
			if !has {
				r.JSON(http.StatusNotFound,
					map[string]string{"err": "tag: " + tagName + " not found."})
				return
			}
			q = q.And("tag_id = ?", tag.Id)
		}

		if dataType == "train" {
			q = q.And("data_type = ?", backend.TRAIN)
		} else if dataType == "val" {
			q = q.And("data_type = ?", backend.VAL)
		}

		q = q.Limit(limit)
		if err = q.Find(&datasets); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		}

		for idx, dataset := range datasets {
			dataset.FillObject()
			datasets[idx] = dataset
		}

		r.JSON(http.StatusOK, map[string][]backend.Dataset{"datasets": datasets})
	})

	mart.Post(API+"/upload/?", binding.Bind(FileForm{}), func(form FileForm, r render.Render) {
		var err error
		var file *backend.File

		if file, err = backend.UploadFile(form.File); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]interface{}{"err": err.Error()})
			return
		}

		r.JSON(http.StatusOK, map[string]*backend.File{"file": file})
	})

	mart.Post(API+"/tags/?", binding.Bind(TagForm{}), func(form TagForm, r render.Render) {
		var err error
		var tag *backend.Tag

		if tag, err = backend.SaveTag(form.Tag); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]interface{}{"err": err.Error()})
		}

		r.JSON(http.StatusOK, map[string]*backend.Tag{"file": tag})
	})

	mart.Post(API+"/tags/(?P<tag_id>\\d+)/?", binding.Bind(TagForm{}), func(form TagForm, params martini.Params, r render.Render) {
		tagId, _ := strconv.Atoi(params["tag_id"])
		var tag = new(backend.Tag)
		if has, err := engine.Id(tagId).Get(tag); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		} else if has {
			if tag.Name != form.Tag {
				tag.Name = form.Tag
				if _, err := engine.Id(tagId).Update(tag); err != nil {
					r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
					return
				}
			}
			r.JSON(http.StatusOK, map[string]*backend.Tag{"tag": tag})
		} else {
			r.JSON(http.StatusNotFound, map[string]string{"err": "Tag not exists."})
		}
	})

	mart.Get(API+"/tags/(?P<tag_id>\\d+)/?", func(params martini.Params, r render.Render) {
		tagId, _ := strconv.Atoi(params["tag_id"])
		var tag = new(backend.Tag)
		if has, err := engine.Id(tagId).Get(tag); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		} else if has {
			r.JSON(http.StatusOK, map[string]*backend.Tag{"tag": tag})
		} else {
			r.JSON(http.StatusNotFound, map[string]string{"err": "Tag not exists."})
		}
	})

	mart.Delete(API+"/tags/(?P<tag_id>\\d+)/?", func(params martini.Params, r render.Render) {
		tagId, _ := strconv.Atoi(params["tag_id"])
		var tag = new(backend.Tag)
		if has, err := engine.Id(tagId).Get(tag); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		} else if has {
			backend.DeleteTag(tag.Id)
			r.JSON(http.StatusOK, map[string]*backend.Tag{"tag": tag})
		} else {
			r.JSON(http.StatusNotFound, map[string]string{"err": "Tag not exists."})
		}
	})

	mart.Get(API+"/tags/hint/?", func(req *http.Request, r render.Render) {
		var qs = req.URL.Query()
		var word = qs.Get("word")
		var q = engine.Desc("id")
		q = q.And("name like \"%" + word + "%\"")
		q = q.Limit(5)
		var tags = make([]backend.Tag, 0)
		var err = q.Find(&tags)
		log.Printf("err: %s\n", err)
		r.JSON(http.StatusOK, map[string][]backend.Tag{"tags": tags})
	})

	mart.Get(API+"/tags/?", func(req *http.Request, r render.Render) {
		var qs = req.URL.Query()
		var err error
		var max, limit int
		if max, err = strconv.Atoi(qs.Get("max")); err != nil {
			max = -1
		}

		if limit, err = strconv.Atoi(qs.Get("limit")); err != nil {
			limit = 10
		}

		if limit > 100 {
			limit = 100
		}

		var tags = make([]backend.Tag, 0)
		var q = engine.Desc("id")
		if max > -1 {
			q = q.Where("id < ?", max)
		}
		q = q.Limit(limit)
		if err = q.Find(&tags); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		}

		r.JSON(http.StatusOK, map[string][]backend.Tag{"tags": tags})
	})

	mart.Post(API+"/solve/?", func(r render.Render) {
		caffe.Solve()
		r.JSON(http.StatusOK, caffe.LastStatus())
		return
	})

	mart.Get(API+"/solve/?", func(r render.Render) {
		r.JSON(http.StatusOK, caffe.LastStatus())
		return
	})

	mart.Delete(API+"/solve/?", func(r render.Render) {
		caffe.StopSolve()
		r.JSON(http.StatusOK, caffe.LastStatus())
		return
	})

	mart.Get(API+"/train.txt", func(r render.Render) {
		text, err := backend.ExportDataset(backend.TRAIN)
		if err != nil {
			r.Data(http.StatusInternalServerError, nil)
			return
		}
		r.Data(http.StatusOK, []byte(text))
	})

	mart.Get(API+"/val.txt", func(r render.Render) {
		text, err := backend.ExportDataset(backend.VAL)
		if err != nil {
			r.Data(http.StatusInternalServerError, nil)
			return
		}
		r.Data(http.StatusOK, []byte(text))
	})

	mart.Get(API+"/loss.png", func(r render.Render) {
		result, err := caffe.Plot("loss")
		if err != nil {
			r.Redirect("/static/images/loading.png")
			return
		}
		r.Data(http.StatusOK, result)
		return
	})

	mart.Get(API+"/acc.png", func(r render.Render) {
		result, err := caffe.Plot("acc")
		if err != nil {
			r.Redirect("/static/images/loading.png")
			return
		}
		r.Data(http.StatusOK, result)
		return
	})

	mart.Post(API+"/predict/?", binding.Bind(PredictForm{}), func(form PredictForm, r render.Render) {
		var err error
		var result caffe.PredictResult
		if len(form.ImgUrl) > 0 {
			result, err = caffe.PredictUrl(form.ImgUrl)
		} else {
			var source multipart.File
			if source, err = form.File.Open(); err == nil {
				defer source.Close()
				result, err = caffe.Predict(source)
			}
		}
		if err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, result)
		return
	})

	mart.Post(API+"/predict/process/?", func(r render.Render) {
		if err := caffe.StartPredict(); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, map[string]string{})
		return
	})

	mart.Delete(API+"/predict/process/?", func(r render.Render) {
		if err := caffe.StopPredict(); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, map[string]string{})
		return
	})

	mart.Get(API+"/proxy/?", func(req *http.Request, r render.Render) {
		var qs = req.URL.Query()
		var url = qs.Get("url")
		var resp *http.Response
		var err error
		if resp, err = http.Get(url); err != nil {
			r.Data(http.StatusNotFound, nil)
			return
		}
		defer resp.Body.Close()
		data, _ := ioutil.ReadAll(resp.Body)
		r.Data(http.StatusOK, data)
		return
	})

	mart.Get(API+"/models", func(r render.Render) {
		modelNames, err := caffe.ListModels()
		if err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, map[string][]string{"models": modelNames})
		return
	})

	mart.Get(API+"/models/current", func(r render.Render) {
		modelName, err := caffe.GetCurrentModel()
		if err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, map[string]string{"model": modelName})
		return
	})

	mart.Post(API+"/models/apply", binding.Bind(ModelForm{}), func(model ModelForm, r render.Render) {
		err := caffe.ApplyModel(model.ModelName)
		if err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, map[string]string{})
		return
	})

	mart.Delete(API+"/models/(?P<modelName>[^/]+.caffemodel)", func(params martini.Params, r render.Render) {
		err := caffe.RemoveModel(params["modelName"])
		if err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, map[string]string{})
		return
	})
}