- 投稿日:2021-01-03T23:08:46+09:00
QiitaAPIでインフラエンジニアyutaさんのストック記事を取得する
インフラエンジニアyutaさんが主催のオンラインサロンで実施したもくもく会の時間で作ったプログラムになります。
GoでAPIを利用して情報を取得するための勉強をしていた時に作りました。ソースコード
main.gopackage main import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http" ) type Item struct { Title string `json:"title"` User Users } type Users struct { Name string `json:"name"` } func main() { targetName := "yuta_vamdemic" getUrl := "http://qiita.com/api/v2/users/" + targetName + "/stocks?page=1&per_page=5" resp, err := http.Get(getUrl) if err != nil { log.Fatal(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } var data []Item if err := json.Unmarshal(body, &data); err != nil { log.Fatal(err) } fmt.Printf("##[%s]さんのストック記事##\n", targetName) for _, item := range data { fmt.Printf("%s %s\n", item.User.Name, item.Title) } }実行結果
##[yuta_vamdemic]さんのストック記事## PowerShellでSQL Server Management Studioをサイレントインストールする [Terraform]CodeBuildでTerraformリポジトリのCIを行う こめたん React初心者のための Material-UI 【Dialogの使い方編】 四角 最初に一回だけ動くuseEffect(componentDidMount的な) seira React hooksを基礎から理解する (useState編)ポイント
このあたりの構造体の書き方がわからなくて悩みました。
type Item struct { Title string `json:"title"` User Users } type Users struct { Name string `json:"name"` }感想
ストック記事を取得するって、ストーカーっぽいですよね?
最近忙しくてサロンに参加できてないなあ。
- 投稿日:2021-01-03T20:08:36+09:00
AWS AmplifyでLambdaを使うwith Go
Amplifyはちょくちょく使う機会があるのですが、Amplifyを介したLambdaってほぼ使った事ないぞ、と思ったので使ってみようと思い立ちました。
そしてLambdaでGoが使えるけど、ちゃんと使った事なかった気がするので使ってみたいなとついでに。Functionが使えるらしい
FUNCTIONS - Overview
https://docs.amplify.aws/cli/function環境
- win10 Home(surface go) + WSL2 + ubuntu18.04
- node v14.1.0
- npm 6.14.5
- go version go1.10.4 linux/amd64
使ってみる
前置き
vueを使う設定にてamplifyをsetupしておきます。
詳しくはこのあたりから。
https://docs.amplify.aws/start/getting-started/installation/q/integration/vue$ vue create myamplifyproject $ cd myamplifyproject $ npm install $ amplify init $ npm install aws-amplify @aws-amplify/ui-vue $ vi src/main.js $ npm run serveadd function
$ amplify add function ? Select which capability you want to add: Lambda function (serverless function) ? Provide a friendly name for your resource to be used as a label for this category in the project: myamplifyproject3e7c 0b2b ? Provide the AWS Lambda function name: myamplifyproject3e7c0b2b ? Choose the runtime that you want to use: Go go executable was not found in PATH, make sure it's available. It can be installed from https://golang.org/doc/install Only one template found - using Hello World by default. ? Do you want to access other resources in this project from your Lambda function? No ? Do you want to invoke this function on a recurring schedule? No ? Do you want to configure Lambda layers for this function? No ? Do you want to edit the local lambda function now? Yes Please edit the file in your editor: /mnt/c/Users/user/myamplifyproject/amplify/backend/function/myamplifyproject3e7c0b2b/src/main.go ? Press enter to continue Successfully added resource myamplifyproject3e7c0b2b locally. Next steps: Check out sample function code generated in <project-dir>/amplify/backend/function/myamplifyproject3e7c0b2b/src "amplify function build" builds all of your functions currently in the project "amplify mock function <functionName>" runs your function locally "amplify push" builds all of your local backend resources and provisions them in the cloud "amplify publish" builds all of your local backend and front-end resources (if you added hosting category) and provisions them in the cloudそうすると
main.go
ってファイルができました。main.gopackage main import ( "fmt" "context" "github.com/aws/aws-lambda-go/lambda" ) type MyEvent struct { Name string `json:"name"` } func HandleRequest(ctx context.Context, name MyEvent) (string, error) { return fmt.Sprintf("Hello %s!", name.Name ), nil } func main() { lambda.Start(HandleRequest) }
github.com/aws/aws-lambda-go/lambda
をGETしておきましょう。$ go get github.com/aws/aws-lambda-go/lambda
試しに実行
$ amplify function build ? Are you sure you want to continue building the resources? Yes ✔ All resources are built. $ amplify mock function myamplifyproject3e7c0b2b ? Provide the path to the event JSON object relative to /mnt/c/Users/masra/myamplifyproject/amplify/backend/function/mya mplifyproject3e7c0b2b src/event.json Starting execution... Local invoker binary was not found, building it... Launching Lambda process, port: 8000 Result: Hello Amplify! Finished execution.
amplify function build
でbuildした後、amplify mock function [functionname]
でローカル実行できるみたいです。
なんかsamみたいな仕組み使ってそうだなー。deploy
おなじみの
amplify push
&lify publish
でdeployできます。cli使ってる人はgit経由ですかね
その前にhosting add
もしておきますね。お試しなのでS3に直deployします。$ amplify hosting add ? Select the plugin module to execute Amazon CloudFront and S3 ? Select the environment setup: DEV (S3 only with HTTP) ? hosting bucket name myamplifyproject-20210102225615-hostingbucket ? index doc for the website index.html ? error doc for the website index.html You can now publish your app using the following command: Command: amplify publish$ amplify push | Category | Resource name | Operation | Provider plugin | | -------- | ------------------------ | --------- | ----------------- | | Function | myamplifyproject3e7c0b2b | Create | awscloudformation | | Hosting | S3AndCloudFront | Create | awscloudformation | ? Are you sure you want to continue? Yes $ amplify publish ✔ Successfully pulled backend environment test from the cloud. Current Environment: test | Category | Resource name | Operation | Provider plugin | | -------- | ------------------------ | --------- | ----------------- | | Function | myamplifyproject3e7c0b2b | Create | awscloudformation | | Hosting | S3AndCloudFront | Create | awscloudformation | ? Are you sure you want to continue? Yes ⠋ Updating resources in the cloud. This may take a few minutes... ... ✔ All resources are updated in the cloud Hosting endpoint: http://myamplifyproject-20210102233234-hostingbucket-test.s3-website-ap-northeast-1.amazonaws.com > myamplifyproject@0.1.0 build /mnt/c/Users/masra/myamplifyproject > vue-cli-service build ⠋ Building for production... ... frontend build command exited with code 0 Publish started for S3AndCloudFront ✔ Uploaded files successfully. Your app is published successfully. http://myamplifyproject-20210102233234-hostingbucket-test.s3-website-ap-northeast-1.amazonaws.com・・とまあdeployできました。
vueのdefault画面は表示できてます。functionはというと・・こんな感じで作成されてました。
はて?api gatewayも何もないけどどうやって実行するのだろう?
AppSyncの裏で使ったり、Scheduleから起動する方法しか書かれてないなー。
webapi的な使い方は想定されてないのだろうか。amplify add apiしてみる。
REST APIとして使えば使えるのでは?と思ったので試してみます。
$ amplify add api ? Please select from one of the below mentioned services: REST ? Provide a friendly name for your resource to be used as a label for this category in the project: api090983e4 ? Provide a path (e.g., /book/{isbn}): /items ? Choose a Lambda source Use a Lambda function already added in the current Amplify project ? Choose the Lambda function to invoke by this path myamplifyproject3e7c0b2b ? Restrict API access Yes ? Who should have access? Authenticated and Guest users ? What kind of access do you want for Authenticated users? create, read, update, delete ? What kind of access do you want for Guest users? create, read, update, delete Successfully added auth resource locally. ? Do you want to add another path? No Successfully added resource api090983e4 locally Some next steps: "amplify push" will build all your local backend resources and provision it in the cloud "amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloudそして
amplify push
します。$ amplify push ✔ Successfully pulled backend environment test from the cloud. Current Environment: test | Category | Resource name | Operation | Provider plugin | | -------- | ------------------------ | --------- | ----------------- | | Auth | cognito557e6fef | Create | awscloudformation | | Api | api090983e4 | Create | awscloudformation | | Function | myamplifyproject3e7c0b2b | No Change | awscloudformation | | Hosting | S3AndCloudFront | No Change | awscloudformation | ? Are you sure you want to continue? Yes ⠼ Updating resources in the cloud. This may take a few minutes... ... REST API endpoint: https://uyyhhs6ca8.execute-api.ap-northeast-1.amazonaws.com/testお。でけた。
しかし、
{"message":"Missing Authentication Token"}
と言われます。
なんかAuth
のリソースも勝手につくられちゃってますしね。Cognitoの認証通ってないと実行できない感じなんでしょう。Amplifyから使うのであればそれでただしい。ただ、今回はその辺はすっとばしてとりあえず実行だけしたい。
Do you want to access other resources in this project from your Lambda function?
って所でNo
にしちゃったのが要因な気がするので今度はYes
にしてみます。Add funtion
$ amplify function add ? Select which capability you want to add: Lambda function (serverless function) ? Provide a friendly name for your resource to be used as a label for this category in the project: myamplifyproject2451 e9a2 ? Provide the AWS Lambda function name: myamplifyproject2451e9a2 ? Choose the runtime that you want to use: Go Only one template found - using Hello World by default. ? Do you want to access other resources in this project from your Lambda function? Yes ? Select the category You can access the following resource attributes as environment variables from your Lambda function ? Do you want to invoke this function on a recurring schedule? No ? Do you want to configure Lambda layers for this function? No ? Do you want to edit the local lambda function now? Yes Please edit the file in your editor: /mnt/c/Users/user/myamplifyproject/amplify/backend/function/myamplifyproject2451e9a2/src/main.go ? Press enter to continue Successfully added resource myamplifyproject2451e9a2 locally. Next steps: Check out sample function code generated in <project-dir>/amplify/backend/function/myamplifyproject2451e9a2/src "amplify function build" builds all of your functions currently in the project "amplify mock function <functionName>" runs your function locally "amplify push" builds all of your local backend resources and provisions them in the cloud "amplify publish" builds all of your local backend and front-end resources (if you added hosting category) and provisions them in the cloud$ $ amplify add api ? Please select from one of the below mentioned services: REST ? Provide a friendly name for your resource to be used as a label for this category in the project: api62457bd3 ? Provide a path (e.g., /book/{isbn}): /hage ? Choose a Lambda source Use a Lambda function already added in the current Amplify project ? Choose the Lambda function to invoke by this path myamplifyproject2451e9a2 ? Restrict API access Yes ? Who should have access? Authenticated and Guest users ? What kind of access do you want for Authenticated users? create, read, update, delete ? What kind of access do you want for Guest users? create, read, update, delete ? Do you want to add another path? No Successfully added resource api62457bd3 locally Some next steps: "amplify push" will build all your local backend resources and provision it in the cloud "amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud$ amplify push ✔ Successfully pulled backend environment test from the cloud. Current Environment: test | Category | Resource name | Operation | Provider plugin | | -------- | ------------------------ | --------- | ----------------- | | Api | api7dcd31c5 | Create | awscloudformation | | Auth | cognito557e6fef | Create | awscloudformation | | Api | api090983e4 | Create | awscloudformation | | Function | myamplifyproject3e7c0b2b | No Change | awscloudformation | | Function | myamplifyproject2451e9a2 | Create | awscloudformation | | Hosting | S3AndCloudFront | No Change | awscloudformation | ? Are you sure you want to continue? Yes ⠼ Updating resources in the cloud. This may take a few minutes... ... REST API endpoint: https://xdewrmoiva.execute-api.ap-northeast-1.amazonaws.com/testん~・・
でもダメっすねー。IAM認証がついてるのでToken必須か。https://github.com/aws-amplify/amplify-cli/blob/fad6377bd384862ca4429cb1a83eee90efd62b58/packages/amplify-category-api/src/provider-utils/awscloudformation/service-walkthroughs/apigw-walkthrough.ts#L246
https://github.com/aws-amplify/amplify-cli/blob/master/packages/amplify-category-api/resources/awscloudformation/cloudformation-templates/apigw-cloudformation-template-default.json.ejs#L248このへんとかみてもIAM認証必須っぽいかな。authでもunauthでも。
まあ、本来はAmplifyの認証を介して使うものだからそれでいいんでしょうけどね。しかたないのでVueを介してAPIを叩いてみる。
なんで、ちゃんとAPI叩くようにしてみます。
authはadd apiの時に入っちゃったぽいので、vueを少し書き換えるだけ。
HelloWorld.vueを再利用してます。
なお、Amplifyの認証は行いません。Guestユーザーで叩いてる想定。それでもAmplifyのライブラリを使うとAPIリクエストの際にTokenが乗ってくるので叩ける・・はず。src/components/HelloWorld.vue<template> <div class="hello"> <h1>{{ msg }}</h1> <button @click="getHelloWorld">API.post</button> </div> </template> <script> import { API } from 'aws-amplify'; export default { name: 'HelloWorld', data: function() { return { msg: "" } }, methods: { getHelloWorld() { API.post('api5afc9d6e', '/hage', { response: true, body: { text: "Amplify" }, headers: {} }).then(result => { console.log(result.data) this.msg = result.data.text }).catch(err => { console.log(err) }) } } } </script>で、そのままAPI叩くとCORSでエラーになったのでResponse HeaderにAccess-Allow-Originしてやらないといけないかも。
GO+Lambdaのあたりはこちらを参考にさせていただきます。
https://qiita.com/coil_msp123/items/b1751dbe74ada7fdd5a1で、main.goはこういう感じに。
やっつけですみません。main.gopackage main import ( "fmt" "log" "encoding/json" "github.com/aws/aws-lambda-go/lambda" "github.com/aws/aws-lambda-go/events" ) func HandleRequest(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { reqBody := request.Body req := make(map[string]string) json.Unmarshal([]byte(reqBody), &req) req["text"] = fmt.Sprintf("Hello, %s!", req["text"]) bytes, _ := json.Marshal(req) log.Print(string(bytes)) return events.APIGatewayProxyResponse{ Body: string(bytes), StatusCode: 200, Headers: map[string]string{ "Content-Type": "application/json", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "withcredentials,origin,Accept,Authorization,Content-Type", }, }, nil } func main() { lambda.Start(HandleRequest) }
go get github.com/aws/aws-lambda-go/events
も忘れずに。
で、amplify function build
とamplify function update
も実行しておきます。
さらにはamplify push
も。でもろもろ終わったら、
npm run serve
しつつ、locahost:8080
を開きまして、、いけましたね。
最後に
$ amplify delete
はわすれません。エンドポイント名とかむき出しにしてましたし、ちゃんと消しておく。
- 投稿日:2021-01-03T13:33:12+09:00
vim学習ゲーム「PacVim」をGoで作ってみた
前置き
パックマンをvimコマンドで操作するゲーム「PacVim」をGoで作ってみました。
Goなので、バイナリをダウンロードしていただければ簡単に実行できます。
https://github.com/TurtleBuild/pacvim/tree/master/bin興味があれば遊んでみてください。
趣味で作った程度のものなので期待しないでください。。。ゲーム仕様
オブジェクト
char what it does o 餌 X 毒 G ゴースト ゲーム状態
state conditions ゲームクリア 全10ステージをクリアする ゲームオーバー 計4回ステージを失敗する ステージ成功 すべての餌を食べる ステージ失敗 毒を食べてしまう
ゴーストに捕まってしまう操作方法
key what it does q ゲーム終了 h 左に1マス移動 j 下に1マス移動 k 上に1マス移動 l 右に1マス移動 w 次の単語の先頭に移動 e 次の単語の末尾に移動 b 前の単語の先頭に移動 $ 行末に移動 0 行頭に移動 gg/1G 最初の行の行頭に移動 NG N行目の行頭に移動 G 最後の行の行頭に移動 ^ 現在の行の最初の単語の先頭に移動 補足
w,e,b
$,0,gg,NG,G,^
自作マップで遊びたい場合
- マップファイルを置き換える(./files/stage/)
- statikを実行する
$ statik -src=files参考