-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
105 lines (100 loc) · 2.57 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"log"
"net/url"
"os"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/nickelghost/yuzu-cms/app/config"
"github.com/nickelghost/yuzu-cms/app/db"
"github.com/nickelghost/yuzu-cms/app/handlers"
"github.com/nickelghost/yuzu-cms/app/template"
"github.com/nickelghost/yuzu-cms/seed"
)
func contains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}
func forwardWebpack(e *echo.Echo, targetURL string) {
g := e.Group("/admin")
webpackURL, err := url.Parse(targetURL)
if err != nil {
log.Fatal(err)
}
g.Use(middleware.Proxy(middleware.NewRandomBalancer(
[]*middleware.ProxyTarget{{URL: webpackURL}},
)))
}
func main() {
config, err := config.GetFromEnv()
if err != nil {
log.Fatal(err)
}
// Get the database connection
dbConn, err := db.Init(config.GetDBConnString())
defer dbConn.Close()
if err != nil {
log.Fatal(err)
}
if contains(os.Args, "-migrate") {
err = db.Migrate(dbConn, "migrations")
if err != nil {
log.Fatal(err)
}
}
if contains(os.Args, "-seed") {
err := seed.Seed(dbConn)
if err != nil {
log.Fatal(err)
}
}
// Load view templates
renderer := template.GetRenderer("themes/%s/views/*.html", config.AppTheme)
// Init Echo
e := echo.New()
// Set our template renderer as the renderer for Echo
e.Renderer = renderer
// Add Echo middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Static("/admin", "public")
e.Static(
fmt.Sprintf("/%s", config.AppTheme),
fmt.Sprintf("themes/%s/public", config.AppTheme),
)
// Init the handlers object
hs := handlers.Handlers{
DB: dbConn,
Config: config,
}
// Public routes
e.GET("/", hs.Homepage)
e.GET("/:slug", hs.Page)
e.GET("/posts/:slug", hs.Post)
e.POST("/api/v1/login", hs.APILogin)
// Auth-only v1 API routes
v1auth := e.Group("/api/v1")
v1auth.Use(middleware.JWT(config.AppSecret))
v1auth.GET("/posts", hs.APIPostsIndex)
v1auth.GET("/posts/titles", hs.APIPostsTitles)
v1auth.GET("/posts/:id", hs.APIPostsGet)
v1auth.POST("/posts", hs.APIPostsCreate)
v1auth.PUT("/posts/:id", hs.APIPostsUpdate)
v1auth.DELETE("/posts/:id", hs.APIPostsDelete)
v1auth.GET("/pages", hs.APIPagesIndex)
v1auth.POST("/pages", hs.APIPagesCreate)
v1auth.PUT("/pages/:id", hs.APIPagesUpdate)
v1auth.DELETE("/pages/:id", hs.APIPagesDelete)
// This forwards webpack's web server for development
if config.AppForwardWebpack {
forwardWebpack(e, "http://localhost:3001")
}
connStr := fmt.Sprintf(":%d", config.AppPort)
// Start the server
e.Logger.Fatal(e.Start(connStr))
}