Skip to content

Commit

Permalink
Investigate gcloud functions (#22)
Browse files Browse the repository at this point in the history
* try to use gcould functions

* mv functions

* mv functions

* Try to use fn

* Elimitate embeded api keys

* Eliminate api keys from binary

* Bump version up
  • Loading branch information
kogai authored May 25, 2018
1 parent 39632ad commit ddef0af
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 82 deletions.
18 changes: 18 additions & 0 deletions .gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
# .git
# .gitignore

node_modules
#!include:.gitignore
!index.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ target/
perf.data
perf.data.old
secrets
index.js
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,3 @@ deploy:
- $PROJECT_NAME-$TRAVIS_TAG-$TARGET.*
on:
tags: true

notifications:
email:
on_success: always
on_failure: change
2 changes: 1 addition & 1 deletion Cargo.lock

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

10 changes: 1 addition & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
[package]
name = "trs"
version = "0.5.3"
version = "0.6.0"
authors = ["shinichi kogai <kogai0121@gmail.com>"]
categories = ["command-line-utilities"]

[[bin]]
name = "trs"
path = "src/main.rs"

[[bin]]
name = "server"
path = "src/server.rs"

[dependencies]
reqwest = "0.5.1"
hyper = "0.11"
Expand Down
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ PWD := $(shell pwd)
GIT_HASH := $(shell git log --pretty=format:"%H" | head -n1)
GCP_PJ_ID := trslt-165501
GCP_REPO := asia.gcr.io/$(GCP_PJ_ID)/github-kogai-trs:$(GIT_HASH)
GCP_FN_NAME := helloGET
GCP_FN_ENDPOINT := https://us-central1-$(GCP_PJ_ID).cloudfunctions.net/$(GCP_FN_NAME)
GOOGLE_CLOUD_PLATFORM_API_KEY := "${GOOGLE_CLOUD_PLATFORM_API_KEY}"
OXFORD_API_ID := "${OXFORD_API_ID}"
OXFORD_API_KEY := "${OXFORD_API_KEY}"
OS := $(shell uname)
VERSION := $(shell cat Cargo.toml | grep version | sed -e 's/version\ =\ \"\(.*\)\"/\1/')

bin/$(OS)/$(NAME): Cargo.toml $(SRC)
GOOGLE_CLOUD_PLATFORM_API_KEY=$(GOOGLE_CLOUD_PLATFORM_API_KEY) && \
OXFORD_API_ID=$(OXFORD_API_ID) && \
OXFORD_API_KEY=$(OXFORD_API_KEY) && \
GCP_FN_ENDPOINT=$(GCP_FN_ENDPOINT ) && \
cargo build --release
mkdir -p bin/$(OS)
cp target/release/$(NAME) bin/$(OS)/$(NAME)
Expand Down Expand Up @@ -74,6 +74,12 @@ clean:
rm -rf bin
cargo clean

.PHONY: functions
functions:
gcloud config set project trslt-165501
gcloud beta functions deploy $(GCP_FN_NAME) --trigger-http
curl -d "key=value" $(GCP_FN_ENDPOINT)

.PHONY: release
release:
git tag -af "v${VERSION}" -m ""
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "trs-functions",
"version": "0.0.1",
"main": "index.js",
"license": "MIT"
}
30 changes: 0 additions & 30 deletions src/api.rs

This file was deleted.

27 changes: 26 additions & 1 deletion src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use reqwest::Client;
use serde_json;
use std::collections::HashMap;
use std::env;
Expand All @@ -11,19 +12,42 @@ const DEFAULT_TARGET_LANGUAGE: &'static str = "ja";
const CACHE_FILE: &'static str = ".trs-cache";
const HIGH_WATER_MARK: u64 = 16 * 1000; // 16kib
const NULL: u8 = 0;
const CONFIG_ENDPOINT: &'static str = env!("GCP_FN_ENDPOINT");

