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,3 @@
Name: shorturl-api
Host: 0.0.0.0
Port: 8888

View File

@@ -0,0 +1,7 @@
package config
import "github.com/tal-tech/go-zero/rest"
type Config struct {
rest.RestConf
}

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)
}
}
}

View 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
}

View 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
}

View 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}
}

View 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"`
}

View File

@@ -0,0 +1,33 @@
type (
expandReq struct {
shorten string `form:"shorten"`
}
expandResp struct {
url string `json:"url"`
}
)
type (
shortenReq struct {
url string `form:"url"`
}
shortenResp struct {
shorten string `json:"shorten"`
}
)
service shorturl-api {
@server(
handler: ShortenHandler
)
get /shorten(shortenReq) returns(shortenResp)
@server(
handler: ExpandHandler
)
get /expand(expandReq) returns(expandResp)
}

View File

@@ -0,0 +1,28 @@
package main
import (
"flag"
"shorturl/api/internal/config"
"shorturl/api/internal/handler"
"shorturl/api/internal/svc"
"github.com/tal-tech/go-zero/core/conf"
"github.com/tal-tech/go-zero/rest"
)
var configFile = flag.String("f", "etc/shorturl-api.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
handler.RegisterHandlers(server, ctx)
server.Start()
}