Initial commit
This commit is contained in:
29
internal/handler/expandurlhandler.go
Normal file
29
internal/handler/expandurlhandler.go
Normal file
@ -0,0 +1,29 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"5feet11/internal/logic"
|
||||
"5feet11/internal/svc"
|
||||
"5feet11/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func ExpandUrlHandler(svcCtx *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.NewExpandUrlLogic(r.Context(), svcCtx)
|
||||
resp, err := l.ExpandUrl(&req)
|
||||
if err != nil {
|
||||
httpx.Error(w, err)
|
||||
} else {
|
||||
http.Redirect(w, r, resp.RedirectUrl, http.StatusTemporaryRedirect)
|
||||
}
|
||||
}
|
||||
}
|
27
internal/handler/routes.go
Normal file
27
internal/handler/routes.go
Normal file
@ -0,0 +1,27 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"5feet11/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/:snowflake",
|
||||
Handler: ExpandUrlHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/redirect",
|
||||
Handler: ShortenUrlHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
29
internal/handler/shortenurlhandler.go
Normal file
29
internal/handler/shortenurlhandler.go
Normal file
@ -0,0 +1,29 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"5feet11/internal/logic"
|
||||
"5feet11/internal/svc"
|
||||
"5feet11/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func ShortenUrlHandler(svcCtx *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.NewShortenUrlLogic(r.Context(), svcCtx)
|
||||
resp, err := l.ShortenUrl(&req)
|
||||
if err != nil {
|
||||
httpx.Error(w, err)
|
||||
} else {
|
||||
httpx.OkJson(w, resp)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user