-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
205 lines (175 loc) · 6.18 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
package main
import (
"flag"
"fmt"
"github.com/Masterminds/sprig"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/koreset/gtf"
"github.com/koreset/homefapp/config/bindatafs"
"github.com/koreset/homefapp/controllers"
"github.com/koreset/homefapp/models"
"github.com/koreset/homefapp/services"
"github.com/koreset/homefapp/utils"
"github.com/qor/admin"
"github.com/qor/media"
"github.com/qor/media/asset_manager"
"github.com/qor/media/media_library"
"github.com/qor/qor"
"github.com/satori/go.uuid"
"github.com/semihalev/gin-stats"
"html/template"
"io/ioutil"
"log"
"net/http"
"runtime"
"strings"
)
var db *gorm.DB
var funcMaps template.FuncMap
var templates *template.Template
// AutoMigrate run auto migration
func AutoMigrate(values ...interface{}) {
for _, value := range values {
db.AutoMigrate(value)
}
}
func SetupDB() {
db = services.Init()
db.LogMode(true)
db.AutoMigrate(&models.Post{}, &models.Video{}, &models.Image{}, &models.Link{}, &models.Category{})
media.RegisterCallbacks(db)
}
func setupTemplateFuncs() template.FuncMap {
funcMaps := sprig.FuncMap()
funcMaps["unsafeHtml"] = utils.UnsafeHtml
funcMaps["stripSummaryTags"] = utils.StripSummaryTags
funcMaps["displayDateString"] = utils.DisplayDateString
funcMaps["displayDate"] = utils.DisplayDateV2
funcMaps["truncateBody"] = utils.TruncateBody
gtf.Inject(funcMaps)
return funcMaps
}
func RequestIdMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
xuid, _ := uuid.NewV4()
c.Writer.Header().Set("X-Request-Id", xuid.String())
c.Next()
}
}
func SetupRouter() *gin.Engine {
mux := http.NewServeMux()
Admin := admin.New(&admin.AdminConfig{DB: db})
Admin.SetAssetFS(bindatafs.AssetFS.NameSpace("admin"))
Admin.MountTo("/admin", mux)
//API Setup
API := admin.New(&qor.Config{DB:db})
API.AddResource(&models.Post{})
API.MountTo("/adminapi", mux)
assetManager := Admin.AddResource(&asset_manager.AssetManager{}, &admin.Config{Invisible: true})
// Add Media Library
Admin.AddResource(&media_library.MediaLibrary{}, &admin.Config{Menu: []string{"Site Management"}})
Admin.AddResource(&models.Category{}, &admin.Config{Name: "Categories", Menu: []string{"Content Management"}})
post := Admin.AddResource(&models.Post{}, &admin.Config{Name: "Posts", Menu: []string{"Content Management"}})
post.IndexAttrs("ID", "Title", "Body", "Summary", "Type", "Categories")
post.NewAttrs("Title", "Body", "Summary", "Images", "Videos", "Links", "Documents","Categories", "Type")
post.Meta(&admin.Meta{Name: "Body", Config: &admin.RichEditorConfig{AssetManager: assetManager}})
post.Meta(&admin.Meta{Name: "Type", Type: "select_one", Config: &admin.SelectOneConfig{Collection: []string{"article", "publication","blog","video", "press_release", "event", "news"}}})
router := gin.Default()
if runtime.GOOS == "linux" {
log.Println("Loading html from binary")
router.SetHTMLTemplate(templates)
}
if runtime.GOOS == "darwin" {
router.SetFuncMap(setupTemplateFuncs())
router.LoadHTMLGlob("views/**/*")
}
router.GET("/stats", func(context *gin.Context) {
context.JSON(http.StatusOK, stats.Report())
})
router.GET("/", controllers.Home)
router.GET("/about-us", controllers.AboutUs)
router.GET("/fossil-politics", controllers.FossilPolitics)
router.GET("/hunger-politics", controllers.HungerPolitics)
router.GET("/categories/:category", controllers.GetPostsForCategory)
router.GET("/resources", controllers.ResourceIndex)
router.GET("/resources/annual-reports", controllers.ResourceAnnualReports)
router.GET("/resources/publications", controllers.ResourcePublications)
router.GET("/resources/publications/:page", controllers.ResourcePublications)
router.GET("/resources/books", controllers.ResourceBooks)
router.GET("/resources/eco-instigator", controllers.ResourceEcoInstigator)
router.GET("/resources/gallery", controllers.ResourceGallery)
router.GET("/resources/gallery/:albumid/:albumtitle", controllers.ResourceGalleryDetail)
router.GET("/sustainability-academy", controllers.SustainabilityAcademy)
router.GET("posts/:slug", controllers.GetPost)
router.GET("publications", controllers.GetPublications)
router.GET("/test", controllers.GetTest)
router.GET("/news", controllers.GetNews)
router.GET("/news/:page", controllers.GetNews)
router.GET("/new", controllers.GetNew)
router.GET("/boot", controllers.GetBoot)
//API Calls
api := router.Group("/api")
{
api.GET("/get-tweets", controllers.GetTweets)
api.GET("/get-flickr", controllers.GetFlickr)
api.GET("/testdata", controllers.GetTestData)
}
router.Static("/public", "./public")
admin := router.Group("/admin", gin.BasicAuth(gin.Accounts{
"jome": "wordpass15",
"nnimmo": "wordpass15"}))
{
admin.Any("/*resources", gin.WrapH(mux))
}
router.Any("/adminapi/*resources", gin.WrapH(mux))
router.NoRoute(func(context *gin.Context) {
fmt.Println(">>>>>>>>>>>>>>>>>> 404 <<<<<<<<<<<<<<<<<<<")
context.HTML(http.StatusNotFound, "content_not_found", nil)
})
return router
}
func loadTemplates() (*template.Template, error) {
templates = template.New("")
templates.Funcs(setupTemplateFuncs())
var myAssets = Assets.Files
for name, file := range myAssets {
if file.IsDir() || !strings.HasSuffix(name, ".html") {
continue
}
h, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
templates, err = templates.New(name).Parse(string(h))
if err != nil {
return nil, err
}
}
return templates, nil
}
func main() {
port := flag.String("port", "4000", "The port the app will listen to")
host := flag.String("host", "0.0.0.0", "The ip address to listen on")
compileTemplate := flag.Bool("compile-templates", false, "Set this to true to compile templates to binary")
flag.Parse()
if *compileTemplate {
Admin := admin.New(&admin.AdminConfig{
DB: db,
AssetFS: bindatafs.AssetFS.NameSpace("admin")})
Admin.SetAssetFS(bindatafs.AssetFS.NameSpace("admin"))
bindatafs.AssetFS.Compile()
} else {
SetupDB()
defer db.Close()
loadTemplates()
r := SetupRouter()
fmt.Println(*host, *port)
//if runtime.GOOS == "linux"{
// log.Fatal(autotls.Run(r, "beta.homef.org", "homef.org", "www.homef.org"))
//}else{
// r.Run(fmt.Sprintf("%s:%s", *host, *port))
//}
r.Run(fmt.Sprintf("%s:%s", *host, *port))
}
}