/
main.go
47 lines (37 loc) · 940 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"github.com/sergystepanov/bee1/Godeps/_workspace/src/github.com/gin-gonic/contrib/gzip"
"github.com/sergystepanov/bee1/Godeps/_workspace/src/github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"log"
"os"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
gin.SetMode(gin.ReleaseMode)
app := gin.Default()
app.Use(gzip.Gzip(gzip.DefaultCompression))
app.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Go away.")
})
app.GET("/s/:name", func(c *gin.Context) {
name := c.Param("name")
c.String(http.StatusOK, "%s", name)
})
app.POST("/url", func(c *gin.Context) {
url := c.PostForm("_url")
log.Print(url)
resp, err := http.Get(url)
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
c.Data(http.StatusOK, "text/html; charset=windows-1251", body)
})
app.Run(":" + port)
}