Skip to content

Commit

Permalink
Update layout
Browse files Browse the repository at this point in the history
  • Loading branch information
kylelemons committed Apr 1, 2012
1 parent 9234502 commit 55a3c65
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 89 deletions.
2 changes: 0 additions & 2 deletions ae_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ for DIR in "webrpc"; do
mkdir -p "${ROOT}/${PREFIX}/${DIR}"
cp -R "${RPCGEN}/${DIR}/"* "${ROOT}/${PREFIX}/${DIR}/"
done

rm "${ROOT}/${PREFIX}/webrpc/proto.go"
60 changes: 53 additions & 7 deletions ae_protoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@

set -e

# This will make the pb only compile for appengine
AE_COPY=1
if [[ "$1" == "--ae-only" ]]; then
AE_COPY=0
fi

if [[ "$#" -eq 0 ]]; then
echo "Usage: ae_protoc.sh <file1.proto> <file2.proto>"
echo "Usage: ae_protoc.sh [--ae-only] <file1.proto> [<file2.proto> ...]"
echo " This script will use the protoc in your path"
echo "to compile each proto into go source and will"
echo "edit each file to be usable on AppEngine by"
echo "removing references to the goprotobuf library."
echo
echo "Unless --ae-also is specified, the compiled protobuf"
echo "output file will be duplicated to an appengine"
echo "specific .ae.go and both will be guarded with the"
echo "appropriate +build directive."
exit 1
fi

Expand All @@ -19,13 +30,48 @@ for FILE in "$@"; do
echo "Compiling $FILE..."
protoc --go_out=. "$FILE"

# Determine file names
PB_FILE="${FILE%.proto}.pb.go"
echo "Sanitizing $PB_FILE..."
if [[ $AE_COPY -ne 0 ]]; then
AE_FILE="${FILE%.proto}.ae.go"
cp "$PB_FILE" "$AE_FILE"
else
AE_FILE="$PB_FILE"
fi

echo "Sanitizing $AE_FILE..."
{
echo "H" # Display human-readable errors, if any
echo "g/goprotobuf/d" # Delete lines containing goprotobuf
echo "g/proto\./d" # Delete lines calling into the library
echo "w" # Write
echo "q" # Quit
echo "H" # Display human-readable errors, if any
echo "g/goprotobuf/d" # Delete lines containing goprotobuf
echo "g/proto\./d" # Delete lines calling into the library
echo "w" # Write
echo "q" # Quit
} | ed -s "$PB_FILE"

if [[ $AE_COPY -eq 0 ]]; then
# If we are sharing the same proto, don't insert guards
continue
fi

echo "Guarding $PB_FILE..."
{
echo "H" # Display human-readable errors, if any
echo "1i" # Insert at the beginning of the file
echo "// +build !appengine" # Don't compile this file under appengine
echo # Blank line to not confuse anything
echo "." # Exit insert mode
echo "w" # Write
echo "q" # Quit
} | ed -s "$PB_FILE"

echo "Guarding $AE_FILE..."
{
echo "H" # Display human-readable errors, if any
echo "1i" # Insert at the beginning of the file
echo "// +build appengine" # Only compile this file under appengine
echo # Blank line to not confuse anything
echo "." # Exit insert mode
echo "w" # Write
echo "q" # Quit
} | ed -s "$AE_FILE"
done
54 changes: 40 additions & 14 deletions example_ae/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,53 @@ When deploying to AppEngine, you need two things:
1. Sanitized .pb.go files

I have provided scripts in the root of the repository to help out with these.
The `ae_install.sh` script should be run from your appengine project's root directory
(the one containing `app.yaml`) and will copy the webrpc package into the proper place.
This will also automatically delete the local copy of `proto.go`,
which removes the protobuf support and the dependency upon it.
The `ae_protoc.go` script (which relies on `protoc-gen-go` being in your `PATH`)
will compile (for Go only) all of the `.proto` files specified on the command-line
with support for web services only (to avoid the dependency on the codec package)
and will sanitize the generated file to remove references to goprotobuf.
A side-effect of the sanitization is that there will no longer be a `.String()`
method on the generated objects; you may add one manually if you wish,
but I would recommend doing it in a parallel `.go` file so that regeneration won't kill it.

Getting `go-rpcgen/webrpc` in your app
--------------------------------------

The `ae_install.sh` script should be run from your appengine project's root
directory (the one containing `app.yaml`) and will copy the webrpc package into
the proper place. The webrpc package contains proto support, but the go1
version of appengine will ignore this file.

Getting sanitized protobufs in your app
---------------------------------------

The `ae_protoc.go` script (which relies on `protoc-gen-go` being in your
`PATH`) will compile (for Go only) all of the `.proto` files specified on the
command-line.

The script has been changed since its previous version to generate both a full
`.pb.go` for use in normal applications and a `.ae.go` for use in appengine.
They both have compilation guards which cause them to be executed in the proper
context.

Both proto files are compiled with support for web services only (to avoid the
dependency on the codec package) and the script will sanitize the duplicate
`.ae.go` file to remove references to goprotobuf. A side-effect of the
sanitization is that there will no longer be a `.String()` method on the
generated objects under appengine; you may add one manually if you wish, but I
would recommend doing it in an adjacent `.go` file (with the proper `+build`
guard for appengine) so that regeneration won't kill it.

Testing This Example
--------------------
====================

To test this out, you will probably need `go1` installed locally so that you
can compile non-appengine binaries. The appengine `go` wrapper script in
particular doesn't like arguments to `go run` though it may be possible to use.

Here are the basic steps:

- Update app.yaml with your application name and the latest Go SDK Version
- Execute the following to run locally:
- Execute the following (in the `example_ae` directory) to run locally:

../ae_install.sh
../ae_protoc.sh whoami/*.proto
(cd github.com/kylelemons/go-rpcgen/; mkdir -p ae_example; ln -s ../../../../whoami ae_example/)
dev_appserver.py .

- Then, in another shell (since `dev_appserver` blocks):

go run client/client.go http://localhost:6060/

- Run the following to test remotely:
Expand Down
2 changes: 1 addition & 1 deletion example_ae/app.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
application: your-application-name
version: 1
runtime: go
api_version: go1beta
api_version: go1

handlers:
- url: /.*
Expand Down
5 changes: 4 additions & 1 deletion example_ae/app/server.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// +build appengine

package server

import (
"net/http"
_ "github.com/kylelemons/go-rpcgen/webrpc"
"whoami"

_ "github.com/kylelemons/go-rpcgen/webrpc"
)

type server struct{}
Expand Down
1 change: 1 addition & 0 deletions example_ae/client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
client
4 changes: 3 additions & 1 deletion example_ae/client/client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// +build !appengine

package main

import (
"os"
"log"
"net/url"
"github.com/kylelemons/go-rpcgen/ae_example/whoami"
"github.com/kylelemons/go-rpcgen/example_ae/whoami"
"github.com/kylelemons/go-rpcgen/webrpc"
)

Expand Down
84 changes: 84 additions & 0 deletions example_ae/whoami/whoami.ae.go

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

66 changes: 3 additions & 63 deletions example_ae/whoami/whoami.pb.go

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

0 comments on commit 55a3c65

Please sign in to comment.