forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ce729e
commit f09806b
Showing
32 changed files
with
666 additions
and
25 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 |
---|---|---|
|
@@ -47,4 +47,5 @@ builds/**/images/* | |
*.jpeg | ||
|
||
# security / ssl | ||
*.pem | ||
*.pem | ||
*.Xjson |
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 |
---|---|---|
|
@@ -2,9 +2,9 @@ package main | |
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"sync" | ||
"time" | ||
"runtime" | ||
) | ||
|
||
var wg sync.WaitGroup | ||
|
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
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
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
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
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
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,55 @@ | ||
THIS PRESENTATION IS HELPFUL: | ||
https://docs.google.com/presentation/d/14NA_G-Wv5aTrRYqti5HIt3oie-wXLBSkkAt3ljEO2y0/edit?usp=sharing | ||
|
||
|
||
- activate GCS API | ||
|
||
- create a bucket | ||
-- the bucket must have a globally unique name, so maybe prefix with your app engine project ID | ||
|
||
- create a service account | ||
-- API's & AUTH | ||
---- Credentials | ||
------- generate service account | ||
-------- create new client ID | ||
-------- service account | ||
-------- GENERATE NEW P12 KEY | ||
|
||
This is the private key's password. It will not be shown again. You must present this password to use the private key. | ||
notasecret | ||
|
||
we can use this private key to connect to google cloud storage | ||
but it's in the wrong format.... | ||
|
||
app engine expects a PEM and we have a p12 | ||
we have to convert ..... | ||
|
||
https://cloud.google.com/storage/docs/authentication#converting-the-private-key | ||
|
||
# Convert the key from pkcs12 to pkcs1 (PEM). | ||
$ cat /path/to/xxxx-privatekey.p12 | openssl pkcs12 -nodes -nocerts -passin pass:notasecret | openssl rsa > /path/to/secret.pem | ||
|
||
for example: | ||
cat ~/Downloads/downloaded_file.p12 | openssl pkcs12 -nodes -nocerts -passin pass:notasecret | openssl rsa > ~/Downloads/secret.pem | ||
|
||
|
||
now we can look at this file | ||
cat ~/Downloads/secret.pem | ||
|
||
-these credentials let your local machine connect to your bucket | ||
-appengine doesn't need these credentials | ||
-we create/use them so that we can develop/test on our local machine / localhost | ||
|
||
FROM HERE: | ||
https://cloud.google.com/appengine/docs/go/googlecloudstorageclient/getstarted | ||
FOLLOW THIS STEP: | ||
- Make note of the Service Account Email Address for your project which will be in the form of <your_app_email_address>@developer.gserviceaccount.com. | ||
|
||
you will need to get your email address key from the | ||
-- API's & AUTH | ||
---- Credentials | ||
|
||
AT TERMINAL: | ||
/path/to/AppEngSDK/dev_appserver.py . --appidentity_email_address <your_app_email_address>@developer.gserviceaccount.com --appidentity_private_key_path pem_file.pem | ||
|
||
/usr/local/go_appengine/dev_appserver.py . --appidentity_email_address [email protected] --appidentity_private_key_path ~/Downloads/secret.pem |
2 changes: 1 addition & 1 deletion
2
...pengine-GCS-storage/01_NewWriter/app.yaml → ...CS-storage/01_NewWriter_PEM-auth/app.yaml
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
application: serious-water-88716 | ||
application: learning-1130 | ||
version: 1 | ||
runtime: go | ||
api_version: go1 | ||
|
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
2 changes: 1 addition & 1 deletion
2
59_appengine-GCS-storage/02/app.yaml → ...S-storage/02_NewWriter_JSON-auth/app.yaml
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
application: serious-water-88716 | ||
application: learning-1130 | ||
version: 1 | ||
runtime: go | ||
api_version: go1 | ||
|
11 changes: 11 additions & 0 deletions
11
59_appengine-GCS-storage/02_NewWriter_JSON-auth/learning-860db08c451a.Xjson
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,11 @@ | ||
{ | ||
"type": "service_account", | ||
"private_key_id": "860db08c451a6c4c0438df43fe9229a68664df82", | ||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCgW8WO4BUk6bj/\n3wPjJUel1ni7ToDjvJpM3sLjDp3AghYxW9C60XPeBU9dQlXJNS6QCX2tLHmZl3BW\ntrseue939NGtsogPkge1MsJW/jrpTdkMgM+sV2T4qx+sPYYrNAnnEAb2dCk5FQTa\nCLOvH3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IABHaYPYSqjgg6jnCd8G3rBsRQ6kcQONcdAoGBAI42\nASn5HXo8w4MqtNNqTZ606DTXH01mjF2iBkpkwx8xAw+dVTOswLdiOH9hNQsnPPFS\nIC7FubO7Dz9r21pPvG/xFkZCciRMSJHMnhSYGPfS6GOr/WEYI5OUxpLh078wszpr\noR8FPFpCxOSbHk1yaBO8yZ4trsSi5KY/XRaQWmUdAoGBAJI70uXHGJ0EDvu/HfS8\nBKKzMu9a0KH8+3BpIEq6hXGIbd2ERmM1ZAOESkjRBn56wKPdVOLgvnQagxvDxB8u\nlJWVCxGNZfO1+P3K6A3YcKAwRHq1TYzbyg6rIaNjZQin3eHf/2GxcUCjJcSLO+Ac\nk2ZkW29GR/HHKVCbsw0f0Uvz\n-----END PRIVATE KEY-----\n", | ||
"client_email": "[email protected]", | ||
"client_id": "102311566442744872575", | ||
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
"token_uri": "https://accounts.google.com/o/oauth2/token", | ||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | ||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/account-2%40learning-1130.iam.gserviceaccount.com" | ||
} |
57 changes: 57 additions & 0 deletions
57
59_appengine-GCS-storage/02_NewWriter_JSON-auth/storage.go
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,57 @@ | ||
package storage | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
|
||
"golang.org/x/oauth2/google" | ||
|
||
"golang.org/x/net/context" | ||
"google.golang.org/appengine" | ||
"google.golang.org/cloud" | ||
"google.golang.org/cloud/storage" | ||
"io/ioutil" | ||
) | ||
|
||
const gcsBucket = "learning-1130-bucket-01" | ||
const aeId = "learning-1130" | ||
|
||
func init() { | ||
http.HandleFunc("/put", handlePut) | ||
} | ||
|
||
func getCloudContext(req *http.Request) (context.Context, error) { | ||
jsonKey, err := ioutil.ReadFile("learning-860db08c451a.Xjson") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
conf, err := google.JWTConfigFromJSON( | ||
jsonKey, | ||
storage.ScopeFullControl, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ctx := appengine.NewContext(req) | ||
hc := conf.Client(ctx) | ||
return cloud.NewContext(aeId, hc), nil | ||
} | ||
|
||
func handlePut(res http.ResponseWriter, req *http.Request) { | ||
|
||
cctx, err := getCloudContext(req) | ||
if err != nil { | ||
http.Error(res, "ERROR GETTING CCTX: "+err.Error(), 500) | ||
return | ||
} | ||
|
||
writer := storage.NewWriter(cctx, gcsBucket, "exampleJSON2.txt") | ||
io.WriteString(writer, "AGAIN WITH JSON AUTH") | ||
err = writer.Close() | ||
if err != nil { | ||
http.Error(res, "ERROR WRITING TO BUCKET: "+err.Error(), 500) | ||
return | ||
} | ||
} |
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,8 @@ | ||
application: learning-1130 | ||
version: 1 | ||
runtime: go | ||
api_version: go1 | ||
|
||
handlers: | ||
- url: /.* | ||
script: _go_app |
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
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,8 @@ | ||
application: astute-curve-100822 | ||
version: 1 | ||
runtime: go | ||
api_version: go1 | ||
|
||
handlers: | ||
- url: /.* | ||
script: _go_app |
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,21 @@ | ||
package movieinfo | ||
|
||
import "net/http" | ||
|
||
type indexModel struct { | ||
} | ||
|
||
var fileServer = http.FileServer(http.Dir("public/")) | ||
|
||
func handleIndex(res http.ResponseWriter, req *http.Request) { | ||
if req.URL.Path != "/" { | ||
fileServer.ServeHTTP(res, req) | ||
return | ||
} | ||
model := &indexModel{} | ||
err := tpl.ExecuteTemplate(res, "index", model) | ||
if err != nil { | ||
http.Error(res, err.Error(), 500) | ||
return | ||
} | ||
} |
Oops, something went wrong.