Skip to content

Commit

Permalink
Merge pull request #13 from metalbear-co/grpc
Browse files Browse the repository at this point in the history
add ip-info-grpc
  • Loading branch information
aviramha authored Feb 5, 2025
2 parents ea67226 + dce3853 commit 87e5667
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/build-ip-info-grpc.yaml
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 }}
17 changes: 17 additions & 0 deletions ip-info-grpc/Dockerfile
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" ]
73 changes: 73 additions & 0 deletions ip-info-grpc/main.go
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)
}
}
17 changes: 17 additions & 0 deletions proto/ipinfo.proto
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;
}
189 changes: 189 additions & 0 deletions protogen/ipinfo.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 87e5667

Please sign in to comment.