1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Remove final global var

This commit is contained in:
Daniel Heath 2018-04-28 19:29:35 +10:00
parent bc1a9ee86b
commit d9b8bfc95d
3 changed files with 20 additions and 24 deletions

View File

@ -24,8 +24,6 @@ import (
const minutesToUnlock = 10.0 const minutesToUnlock = 10.0
var pathToData string
type Site struct { type Site struct {
PathToData string PathToData string
Css []byte Css []byte
@ -108,8 +106,6 @@ func Serve(
} }
func (s Site) Router() *gin.Engine { func (s Site) Router() *gin.Engine {
pathToData = s.PathToData
if s.Logger == nil { if s.Logger == nil {
s.Logger = lumber.NewConsoleLogger(lumber.TRACE) s.Logger = lumber.NewConsoleLogger(lumber.TRACE)
} }
@ -122,13 +118,13 @@ func (s Site) Router() *gin.Engine {
router := gin.Default() router := gin.Default()
router.SetFuncMap(template.FuncMap{ router.SetFuncMap(template.FuncMap{
"sniffContentType": sniffContentType, "sniffContentType": s.sniffContentType,
}) })
if s.HotTemplateReloading { if s.HotTemplateReloading {
router.LoadHTMLGlob("templates/*.tmpl") router.LoadHTMLGlob("templates/*.tmpl")
} else { } else {
router.HTMLRender = loadTemplates("index.tmpl") router.HTMLRender = s.loadTemplates("index.tmpl")
} }
router.Use(sessions.Sessions(s.PathToData, s.SessionStore)) router.Use(sessions.Sessions(s.PathToData, s.SessionStore))
@ -191,7 +187,7 @@ func (s Site) Router() *gin.Engine {
return router return router
} }
func loadTemplates(list ...string) multitemplate.Render { func (s *Site) loadTemplates(list ...string) multitemplate.Render {
r := multitemplate.New() r := multitemplate.New()
for _, x := range list { for _, x := range list {
@ -201,7 +197,7 @@ func loadTemplates(list ...string) multitemplate.Render {
} }
tmplMessage, err := template.New(x).Funcs(template.FuncMap{ tmplMessage, err := template.New(x).Funcs(template.FuncMap{
"sniffContentType": sniffContentType, "sniffContentType": s.sniffContentType,
}).Parse(string(templateString)) }).Parse(string(templateString))
if err != nil { if err != nil {
panic(err) panic(err)
@ -279,7 +275,7 @@ func (s *Site) thread_SiteMap() {
if !s.sitemapUpToDate { if !s.sitemapUpToDate {
s.Logger.Info("Generating sitemap...") s.Logger.Info("Generating sitemap...")
s.sitemapUpToDate = true s.sitemapUpToDate = true
ioutil.WriteFile(path.Join(pathToData, "sitemap.xml"), []byte(s.generateSiteMap()), 0644) ioutil.WriteFile(path.Join(s.PathToData, "sitemap.xml"), []byte(s.generateSiteMap()), 0644)
s.Logger.Info("..finished generating sitemap") s.Logger.Info("..finished generating sitemap")
} }
time.Sleep(time.Second) time.Sleep(time.Second)
@ -287,7 +283,7 @@ func (s *Site) thread_SiteMap() {
} }
func (s *Site) generateSiteMap() (sitemap string) { func (s *Site) generateSiteMap() (sitemap string) {
files, _ := ioutil.ReadDir(pathToData) files, _ := ioutil.ReadDir(s.PathToData)
lastEdited := make([]string, len(files)) lastEdited := make([]string, len(files))
names := make([]string, len(files)) names := make([]string, len(files))
i := 0 i := 0
@ -321,7 +317,7 @@ func (s *Site) handlePageRequest(c *gin.Context) {
command := c.Param("command") command := c.Param("command")
if page == "sitemap.xml" { if page == "sitemap.xml" {
siteMap, err := ioutil.ReadFile(path.Join(pathToData, "sitemap.xml")) siteMap, err := ioutil.ReadFile(path.Join(s.PathToData, "sitemap.xml"))
if err != nil { if err != nil {
c.Data(http.StatusInternalServerError, contentType("sitemap.xml"), []byte("")) c.Data(http.StatusInternalServerError, contentType("sitemap.xml"), []byte(""))
} else { } else {
@ -358,7 +354,7 @@ func (s *Site) handlePageRequest(c *gin.Context) {
if !strings.HasSuffix(command, ".upload") { if !strings.HasSuffix(command, ".upload") {
command = command + ".upload" command = command + ".upload"
} }
pathname := path.Join(pathToData, command) pathname := path.Join(s.PathToData, command)
if allowInsecureHtml { if allowInsecureHtml {
c.Header( c.Header(
@ -470,7 +466,7 @@ func (s *Site) handlePageRequest(c *gin.Context) {
if page == "uploads" { if page == "uploads" {
command = "/view" command = "/view"
var err error var err error
DirectoryEntries, err = UploadList() DirectoryEntries, err = s.UploadList()
if err != nil { if err != nil {
c.AbortWithError(http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
@ -757,7 +753,7 @@ func (s *Site) handleUpload(c *gin.Context) {
newName := "sha256-" + encodeBytesToBase32(h.Sum(nil)) newName := "sha256-" + encodeBytesToBase32(h.Sum(nil))
// Replaces any existing version, but sha256 collisions are rare as anything. // Replaces any existing version, but sha256 collisions are rare as anything.
outfile, err := os.Create(path.Join(pathToData, newName+".upload")) outfile, err := os.Create(path.Join(s.PathToData, newName+".upload"))
if err != nil { if err != nil {
c.AbortWithError(http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return

View File

@ -45,7 +45,7 @@ func (s *Site) Open(name string) (p *Page) {
p.Name = name p.Name = name
p.Text = versionedtext.NewVersionedText("") p.Text = versionedtext.NewVersionedText("")
p.Render() p.Render()
bJSON, err := ioutil.ReadFile(path.Join(pathToData, encodeToBase32(strings.ToLower(name))+".json")) bJSON, err := ioutil.ReadFile(path.Join(s.PathToData, encodeToBase32(strings.ToLower(name))+".json"))
if err != nil { if err != nil {
return return
} }
@ -92,7 +92,7 @@ func (d DirectoryEntry) Sys() interface{} {
} }
func (s *Site) DirectoryList() []os.FileInfo { func (s *Site) DirectoryList() []os.FileInfo {
files, _ := ioutil.ReadDir(pathToData) files, _ := ioutil.ReadDir(s.PathToData)
entries := make([]os.FileInfo, len(files)) entries := make([]os.FileInfo, len(files))
for i, f := range files { for i, f := range files {
name := DecodeFileName(f.Name()) name := DecodeFileName(f.Name())
@ -112,8 +112,8 @@ type UploadEntry struct {
os.FileInfo os.FileInfo
} }
func UploadList() ([]os.FileInfo, error) { func (s *Site) UploadList() ([]os.FileInfo, error) {
paths, err := filepath.Glob(path.Join(pathToData, "sha256*")) paths, err := filepath.Glob(path.Join(s.PathToData, "sha256*"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -173,12 +173,12 @@ func (p *Page) Save() error {
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"), bJSON, 0644) return ioutil.WriteFile(path.Join(p.Site.PathToData, encodeToBase32(strings.ToLower(p.Name))+".json"), bJSON, 0644)
} }
func (p *Page) ChildPageNames() []string { func (p *Page) ChildPageNames() []string {
prefix := strings.ToLower(p.Name + ": ") prefix := strings.ToLower(p.Name + ": ")
files, err := filepath.Glob(path.Join(pathToData, "*")) files, err := filepath.Glob(path.Join(p.Site.PathToData, "*"))
if err != nil { if err != nil {
panic("Filepath pattern cannot be malformed") panic("Filepath pattern cannot be malformed")
} }
@ -197,12 +197,12 @@ func (p *Page) ChildPageNames() []string {
} }
func (p *Page) IsNew() bool { func (p *Page) IsNew() bool {
return !exists(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json")) return !exists(path.Join(p.Site.PathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
} }
func (p *Page) Erase() error { func (p *Page) Erase() error {
p.Site.Logger.Trace("Erasing " + p.Name) p.Site.Logger.Trace("Erasing " + p.Name)
return os.Remove(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json")) return os.Remove(path.Join(p.Site.PathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
} }
func (p *Page) Published() bool { func (p *Page) Published() bool {

View File

@ -85,8 +85,8 @@ func contentType(filename string) string {
return "text/html" return "text/html"
} }
func sniffContentType(name string) (string, error) { func (s *Site) sniffContentType(name string) (string, error) {
file, err := os.Open(path.Join(pathToData, name)) file, err := os.Open(path.Join(s.PathToData, name))
if err != nil { if err != nil {
return "", err return "", err