From 52cc3b4dffed5ef6498a17dd02f21a7d83a13eea Mon Sep 17 00:00:00 2001 From: andreimarcu Date: Thu, 1 Oct 2015 10:32:59 -0400 Subject: [PATCH] Add fastcgi support and static cache headers --- README.md | 34 ++++++++++++++++++++++++++-------- fileserve.go | 2 ++ server.go | 13 ++++++++++++- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4c1a593..e62036e 100644 --- a/README.md +++ b/README.md @@ -11,19 +11,37 @@ Soon-to-be opensource replacement of Linx (media-sharing website) Get release and run ------------------- 1. Grab the latest binary from the [releases](https://github.com/andreimarcu/linx-server/releases) -2. Run ```./linx-server-v...``` +2. Run ```./linx-server...``` Command-line options -------------------- -- Specify what to bind to ```-b 0.0.0.0:8080``` -- Specify the sitename ```-sitename myLinx``` -- Specify the siteurl (for generating link) ```-siteurl "http://mylinx.example.org/"``` -- Specify the filespath (where files are uploaded to) ```-filespath files/"``` -- Specify the metapath (where metadata for files are stored) ```-metapath meta/``` -- Optionally: Specify to disable request logs in stdout ```-nologs``` - +- ```-b 127.0.0.1:8080``` -- what to bind to (default is 127.0.0.1:8080) +- ```-sitename myLinx``` -- the site name displayed on top (default is linx) +- ```-siteurl "http://mylinx.example.org/"``` -- the site url (for generating links) +- ```-filespath files/"``` -- Path to store uploads (default is files/) +- ```-metapath meta/``` -- Path to store information about uploads (default is meta/) +- ```-fastcgi``` -- (optionally) serve through fastcgi +- ```-nologs``` -- (optionally) disable request logs in stdout + +Deployment +---------- +A suggetsed deployment is running nginx in front of linx-server serving through fastcgi. +An example configuration: +``` +server { + ... + server_name yourlinx.example.org; + ... + + client_max_body_size 4096M; + location / { + fastcgi_pass 127.0.0.1:8080; + include fastcgi_params; + } +} +``` Development ----------- diff --git a/fileserve.go b/fileserve.go index 45a0e63..8097bfd 100644 --- a/fileserve.go +++ b/fileserve.go @@ -42,6 +42,8 @@ func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) { return } + w.Header().Set("Etag", timeStartedStr) + w.Header().Set("Cache-Control", "max-age=86400") http.ServeContent(w, r, filePath, timeStarted, file) return } diff --git a/server.go b/server.go index 963aa3a..00bbbb9 100644 --- a/server.go +++ b/server.go @@ -6,8 +6,10 @@ import ( "log" "net" "net/http" + "net/http/fcgi" "os" "regexp" + "strconv" "time" "github.com/GeertJohan/go.rice" @@ -24,12 +26,14 @@ var Config struct { allowHotlink bool siteName string siteURL string + fastcgi bool } var Templates = make(map[string]*pongo2.Template) var TemplateSet *pongo2.TemplateSet var staticBox *rice.Box var timeStarted time.Time +var timeStartedStr string func setup() { if Config.noLogs { @@ -70,6 +74,7 @@ func setup() { staticBox = rice.MustFindBox("static") timeStarted = time.Now() + timeStartedStr = strconv.FormatInt(timeStarted.Unix(), 10) // Routing setup nameRe := regexp.MustCompile(`^/(?P[a-z0-9-\.]+)$`) @@ -110,6 +115,8 @@ func main() { "name of the site") flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/", "site base url (including trailing slash)") + flag.BoolVar(&Config.fastcgi, "fastcgi", false, + "serve through fastcgi") flag.Parse() setup() @@ -119,5 +126,9 @@ func main() { log.Fatal("Could not bind: ", err) } - goji.ServeListener(listener) + if Config.fastcgi { + fcgi.Serve(listener, goji.DefaultMux) + } else { + goji.ServeListener(listener) + } }