Skip to content

Instantly share code, notes, and snippets.

@scottmmjackson
Created March 27, 2017 15:47
Show Gist options
  • Save scottmmjackson/c35f4609750d9c6907a464780e55097a to your computer and use it in GitHub Desktop.
Save scottmmjackson/c35f4609750d9c6907a464780e55097a to your computer and use it in GitHub Desktop.
Closed streams should not panic
package main
import (
"gopkg.in/gin-gonic/gin.v1"
"net/http"
"crypto/tls"
"fmt"
"time"
)
func main() {
r := gin.New()
r.Use(gin.Logger())
r.GET("/", func(c *gin.Context) {
fiveHundredKilobytes := []string{}
oneKilobyte := `cgdtWb02T5YosxdNAueaCvoeLqhGCYrPuAxsqT9G3CW7MGfSdN16RY0rgCxe6nwT
tqd6FQlKtUj0i3h79hpJ5mbfxdHFlhgXn4kXwKhCj4/9JCeOdUeinJCHUISh4L0U
N2d+0ycigJFu7VVkS3WGq8u1Z0ErHg6CV4YVB+Woeb3B4QUnPnkqm41+KVCONKfR
5h7FJ6c3dh0XO/pcqoQXwJcqmUQ1zUUM2ElRgXlY5c5H0X3USvGhwtTSfUb9uxIi
4cuJxHj99mArDwMyUpQw636VdSlOlwzFUntKoQUG7Nft6ujT8lFy4qV4CtUycmh8
UrH5Cgkvu1RiQLSLnv/KyfMW2wWYc/P60lJyfFxPCSiGUAXIs38zqnpn6O37FJC4
N7E5XztyRISE9WRX+OG4J/Yc4DNHePoXTNBGQqS9YBRPSJDqQI8eU91M3XMQgXjv
1KkRE0Zutgs0MH3T9nXnT5Nr3szKN7voo3V78/4kU0yA0G0zvlUK2w7C68BYykT7
rJkpeSRNA6dTThO8vUb6vfFz7s9DD4RQu48KX3sj2Q3WmjkH4D6D1llHYwYMlg/h
RE0nFqPw1Qe+G6qS9C2mmJzs1UapzRbdEikvJthNxyYphaRsit+nLqDF4xpYnGih
KaKu16yI5wcCdxEpcpmT5urHk9AfrTm4OpsQdRmv8Qh0UL0WTrb3sBcSc6u3GRbc
/YIIvUM5iXTdjRbAkmHQo5VNoO9GedxBY3UrYPK9KMYxEbvy72EDePzlGmkESWbu
sjiRqMQBNCQfrQJKC/JDJ4MtbRlMQT4MkraTEt4JklfupcL1TiVZdyfmqlwkWXXT
dODBZ1FgnF1p9AHJI6/ByEvzrb0rVAX+p7abEg+wJtUaS5/OtoHyDXG0OceXvhQW
njYTiAXbcdQ7MnB3DMPW1kdsnaD7qqvY10aQf31XqpOQpN8Ccj+6QPU7SS9XRyVq
aas412hPXbBhnOYMS3y0z96E3styfN6CD6b7DOYIEMG2zjRwG7ujbBdvCvPSFWIx
VtYiGiO0IioTS6tLQ7LubxeDnmtvx+7dqgGoafp1YHPyursRG+DQ3T6qRTjggyHL
Ga4WTy529CY5utcAqKtvHCX/FSlQcYlwMGHf5R4gi76md+mhne1TNnb+hHgZJZvv
188ILbJJ04aywwERpRkpoLU+fukZ6D4BtuQMcf6Or9g25W0ht4sdD7ePMKSkJNA6
Qn72QMW52r0P5d59kWOpoNkhmHeqhHFE3O4m4BRQuZ5S9kf3iXyRu1yxjPak6lI1
Lg3EM8g5/rqeYDU/yGV5hxOyEDlJJr1HQgMcWz/72dTCOGgJRB5+F4/hXwAA/M/Q
YG4+B1/I16onIEugQrUUkQ==`
for i := 0; i < 500; i++ {
time.Sleep(10 * time.Millisecond)
fiveHundredKilobytes = append(
fiveHundredKilobytes,
oneKilobyte,
)
}
c.JSON(
200,
struct{
Res []string `json:"response"`
}{
Res: fiveHundredKilobytes,
},
)
})
server := http.Server{
Addr: ":8080",
Handler: r,
TLSConfig: &tls.Config{
PreferServerCipherSuites: true,
},
}
regularServer := http.Server{
Addr: ":8181",
Handler: r,
}
forever := make(chan interface{}, 0)
go func() {
err := server.ListenAndServeTLS("server.crt", "server.key")
if err != nil {
fmt.Println(err)
}
}()
go func() {
err := regularServer.ListenAndServe()
if err != nil {
fmt.Println(err)
}
}()
<-forever
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment