简单 web 服务与客户端开发实战

本文总结了一个个人项目,其中主要负责后端API的设计,包括用户注册、登录、资源目录获取等接口,并对API数量和页面进行了限制。同时,介绍了使用curl进行mock测试,确保资源访问的正确性。
摘要由CSDN通过智能技术生成

个人项目小结

分工

本次项目中我主要负责后端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

具体代码

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
在这里插入图片描述
访问分页
在这里插入图片描述
用户注册
在这里插入图片描述
用户登陆
在这里插入图片描述
访问受限资源
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值