-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmain.go
70 lines (55 loc) · 1.51 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Package main is the CLI.
// You can use the CLI via Terminal.
package main
import (
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/madhums/go-gin-mgo-demo/db"
"github.com/madhums/go-gin-mgo-demo/gin_html_render"
"github.com/madhums/go-gin-mgo-demo/handlers/articles"
"github.com/madhums/go-gin-mgo-demo/middlewares"
)
const (
// Port at which the server starts listening
Port = "7000"
)
func init() {
db.Connect()
}
func main() {
// Configure
router := gin.Default()
// Set html render options
htmlRender := GinHTMLRender.New()
htmlRender.Debug = gin.IsDebugging()
htmlRender.Layout = "layouts/default"
// htmlRender.TemplatesDir = "templates/" // default
// htmlRender.Ext = ".html" // default
// Tell gin to use our html render
router.HTMLRender = htmlRender.Create()
router.RedirectTrailingSlash = true
router.RedirectFixedPath = true
// Middlewares
router.Use(middlewares.Connect)
router.Use(middlewares.ErrorHandler)
// Statics
router.Static("/public", "./public")
// Routes
router.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/articles")
})
// Articles
router.GET("/new", articles.New)
router.GET("/articles/:_id", articles.Edit)
router.GET("/articles", articles.List)
router.POST("/articles", articles.Create)
router.POST("/articles/:_id", articles.Update)
router.POST("/delete/articles/:_id", articles.Delete)
// Start listening
port := Port
if len(os.Getenv("PORT")) > 0 {
port = os.Getenv("PORT")
}
router.Run(":" + port)
}