This project demonstrates a simple HTTP server that accepts JSON requests and returns Protocol Buffer responses.
- Go 1.21 or later
- Protocol Buffer Compiler (protoc)
# Install Go
brew install go
# Install Protocol Buffer Compiler
brew install protobuf- Clone the repository:
git clone https://github.com/your-username/proto-http.git
cd proto-http- Install the Protocol Buffer compiler plugin for Go:
export PATH=$PATH:$(go env GOPATH)/bin && go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
- Generate Protocol Buffer code:
chmod +x generate.sh
./generate.sh- Install dependencies:
go mod tidyStart the server:
go run main.goThe server will start on port 8080.
- Get a user (returns protobuf):
curl http://localhost:8080/user?id=1- Create a user (JSON input, protobuf output):
curl -X POST \
http://localhost:8080/user \
-H 'Content-Type: application/json' \
-d '{
"id": "2",
"name": "Jane Doe",
"email": "jane@example.com"
}'-
GET Request:
- URL:
http://localhost:8080/user?id=1 - Method: GET
- Response will be in protobuf format
- URL:
-
POST Request:
- URL:
http://localhost:8080/user - Method: POST
- Headers:
Content-Type: application/json - Body (raw JSON):
{ "id": "2", "name": "Jane Doe", "email": "jane@example.com" } - Response will be in protobuf format
- URL:
api/user.proto: Protocol Buffer definitionserver/server.go: HTTP server implementationmain.go: Server startup codegenerate.sh: Script to generate Go code from proto files