add shorturl example code
This commit is contained in:
3
example/shorturl/api/etc/shorturl-api.yaml
Normal file
3
example/shorturl/api/etc/shorturl-api.yaml
Normal file
@@ -0,0 +1,3 @@
|
||||
Name: shorturl-api
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
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"`
|
||||
}
|
||||
33
example/shorturl/api/shorturl.api
Normal file
33
example/shorturl/api/shorturl.api
Normal 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)
|
||||
}
|
||||
28
example/shorturl/api/shorturl.go
Normal file
28
example/shorturl/api/shorturl.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user