当前位置: 首页>>代码示例>>Golang>>正文


Golang Render.JSON方法代码示例

本文整理汇总了Golang中github.com/unrolled/render.Render.JSON方法的典型用法代码示例。如果您正苦于以下问题:Golang Render.JSON方法的具体用法?Golang Render.JSON怎么用?Golang Render.JSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/unrolled/render.Render的用法示例。


在下文中一共展示了Render.JSON方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: CommonSelectFunc

func CommonSelectFunc(r *render.Render, store *sessions.CookieStore) ServePrimeFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		id := req.PostFormValue("courseID")
		_, school, ok := validateSession(req, store)
		if ok {
			if id == "" {
				http.ServeFile(w, req, "DHUCourseChooseHTML/commonselect.html")
				return
			} else {
				var done bool
				var err error
				var course CourseContent
				DBsession := GetSession()
				defer DBsession.Close()
				cTable := DBsession.DB(school).C("CourseTable")
				for i := 0; i < 3; i++ {
					course, err = APICommonselect(cTable, id)
					if err == nil {
						done = true
						break
					}
				}
				if done {
					r.JSON(w, http.StatusOK, course)
					return
				}
			}
		}
		http.Redirect(w, req, "/errMessage", http.StatusMovedPermanently)
	}
}
开发者ID:XingLong9630,项目名称:DHUCourseSelection,代码行数:31,代码来源:WebAPI.go

示例2: HomeSelectFunc

func HomeSelectFunc(r *render.Render, store *sessions.CookieStore) ServePrimeFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		coursetype := req.PostFormValue("coursetype")
		if ok := validateCourseType(coursetype); ok {
			sessionid, school, ok := validateSession(req, store)
			if ok {
				var done bool
				var err error
				var teachSchemas []TeachSchema
				DBsession := GetSession()
				defer DBsession.Close()
				for i := 0; i < 3; i++ {
					cTable := DBsession.DB(school).C("CourseTable")
					cIndex := DBsession.DB(school).C("CourseIndex")
					teachSchemas, err = APIHomeSelect(cTable, cIndex, sessionid[:6], coursetype)
					if err == nil {
						done = true
						break
					} else {
						fmt.Println(err)
					}
				}
				if done {
					r.JSON(w, http.StatusOK, map[string]([]TeachSchema){"TeachSchema": teachSchemas})
					return
				}
			}
		} else {
			http.ServeFile(w, req, "DHUCourseChooseHTML/homeselect.html")
		}
		// http.Redirect(w,req,"/errMessage",http.StatusMovedPermanently)
	}
}
开发者ID:XingLong9630,项目名称:DHUCourseSelection,代码行数:33,代码来源:WebAPI.go

示例3: HomeRegisterFunc

func HomeRegisterFunc(r *render.Render, store *sessions.CookieStore) ServePrimeFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		var slist SelectLists
		stuid, school, ok := validateSession(req, store)
		if ok {
			result, err := ioutil.ReadAll(req.Body)
			if err == nil {
				err = json.Unmarshal([]byte(result), &slist)
				if err == nil {
					DBsession := GetSession()
					defer DBsession.Close()
					cTable := DBsession.DB(school).C("CourseTable")
					if ok := validateCourse(slist, cTable); ok {
						courseInfo := saveAndRegister(stuid, school, slist)
						r.JSON(w, http.StatusOK, courseInfo)
						return
					}
					//Note:If the user post some illegal courses then we think it is a crawler or program
				}
			}
		}
		http.Redirect(w, req, "/errMessage", http.StatusMovedPermanently)
		return
	}
}
开发者ID:XingLong9630,项目名称:DHUCourseSelection,代码行数:25,代码来源:WebAPI.go

示例4: HomeFunc

func HomeFunc(r *render.Render, store *sessions.CookieStore) ServePrimeFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		status := req.PostFormValue("userstatus")
		if validateHomeStatus(status) {
			stuid, school, ok := validateSession(req, store)
			if ok {
				if status == "" {
					http.ServeFile(w, req, "DHUCourseChooseHTML/home.html")
					return
				} else {
					var err error
					var done bool
					var courselist []RigisteredCourse
					DBsession := GetSession()
					defer DBsession.Close()
					cRigister := DBsession.DB(school).C("RigisterInfo")
					for i := 0; i < 3; i++ {
						courselist, err = getrigisteredFunc(stuid, cRigister)
						if err == nil {
							done = true
							break
						}
					}
					if done {
						r.JSON(w, http.StatusOK, map[string]([]RigisteredCourse){"RigisterCourse": courselist})
						return
					}
				}
			}
		}
		http.Redirect(w, req, "/errMessage", http.StatusMovedPermanently)
	}
}
开发者ID:XingLong9630,项目名称:DHUCourseSelection,代码行数:33,代码来源:WebAPI.go

