Skip to content

Commit

Permalink
chore: re-arrange struct fields to use less memory
Browse files Browse the repository at this point in the history
  • Loading branch information
chuhlomin committed Aug 25, 2024
1 parent 33678d8 commit bbced2e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
12 changes: 6 additions & 6 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ const (

// Generator holds all the data needed to generate the site
type Generator struct {
cfg Config
t *template.Template
md map[string]*MarkdownFile // relative source path -> MarkdownFile
mdSorted []*MarkdownFile // sorted MarkdownFiles by creation date
mdMu sync.Mutex
tempDir string // temporary directory used to templates
md map[string]*MarkdownFile // relative source path -> MarkdownFile
templates map[string]map[string]*MarkdownFile // id -> hashed path -> MarkdownFile
templatesMu sync.Mutex
searchClient *meilisearch.Client
og *openGraphClient
cfg Config
tempDir string // temporary directory used to templates
mdSorted []*MarkdownFile // sorted MarkdownFiles by creation date
mdMu sync.Mutex
templatesMu sync.Mutex
}

// NewGenerator creates a new Generator
Expand Down
20 changes: 10 additions & 10 deletions generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@ import (

// Config is a struct that holds all configuration options
type Config struct {
Debug bool `env:"DEBUG" long:"debug" description:"debug mode"`
ContentDirectory string `env:"CONTENT_DIR" long:"content" description:"content directory" default:"content"`
TemplatesDirectory string `env:"TEMPLATES_DIR" long:"templates" description:"templates directory" default:"templates"`
OutputDirectory string `env:"OUTPUT_DIR" long:"output" description:"output directory" default:"output"`
TempDirectory string `env:"TEMP_DIR" long:"temp" description:"temp directory" default:""`
DefaultLanguage string `env:"DEFAULT_LANG" long:"default-lang" description:"default language" default:"en"`
RootURL string `env:"ROOT_URL" long:"root-url" description:"root url" default:"https://local.chuhlomin.com"`
RemoveHTMLExtension bool `env:"REMOVE_HTML_EXT" long:"remove-html-ext" description:"remove .html extension from urls"`
CommentsEnabled bool `env:"COMMENTS_ENABLED" long:"comments-enabled" description:"enable comments"`
CommentsSiteID string `env:"COMMENTS_SITE_ID" long:"comments-site-id" description:"comments site id"`
ShowDrafts bool `env:"SHOW_DRAFTS" long:"show-drafts" description:"show drafts"`
FilesChannelSize int `env:"FILES_CHANNEL_SIZE" long:"files-channel-size" description:"size of file channel" default:"100"`
ImagesChannelSize int `env:"IMAGES_CHANNEL_SIZE" long:"images-channel-size" description:"size of images channel" default:"100"`
CacheDirectory string `env:"CACHE_DIR" long:"cache-dir" description:"cache directory" default:"cache"`
I18NDirectory string `env:"I18N_DIR" long:"i18n-dir" description:"i18n directory" default:"i18n"`
SearchEnabled bool `env:"SEARCH_ENABLED" long:"search-enabled" description:"enable search"`
SearchHost string `env:"SEARCH_HOST" long:"search-host" description:"search host" default:"https://local.chuhlomin.com/blog/search"`
SearchMasterKey string `env:"SEARCH_MASTER_KEY" long:"search-master-key" description:"search master key, used to create index"`
SearchAPIKey string `env:"SEARCH_API_KEY" long:"search-api-key" description:"search api key, used on frontend to search"`
ThumbPath string `env:"THUMB_PATH" long:"thumb-path" description:"path to thumbnails" default:"/img/thumbs/"`
OpenGraphCacheFile string `env:"OPENGRAPH_CACHE_FILE" long:"opengraph-cache-file" description:"opengraph cache file" default:"cache.yml"`
PhotosDomain string `env:"PHOTOS_DOMAIN" long:"photos-domain" description:"photos domain" default:"https://photos.chuhlomin.com/"`
FilesChannelSize int `env:"FILES_CHANNEL_SIZE" long:"files-channel-size" description:"size of file channel" default:"100"`
ImagesChannelSize int `env:"IMAGES_CHANNEL_SIZE" long:"images-channel-size" description:"size of images channel" default:"100"`
SearchTimeout time.Duration `env:"SEARCH_TIMEOUT" long:"search-timeout" description:"search timeout" default:"5s"`
NumWorkers int `env:"NUM_WORKERS" long:"workers" description:"number of workers" default:"4"`
ThumbPath string `env:"THUMB_PATH" long:"thumb-path" description:"path to thumbnails" default:"/img/thumbs/"`
ThumbMaxWidth int `env:"THUMB_MAX_WIDTH" long:"thumb-max-width" description:"max width of thumbnails" default:"140"`
ThumbMaxHeight int `env:"THUMB_MAX_HEIGHT" long:"thumb-max-height" description:"max height of thumbnails" default:"140"`
OpenGraphTimeout time.Duration `env:"OPENGRAPH_TIMEOUT" long:"opengraph-timeout" description:"opengraph timeout" default:"5s"`
OpenGraphCacheFile string `env:"OPENGRAPH_CACHE_FILE" long:"opengraph-cache-file" description:"opengraph cache file" default:"cache.yml"`
PhotosDomain string `env:"PHOTOS_DOMAIN" long:"photos-domain" description:"photos domain" default:"https://photos.chuhlomin.com/"`
Debug bool `env:"DEBUG" long:"debug" description:"debug mode"`
RemoveHTMLExtension bool `env:"REMOVE_HTML_EXT" long:"remove-html-ext" description:"remove .html extension from urls"`
CommentsEnabled bool `env:"COMMENTS_ENABLED" long:"comments-enabled" description:"enable comments"`
ShowDrafts bool `env:"SHOW_DRAFTS" long:"show-drafts" description:"show drafts"`
SearchEnabled bool `env:"SEARCH_ENABLED" long:"search-enabled" description:"enable search"`
}

// GetString returns the value of the environment variable named by the key.
Expand Down
10 changes: 5 additions & 5 deletions generator/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ var (
type Data struct {
File *MarkdownFile
All map[string]*MarkdownFile
Timestamp string
AllSorted []*MarkdownFile
Alternates []*MarkdownFile // used only for index.html
Timestamp string
}

// MarkdownFile represents a markdown file, for example
Expand All @@ -42,6 +42,7 @@ type Data struct {
// # Title
// Page content
type MarkdownFile struct {
CommentsEnabled *bool `yaml:"comments_enabled"` // comments_enabled overrides config.CommentsEnabled
Source string `yaml:"-"` // path to the source markdown file
Path string `yaml:"-"` // path to the generated HTML file
Canonical string `yaml:"-"` // canonical URL
Expand All @@ -52,18 +53,17 @@ type MarkdownFile struct {
Body string `yaml:"-" indexer:"no_store"` // html body, generated from markdown
Date string `yaml:"date" indexer:"date"` // date when post was published, in format "2006-01-02"
Type string `yaml:"type"` // "post" (by default), "page", etc.
Tags tags `yaml:"tags"` // post tags, by default parsed from the post
Language string `yaml:"language"` // language ("en", "ru", ...), parsed from filename, overrides config.DefaultLanguage
Draft bool `yaml:"draft"` // draft is used to mark post as draft
Template string `yaml:"template"` // template to use in config.TemplatesDirectory, overrides default "post.html"
Order string `yaml:"order"` // can be used to sort pages
CommentsEnabled *bool `yaml:"comments_enabled"` // comments_enabled overrides config.CommentsEnabled
Description string `yaml:"description" indexer:"text"` // description is used for the meta description
Author string `yaml:"author"` // author is used for the meta author
Keywords string `yaml:"keywords"` // keywords is used for the meta keywords
Refs []string `yaml:"refs"` // references to other posts, used to generate the list of related posts
Image string `yaml:"image"` // image associated with the post; it's used to generate the thumbnailPath
Tags tags `yaml:"tags"` // post tags, by default parsed from the post
Refs []string `yaml:"refs"` // references to other posts, used to generate the list of related posts
Images []image `yaml:"-"` // images in the post
Draft bool `yaml:"draft"` // draft is used to mark post as draft
}

// image is a struct that contains metadata of image from the post
Expand Down
6 changes: 3 additions & 3 deletions generator/photos.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ type Photo struct {
Path string
Title string `yaml:"title,omitempty"`
TitleRu string `yaml:"title_ru,omitempty"`
ThumbPath string `yaml:"thumb,omitempty"`
Blurhash string `yaml:"blurhash,omitempty"`
BlurhashImageBase64 string `yaml:"blurhash_image_base64,omitempty"`
Width int `yaml:"width,omitempty"`
Height int `yaml:"height,omitempty"`
ThumbPath string `yaml:"thumb,omitempty"`
ThumbXOffset int `yaml:"thumb_x,omitempty"`
ThumbYOffset int `yaml:"thumb_y,omitempty"`
ThumbWidth int `yaml:"thumb_width,omitempty"`
ThumbHeight int `yaml:"thumb_height,omitempty"`
ThumbTotalWidth int `yaml:"thumb_total_width,omitempty"`
ThumbTotalHeight int `yaml:"thumb_total_height,omitempty"`
Blurhash string `yaml:"blurhash,omitempty"`
BlurhashImageBase64 string `yaml:"blurhash_image_base64,omitempty"`
}

func (g *Generator) processPhotos(fileContent []byte) (interface{}, error) {
Expand Down

0 comments on commit bbced2e

Please sign in to comment.