From 3fb3ca8dea2d00758dd791d4f7ffecad24f42e55 Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Wed, 20 Sep 2023 23:10:34 +0800 Subject: [PATCH] update examples --- admission/example/{ => grpc}/main.go | 6 ++- admission/example/http/main.go | 57 ++++++++++++++++++++++++++++ auth/example/{ => grpc}/main.go | 0 auth/example/http/main.go | 56 +++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-) rename admission/example/{ => grpc}/main.go (88%) create mode 100644 admission/example/http/main.go rename auth/example/{ => grpc}/main.go (100%) create mode 100644 auth/example/http/main.go diff --git a/admission/example/main.go b/admission/example/grpc/main.go similarity index 88% rename from admission/example/main.go rename to admission/example/grpc/main.go index dec8b7a..ccafb3e 100644 --- a/admission/example/main.go +++ b/admission/example/grpc/main.go @@ -21,7 +21,11 @@ type server struct { func (s *server) Admit(ctx context.Context, in *proto.AdmissionRequest) (*proto.AdmissionReply, error) { reply := &proto.AdmissionReply{} - if in.GetAddr() == "127.0.0.1" { + host := in.GetAddr() + if v, _, _ := net.SplitHostPort(host); v != "" { + host = v + } + if host == "127.0.0.1" { reply.Ok = true } log.Printf("admission: %s, %v", in.GetAddr(), reply.Ok) diff --git a/admission/example/http/main.go b/admission/example/http/main.go new file mode 100644 index 0000000..0ad4507 --- /dev/null +++ b/admission/example/http/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "log" + "net" + "net/http" +) + +var ( + port = flag.Int("port", 8000, "The server port") +) + +type admissionRequest struct { + Addr string `json:"addr"` +} + +type admissionResponse struct { + OK bool `json:"ok"` +} + +func main() { + flag.Parse() + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + log.Printf("server listening at %v", lis.Addr()) + + http.HandleFunc("/admission", func(w http.ResponseWriter, r *http.Request) { + rb := admissionRequest{} + if err := json.NewDecoder(r.Body).Decode(&rb); err != nil { + log.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + resp := admissionResponse{} + + host := rb.Addr + if v, _, _ := net.SplitHostPort(host); v != "" { + host = v + } + if host == "127.0.0.1" { + resp.OK = true + } + log.Printf("admission: %s, %v", rb.Addr, resp.OK) + + json.NewEncoder(w).Encode(resp) + }) + + if err := http.Serve(lis, nil); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} diff --git a/auth/example/main.go b/auth/example/grpc/main.go similarity index 100% rename from auth/example/main.go rename to auth/example/grpc/main.go diff --git a/auth/example/http/main.go b/auth/example/http/main.go new file mode 100644 index 0000000..01f2315 --- /dev/null +++ b/auth/example/http/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "log" + "net" + "net/http" +) + +var ( + port = flag.Int("port", 8000, "The server port") +) + +type autherRequest struct { + Username string `json:"username"` + Password string `json:"password"` +} + +type autherResponse struct { + OK bool `json:"ok"` + ID string `json:"id"` +} + +func main() { + flag.Parse() + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + log.Printf("server listening at %v", lis.Addr()) + + http.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) { + rb := autherRequest{} + if err := json.NewDecoder(r.Body).Decode(&rb); err != nil { + log.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + resp := autherResponse{} + if rb.Username == "gost" && rb.Password == "gost" { + resp.OK = true + resp.ID = "gost" + } + + log.Printf("auth: %s, %s, %v", rb.Username, rb.Password, resp.OK) + + json.NewEncoder(w).Encode(resp) + }) + + if err := http.Serve(lis, nil); err != nil { + log.Fatalf("failed to serve: %v", err) + } +}