linx-server/server.go

180 lines
5.5 KiB
Go
Raw Normal View History

2015-09-24 01:44:49 -04:00
package main
import (
"flag"
"log"
"net"
"net/http"
"net/http/fcgi"
2015-09-25 22:03:14 -04:00
"os"
2015-09-27 22:17:12 -04:00
"regexp"
"strconv"
2015-09-30 15:54:30 -04:00
"time"
2015-09-24 01:44:49 -04:00
"github.com/GeertJohan/go.rice"
"github.com/flosch/pongo2"
2015-09-24 01:44:49 -04:00
"github.com/zenazn/goji"
2015-10-07 12:48:44 -04:00
"github.com/zenazn/goji/graceful"
"github.com/zenazn/goji/web/middleware"
2015-09-24 01:44:49 -04:00
)
var Config struct {
bind string
filesDir string
metaDir string
siteName string
siteURL string
2015-10-07 12:48:44 -04:00
certFile string
keyFile string
contentSecurityPolicy string
fileContentSecurityPolicy string
xFrameOptions string
maxSize int64
2015-10-06 02:51:49 -04:00
noLogs bool
allowHotlink bool
fastcgi bool
remoteUploads bool
2015-09-24 01:44:49 -04:00
}
var Templates = make(map[string]*pongo2.Template)
var TemplateSet *pongo2.TemplateSet
2015-09-30 15:54:30 -04:00
var staticBox *rice.Box
var timeStarted time.Time
var timeStartedStr string
func setup() {
goji.Use(ContentSecurityPolicy(CSPOptions{
policy: Config.contentSecurityPolicy,
frame: Config.xFrameOptions,
}))
if Config.noLogs {
goji.Abandon(middleware.Logger)
}
2015-09-27 22:17:12 -04:00
// make directories if needed
err := os.MkdirAll(Config.filesDir, 0755)
2015-09-25 22:03:14 -04:00
if err != nil {
2015-10-06 02:49:57 -04:00
log.Fatal("Could not create files directory:", err)
2015-09-25 22:03:14 -04:00
}
2015-09-27 22:17:12 -04:00
err = os.MkdirAll(Config.metaDir, 0700)
if err != nil {
2015-10-06 02:49:57 -04:00
log.Fatal("Could not create metadata directory:", err)
2015-09-27 22:17:12 -04:00
}
// ensure siteURL ends wth '/'
if lastChar := Config.siteURL[len(Config.siteURL)-1:]; lastChar != "/" {
Config.siteURL = Config.siteURL + "/"
}
// Template setup
2015-09-28 21:58:50 -04:00
p2l, err := NewPongo2TemplatesLoader()
if err != nil {
2015-10-06 02:49:57 -04:00
log.Fatal("Error: could not load templates", err)
}
TemplateSet := pongo2.NewSet("templates", p2l)
TemplateSet.Globals["sitename"] = Config.siteName
2015-10-08 21:48:06 -04:00
TemplateSet.Globals["siteurl"] = Config.siteURL
err = populateTemplatesMap(TemplateSet, Templates)
if err != nil {
2015-10-06 02:49:57 -04:00
log.Fatal("Error: could not load templates", err)
}
2015-09-30 15:54:30 -04:00
staticBox = rice.MustFindBox("static")
timeStarted = time.Now()
timeStartedStr = strconv.FormatInt(timeStarted.Unix(), 10)
2015-09-30 15:54:30 -04:00
// Routing setup
2015-09-24 01:44:49 -04:00
nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`)
2015-09-28 23:46:43 -04:00
selifIndexRe := regexp.MustCompile(`^/selif/$`)
torrentRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)/torrent$`)
2015-09-24 01:44:49 -04:00
goji.Get("/", indexHandler)
2015-09-30 00:56:51 -04:00
goji.Get("/paste/", pasteHandler)
goji.Get("/paste", http.RedirectHandler("/paste/", 301))
2015-10-08 21:48:06 -04:00
goji.Get("/API/", apiDocHandler)
goji.Get("/API", http.RedirectHandler("/API/", 301))
2015-10-01 20:58:08 -04:00
if Config.remoteUploads {
goji.Get("/upload", uploadRemote)
goji.Get("/upload/", uploadRemote)
}
2015-09-24 01:44:49 -04:00
goji.Post("/upload", uploadPostHandler)
2015-09-28 16:02:03 -04:00
goji.Post("/upload/", uploadPostHandler)
2015-09-24 01:44:49 -04:00
goji.Put("/upload", uploadPutHandler)
goji.Put("/upload/:name", uploadPutHandler)
2015-09-28 23:46:43 -04:00
goji.Delete("/:name", deleteHandler)
2015-09-30 15:54:30 -04:00
goji.Get("/static/*", staticHandler)
2015-10-04 12:58:30 -04:00
goji.Get("/favicon.ico", staticHandler)
goji.Get("/robots.txt", staticHandler)
2015-09-24 01:44:49 -04:00
goji.Get(nameRe, fileDisplayHandler)
goji.Get(selifRe, fileServeHandler)
2015-09-28 23:46:43 -04:00
goji.Get(selifIndexRe, unauthorizedHandler)
goji.Get(torrentRe, fileTorrentHandler)
goji.NotFound(notFoundHandler)
}
func main() {
flag.StringVar(&Config.bind, "b", "127.0.0.1:8080",
"host to bind to (default: 127.0.0.1:8080)")
flag.StringVar(&Config.filesDir, "filespath", "files/",
"path to files directory")
flag.StringVar(&Config.metaDir, "metapath", "meta/",
"path to metadata directory")
flag.BoolVar(&Config.noLogs, "nologs", false,
"remove stdout output for each request")
2015-09-29 19:28:10 -04:00
flag.BoolVar(&Config.allowHotlink, "allowhotlink", false,
"Allow hotlinking of files")
flag.StringVar(&Config.siteName, "sitename", "linx",
"name of the site")
flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/",
"site base url (including trailing slash)")
flag.Int64Var(&Config.maxSize, "maxsize", 4*1024*1024*1024,
"maximum upload file size in bytes (default 4GB)")
2015-10-07 12:48:44 -04:00
flag.StringVar(&Config.certFile, "certfile", "",
"path to ssl certificate (for https)")
flag.StringVar(&Config.keyFile, "keyfile", "",
"path to ssl key (for https)")
flag.BoolVar(&Config.fastcgi, "fastcgi", false,
"serve through fastcgi")
2015-10-01 20:58:08 -04:00
flag.BoolVar(&Config.remoteUploads, "remoteuploads", false,
"enable remote uploads")
2015-10-04 22:43:42 -04:00
flag.StringVar(&Config.contentSecurityPolicy, "contentsecuritypolicy",
"default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; referrer none;",
"value of default Content-Security-Policy header")
2015-10-04 22:43:42 -04:00
flag.StringVar(&Config.fileContentSecurityPolicy, "filecontentsecuritypolicy",
"default-src 'none'; img-src 'self'; object-src 'self'; media-src 'self'; sandbox; referrer none;",
"value of Content-Security-Policy header for file access")
2015-10-04 22:43:42 -04:00
flag.StringVar(&Config.xFrameOptions, "xframeoptions", "SAMEORIGIN",
"value of X-Frame-Options header")
flag.Parse()
setup()
if Config.fastcgi {
2015-10-07 12:48:44 -04:00
listener, err := net.Listen("tcp", Config.bind)
if err != nil {
log.Fatal("Could not bind: ", err)
}
log.Printf("Serving over fastcgi, bound on %s, using siteurl %s", Config.bind, Config.siteURL)
fcgi.Serve(listener, goji.DefaultMux)
2015-10-07 12:48:44 -04:00
} else if Config.certFile != "" {
log.Printf("Serving over https, bound on %s, using siteurl %s", Config.bind, Config.siteURL)
err := graceful.ListenAndServeTLS(Config.bind, Config.certFile, Config.keyFile, goji.DefaultMux)
if err != nil {
log.Fatal(err)
}
} else {
2015-10-07 12:48:44 -04:00
log.Printf("Serving over http, bound on %s, using siteurl %s", Config.bind, Config.siteURL)
err := graceful.ListenAndServe(Config.bind, goji.DefaultMux)
if err != nil {
log.Fatal(err)
}
}
2015-09-24 01:44:49 -04:00
}