func NewSessionService(apiRoot string, repository SessionRepository) *sessionService { s := new(sessionService) ws := new(restful.WebService) ws.Path(apiRoot + "/project/{project-id}/sessions"). Consumes(restful.MIME_JSON). Produces(restful.MIME_JSON) ws.Route(ws.GET("").To(s.listSessions). Doc("List sessions"). Param(ws.PathParameter("project-id", "identifier of the project").DataType("int")). Param(ws.QueryParameter("from", "minimum identifier of a project")). Param(ws.QueryParameter("to", "maximum identifier of a project")). Writes(Session{})) ws.Route(ws.GET("/{session-id}").To(s.findSession). Doc("Get a session"). Param(ws.PathParameter("project-id", "identifier of the project").DataType("int")). Param(ws.PathParameter("session-id", "identifier of the session").DataType("int")). Writes(Session{})) ws.Route(ws.POST("").To(s.createSession). Doc("Create a session"). Param(ws.PathParameter("project-id", "identifier of the project").DataType("int")). Reads(Session{})) ws.Route(ws.PUT("/{session-id}").To(s.updateSession). Doc("Update a session"). Param(ws.PathParameter("project-id", "identifier of the project").DataType("int")). Param(ws.PathParameter("session-id", "identifier of the session").DataType("int")). Param(ws.BodyParameter("Session", "the session entity").DataType("string"))) ws.Route(ws.DELETE("/{session-id}").To(s.removeSession). Doc("Delete a session"). Param(ws.PathParameter("project-id", "identifier of the project").DataType("int")). Param(ws.PathParameter("session-id", "identifier of the session").DataType("int"))) s.WebService = ws s.repository = repository projects, _ := repository.Group().Projects().Scan(0, 0) totalSessions := 0 for _, project := range projects { sessions, _ := repository.Scan(project.Id, 0, 0) totalSessions += len(sessions) stats.ChangeStat(fmt.Sprintf("sessions:%d", project.Id), len(sessions)) } stats.ChangeStat("sessions", totalSessions) return s }
func (s *sessionService) createSession(request *restful.Request, response *restful.Response) { projectId, err := strconv.Atoi(request.PathParameter("project-id")) if err != nil { response.WriteError(http.StatusBadRequest, err) return } session := Session{ ProjectId: projectId, Created: time.Now(), UserAgent: request.Request.UserAgent(), } session, err = s.repository.Put(session) if err != nil { response.WriteError(http.StatusBadRequest, err) return } stats.ChangeStat("sessions", 1) stats.ChangeStat(fmt.Sprintf("sessions:%d", projectId), 1) response.WriteHeader(http.StatusCreated) response.WriteEntity(session) }
func (s *sampleService) createVoiceSample(request *restful.Request, response *restful.Response) { sessionId, _ := strconv.Atoi(request.PathParameter("session-id")) sample := VoiceSample{ SessionId: sessionId, Created: time.Now(), } var err error sample, err = s.repository.Put(sample) if err != nil { response.WriteError(http.StatusBadRequest, err) return } stats.ChangeStat("samples", 1) response.WriteHeader(http.StatusCreated) response.WriteEntity(sample) }
func NewProjectService(apiRoot string, repository ProjectRepository) *projectService { ps := new(projectService) ws := new(restful.WebService) ws.Path(apiRoot + "/projects"). Consumes(restful.MIME_JSON). Produces(restful.MIME_JSON) ws.Route(ws.GET("").To(ps.listProjects). Doc("List projects"). Param(ws.QueryParameter("from", "minimum identifier of a project")). Param(ws.QueryParameter("to", "maximum identifier of a project")). Writes(Project{})) ws.Route(ws.GET("/{project-id}").To(ps.findProject). Doc("Get a project"). Param(ws.PathParameter("project-id", "identifier of the project")). Writes(Project{})) ws.Route(ws.POST("").To(ps.createProject). Doc("Create a project"). Reads(Project{})) ws.Route(ws.PUT("/{project-id}").To(ps.updateProject). Doc("Update a project"). Param(ws.PathParameter("project-id", "identifier of the project"))) ws.Route(ws.DELETE("/{project-id}").To(ps.removeProject). Doc("Delete a project"). Param(ws.PathParameter("project-id", "identifier of the project"))) ps.WebService = ws ps.repository = repository // set initial stats projects, _ := repository.Scan(0, 0) stats.ChangeStat("projects", len(projects)) return ps }
func (w *loggedResponseWriter) WriteHeader(status int) { w.status = status w.ResponseWriter.WriteHeader(status) stats.ChangeStat("requests", 1) }