Skip to content

Commit

Permalink
Extract some strings as constants
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Jul 24, 2024
1 parent 8ed446d commit ad09a66
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion engine/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (ac *Config) LoadBasicWeb(w http.ResponseWriter, req *http.Request, L *lua.
}

lv := L.ToString(1)
w.Header().Add("Content-Type", lv)
w.Header().Add(contentType, lv)
return 0 // number of results
}))

Expand Down
2 changes: 1 addition & 1 deletion engine/dirhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (ac *Config) DirectoryListing(w http.ResponseWriter, req *http.Request, roo
}

// Serve the page
w.Header().Add("Content-Type", "text/html;charset=utf-8")
w.Header().Add(contentType, htmlUTF8)
ac.DataToClient(w, req, dirname, htmldata)
}

Expand Down
35 changes: 19 additions & 16 deletions engine/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const (

// Used for deciding how long to wait before quitting when only serving a single file and starting a browser
defaultSoonDuration = time.Second * 3

contentType = "Content-Type"
htmlUTF8 = "text/html;charset=utf-8"
)

var oc *ollamaclient.Config
Expand All @@ -48,7 +51,7 @@ func (ac *Config) ClientCanGzip(req *http.Request) bool {

// PongoHandler renders and serves a Pongo2 template
func (ac *Config) PongoHandler(w http.ResponseWriter, req *http.Request, filename, ext string) {
w.Header().Add("Content-Type", "text/html;charset=utf-8")
w.Header().Add(contentType, htmlUTF8)
pongoblock, err := ac.cache.Read(filename, ac.shouldCache(ext))
if err != nil {
if ac.debugMode {
Expand Down Expand Up @@ -147,7 +150,7 @@ func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, l

// HTML pages are handled differently, if auto-refresh has been enabled
case ".html", ".htm":
w.Header().Add("Content-Type", "text/html;charset=utf-8")
w.Header().Add(contentType, htmlUTF8)

// Read the file (possibly in compressed format, straight from the cache)
htmlblock, err := ac.ReadAndLogErrors(w, filename, ext)
Expand All @@ -171,15 +174,15 @@ func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, l
return

case ".md", ".markdown":
w.Header().Add("Content-Type", "text/html;charset=utf-8")
w.Header().Add(contentType, htmlUTF8)
if markdownblock, err := ac.ReadAndLogErrors(w, filename, ext); err == nil { // success
// Render the markdown page
ac.MarkdownPage(w, req, markdownblock.Bytes(), filename)
}
return

case ".frm", ".form":
w.Header().Add("Content-Type", "text/html;charset=utf-8")
w.Header().Add(contentType, htmlUTF8)
formblock, err := ac.cache.Read(filename, ac.shouldCache(ext))
if err != nil {
return
Expand All @@ -194,7 +197,7 @@ func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, l
return

case ".amber", ".amb":
w.Header().Add("Content-Type", "text/html;charset=utf-8")
w.Header().Add(contentType, htmlUTF8)
amberblock, err := ac.ReadAndLogErrors(w, filename, ext)
if err != nil {
return
Expand Down Expand Up @@ -323,7 +326,7 @@ func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, l

case ".gcss":
if gcssblock, err := ac.ReadAndLogErrors(w, filename, ext); err == nil { // success
w.Header().Add("Content-Type", "text/css;charset=utf-8")
w.Header().Add(contentType, "text/css;charset=utf-8")
// Render the GCSS page as CSS
ac.GCSSPage(w, req, filename, gcssblock.Bytes())
}
Expand All @@ -332,15 +335,15 @@ func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, l
case ".scss":
if scssblock, err := ac.ReadAndLogErrors(w, filename, ext); err == nil { // success
// Render the SASS page (with .scss extension) as CSS
w.Header().Add("Content-Type", "text/css;charset=utf-8")
w.Header().Add(contentType, "text/css;charset=utf-8")
ac.SCSSPage(w, req, filename, scssblock.Bytes())
}
return

case ".happ", ".hyper", ".hyper.jsx", ".hyper.js": // hyperApp JSX -> JS, wrapped in HTML
if jsxblock, err := ac.ReadAndLogErrors(w, filename, ext); err == nil { // success
// Render the JSX page as HTML with embedded JavaScript
w.Header().Add("Content-Type", "text/html;charset=utf-8")
w.Header().Add(contentType, htmlUTF8)
ac.HyperAppPage(w, req, filename, jsxblock.Bytes())
} else {
logrus.Error("Error when serving " + filename + ":" + err.Error())
Expand All @@ -351,7 +354,7 @@ func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, l
case ".jsx":
if jsxblock, err := ac.ReadAndLogErrors(w, filename, ext); err == nil { // success
// Render the JSX page as JavaScript
w.Header().Add("Content-Type", "text/javascript;charset=utf-8")
w.Header().Add(contentType, "text/javascript;charset=utf-8")
ac.JSXPage(w, req, filename, jsxblock.Bytes())
} else {
logrus.Error("Error when serving " + filename + ":" + err.Error())
Expand All @@ -370,7 +373,7 @@ func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, l
contentType := strings.TrimSpace(lines[0])
model := strings.TrimSpace(lines[1])
prompt := strings.TrimSpace(strings.Join(lines[3:], "\n"))
w.Header().Add("Content-Type", contentType)
w.Header().Add(contentType, contentType)
if oc == nil {
oc = ollamaclient.New()
}
Expand Down Expand Up @@ -401,20 +404,20 @@ func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, l
// Text and configuration files (most likely)
case "", ".asciidoc", ".conf", ".config", ".diz", ".example", ".gitignore", ".gitmodules", ".ini", ".log", ".lst", ".me", ".nfo", ".pem", ".readme", ".sub", ".sum", ".tml", ".toml", ".txt", ".yaml", ".yml":
// Set headers for displaying it in the browser.
w.Header().Set("Content-Type", "text/plain;charset=utf-8")
w.Header().Set(contentType, "text/plain;charset=utf-8")

// Source files that may be used by web pages
case ".js":
w.Header().Add("Content-Type", "text/javascript;charset=utf-8")
w.Header().Add(contentType, "text/javascript;charset=utf-8")

// JSON
case ".json":
w.Header().Add("Content-Type", "application/json;charset=utf-8")
w.Header().Add(contentType, "application/json;charset=utf-8")

// Source code files for viewing
case ".S", ".ada", ".asm", ".bash", ".bat", ".c", ".c++", ".cc", ".cl", ".clj", ".cpp", ".cs", ".cxx", ".el", ".elm", ".erl", ".fish", ".go", ".h", ".h++", ".hpp", ".hs", ".java", ".kt", ".lisp", ".mk", ".ml", ".pas", ".pl", ".py", ".r", ".rb", ".rs", ".scm", ".sh", ".ts", ".tsx":
// Set headers for displaying it in the browser.
w.Header().Set("Content-Type", "text/plain;charset=utf-8")
w.Header().Set(contentType, "text/plain;charset=utf-8")

// Common binary file extensions
case ".7z", ".arj", ".bin", ".com", ".dat", ".db", ".elf", ".exe", ".gz", ".iso", ".lz", ".rar", ".tar.bz", ".tar.bz2", ".tar.gz", ".tar.xz", ".tbz", ".tbz2", ".tgz", ".txz", ".xz", ".zip":
Expand All @@ -424,7 +427,7 @@ func (ac *Config) FilePage(w http.ResponseWriter, req *http.Request, filename, l
default:
// If the filename starts with a ".", assume it's a plain text configuration file
if strings.HasPrefix(filepath.Base(lowercaseFilename), ".") {
w.Header().Set("Content-Type", "text/plain;charset=utf-8")
w.Header().Set(contentType, "text/plain;charset=utf-8")
} else {
// Set the correct Content-Type
if ac.mimereader != nil {
Expand Down Expand Up @@ -603,7 +606,7 @@ func (ac *Config) RegisterHandlers(mux *http.ServeMux, handlePath, servedir stri
} else {
limiter := tollbooth.NewLimiter(float64(ac.limitRequests), nil)
limiter.SetMessage(themes.MessagePage("Rate-limit exceeded", "<div style='color:red'>You have reached the maximum request limit.</div>", theme))
limiter.SetMessageContentType("text/html;charset=utf-8")
limiter.SetMessageContentType(htmlUTF8)
mux.Handle(handlePath, tollbooth.LimitFuncHandler(limiter, allRequests))
}
}
2 changes: 1 addition & 1 deletion engine/luahandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (ac *Config) LoadLuaHandlerFunctions(L *lua.LState, filename string, mux *h
} else {
limiter := tollbooth.NewLimiter(float64(ac.limitRequests), nil)
limiter.SetMessage(themes.MessagePage("Rate-limit exceeded", "<div style='color:red'>You have reached the maximum request limit.</div>", theme))
limiter.SetMessageContentType("text/html;charset=utf-8")
limiter.SetMessageContentType(htmlUTF8)
mux.Handle(handlePath, tollbooth.LimitFuncHandler(limiter, wrappedHandleFunc))
}

Expand Down
2 changes: 1 addition & 1 deletion engine/prettyerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (ac *Config) PrettyError(w http.ResponseWriter, req *http.Request, filename
w.WriteHeader(http.StatusOK)

// HTTP content type
w.Header().Add("Content-Type", "text/html;charset=utf-8")
w.Header().Add(contentType, htmlUTF8)

var (
code string
Expand Down
26 changes: 15 additions & 11 deletions engine/rendering.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import (
_ "embed"
)

const markdownCodeStyle = "base16-snazzy" // using xyproto/splash
const (
markdownCodeStyle = "base16-snazzy" // using xyproto/splash
linkHref = `<link href="`
stylesheetCSS = `" rel="stylesheet" type="text/css">`
)

var (
//go:embed static/tex-svg.js
Expand Down Expand Up @@ -394,9 +398,9 @@ func (ac *Config) MarkdownPage(w http.ResponseWriter, req *http.Request, mdConte
switch {
case ac.fs.Exists(CSSFilename):
// Link to stylesheet (without checking if the CSS file is valid first)
head.WriteString(`<link href="`)
head.WriteString(linkHref)
head.WriteString(themes.DefaultCSSFilename)
head.WriteString(`" rel="stylesheet" type="text/css">`)
head.WriteString(stylesheetCSS)
case ac.fs.Exists(GCSSFilename):
if ac.debugMode {
gcssblock, err := ac.cache.Read(GCSSFilename, ac.shouldCache(".gcss"))
Expand All @@ -417,9 +421,9 @@ func (ac *Config) MarkdownPage(w http.ResponseWriter, req *http.Request, mdConte
}
}
// Link to stylesheet (without checking if the GCSS file is valid first)
head.WriteString(`<link href="`)
head.WriteString(linkHref)
head.WriteString(themes.DefaultGCSSFilename)
head.WriteString(`" rel="stylesheet" type="text/css">`)
head.WriteString(stylesheetCSS)
default:
// If not, use the theme by inserting the CSS style directly
head.Write(themes.StyleHead(string(theme)))
Expand All @@ -439,9 +443,9 @@ func (ac *Config) MarkdownPage(w http.ResponseWriter, req *http.Request, mdConte
cssdata := cssblock.Bytes()
head.WriteString("<style>" + string(cssdata) + "</style>")
} else {
head.WriteString(`<link href="`)
head.WriteString(linkHref)
head.WriteString(additionalCSSfile)
head.WriteString(`" rel="stylesheet" type="text/css">`)
head.WriteString(stylesheetCSS)
}
}

Expand Down Expand Up @@ -845,9 +849,9 @@ func (ac *Config) HyperAppPage(w http.ResponseWriter, req *http.Request, filenam
switch {
case ac.fs.Exists(CSSFilename):
// Link to stylesheet (without checking if the GCSS file is valid first)
htmlbuf.WriteString(`<link href="`)
htmlbuf.WriteString(linkHref)
htmlbuf.WriteString(themes.DefaultCSSFilename)
htmlbuf.WriteString(`" rel="stylesheet" type="text/css">`)
htmlbuf.WriteString(stylesheetCSS)
case ac.fs.Exists(GCSSFilename):
if ac.debugMode {
gcssblock, err := ac.cache.Read(GCSSFilename, ac.shouldCache(".gcss"))
Expand All @@ -868,9 +872,9 @@ func (ac *Config) HyperAppPage(w http.ResponseWriter, req *http.Request, filenam
}
}
// Link to stylesheet (without checking if the GCSS file is valid first)
htmlbuf.WriteString(`<link href="`)
htmlbuf.WriteString(linkHref)
htmlbuf.WriteString(themes.DefaultGCSSFilename)
htmlbuf.WriteString(`" rel="stylesheet" type="text/css">`)
htmlbuf.WriteString(stylesheetCSS)
default:
// If not, use the default hyperapp theme by inserting the CSS style directly
theme := ac.defaultTheme
Expand Down

0 comments on commit ad09a66

Please sign in to comment.