add shorturl example code
This commit is contained in:
7
example/shorturl/api/internal/config/config.go
Normal file
7
example/shorturl/api/internal/config/config.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package config
|
||||
|
||||
import "github.com/tal-tech/go-zero/rest"
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
}
|
||||
29
example/shorturl/api/internal/handler/expandhandler.go
Normal file
29
example/shorturl/api/internal/handler/expandhandler.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
25
example/shorturl/api/internal/handler/routes.go
Normal file
25
example/shorturl/api/internal/handler/routes.go
Normal 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),
|
||||
},
|
||||
})
|
||||
}
|
||||
29
example/shorturl/api/internal/handler/shortenhandler.go
Normal file
29
example/shorturl/api/internal/handler/shortenhandler.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
26
example/shorturl/api/internal/logic/expandlogic.go
Normal file
26
example/shorturl/api/internal/logic/expandlogic.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"shorturl/api/internal/svc"
|
||||
"shorturl/api/internal/types"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ExpandLogic struct {
|
||||
ctx context.Context
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewExpandLogic(ctx context.Context, svcCtx *svc.ServiceContext) ExpandLogic {
|
||||
return ExpandLogic{
|
||||
ctx: ctx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ExpandLogic) Expand(req types.ExpandReq) (*types.ExpandResp, error) {
|
||||
return &types.ExpandResp{}, nil
|
||||
}
|
||||
26
example/shorturl/api/internal/logic/shortenlogic.go
Normal file
26
example/shorturl/api/internal/logic/shortenlogic.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"shorturl/api/internal/svc"
|
||||
"shorturl/api/internal/types"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ShortenLogic struct {
|
||||
ctx context.Context
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewShortenLogic(ctx context.Context, svcCtx *svc.ServiceContext) ShortenLogic {
|
||||
return ShortenLogic{
|
||||
ctx: ctx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ShortenLogic) Shorten(req types.ShortenReq) (*types.ShortenResp, error) {
|
||||
return &types.ShortenResp{}, nil
|
||||
}
|
||||
11
example/shorturl/api/internal/svc/servicecontext.go
Normal file
11
example/shorturl/api/internal/svc/servicecontext.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package svc
|
||||
|
||||
import "shorturl/api/internal/config"
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{Config: c}
|
||||
}
|
||||
18
example/shorturl/api/internal/types/types.go
Normal file
18
example/shorturl/api/internal/types/types.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// DO NOT EDIT, generated by goctl
|
||||
package types
|
||||
|
||||
type ExpandReq struct {
|
||||
Shorten string `form:"shorten"`
|
||||
}
|
||||
|
||||
type ExpandResp struct {
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type ShortenReq struct {
|
||||
Url string `form:"url"`
|
||||
}
|
||||
|
||||
type ShortenResp struct {
|
||||
Shorten string `json:"shorten"`
|
||||
}
|
||||
Reference in New Issue
Block a user