add shorturl example code

This commit is contained in:
kevin
2020-09-01 16:04:39 +08:00
parent ea1c9aa250
commit b7a018b33a
28 changed files with 1336 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
package handler
import (
"net/http"
"shorturl/api/internal/logic"
"shorturl/api/internal/svc"
"shorturl/api/internal/types"
"github.com/tal-tech/go-zero/rest/httpx"
)
func expandHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ExpandReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
return
}
l := logic.NewExpandLogic(r.Context(), ctx)
resp, err := l.Expand(req)
if err != nil {
httpx.Error(w, err)
} else {
httpx.WriteJson(w, http.StatusOK, resp)
}
}
}

View File

@@ -0,0 +1,25 @@
// DO NOT EDIT, generated by goctl
package handler
import (
"net/http"
"shorturl/api/internal/svc"
"github.com/tal-tech/go-zero/rest"
)
func RegisterHandlers(engine *rest.Server, serverCtx *svc.ServiceContext) {
engine.AddRoutes([]rest.Route{
{
Method: http.MethodGet,
Path: "/shorten",
Handler: shortenHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/expand",
Handler: expandHandler(serverCtx),
},
})
}

View File

@@ -0,0 +1,29 @@
package handler
import (
"net/http"
"shorturl/api/internal/logic"
"shorturl/api/internal/svc"
"shorturl/api/internal/types"
"github.com/tal-tech/go-zero/rest/httpx"
)
func shortenHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ShortenReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
return
}
l := logic.NewShortenLogic(r.Context(), ctx)
resp, err := l.Shorten(req)
if err != nil {
httpx.Error(w, err)
} else {
httpx.WriteJson(w, http.StatusOK, resp)
}
}
}