main.go 874 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. )
  8. func Index(w http.ResponseWriter, r *http.Request) {
  9. fmt.Fprintf(w, "Hello world")
  10. fmt.Println("File opening...")
  11. file, err := os.Open("main.go")
  12. if err != nil {
  13. fmt.Println(err)
  14. os.Exit(1)
  15. }
  16. defer file.Close()
  17. data := make([]byte, 64)
  18. for {
  19. n, err := file.Read(data)
  20. if err == io.EOF {
  21. break
  22. }
  23. w.Write(data)
  24. fmt.Print(string(data[:n]))
  25. }
  26. }
  27. func Update(w http.ResponseWriter, r *http.Request) {
  28. fmt.Println(r.URL.Path)
  29. fmt.Println(r.Method)
  30. fmt.Println(r.Header)
  31. if r.URL.Path == "/fw.bin" {
  32. fmt.Println("Download update file...")
  33. http.ServeFile(w, r, `fw.bin`)
  34. }
  35. }
  36. func main() {
  37. fmt.Println("Starting HTTP server...")
  38. // http.HandleFunc("/", Index)
  39. http.HandleFunc("/fw.bin", Update)
  40. http.ListenAndServe(":9000", nil)
  41. }