mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
Move serve params into a struct
This commit is contained in:
parent
8944170646
commit
0d984da5b2
@ -171,7 +171,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.Run(os.Args)
|
app.Run(os.Args)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLocalIP returns the local ip address
|
// GetLocalIP returns the local ip address
|
||||||
|
@ -14,7 +14,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
// "github.com/gin-contrib/static"
|
|
||||||
secretRequired "github.com/danielheath/gin-teeny-security"
|
secretRequired "github.com/danielheath/gin-teeny-security"
|
||||||
"github.com/gin-contrib/multitemplate"
|
"github.com/gin-contrib/multitemplate"
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
@ -35,6 +34,27 @@ var needSitemapUpdate = true
|
|||||||
var pathToData string
|
var pathToData string
|
||||||
var log *lumber.ConsoleLogger
|
var log *lumber.ConsoleLogger
|
||||||
|
|
||||||
|
type Site struct {
|
||||||
|
PathToData string
|
||||||
|
Host string
|
||||||
|
Port string
|
||||||
|
CertPath string
|
||||||
|
KeyPath string
|
||||||
|
TLS bool
|
||||||
|
CssFile string
|
||||||
|
DefaultPage string
|
||||||
|
DefaultPassword string
|
||||||
|
Debounce int
|
||||||
|
Diary bool
|
||||||
|
Secret string
|
||||||
|
SecretCode string
|
||||||
|
AllowInsecure bool
|
||||||
|
HotTemplateReloading bool
|
||||||
|
Fileuploads bool
|
||||||
|
MaxUploadSize uint
|
||||||
|
Logger *lumber.ConsoleLogger
|
||||||
|
}
|
||||||
|
|
||||||
func Serve(
|
func Serve(
|
||||||
filepathToData,
|
filepathToData,
|
||||||
host,
|
host,
|
||||||
@ -55,15 +75,38 @@ func Serve(
|
|||||||
maxUploadSize uint,
|
maxUploadSize uint,
|
||||||
logger *lumber.ConsoleLogger,
|
logger *lumber.ConsoleLogger,
|
||||||
) {
|
) {
|
||||||
pathToData = filepathToData
|
Site{
|
||||||
allowFileUploads = fileuploads
|
filepathToData,
|
||||||
maxUploadMB = maxUploadSize
|
host,
|
||||||
log = logger
|
port,
|
||||||
|
crt_path,
|
||||||
|
key_path,
|
||||||
|
TLS,
|
||||||
|
cssFile,
|
||||||
|
defaultPage,
|
||||||
|
defaultPassword,
|
||||||
|
debounce,
|
||||||
|
diary,
|
||||||
|
secret,
|
||||||
|
secretCode,
|
||||||
|
allowInsecure,
|
||||||
|
hotTemplateReloading,
|
||||||
|
fileuploads,
|
||||||
|
maxUploadSize,
|
||||||
|
logger,
|
||||||
|
}.Serve()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Site) Serve() {
|
||||||
|
pathToData = s.PathToData
|
||||||
|
allowFileUploads = s.Fileuploads
|
||||||
|
maxUploadMB = s.MaxUploadSize
|
||||||
|
log = s.Logger
|
||||||
if log == nil {
|
if log == nil {
|
||||||
log = lumber.NewConsoleLogger(lumber.TRACE)
|
log = lumber.NewConsoleLogger(lumber.TRACE)
|
||||||
}
|
}
|
||||||
|
|
||||||
if hotTemplateReloading {
|
if s.HotTemplateReloading {
|
||||||
gin.SetMode(gin.DebugMode)
|
gin.SetMode(gin.DebugMode)
|
||||||
} else {
|
} else {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
@ -75,17 +118,17 @@ func Serve(
|
|||||||
"sniffContentType": sniffContentType,
|
"sniffContentType": sniffContentType,
|
||||||
})
|
})
|
||||||
|
|
||||||
if hotTemplateReloading {
|
if s.HotTemplateReloading {
|
||||||
router.LoadHTMLGlob("templates/*.tmpl")
|
router.LoadHTMLGlob("templates/*.tmpl")
|
||||||
} else {
|
} else {
|
||||||
router.HTMLRender = loadTemplates("index.tmpl")
|
router.HTMLRender = loadTemplates("index.tmpl")
|
||||||
}
|
}
|
||||||
|
|
||||||
store := sessions.NewCookieStore([]byte(secret))
|
store := sessions.NewCookieStore([]byte(s.Secret))
|
||||||
router.Use(sessions.Sessions("mysession", store))
|
router.Use(sessions.Sessions("mysession", store))
|
||||||
if secretCode != "" {
|
if s.SecretCode != "" {
|
||||||
cfg := &secretRequired.Config{
|
cfg := &secretRequired.Config{
|
||||||
Secret: secretCode,
|
Secret: s.SecretCode,
|
||||||
Path: "/login/",
|
Path: "/login/",
|
||||||
RequireAuth: func(c *gin.Context) bool {
|
RequireAuth: func(c *gin.Context) bool {
|
||||||
page := c.Param("page")
|
page := c.Param("page")
|
||||||
@ -110,8 +153,8 @@ func Serve(
|
|||||||
|
|
||||||
// router.Use(static.Serve("/static/", static.LocalFile("./static", true)))
|
// router.Use(static.Serve("/static/", static.LocalFile("./static", true)))
|
||||||
router.GET("/", func(c *gin.Context) {
|
router.GET("/", func(c *gin.Context) {
|
||||||
if defaultPage != "" {
|
if s.DefaultPage != "" {
|
||||||
c.Redirect(302, "/"+defaultPage+"/read")
|
c.Redirect(302, "/"+s.DefaultPage+"/read")
|
||||||
} else {
|
} else {
|
||||||
c.Redirect(302, "/"+randomAlliterateCombo())
|
c.Redirect(302, "/"+randomAlliterateCombo())
|
||||||
}
|
}
|
||||||
@ -138,9 +181,9 @@ func Serve(
|
|||||||
go thread_SiteMap()
|
go thread_SiteMap()
|
||||||
|
|
||||||
// collect custom CSS
|
// collect custom CSS
|
||||||
if len(cssFile) > 0 {
|
if len(s.CssFile) > 0 {
|
||||||
var errRead error
|
var errRead error
|
||||||
customCSS, errRead = ioutil.ReadFile(cssFile)
|
customCSS, errRead = ioutil.ReadFile(s.CssFile)
|
||||||
if errRead != nil {
|
if errRead != nil {
|
||||||
fmt.Println(errRead.Error())
|
fmt.Println(errRead.Error())
|
||||||
return
|
return
|
||||||
@ -149,24 +192,24 @@ func Serve(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// lock all pages automatically
|
// lock all pages automatically
|
||||||
if defaultPassword != "" {
|
if s.DefaultPassword != "" {
|
||||||
fmt.Println("running with locked pages")
|
fmt.Println("running with locked pages")
|
||||||
defaultLock = HashPassword(defaultPassword)
|
defaultLock = HashPassword(s.DefaultPassword)
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the debounce time
|
// set the debounce time
|
||||||
debounceTime = debounce
|
debounceTime = s.Debounce
|
||||||
|
|
||||||
// set diary mode
|
// set diary mode
|
||||||
diaryMode = diary
|
diaryMode = s.Diary
|
||||||
|
|
||||||
// Allow iframe/scripts in markup?
|
// Allow iframe/scripts in markup?
|
||||||
allowInsecureHtml = allowInsecure
|
allowInsecureHtml = s.AllowInsecure
|
||||||
|
|
||||||
if TLS {
|
if s.TLS {
|
||||||
http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router)
|
http.ListenAndServeTLS(s.Host+":"+s.Port, s.CertPath, s.KeyPath, router)
|
||||||
} else {
|
} else {
|
||||||
panic(router.Run(host + ":" + port))
|
panic(router.Run(s.Host + ":" + s.Port))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user