こんにちは。
Go言語って、書いていてとても楽しい言語ですよね?
今日は、terminalAPIを作成しようと思います。
<目次>
諸注意
本サンプルは、MacOS環境で動かしています。
概要
例えば、以下のコマンドをterminal上で打ち込むとGo言語のバージョンが出力されます。
1 2 3 |
go version go version go1.14 darwin/amd64 |
このコマンドをリモート上のサーバーで実行したい場合は、SSHログインか何かしてリモートサーバー上にログインし、コマンドを実行しなければなりません。
そこで、ローカルから情報を取得できるAPIを自作して、リモートサーバーへのログインなしで、情報を取得できるようにします!
Go version取得API
まずは、簡単にGo Versionを取得できるAPIを作成してみましょう。
次のモジュールをインストールしてください。
1 |
go get github.com/julienschmidt/httproute |
このモジュールは、簡単にhttp routerを作成することができるモジュールです。
また、次のファイルを任意の場所に作成してください。
1 |
touch main.go |
実装例は以下に記述します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package main import ( "io" "log" "net/http" "os/exec" "github.com/julienschmidt/httprouter" ) func main() { router := httprouter.New() // curl -X GET http://localhost:8000/api/v1/go-version router.GET("/api/v1/go-version", goVersion) // localhost:8000で起動 log.Fatal(http.ListenAndServe(":8000", router)) } // terminalから受け取ったコマンドを実行 func getCommandOutput(command string, arguments ...string) string { out, _ := exec.Command(command, arguments...).Output() return string(out) } // Goバージョンを取得 func goVersion(w http.ResponseWriter, r *http.Request, params httprouter.Params) { response := getCommandOutput("/usr/local/go/bin/go", "version") io.WriteString(w, response) return } |
main関数では、localhost:8000でAPIサーバーにアクセスできるように処理を設定しています。
サーバーを立ち上げたら「curl -X GET http://localhost:8000/api/v1/go-version」を実行するとGo Versionが取得できる仕組みです。
getCommandOutput関数では、terminal上で取得したコマンドを渡してあげます。ここでは、「/usr/local/go/bin/go」と「version」ですね。
goVersion関数は、API URLにアクセスした時に実行される関数として定義しています。
本来ならリモートサーバーに設置したところですが、ローカルで動作確認してみましょう。
1 |
go run main.go |
次に、terminal上からCurlを実行します。
1 2 3 |
curl -X GET http://localhost:8000/api/v1/go-version go version go1.11 darwin/amd64 |
これは、使えますね!
今までは、リモートサーバーにログインしないと取得できない情報をローカル上で取得できるようになります。
ログ情報取得 API
もう少し、実践的な使い方をみていきましょう。
例えば、「リモートサーバー上のログを取得したい」などの要求に答えてみることにしましょう。
先ほどのプログラムを少し改造します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package main import ( "fmt" "log" "net/http" "os/exec" "github.com/julienschmidt/httprouter" ) func main() { router := httprouter.New() router.GET("/api/v1/show-log/:name", getLogContent) log.Fatal(http.ListenAndServe(":8000", router)) } func getCommandOutput(command string, arguments ...string) string { out, _ := exec.Command(command, arguments...).Output() return string(out) } func getLogContent(w http.ResponseWriter, r *http.Request, params httprouter.Params) { fmt.Fprintf(w, getCommandOutput("/bin/cat", params.ByName("name"))) } |
main.goファイルと同階層に、ログファイルを作成しましょう。
1 2 3 4 5 6 7 8 9 10 |
touch sample.log ## 適当なログを入力 ``` #Software: Microsoft Internet Information Services 10.0 #Version: 1.0 #Date: 2017-11-18 00:01:07 #Fields: date time cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs-version cs(User-Agent) cs(Cookie) cs(Referer) cs-host sc-status sc-substatus sc-win32-status sc-bytes cs-bytes time-taken 2017-11-18 08:48:20 GET /de adpar=12345&gclid=1234567890 443 - 149.172.138.41 HTTP/2.0 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/62.0.3202.89+Safari/537.36+OPR/49.0.2725.39 - https://www.google.de/ www.site-logfile-explorer.com 301 0 0 624 543 46 ``` |
それでは、プログラムを実行してみましょう。
1 2 3 4 5 6 7 8 9 |
go run main.go $ curl -X GET http://localhost:8000/api/v1/show-log/sample.log #Software: Microsoft Internet Information Services 10.0 #Version: 1.0 #Date: 2017-11-18 00:01:07#Fields: date time cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs-version cs(User-Agent) cs(Cookie) cs(Ref erer) cs-host sc-status sc-substatus sc-win32-status sc-bytes cs-bytes time-taken2017-11-18 08:48:20 GET /de adpar=12345&gclid=1234567890 443 - 149.172.138.41 HTTP/2.0 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/62.0.3202.89+Safari/537.36+OPR/49.0.2725.39 - https://www.google.de/ www.site-logfile-explorer.com 301 0 0 624 543 46 |
うん。これは便利そうですね!
終わりに
Go言語の便利な活用方法がまた一つ明らかになりました^^
Go言語でガンガンツールを作りまくって、業務改善しましょう!
それでは、また!!
コメントを残す
コメントを投稿するにはログインしてください。