update examples
This commit is contained in:
parent
0e42c7c67e
commit
3fb3ca8dea
@ -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)
|
57
admission/example/http/main.go
Normal file
57
admission/example/http/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
56
auth/example/http/main.go
Normal file
56
auth/example/http/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user