-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
246 lines (199 loc) · 5.11 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"encoding/json"
"log"
"os"
"time"
"github.com/jaredfolkins/badactor"
"github.com/labstack/echo"
"github.com/labstack/echo/engine/fasthttp"
"github.com/labstack/echo/middleware"
_ "github.com/lib/pq"
"github.com/mewben/config-echo"
"github.com/mewben/db-go-env"
"projects/onix/controllers"
mw "projects/onix/middleware"
"projects/onix/models"
"projects/onix/theme"
"projects/onix/utils"
)
// Initialize Port and DB Connection config
func init() {
type Config struct {
SERVERPORT string
DB db.Database
}
configFile, err := os.Open("env.json")
if err != nil {
panic(err)
}
var devConfig Config
jsonParser := json.NewDecoder(configFile)
if err = jsonParser.Decode(&devConfig); err != nil {
panic(err)
}
// setup postgres db connection
db.Setup(devConfig.DB)
// get mode from environment
mode := os.Getenv("MODE")
// setup port
// This sets the global Port string
// If you set an environment variable DATABASE_URL,
// it sets Mode = "prod" and uses the env Port instead
config.Setup(devConfig.SERVERPORT, mode)
}
func main() {
app := echo.New()
// create new Studio
utils.ST = badactor.NewStudio(1024) // studio capacity... RAM?
// add the rule to the stack
utils.ST.AddRule(mw.LoginRule)
err := utils.ST.CreateDirectors(1024)
if err != nil {
log.Fatal(err)
}
//poll duration
dur := time.Minute * time.Duration(60)
// Start the reaper
utils.ST.StartReaper(dur)
app.Use(middleware.Recover())
app.Use(middleware.Gzip())
app.Use(middleware.Secure())
app.Use(middleware.BodyLimit("100K"))
api := app.Group("/api")
if config.Mode == "dev" {
// Enable Debug
app.Use(middleware.Logger())
app.SetDebug(true)
corsEnabled := middleware.CORSWithConfig(middleware.CORSConfig{
AllowHeaders: []string{
echo.HeaderOrigin,
echo.HeaderContentType,
echo.HeaderAcceptEncoding,
echo.HeaderAuthorization,
},
})
// Enable CORS /
app.Use(corsEnabled)
// Enable CORS /api
api.Use(corsEnabled)
}
// admin routing
// app.Static("/admin", "public/admin")
app.Static("/admin/assets", "public/admin/assets")
app.Static("/tinymce", "public/tinymce")
app.File("/admin*", "public/admin/index.html")
app.File("/admin/*", "public/admin/index.html")
// Public
// Setup Theme
theme.Setup(app, config.Mode)
users := controllers.UsersController{}
app.POST("/auth/login", users.Login, mw.Jailer)
app.POST("/auth/delegation", users.Delegate)
// Get jwt signingkey
signingKey, err := models.GetSettingString("admin_signingkey")
if err != nil {
panic(err)
}
// get api routes
api.Use(middleware.JWT([]byte(signingKey)))
APIRoutes(api)
// ======= SITES =====
site := controllers.SiteController{}
app.GET("/", site.Home)
app.GET("/:slug", site.Single)
app.GET("/sitemap.xml", site.Sitemap)
app.Run(fasthttp.New(config.Port))
}
// APIRoutes definition
// ========== API ROUTES ======
func APIRoutes(api *echo.Group) {
// ======= ADMIN API ======
admin := controllers.AdminController{}
api.Get("/time", admin.GetTime)
posts := controllers.PostsController{}
api.GET("/posts/:id", posts.GetOne)
api.GET("/posts", posts.Get)
api.POST("/posts", posts.Save)
api.PUT("/posts/:id", posts.Update)
// CRUD /api/tags
tags := controllers.TagsController{}
api.GET("/tags/:id", tags.GetOne)
api.GET("/tags", tags.Get)
api.POST("/tags", tags.Save)
api.PUT("/tags/:id", tags.Update)
/* api.Delete("/tags/:id", tags.Destroy)
*/
}
/*
package main
import (
"log"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
_ "github.com/lib/pq"
"github.com/mewben/config-echo"
"github.com/mewben/db-go-env"
"github.com/rs/cors"
"github.com/thoas/stats"
"projects/onix/controllers"
"projects/onix/theme"
)
func init() {
// get env
conn, port := utils.GetEnv()
// setup DB
db.Setup(conn)
// setup config
config.Setup(port)
}
func main() {
e := echo.New()
e.Use(mw.Recover())
e.Use(mw.Gzip())
key, err := utils.GetConfigString("admin_signingkey")
if err != nil {
panic(err)
}
api := e.Group("/api")
if config.Mode == "dev" {
e.SetDebug(true)
e.Use(mw.Logger())
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"POST", "GET", "PUT", "DELETE"},
AllowedHeaders: []string{"Accept", "Content-Type", "Authorization"},
AllowCredentials: true,
})
e.Use(cors.Default().Handler)
api.Use(c.Handler)
}
// Public
// Setup Theme
theme.Setup(e)
e.ServeFile("/admin*", "public/admin/index.html")
e.ServeFile("/admin/*", "public/admin/index.html")
e.Static("/admin/build", "public/admin/build")
// Public Routes
s := stats.New()
e.Get("/stats", func(c *echo.Context) error {
return c.JSON(200, s.Data())
})
e.Get("/mew", func(c *echo.Context) error {
theme.Change("mew", e)
return c.String(200, "Mew")
})
e.Get("/eevee", func(c *echo.Context) error {
theme.Change("eevee", e)
return c.String(200, "Eevee")
})
site := &controllers.SiteController{}
e.Get("/", site.Home)
// ======== API
content := controllers.ContentController{}
api.Post("/content", content.Save)
api.Put("/content/:id", content.Save)
log.Println("Listening at " + config.Port)
e.Run(config.Port)
}
*/