SHARE
TWEET

webserver

deim Jul 18th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.   "fmt"
  5.   "log"
  6.   "net/http"
  7.   "sync"
  8.   "image"
  9.   "image/color"
  10.   "image/gif"
  11.   "io"
  12.   "math"
  13.   "math/rand"
  14.   "strconv"
  15. )
  16.  
  17. var mu sync.Mutex
  18. var count int
  19. var palette = []color.Color{color.RGBA{0x00, 0x80, 0x00, 0xff}, color.Black, color.RGBA{0xFF, 0xD7, 0x00, 0xFF}}
  20.  
  21. const (
  22.   whiteIndex = 0
  23.   blackIndex = 1
  24.   goldIndex = 2
  25. )
  26.  
  27. func main() {
  28.   http.HandleFunc("/", handler)
  29.   http.HandleFunc("/info", infoer)
  30.   http.HandleFunc("/count", counter)
  31.   http.HandleFunc("/gif", picture)
  32.   log.Fatal(http.ListenAndServe(":8000", nil))
  33. }
  34.  
  35. func handler(w http.ResponseWriter, r *http.Request) {
  36.   mu.Lock()
  37.   count++
  38.   mu.Unlock()
  39.   keys, ok := r.URL.Query()["key"]
  40.   if !ok || len(keys[0]) < 1 {
  41.     fmt.Fprintf(w, "URL.Path = %q\nSimple webserver written in GO na kolenke", r.URL.Path)
  42.   } else {
  43.     fmt.Fprintf(w, "URL.Path = %q\nkey parameter: %v", r.URL.Path, keys[0])
  44.   }
  45. }
  46.  
  47. func infoer(w http.ResponseWriter, r *http.Request) {
  48.   mu.Lock()
  49.   count++
  50.   mu.Unlock()
  51.   fmt.Fprintf(w, "%s %s %s\n", r.Method, r.URL, r.Proto)
  52.   for k, v := range r.Header {
  53.     fmt.Fprintf(w, "Header[%q] = %q\n", k, v)
  54.   }
  55.   fmt.Fprintf(w, "Host = %6q\n", r.Host)
  56.   fmt.Fprintf(w, "RemoteAddr = %q\n", r.RemoteAddr)
  57.   if err := r.ParseForm(); err != nil {
  58.     log.Print(err)
  59.   }
  60.   for k, v := range r.Form {
  61.     fmt.Fprintf(w, "Form[%q] = %q\n", k, v)
  62.   }
  63. }
  64.  
  65. func counter(w http.ResponseWriter, r *http.Request) {
  66.   mu.Lock()
  67.   fmt.Fprintf(w, "Count %d\n", count)
  68.   mu.Unlock()
  69. }
  70.  
  71. func picture(w http.ResponseWriter, r *http.Request) {
  72.   mu.Lock()
  73.   count++
  74.   mu.Unlock()
  75.   cycles, ok := r.URL.Query()["cycles"]
  76.   if !ok || len(cycles[0]) < 1 {
  77.     lissajous(w, 7.0)
  78.   } else {
  79.     i, _ := strconv.Atoi(cycles[0])
  80.     lissajous(w, float64(i))
  81.   }
  82.  
  83. }
  84.  
  85. func lissajous(out io.Writer, cycs float64) {
  86.   var cycles  = cycs
  87.   const (
  88.     res     = 0.001
  89.     size    = 200
  90.     nframes = 128
  91.     delay   = 1
  92.   )
  93.   freq := rand.Float64() * 3.0
  94.   anim := gif.GIF{LoopCount: nframes}
  95.   phase := 0.0
  96.   for i := 0; i < nframes; i++ {
  97.     rect := image.Rect(0, 0, 2*size+1, 2*size+2)
  98.     img := image.NewPaletted(rect, palette)
  99.     for t := 0.0; t < cycles*2*math.Pi; t += res {
  100.       x := math.Sin(t)
  101.       y := math.Sin(t*freq + phase)
  102.       if t < 5 {
  103.         img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), goldIndex)
  104.       } else {
  105.         img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), blackIndex)
  106.       }
  107.     }
  108.     phase += 0.1
  109.     anim.Delay = append(anim.Delay, delay)
  110.     anim.Image = append(anim.Image, img)
  111.   }
  112.   gif.EncodeAll(out, &anim)
  113. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top