个人项目小结
分工
本次项目中我主要负责后端api设计和URL的解析
实验内容
API实现目标
页面数与API数限制
- 界面不能少于三个界面
- 用户注册(/register POST)
- 用户登陆(/login POST)
- 获取资源目录(/api/ GET)
- 游客登陆
- 服务API不能少于六个
- films资源
- /api/films/?page={id} GET
- /api/films/pages GET
- /api/films/{id} GET
- planets资源
- /api/planets/?page={id} GET
- /api/planets/pages GET
- /api/planets/{id} GET
- people资源
- /api/people/?page={id} GET
- /api/people/pages GET
- /api/people/{id} GET
- species资源
- /api/species/?page={id} GET
- /api/species/pages GET
- /api/species/{id} GET
- starships资源
- /api/starships/?page={id} GET
- /api/starships/pages GET
- /api/starships/{id} GET
- vehicles资源
- /api/vehicles/?page={id} GET
- /api/vehicles/pages GET
- /api/vehicles/{id} GET
- films资源
具体代码
json序列化和反序列化
服务器端收到请求后使用json解析器反序列化为结构体
comment := &Comment{
Date: time.Now().Format("2006-01-02 15:04:05"),
Content: "",
Author: "",
ArticleId: Id,
}
err = json.NewDecoder(r.Body).Decode(&comment)
服务器端发送响应时将结构体序列化为字符串
json, err := json.Marshal(response)
用户注册登陆
func registerHandler(w http.ResponseWriter, req *http.Request) {
}
func loginHandler(w http.ResponseWriter, req *http.Request) {
}
handle一个GET 请求
func apiHandler(formatter *render.Render) negroni.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
}
}
handle资源filems等的GET请求
func filmsHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
}
}
数据库配置和路由匹配
func NewServer() *negroni.Negroni {
}
func initRoutes(mx *mux.Router, formatter *render.Render) {
}
JWTToken认证
func ValidateMid(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
}
新建路由对象
mx := mux.NewRouter()
路由结构,设置静态文件服务.
mx.Handle("/api/", negroni.New(
negroni.HandlerFunc(models.ValidateMid),
negroni.HandlerFunc(apiHandler(formatter)),
))
mx.HandleFunc("/login", loginHandler).Methods("POST")
mx.HandleFunc("/register", registerHandler).Methods("POST")
mx.HandleFunc("/api/films/", filmsHandler(formatter)).Methods("GET")
mx.HandleFunc("/api/films/pages", filmsPagesHandler).Methods("GET")
filmsSubRouter := mx.PathPrefix("/api/films").Subrouter()
filmsSubRouter.HandleFunc("/{id:[0-9]+}", getFilmsById).Methods("GET")
mx.HandleFunc("/api/people/", peopleHandler(formatter)).Methods("GET")
mx.HandleFunc("/api/people/pages", peoplePagesHandler).Methods("GET")
peopleSubRouter := mx.PathPrefix("/api/people").Subrouter()
peopleSubRouter.HandleFunc("/{id:[0-9]+}", getPeopleById).Methods("GET")
mx.HandleFunc("/api/planets/", planetsHandler(formatter)).Methods("GET")
mx.HandleFunc("/api/planets/pages", planetsPagesHandler).Methods("GET")
planetsSubRouter := mx.PathPrefix("/api/planets").Subrouter()
planetsSubRouter.HandleFunc("/{id:[0-9]+}", getPlanetsById).Methods("GET")
mx.HandleFunc("/api/species/", speciesHandler(formatter)).Methods("GET")
mx.HandleFunc("/api/species/pages", speciesPagesHandler).Methods("GET")
speciesSubRouter := mx.PathPrefix("/api/species").Subrouter()
speciesSubRouter.HandleFunc("/{id:[0-9]+}", getSpeciesById).Methods("GET")
mx.HandleFunc("/api/starships/", starshipsHandler(formatter)).Methods("GET")
mx.HandleFunc("/api/starships/pages", starshipsPagesHandler).Methods("GET")
starshipsSubRouter := mx.PathPrefix("/api/starships").Subrouter()
starshipsSubRouter.HandleFunc("/{id:[0-9]+}", getStarshipsById).Methods("GET")
mx.HandleFunc("/api/vehicles/", vehiclesHandler(formatter)).Methods("GET")
mx.HandleFunc("/api/vehicles/pages", vehiclesPagesHandler).Methods("GET")
vehiclesSubRouter := mx.PathPrefix("/api/vehicles").Subrouter()
vehiclesSubRouter.HandleFunc("/{id:[0-9]+}", getVehiclesById).Methods("GET")
使用curl进行mock测试
资源访问
访问某ID的film
访问分页
用户注册
用户登陆
访问受限资源