This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
72 lines (59 loc) · 1.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"context"
"fmt"
"os"
"github.com/go-kratos/grpc-gateway/v2/protoc-gen-openapiv2/generator"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/transport/http"
pb "github.com/go-kratos/swagger-api/examples/helloworld/helloworld"
reply "github.com/go-kratos/swagger-api/examples/helloworld/reply"
"github.com/go-kratos/swagger-api/openapiv2"
)
// go build -ldflags "-X main.Version=x.y.z"
var (
// Name is the name of the compiled software.
Name = "helloworld"
// Version is the version of the compiled software.
Version = "v1.0.0"
)
// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGreeterServer
}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
if in.Name == "error" {
return nil, errors.BadRequest("custom_error", fmt.Sprintf("invalid argument %s", in.Name))
}
if in.Name == "panic" {
panic("grpc panic")
}
return &pb.HelloReply{Reply: &reply.Reply{Value: fmt.Sprintf("Hello %+v", in.Name)}}, nil
}
func main() {
logger := log.NewStdLogger(os.Stdout)
log := log.NewHelper(logger)
s := &server{}
httpSrv := http.NewServer(http.Address(":8000"))
pb.RegisterGreeterHTTPServer(httpSrv, s)
h := openapiv2.NewHandler(openapiv2.WithGeneratorOptions(
// you can set UseJSONNamesForFields to false
// default is true
generator.UseJSONNamesForFields(true),
))
httpSrv.HandlePrefix("/q/", h)
/* fs := httpx.FileServer(httpx.Dir("./dist"))
httpSrv.HandlePrefix("/swaggerui/", httpx.StripPrefix("/swaggerui/", fs))*/
app := kratos.New(
kratos.Name(Name),
kratos.Server(
httpSrv,
),
)
if err := app.Run(); err != nil {
log.Error(err)
}
}