pub enum Namespace {
Translate,
Dictionary,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct ApiKeys {
pub gcloud_translate_api_key: String,
pub oxford_api_id: String,
pub oxford_api_key: String,
}

impl ApiKeys {
pub fn new() -> Self {
let http_client = Client::new().expect("Create HTTP client is failed");
let mut buffer = String::new();
http_client
.get(CONFIG_ENDPOINT)
.send()
.expect("send Request failed")
.read_to_string(&mut buffer)
.expect("read response failed");
serde_json::from_str::<ApiKeys>(&buffer).expect("Cloud function does not return value propery")
}
}

#[derive(Deserialize, Serialize, Clone, Debug)]
struct FsCacheValue(SystemTime, String);

#[derive(Deserialize, Serialize)]
pub struct FSCache {
version: u8,
language: String,
pub api_keys: ApiKeys,
translate: HashMap<String, HashMap<String, FsCacheValue>>,
dictionary: HashMap<String, FsCacheValue>,
}
Expand Down Expand Up @@ -63,10 +87,11 @@ impl FSCache {
None => {
let _ = fs::File::create(cache_file);
let mut fs_cache = FSCache {
version: 2,
version: 3,
language: DEFAULT_TARGET_LANGUAGE.to_owned(),
translate: HashMap::new(),
dictionary: HashMap::new(),
api_keys: ApiKeys::new(),
};
fs_cache.update_cache();
fs_cache
Expand Down
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ fn translate_between(
match fs_cache.get(&namespace, &query_text) {
Some(definitions) => definitions,
None => {
let new_def = translate::translate(&target_language, &query_text);
let new_def = translate::translate(
&target_language,
&query_text,
(&fs_cache).api_keys.gcloud_translate_api_key.to_owned(),
);
fs_cache.set(&namespace, &query_text, &new_def);
new_def
}
Expand Down Expand Up @@ -89,7 +93,10 @@ fn run() {
let default_language = fs_cache.get_language();

if matches.is_present("languages") {
let result = translate::language(&default_language);
let result = translate::language(
&default_language,
(&fs_cache).api_keys.gcloud_translate_api_key.to_owned(),
);
println!("{}", result);
exit(0);
}
Expand All @@ -115,7 +122,7 @@ fn run() {
let definitions = match fs_cache.get(&namespace, &escaped_query_words) {
Some(definitions) => definitions,
None => {
let new_def = oxford::definitions(query_words);
let new_def = oxford::definitions(query_words, (&fs_cache).api_keys.to_owned());
fs_cache.set(&namespace, &escaped_query_words, &new_def);
new_def
}
Expand Down
13 changes: 7 additions & 6 deletions src/oxford.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use oxford_dictionary_api_rs::{apis, models};

use cache::ApiKeys;
use hyper::Client;
use hyper_tls::HttpsConnector;
use tokio_core::reactor::Core;
use std::fmt::{self, Display, Formatter};
use termion::{color, style};
use tokio_core::reactor::Core;
use utils::space_to_underscore;

pub fn definitions(query_words: Vec<String>) -> String {
match call(query_words) {
pub fn definitions(query_words: Vec<String>, api_keys: ApiKeys) -> String {
match call(query_words, api_keys) {
Ok(result) => enumlate_examples(result)
.iter()
.map(|d| format!("{}", d))
Expand All @@ -18,7 +19,7 @@ pub fn definitions(query_words: Vec<String>) -> String {
}
}

fn call(query_words: Vec<String>) -> Result<models::RetrieveEntry, String> {
fn call(query_words: Vec<String>, api_keys: ApiKeys) -> Result<models::RetrieveEntry, String> {
let query_text = space_to_underscore(&query_words.join(" "));
let mut core = Core::new().unwrap();
let handle = core.handle();
Expand All @@ -33,8 +34,8 @@ fn call(query_words: Vec<String>) -> Result<models::RetrieveEntry, String> {
.entries_source_lang_word_id_get(
"en",
&query_text,
env!("OXFORD_API_ID"),
env!("OXFORD_API_KEY"),
api_keys.oxford_api_id.to_owned().as_str(),
api_keys.oxford_api_key.to_owned().as_str(),
);

match core.run(work) {
Expand Down
17 changes: 0 additions & 17 deletions src/server.rs

This file was deleted.

13 changes: 6 additions & 7 deletions src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::io::Read;

const ENDPOINT_GOOGLE: &'static str = "https://translation.googleapis.com/language/translate/v2";
const API_LANGUAGES: &'static str = "/languages";
const API_KEY: &'static str = env!("GOOGLE_CLOUD_PLATFORM_API_KEY");

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct ErrorReason {
Expand Down Expand Up @@ -44,9 +43,9 @@ enum Method {
Post,
}

fn request(path: String, method: Method) -> String {
fn request(path: String, method: Method, api_key: String) -> String {
let http_client = Client::new().expect("Create HTTP client is failed");
let url = format!("{}{}&key={}", ENDPOINT_GOOGLE, path, API_KEY);
let url = format!("{}{}&key={}", ENDPOINT_GOOGLE, path, api_key);
let mut buffer = String::new();
match method {
Method::Get => http_client.get(url.as_str()),
Expand All @@ -59,9 +58,9 @@ fn request(path: String, method: Method) -> String {
buffer
}

pub fn translate(target_language: &String, query_text: &String) -> String {
pub fn translate(target_language: &String, query_text: &String, api_key: String) -> String {
let path = format!("?q={}&target={}&format=text", query_text, target_language);
let buffer = request(path, Method::Post);
let buffer = request(path, Method::Post, api_key);
let response = serde_json::from_str::<Translate>(&buffer).unwrap();

match response {
Expand Down Expand Up @@ -92,9 +91,9 @@ enum Language {
Error { error: Errors },
}

pub fn language(target_language: &String) -> String {
pub fn language(target_language: &String, api_key: String) -> String {
let path = format!("{}?target={}", API_LANGUAGES, target_language);
let buffer = request(path, Method::Get);
let buffer = request(path, Method::Get, api_key);

let response = serde_json::from_str::<Language>(&buffer).unwrap();

Expand Down

0 comments on commit ddef0af

Please sign in to comment.