mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
Move serving and CSS reading out of Site
This commit is contained in:
parent
0d984da5b2
commit
465e9c8e93
@ -24,7 +24,6 @@ import (
|
|||||||
|
|
||||||
const minutesToUnlock = 10.0
|
const minutesToUnlock = 10.0
|
||||||
|
|
||||||
var customCSS []byte
|
|
||||||
var defaultLock string
|
var defaultLock string
|
||||||
var debounceTime int
|
var debounceTime int
|
||||||
var diaryMode bool
|
var diaryMode bool
|
||||||
@ -36,12 +35,7 @@ var log *lumber.ConsoleLogger
|
|||||||
|
|
||||||
type Site struct {
|
type Site struct {
|
||||||
PathToData string
|
PathToData string
|
||||||
Host string
|
Css []byte
|
||||||
Port string
|
|
||||||
CertPath string
|
|
||||||
KeyPath string
|
|
||||||
TLS bool
|
|
||||||
CssFile string
|
|
||||||
DefaultPage string
|
DefaultPage string
|
||||||
DefaultPassword string
|
DefaultPassword string
|
||||||
Debounce int
|
Debounce int
|
||||||
@ -75,14 +69,21 @@ func Serve(
|
|||||||
maxUploadSize uint,
|
maxUploadSize uint,
|
||||||
logger *lumber.ConsoleLogger,
|
logger *lumber.ConsoleLogger,
|
||||||
) {
|
) {
|
||||||
Site{
|
var customCSS []byte
|
||||||
|
// collect custom CSS
|
||||||
|
if len(cssFile) > 0 {
|
||||||
|
var errRead error
|
||||||
|
customCSS, errRead = ioutil.ReadFile(cssFile)
|
||||||
|
if errRead != nil {
|
||||||
|
fmt.Println(errRead)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("Loaded CSS file, %d bytes\n", len(customCSS))
|
||||||
|
}
|
||||||
|
|
||||||
|
router := Site{
|
||||||
filepathToData,
|
filepathToData,
|
||||||
host,
|
customCSS,
|
||||||
port,
|
|
||||||
crt_path,
|
|
||||||
key_path,
|
|
||||||
TLS,
|
|
||||||
cssFile,
|
|
||||||
defaultPage,
|
defaultPage,
|
||||||
defaultPassword,
|
defaultPassword,
|
||||||
debounce,
|
debounce,
|
||||||
@ -94,10 +95,16 @@ func Serve(
|
|||||||
fileuploads,
|
fileuploads,
|
||||||
maxUploadSize,
|
maxUploadSize,
|
||||||
logger,
|
logger,
|
||||||
}.Serve()
|
}.Router()
|
||||||
|
|
||||||
|
if TLS {
|
||||||
|
http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router)
|
||||||
|
} else {
|
||||||
|
panic(router.Run(host + ":" + port))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Site) Serve() {
|
func (s Site) Router() *gin.Engine {
|
||||||
pathToData = s.PathToData
|
pathToData = s.PathToData
|
||||||
allowFileUploads = s.Fileuploads
|
allowFileUploads = s.Fileuploads
|
||||||
maxUploadMB = s.MaxUploadSize
|
maxUploadMB = s.MaxUploadSize
|
||||||
@ -113,7 +120,6 @@ func (s Site) Serve() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
router.SetFuncMap(template.FuncMap{
|
router.SetFuncMap(template.FuncMap{
|
||||||
"sniffContentType": sniffContentType,
|
"sniffContentType": sniffContentType,
|
||||||
})
|
})
|
||||||
@ -166,7 +172,7 @@ func (s Site) Serve() {
|
|||||||
page := c.Param("page")
|
page := c.Param("page")
|
||||||
c.Redirect(302, "/"+page+"/")
|
c.Redirect(302, "/"+page+"/")
|
||||||
})
|
})
|
||||||
router.GET("/:page/*command", handlePageRequest)
|
router.GET("/:page/*command", s.handlePageRequest)
|
||||||
router.POST("/update", handlePageUpdate)
|
router.POST("/update", handlePageUpdate)
|
||||||
router.POST("/relinquish", handlePageRelinquish) // relinquish returns the page no matter what (and destroys if nessecary)
|
router.POST("/relinquish", handlePageRelinquish) // relinquish returns the page no matter what (and destroys if nessecary)
|
||||||
router.POST("/exists", handlePageExists)
|
router.POST("/exists", handlePageExists)
|
||||||
@ -180,17 +186,6 @@ func (s Site) Serve() {
|
|||||||
// start long-processes as threads
|
// start long-processes as threads
|
||||||
go thread_SiteMap()
|
go thread_SiteMap()
|
||||||
|
|
||||||
// collect custom CSS
|
|
||||||
if len(s.CssFile) > 0 {
|
|
||||||
var errRead error
|
|
||||||
customCSS, errRead = ioutil.ReadFile(s.CssFile)
|
|
||||||
if errRead != nil {
|
|
||||||
fmt.Println(errRead.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Printf("Loaded CSS file, %d bytes\n", len(customCSS))
|
|
||||||
}
|
|
||||||
|
|
||||||
// lock all pages automatically
|
// lock all pages automatically
|
||||||
if s.DefaultPassword != "" {
|
if s.DefaultPassword != "" {
|
||||||
fmt.Println("running with locked pages")
|
fmt.Println("running with locked pages")
|
||||||
@ -205,12 +200,7 @@ func (s Site) Serve() {
|
|||||||
|
|
||||||
// Allow iframe/scripts in markup?
|
// Allow iframe/scripts in markup?
|
||||||
allowInsecureHtml = s.AllowInsecure
|
allowInsecureHtml = s.AllowInsecure
|
||||||
|
return router
|
||||||
if s.TLS {
|
|
||||||
http.ListenAndServeTLS(s.Host+":"+s.Port, s.CertPath, s.KeyPath, router)
|
|
||||||
} else {
|
|
||||||
panic(router.Run(s.Host + ":" + s.Port))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadTemplates(list ...string) multitemplate.Render {
|
func loadTemplates(list ...string) multitemplate.Render {
|
||||||
@ -338,7 +328,7 @@ func generateSiteMap() (sitemap string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlePageRequest(c *gin.Context) {
|
func (s Site) handlePageRequest(c *gin.Context) {
|
||||||
page := c.Param("page")
|
page := c.Param("page")
|
||||||
command := c.Param("command")
|
command := c.Param("command")
|
||||||
|
|
||||||
@ -359,7 +349,7 @@ func handlePageRequest(c *gin.Context) {
|
|||||||
filename := page + command
|
filename := page + command
|
||||||
var data []byte
|
var data []byte
|
||||||
if filename == "static/css/custom.css" {
|
if filename == "static/css/custom.css" {
|
||||||
data = customCSS
|
data = s.Css
|
||||||
} else {
|
} else {
|
||||||
var errAssset error
|
var errAssset error
|
||||||
data, errAssset = Asset(filename)
|
data, errAssset = Asset(filename)
|
||||||
@ -531,7 +521,7 @@ func handlePageRequest(c *gin.Context) {
|
|||||||
"HasDotInName": strings.Contains(page, "."),
|
"HasDotInName": strings.Contains(page, "."),
|
||||||
"RecentlyEdited": getRecentlyEdited(page, c),
|
"RecentlyEdited": getRecentlyEdited(page, c),
|
||||||
"IsPublished": p.IsPublished,
|
"IsPublished": p.IsPublished,
|
||||||
"CustomCSS": len(customCSS) > 0,
|
"CustomCSS": len(s.Css) > 0,
|
||||||
"Debounce": debounceTime,
|
"Debounce": debounceTime,
|
||||||
"DiaryMode": diaryMode,
|
"DiaryMode": diaryMode,
|
||||||
"Date": time.Now().Format("2006-01-02"),
|
"Date": time.Now().Format("2006-01-02"),
|
||||||
|
Loading…
Reference in New Issue
Block a user