Skip to content

Commit

Permalink
Add gRPC and Http SSE (#28)
Browse files Browse the repository at this point in the history
* Add grpc

* Ignore generated files

* Update makefile command

* Fix docker

* Add rpc server

* Fix make test

* Fix build

* Fix make check

* Add SSE streaming

* Remove generated swagger docs

* Adjust gitignore

* Fix makefile
  • Loading branch information
slhmy authored Oct 3, 2023
1 parent cfe170b commit 0c1461f
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 165 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ dist.zip
frontend/dist/

# Ignore generated files
*.pb.go
*.pb.go
**/swaggo-gen/docs.go
18 changes: 13 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ gen-proto: install-tools
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
service/proto/*.proto

.PHONY: build
build: install-tools
@echo "Building on $(OS)"
.PHONY: gen-swagger
gen-swagger: install-tools
swag fmt -d application/server
swag init -d application/server -ot go -o application/server/swaggo-gen

.PHONY: build
build: gen-proto gen-swagger
@echo "Building on $(OS)"
go mod tidy
go build -o bin/migrate_db application/migrate_db/main.go
go build -o bin/service application/server/main.go
go build -o bin/asynq_worker application/asynq_worker/main.go
go build -o bin/rpc_server application/rpc_server/main.go

.PHONY: clear-db
clear-db:
Expand All @@ -42,11 +46,11 @@ setup-db: clear-db build
./bin/migrate_db

.PHONY: check
check:
check: gen-proto
go vet ./...

.PHONY: test
test: setup-db check
test: gen-swagger check setup-db
go test -cover -v ./...

.PHONY: run-task-worker
Expand All @@ -57,6 +61,10 @@ run-task-worker: build check
run-server: build check
./bin/service

.PHONY: run-rpc-server
run-rpc-server: build check
./bin/rpc_server

.PHONY: run
run: build check
make -j run-task-worker run-server
Expand Down
17 changes: 17 additions & 0 deletions application/rpc_server/impls/greeter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package impls

import (
"context"
"log"

"github.com/OJ-lab/oj-lab-services/service/proto"
)

type GreeterServer struct {
proto.UnimplementedGreeterServer
}

func (s *GreeterServer) Greeting(ctx context.Context, request *proto.GreetingRequest) (*proto.GreetingResponse, error) {
log.Printf("Received: %v", request.GetName())
return &proto.GreetingResponse{Message: "Hello " + request.GetName()}, nil
}
32 changes: 32 additions & 0 deletions application/rpc_server/impls/streamer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package impls

import (
"log"
"time"

"github.com/OJ-lab/oj-lab-services/service/proto"
)

type StreamerServer struct {
proto.UnimplementedStreamerServer
}

func (s *StreamerServer) StartStream(request *proto.StreamRequest, server proto.Streamer_StartStreamServer) error {
tick := time.NewTicker(1 * time.Second)
for range tick.C {
if server.Context().Err() != nil {
if server.Context().Err().Error() == "context canceled" {
log.Printf("client closed stream")
return nil
}
log.Printf("client closed stream with: %v", server.Context().Err().Error())
return nil
}

server.Send(&proto.StreamResponse{Body: &proto.StreamResponse_Health{
Health: &proto.ServerHealth{},
}})
}

return nil
}
39 changes: 39 additions & 0 deletions application/rpc_server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"flag"
"fmt"
"log"
"net"

"github.com/OJ-lab/oj-lab-services/application/rpc_server/impls"
"github.com/OJ-lab/oj-lab-services/core"
"github.com/OJ-lab/oj-lab-services/service/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

const (
portProp = "rpc-server.port"
)

var (
port = core.AppConfig.GetInt(portProp)
)

func main() {
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
proto.RegisterGreeterServer(s, &impls.GreeterServer{})
proto.RegisterStreamerServer(s, &impls.StreamerServer{})

reflection.Register(s)
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
28 changes: 28 additions & 0 deletions application/server/handler/user.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package handler

import (
"fmt"
"io"
"net/http"
"time"

"github.com/OJ-lab/oj-lab-services/core"
"github.com/OJ-lab/oj-lab-services/core/middleware"
"github.com/OJ-lab/oj-lab-services/service"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

func SetupUserRouter(baseRoute *gin.RouterGroup) {
Expand All @@ -18,9 +22,33 @@ func SetupUserRouter(baseRoute *gin.RouterGroup) {
g.POST("/login", login)
g.GET("/me", middleware.HandleRequireLogin, me)
g.GET("/check-exist", checkUserExist)
g.GET("/stream", Stream)
}
}

// Stream
//
// @Summary Stream
// @Description Stream
// @Tags user
// @Router /user/stream [get]
// @Accept text/event-stream
// @Produce text/event-stream
// @Success 200 {string} string "data: {message}"
// @Router /user/stream [get]
func Stream(ginCtx *gin.Context) {
ginCtx.Header("Content-Type", "text/event-stream")
ginCtx.Header("Cache-Control", "no-cache")

ginCtx.Stream(func(w io.Writer) bool {
// 每秒钟向客户端发送一条消息
logrus.Info("send message")
fmt.Fprintf(w, "data: %s\n\n", time.Now().String())
time.Sleep(1 * time.Second)
return true
})
}

type loginBody struct {
Account string `json:"account" example:"admin"`
Password string `json:"password" example:"admin"`
Expand Down
156 changes: 0 additions & 156 deletions application/server/swaggo-gen/docs.go

This file was deleted.

3 changes: 3 additions & 0 deletions config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ mode = "debug"
swagger_on = true
serve_front = false

[rpc-server]
port = 50051

[judger]
host = "http://localhost:8000"

Expand Down
3 changes: 3 additions & 0 deletions config/production.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ mode = "debug"
swagger_on = true
serve_front = true

[rpc-server]
port = 50051

[judger]
host = "http://host.docker.internal:8000"

Expand Down
17 changes: 17 additions & 0 deletions service/proto/oj_lab_greeting.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";

package oj_lab_greeting.protos;

option go_package = "github.com/OJ-lab/oj-lab-services/service/proto";

service Greeter {
rpc Greeting (GreetingRequest) returns (GreetingResponse) {}
}

message GreetingRequest {
string name = 1;
}

message GreetingResponse {
string message = 1;
}
Loading

0 comments on commit 0c1461f

Please sign in to comment.