test-webserver.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // A tiny web server that serves a static file.
  14. package main
  15. import (
  16. "flag"
  17. "fmt"
  18. "log"
  19. "net/http"
  20. )
  21. var (
  22. port = flag.Int("port", 80, "Port number.")
  23. )
  24. func main() {
  25. flag.Parse()
  26. fs := http.StripPrefix("/", http.FileServer(http.Dir("/")))
  27. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  28. w.Header().Set("Cache-Control", "private")
  29. // Needed for local proxy to Kubernetes API server to work.
  30. w.Header().Set("Access-Control-Allow-Origin", "*")
  31. w.Header().Set("Access-Control-Allow-Credentials", "true")
  32. w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
  33. w.Header().Set("Access-Control-Allow-Headers", "DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type")
  34. // Disable If-Modified-Since so update-demo isn't broken by 304s
  35. r.Header.Del("If-Modified-Since")
  36. fs.ServeHTTP(w, r)
  37. })
  38. go log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
  39. select {}
  40. }