示例5: ScenariosByNameHandler

// ScenariosByNameHandler
func ScenariosByNameHandler(ren *render.Render, conf *config.Config) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		scenarioName := vars["name"]
		ren.JSON(w, http.StatusOK, ScenarioContent(conf.Scenarios.ScenarioDir, scenarioName))
	}
}
开发者ID:briandowns,项目名称:raceway,代码行数:8,代码来源:scenarios.go

示例6: TaskDeleteByIDRouteHandler

// TaskDeleteByIDRouteHandler deletes the task data for the given ID
func TaskDeleteByIDRouteHandler(ren *render.Render, conf *config.Config, dispatch *dispatcher.Dispatcher) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		tid := vars["id"]
		taskID, err := strconv.Atoi(tid)
		if err != nil {
			log.Println(err)
		}
		db, err := database.NewDatabase(conf)
		if err != nil {
			log.Println(err)
		}
		defer db.Conn.Close()

		task := db.GetTaskByID(taskID)
		if len(task) > 0 {
			dispatch.RemoveTaskChan <- task[0]
			db.DeleteTask(taskID)
		} else {
			ren.JSON(w, http.StatusOK, map[string]interface{}{"error": ErrNoEntryFound.Error()})
			return
		}
		ren.JSON(w, http.StatusOK, map[string]interface{}{"task": taskID})
	}
}
开发者ID:srhopkins,项目名称:aion,代码行数:26,代码来源:tasks.go

示例7: addTelemetryHandler

func addTelemetryHandler(formatter *render.Render, dispatcher queueDispatcher) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		payload, _ := ioutil.ReadAll(req.Body)
		var newTelemetryCommand telemetryCommand
		err := json.Unmarshal(payload, &newTelemetryCommand)
		if err != nil {
			formatter.Text(w, http.StatusBadRequest, "Failed to parse add telemetry command.")
			return
		}
		if !newTelemetryCommand.isValid() {
			formatter.Text(w, http.StatusBadRequest, "Invalid telemetry command.")
			return
		}

		evt := dronescommon.TelemetryUpdatedEvent{
			DroneID:          newTelemetryCommand.DroneID,
			RemainingBattery: newTelemetryCommand.RemainingBattery,
			Uptime:           newTelemetryCommand.Uptime,
			CoreTemp:         newTelemetryCommand.CoreTemp,
			ReceivedOn:       time.Now().Unix(),
		}
		fmt.Printf("Dispatching telemetry event for drone %s\n", newTelemetryCommand.DroneID)
		dispatcher.DispatchMessage(evt)
		formatter.JSON(w, http.StatusCreated, evt)
	}
}
开发者ID:cloudnativego,项目名称:drones-cmds,代码行数:26,代码来源:handlers.go

示例8: HomeDeleteFunc

func HomeDeleteFunc(r *render.Render, store *sessions.CookieStore) ServePrimeFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		courseNo := req.PostFormValue("courseNo")
		if courseNo == "" {
			http.Redirect(w, req, "/errMessage", http.StatusMovedPermanently)
		}
		jsonstatus := map[string]bool{courseNo: false}
		stuid, school, ok := validateSession(req, store)
		if ok {
			var stuInfo StudentRigisterCourse
			DBsession := GetSession()
			defer DBsession.Close()
			cRigister := DBsession.DB(school).C("RigisterInfo")
			err := cRigister.Find(bson.M{"studentid": stuid, "courselist": bson.M{"$elemMatch": bson.M{"courseno": courseNo, "coursestate": courseInQueue}}}).One(&stuInfo)
			if err == nil {
				conflict := true
				//alterDatabase function use conflict(true) to set the course status from inqueue to deleted
				for _, courseInfo := range stuInfo.CourseList {
					if courseInfo.CourseNo == courseNo {
						// fmt.Println(courseNo)
						// fmt.Println(conflict)
						alterDatabase(conflict, school, courseInfo.CourseID, courseNo, stuid, courseInfo.QueueNumber)
						break
					}
				}
				jsonstatus[courseNo] = true
			}
		}
		r.JSON(w, http.StatusOK, jsonstatus)
	}
}
开发者ID:XingLong9630,项目名称:DHUCourseSelection,代码行数:31,代码来源:WebAPI.go

示例9: LoginFunc

