-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from metalbear-co/grpc
add ip-info-grpc
- Loading branch information
Showing
6 changed files
with
450 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Release ip info grpc | ||
on: | ||
push: | ||
branches: [main] | ||
workflow_dispatch: | ||
|
||
jobs: | ||
image: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
packages: write | ||
contents: read | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v2 | ||
- name: Login to GitHub Container Registry | ||
uses: docker/login-action@v2 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.repository_owner }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Build and push | ||
uses: docker/build-push-action@v3 | ||
with: | ||
file: ip-info-grpc/Dockerfile | ||
platforms: linux/amd64,linux/arm64,linux/arm/v7 | ||
context: ./ | ||
push: true | ||
tags: | | ||
ghcr.io/metalbear-co/playground-ip-info-grpc:latest | ||
ghcr.io/metalbear-co/playground-ip-info-grpc:${{ github.sha }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FROM --platform=$BUILDPLATFORM golang:1.23-alpine as build-env | ||
|
||
WORKDIR /app | ||
COPY go.mod ./ | ||
COPY go.sum ./ | ||
RUN go mod download | ||
COPY ip-info-grpc ./ip-info-grpc | ||
COPY protogen ./protogen | ||
COPY proto ./proto | ||
|
||
RUN GOARCH=$TARGETARCH go build -o /main ./ip-info-grpc/main.go | ||
|
||
FROM gcr.io/distroless/static-debian11 | ||
|
||
COPY --from=build-env /main /main | ||
|
||
CMD [ "/main" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net" | ||
|
||
pb "github.com/metalbear-co/playground/protogen" | ||
"github.com/spf13/viper" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
var ctx = context.Background() | ||
|
||
// Config | ||
// Struct that holds local service port, remote redis host and port | ||
type Config struct { | ||
Port int16 | ||
KafkaAddress string | ||
KafkaTopic string | ||
KafkaConsumerGroup string | ||
} | ||
|
||
type IpInfo struct { | ||
Ip string `json:"ip"` | ||
Info string `json:"name"` | ||
} | ||
|
||
// albums slice to seed record album data. | ||
var ipInfos = []IpInfo{ | ||
{Ip: "84.229.14.82", Info: "Aviram, Loves coffee!"}, | ||
} | ||
|
||
func loadConfig() Config { | ||
viper.BindEnv("port") | ||
|
||
config := Config{} | ||
config.Port = int16(viper.GetInt("port")) | ||
return config | ||
} | ||
|
||
type server struct { | ||
pb.UnimplementedIpInfoServiceServer | ||
} | ||
|
||
func (s *server) GetIpInfo(ctx context.Context, req *pb.IpRequest) (*pb.IpResponse, error) { | ||
ip := req.GetIp() | ||
|
||
for _, info := range ipInfos { | ||
if info.Ip == ip { | ||
return &pb.IpResponse{Ip: info.Ip, Info: info.Info}, nil | ||
} | ||
} | ||
return &pb.IpResponse{Ip: ip, Info: "Unknown"}, nil | ||
} | ||
|
||
func main() { | ||
config := loadConfig() | ||
|
||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port)) | ||
if err != nil { | ||
log.Fatalf("failed to listen: %v", err) | ||
} | ||
|
||
s := grpc.NewServer() | ||
pb.RegisterIpInfoServiceServer(s, &server{}) | ||
|
||
fmt.Printf("gRPC server listening on port %d\n", config.Port) | ||
if err := s.Serve(lis); err != nil { | ||
log.Fatalf("failed to serve: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
syntax = "proto3"; | ||
|
||
package ipinfo; | ||
option go_package = "github.com/metalbear-co/playground/protogen"; | ||
|
||
service IpInfoService { | ||
rpc GetIpInfo(IpRequest) returns (IpResponse); | ||
} | ||
|
||
message IpRequest { | ||
string ip = 1; | ||
} | ||
|
||
message IpResponse { | ||
string ip = 1; | ||
string info = 2; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.