Upgrade to Pro — share decks privately, control downloads, hide ads and more …

はてなリモートインターン2021 Web API 講義資料

Hatena
October 08, 2021

はてなリモートインターン2021 Web API 講義資料

Hatena

October 08, 2021
Tweet

More Decks by Hatena

Other Decks in Technology

Transcript

  1. )551סذّؕٚؕ٤  )551  )551  )551  (PPHMFֿ41%:؅ 氦辐

     (PPHMFֿ26*$؅ 氦辐  41%:؅⩕מ׊ג)551ס嘅嶖⴫  )551PWFS26*$؅)551מ䷉ ⺲  26*$ס嘅嶖⴫
  2. )551 ˝ 3'$)ZQFSUFYU5SBOTGFS1SPUPDPM)551 ˝ 3'$)ZQFSUFYU5SBOTGFS1SPUPDPM)551 ˝ 3'$)ZQFSUFYU5SBOTGFS1SPUPDPM )551 .FTTBHF4ZOUBYBOE3PVUJOH ˝

    3'$)ZQFSUFYU5SBOTGFS1SPUPDPM )551 4FNBOUJDTBOE$POUFOU ˝ 3'$)ZQFSUFYU5SBOTGFS1SPUPDPM )551 $POEJUJPOBM3FRVFTUT ˝ 3'$)ZQFSUFYU5SBOTGFS1SPUPDPM )551 3BOHF3FRVFTUT ˝ 3'$)ZQFSUFYU5SBOTGFS1SPUPDPM )551 $BDIJOH ˝ 3'$)ZQFSUFYU5SBOTGFS1SPUPDPM )551 "VUIFOUJDBUJPO
  3. HTTP/1.1 301 Moved Permanently Server: awselb/2.0 Date: Mon, 03 Aug

    2020 01:58:38 GMT Content-Type: text/html Content-Length: 150 Connection: keep-alive Location: https:!"www.hatena.ne.jp:443/ <html> <head><title>301 Moved Permanently!#title>!#head> <body bgcolor="white"> <center><h1>301 Moved Permanently!#h1>!#center> !#body> !#html>
  4. ٛؠؙتع车 METHOD target HTTP/1.1 GET POST PUT HEAD DELETE OPTIONS

    TRACE CONNECT PATCH / /search?q=text HTTP/1.0 HTTP/1.1
  5. نٝ٭ّ +-----------------------------------------------+ | Length (24) | +---------------+---------------+---------------+ | Type (8)

    | Flags (8) | +-+-------------+---------------+-------------------------------+ |R| Stream Identifier (31) | +=+=============================================================+ | Frame Payload (0!!") !!" +---------------------------------------------------------------+ )ZQFSUFYU5SBOTGFS1SPUPDPM7FSTJPO )551 'SBNF'PSNBU
  6. %"5"نٝ٭ّ0x0 +---------------+ |Pad Length? (8)| +---------------+-----------------------------------------------+ | Data (*) !!"

    +---------------------------------------------------------------+ | Padding (*) !!" +---------------------------------------------------------------+ END_STREAM 0x1 ]PADDED 0x8
  7. )&"%&34نٝ٭ّ0x1 +---------------+ |Pad Length? (8)| +-+-------------+-----------------------------------------------+ |E| Stream Dependency? (31)

    | +-+-------------+-----------------------------------------------+ | Weight? (8) | +-+-------------+-----------------------------------------------+ | Header Block Fragment (*) !!" +---------------------------------------------------------------+ | Padding (*) !!" +---------------------------------------------------------------+ END_STREAM 0x1 ]END_HEADERS 0x4 ]PADDED 0x8 ]PRIORITY 0x20
  8. *OEFY )FBEFS/BNF )FBEFS7BMVF  :authority  :method GET  :method

    POST  :path /  :path /index.html  :scheme http  :scheme https  :status 200
  9. (JU)VCסؕبٖ٭ؤْ٤ع ˝ GET /repos/:owner/:repo/issues/ comments/:comment_id ˝ POST /repos/:owner/:repo/issues/:issue_number/ comments ˝

    PATCH /repos/:owner/:repo/issues/ comments/:comment_id ˝ DELETE /repos/:owner/:repo/issues/ comments/:comment_id
  10. 4%- 4DIFNB%FOJUJPO-BOHVBHF interface Actor { login: String! } type Issue

    { author: Actor body: String! title: String! } type Repository { issue(number: Int!): Issue } type Query { repository(name: String!, owner: String!): Repository }
  11. syntax = "proto3"; package account; service Account { rpc Signup(SignupRequest)

    returns (SignupReply); } message SignupRequest { string name = 1; string password = 2; } message SignupReply { string token = 1; }
  12. ْشج٭ة㑔 message SignupRequest { string name = 1; string password

    = 2; } ϑΟʔϧυܕ ϑΟʔϧυ໊ = ϑΟʔϧυ൪߸; ˝ نؔ٭ٜغ樑⺘ֿꄆ釐 ˝ ױךעCZUF ˝ ע◀硜岷ײ
  13. repeated Result results = 1; map<string, Project> projects = 3;

    oneof test_oneof { string name = 4; SubMessage sub_message = 9; }
  14. message SearchRequest { string query = 1; int32 page_number =

    2; int32 result_per_page = 3; enum Corpus { UNIVERSAL = 0; WEB = 1; IMAGES = 2; LOCAL = 3; NEWS = 4; PRODUCTS = 5; VIDEO = 6; } Corpus corpus = 4; }
  15. import "google/protobuf/any.proto"; message Message { google.protobuf.Any field = 1; }

    ˝ google.protobuf.Empty ˝ google.protobuf.Duration ˝ google.protobuf.Timestamp
  16. ◍䬵䓪 ˝ نؔ٭ٜغ樑⺘ int32 old_field = 6 [deprecated = true];

    reserved 2, 15, 9 to 11; reserved "foo", "bar";
  17. ؛وب٘٤ option go_package = "github.com/hatena/example/pb"; enum EnumAllowingAlias { option allow_alias

    = true; UNKNOWN = 0; STARTED = 1; RUNNING = 1; } message Example { int32 old_field = 6 [deprecated = true]; }
  18. ئ٭لتכتعٛ٭ّ service RouteGuide { rpc GetFeature(Point) returns (Feature) {} rpc

    ListFeatures(Rectangle) returns (stream Feature) {} rpc RecordRoute(stream Point) returns (RouteSummary) {} rpc RouteChat(stream RouteNote) returns (stream RouteNote) {} }
  19. syntax = "proto3"; option go_package = "./pb"; package welcome; service

    Welcome { rpc Greet(GreetRequest) returns (GreetReply); } message GreetRequest { string name = 1; } message GreetReply { string message = 1; }
  20. 1SPUPDPM$PNQJMFS protoc $ # protoc ͸ brew install protobuf ΍

    apt install protobuf-compiler ͳͲͰ $ go install google.golang.org/protobuf/cmd/protoc-gen-go $ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc $ protoc !"go_out=. !"go-grpc_out=. welcome.proto
  21. type welcomeServer struct { pb.UnimplementedWelcomeServer } func (s *welcomeServer) Greet(

    ctx context.Context, req *pb.GreetRequest ) (*pb.GreetReply, error) { return &pb.GreetReply{ Message: fmt.Sprintf("Welcome %s", req.Name), }, nil } func newServer() *welcomeServer { return &welcomeServer{} }
  22. func main() { lis, err !" net.Listen("tcp", fmt.Sprintf(":%d", *port)) if

    err !# nil { log.Fatalf("failed to listen: %v", err) } grpcServer !" grpc.NewServer() pb.RegisterWelcomeServer(grpcServer, newServer()) reflection.Register(grpcServer) go func() { log.Printf("start gRPC server port: %v", *port) grpcServer.Serve(lis) }() quit !" make(chan os.Signal) signal.Notify(quit, os.Interrupt) !$quit log.Printf("stopping gRPC server!!%") grpcServer.GracefulStop() }
  23. grpc_cli $ docker run !"rm !"publish 10000:10000 hatena/intern-2020-grpc-adventure:latest $ docker

    run !"rm hatena/grpc_cli:latest ls host.docker.internal:10000 -l
  24. grpc_cli $ grpc_cli ls localhost:10000 -l $ grpc_cli type localhost:10000

    welcome.GreetRequest $ grpc_cli call localhost:10000 welcome.Welcome.Greet 'name: "John Appleseed"'