func LoginFunc(r *render.Render, store *sessions.CookieStore) ServePrimeFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		loginJSON := map[string]int{"LoginStatus": networkErrStatus}
		id := req.PostFormValue("UserID")
		if id == "" {
			http.ServeFile(w, req, "DHUCourseChooseHTML/login.html")
			return
		}
		pw := req.PostFormValue("UserPassword")
		//TODO It should be a form value that come from the user request
		school := "DHU"
		DBsession := GetSession()
		defer DBsession.Close()
		cLogin := DBsession.DB(school).C("StudentInfo")
		name, err := validateLogin(id, pw, school, cLogin)
		switch err {
		case nil:
			name = url.QueryEscape(name)
			http.SetCookie(w, &http.Cookie{Name: "stuName", Value: name})
			session, _ := store.Get(req, "sessionid")
			session.Values["stuid"] = id
			session.Values["school"] = school
			session.Save(req, w)
			loginJSON["LoginStatus"] = successStatus
		case passwordErr:
			loginJSON["LoginStatus"] = passwordErrStatus
		}

		r.JSON(w, http.StatusOK, loginJSON)
	}
}
开发者ID:XingLong9630,项目名称:DHUCourseSelection,代码行数:31,代码来源:WebAPI.go

示例10: addPositionHandler

func addPositionHandler(formatter *render.Render, dispatcher queueDispatcher) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		payload, _ := ioutil.ReadAll(req.Body)
		var newPositionCommand positionCommand
		err := json.Unmarshal(payload, &newPositionCommand)
		if err != nil {
			formatter.Text(w, http.StatusBadRequest, "Failed to parse add position command.")
			return
		}
		if !newPositionCommand.isValid() {
			formatter.Text(w, http.StatusBadRequest, "Invalid position command.")
			return
		}
		evt := dronescommon.PositionChangedEvent{
			DroneID:         newPositionCommand.DroneID,
			Longitude:       newPositionCommand.Longitude,
			Latitude:        newPositionCommand.Latitude,
			Altitude:        newPositionCommand.Altitude,
			CurrentSpeed:    newPositionCommand.CurrentSpeed,
			HeadingCardinal: newPositionCommand.HeadingCardinal,
			ReceivedOn:      time.Now().Unix(),
		}
		dispatcher.DispatchMessage(evt)
		formatter.JSON(w, http.StatusCreated, evt)
	}
}
开发者ID:cloudnativego,项目名称:drones-cmds,代码行数:26,代码来源:handlers.go

示例11: cookieWriteHandler

func cookieWriteHandler(formatter *render.Render) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		expiration := time.Now().Add(365 * 24 * time.Hour)
		cookie := http.Cookie{Name: "sample", Value: "this is a cookie", Expires: expiration}
		http.SetCookie(w, &cookie)
		formatter.JSON(w, http.StatusOK, "cookie set")
	}
}
开发者ID:cloudnativego,项目名称:web-application,代码行数:8,代码来源:handlers.go

示例12: MakeBuildListHandler

func MakeBuildListHandler(r *render.Render) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, req *http.Request) {
		var (
			buildList = []AutomatedBuild{}
		)

		r.JSON(w, http.StatusOK, buildList)
	}
}
开发者ID:squishykid,项目名称:tarzan,代码行数:9,代码来源:main.go

示例13: CommandsRouteHandler

// CommandsRouteHandler provides the handler for commands data
func CommandsRouteHandler(ren *render.Render, conf *config.Config) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		db, err := database.NewDatabase(conf)
		if err != nil {
			log.Println(err)
		}
		defer db.Conn.Close()
		ren.JSON(w, http.StatusOK, map[string]interface{}{"commands": db.GetCommands()})
	}
}
开发者ID:srhopkins,项目名称:aion,代码行数:11,代码来源:commands.go

示例14: TasksRunningHandler

// TasksRunningHandler
func TasksRunningHandler(ren *render.Render, conf *config.Config) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		db, err := database.NewDatabase(conf)
		if err != nil {
			log.Fatalln(err)
			return
		}
		ren.JSON(w, http.StatusOK, db.TasksRunning())
	}
}
开发者ID:briandowns,项目名称:raceway,代码行数:11,代码来源:tasks.go

示例15: vcapHandler

func vcapHandler(formatter *render.Render, appEnv *cfenv.App) http.HandlerFunc {
	val, err := cftools.GetVCAPServiceProperty("beer-service", "target-url", appEnv)

	return func(w http.ResponseWriter, req *http.Request) {
		if err != nil {
			formatter.JSON(w, http.StatusInternalServerError, struct{ Error string }{err.Error()})
			return
		}
		formatter.JSON(w, http.StatusOK, struct{ Foo string }{val})
	}
}
开发者ID:cloudnativego,项目名称:vcap,代码行数:11,代码来源:server.go


注:本文中的github.com/unrolled/render.Render.JSON方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。