-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (43 loc) · 924 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
48
49
50
51
52
package main
import (
"log"
"math/rand"
"net/http"
"os"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/kelvintaywl/ramenipsum/lib"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func paragraphHandler(c *gin.Context) {
para := c.Param("num")
locale := c.DefaultQuery("lc", "en_UK")
n, err := strconv.Atoi(para)
if err != nil {
n = 1
}
c.String(http.StatusOK, lib.RandomText(n, locale))
}
func indexHandler(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl",
gin.H{
"title": "Ramen Ipsum: A brothier Lorem Ipsum generator",
"url": c.Request.Host,
})
}
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
router := gin.New()
router.Use(gin.Logger())
router.Static("/assets", "assets")
router.LoadHTMLGlob("templates/*")
router.GET("/", indexHandler)
router.GET("/paragraphs/:num", paragraphHandler)
router.Run(":" + port)
}