2022-09-08 10:12:06 -05:00
|
|
|
/*
|
2023-01-18 11:19:29 -06:00
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
2022-09-08 10:12:06 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-05-30 18:47:26 -05:00
|
|
|
"bytes"
|
2023-05-31 12:43:07 -05:00
|
|
|
"errors"
|
2022-09-10 18:03:04 -05:00
|
|
|
"fmt"
|
2022-09-08 10:12:06 -05:00
|
|
|
"io"
|
2023-05-08 11:45:57 -05:00
|
|
|
"net"
|
2022-09-08 10:12:06 -05:00
|
|
|
"net/http"
|
2022-09-08 10:57:59 -05:00
|
|
|
"os"
|
2023-02-08 07:50:40 -06:00
|
|
|
"os/signal"
|
2022-09-16 13:45:33 -05:00
|
|
|
"path/filepath"
|
2022-10-23 13:45:49 -05:00
|
|
|
"regexp"
|
2022-10-23 16:29:58 -05:00
|
|
|
"runtime"
|
2022-09-08 10:12:06 -05:00
|
|
|
"strconv"
|
2022-09-08 15:57:52 -05:00
|
|
|
"strings"
|
2023-01-20 22:42:44 -06:00
|
|
|
"sync"
|
2023-02-08 07:50:40 -06:00
|
|
|
"syscall"
|
2022-09-10 18:03:04 -05:00
|
|
|
"time"
|
2022-11-09 23:17:19 -06:00
|
|
|
|
2023-09-06 10:15:11 -05:00
|
|
|
"net/http/pprof"
|
2023-09-06 09:06:44 -05:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
"github.com/julienschmidt/httprouter"
|
2022-11-09 23:17:19 -06:00
|
|
|
"github.com/yosssi/gohtml"
|
2023-09-11 11:25:39 -05:00
|
|
|
"seedno.de/seednode/roulette/formats"
|
2022-09-08 10:12:06 -05:00
|
|
|
)
|
|
|
|
|
2022-10-20 17:12:29 -05:00
|
|
|
const (
|
2023-02-18 14:08:11 -06:00
|
|
|
LogDate string = `2006-01-02T15:04:05.000-07:00`
|
2023-06-03 13:29:49 -05:00
|
|
|
SourcePrefix string = `/source`
|
2023-09-10 20:29:11 -05:00
|
|
|
MediaPrefix string = `/view`
|
2023-02-18 14:08:11 -06:00
|
|
|
RedirectStatusCode int = http.StatusSeeOther
|
|
|
|
Timeout time.Duration = 10 * time.Second
|
2023-09-06 11:53:19 -05:00
|
|
|
)
|
2023-05-12 09:05:14 -05:00
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
func serveStaticFile(paths []string, stats *ServeStats, index *FileIndex) httprouter.Handle {
|
2023-06-03 13:29:49 -05:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
path := strings.TrimPrefix(r.URL.Path, SourcePrefix)
|
|
|
|
|
|
|
|
prefixedFilePath, err := stripQueryParams(path)
|
2023-05-08 13:12:51 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2023-06-03 18:45:32 -05:00
|
|
|
|
2023-08-05 21:38:28 -05:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 10:24:12 -05:00
|
|
|
|
2023-05-08 13:12:51 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
filePath, err := filepath.EvalSymlinks(strings.TrimPrefix(prefixedFilePath, SourcePrefix))
|
2023-05-08 13:12:51 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2023-06-03 18:45:32 -05:00
|
|
|
|
2023-08-05 21:38:28 -05:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 10:24:12 -05:00
|
|
|
|
2023-05-08 13:12:51 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pathIsValid(filePath, paths) {
|
|
|
|
notFound(w, r, filePath)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := fileExists(filePath)
|
2023-01-24 19:06:15 -06:00
|
|
|
if err != nil {
|
2023-02-07 07:39:04 -06:00
|
|
|
fmt.Println(err)
|
2023-06-03 18:45:32 -05:00
|
|
|
|
2023-08-05 21:38:28 -05:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 10:24:12 -05:00
|
|
|
|
2023-05-08 13:12:51 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
notFound(w, r, filePath)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
buf, err := os.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2023-06-03 18:45:32 -05:00
|
|
|
|
2023-08-05 21:38:28 -05:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 10:24:12 -05:00
|
|
|
|
2023-05-08 13:12:51 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(buf)
|
|
|
|
|
|
|
|
fileSize := humanReadableSize(len(buf))
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Russian {
|
2023-09-09 20:57:50 -05:00
|
|
|
err = os.Remove(filePath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
serverError(w, r, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-09-10 21:28:27 -05:00
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Cache {
|
2023-09-10 21:28:27 -05:00
|
|
|
index.Remove(filePath)
|
|
|
|
}
|
2023-09-09 20:57:50 -05:00
|
|
|
}
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Verbose {
|
2023-05-08 13:12:51 -05:00
|
|
|
fmt.Printf("%s | Served %s (%s) to %s in %s\n",
|
|
|
|
startTime.Format(LogDate),
|
|
|
|
filePath,
|
|
|
|
fileSize,
|
|
|
|
realIP(r),
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Statistics {
|
2023-05-08 13:12:51 -05:00
|
|
|
stats.incrementCounter(filePath, startTime, fileSize)
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
2023-09-09 20:57:50 -05:00
|
|
|
|
2023-01-19 20:31:02 -06:00
|
|
|
}
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
2023-01-19 20:31:02 -06:00
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
func serveRoot(paths []string, Regexes *Regexes, index *FileIndex, registeredFormats *formats.SupportedFormats) httprouter.Handle {
|
2023-06-03 13:29:49 -05:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
2023-01-24 19:06:15 -06:00
|
|
|
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
|
|
|
|
if err != nil {
|
2023-02-07 07:39:04 -06:00
|
|
|
fmt.Println(err)
|
2023-06-03 18:37:06 -05:00
|
|
|
|
2023-08-05 21:38:28 -05:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 10:24:12 -05:00
|
|
|
|
2023-02-07 07:39:04 -06:00
|
|
|
return
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
2023-01-20 22:42:44 -06:00
|
|
|
|
2023-09-10 20:29:11 -05:00
|
|
|
strippedRefererUri := strings.TrimPrefix(refererUri, MediaPrefix)
|
2023-06-03 18:37:06 -05:00
|
|
|
|
2023-01-24 19:06:15 -06:00
|
|
|
filters := &Filters{
|
2023-01-27 10:06:10 -06:00
|
|
|
includes: splitQueryParams(r.URL.Query().Get("include"), Regexes),
|
|
|
|
excludes: splitQueryParams(r.URL.Query().Get("exclude"), Regexes),
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
|
|
|
|
2023-05-08 13:12:51 -05:00
|
|
|
sortOrder := SortOrder(r)
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
_, refreshInterval := refreshInterval(r)
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
var filePath string
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
if refererUri != "" {
|
2023-09-11 12:09:08 -05:00
|
|
|
filePath, err = nextFile(strippedRefererUri, sortOrder, Regexes, registeredFormats)
|
2023-06-03 13:29:49 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2023-06-03 18:37:06 -05:00
|
|
|
|
2023-08-05 21:38:28 -05:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 10:24:12 -05:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
return
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
2023-06-03 13:29:49 -05:00
|
|
|
}
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
loop:
|
|
|
|
for timeout := time.After(Timeout); ; {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
break loop
|
|
|
|
default:
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
if filePath != "" {
|
|
|
|
break loop
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
|
|
|
|
2023-09-11 12:09:08 -05:00
|
|
|
filePath, err = newFile(paths, filters, sortOrder, Regexes, index, registeredFormats)
|
2023-06-03 13:29:49 -05:00
|
|
|
switch {
|
2023-09-10 20:29:11 -05:00
|
|
|
case err != nil && err == ErrNoMediaFound:
|
2023-01-24 19:06:15 -06:00
|
|
|
notFound(w, r, filePath)
|
|
|
|
|
|
|
|
return
|
2023-06-03 13:29:49 -05:00
|
|
|
case err != nil:
|
2023-02-07 07:39:04 -06:00
|
|
|
fmt.Println(err)
|
2023-06-03 18:37:06 -05:00
|
|
|
|
2023-08-05 21:38:28 -05:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 10:24:12 -05:00
|
|
|
|
2023-02-07 07:39:04 -06:00
|
|
|
return
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
2023-06-03 13:29:49 -05:00
|
|
|
}
|
2023-05-08 20:05:10 -05:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
queryParams := generateQueryParams(filters, sortOrder, refreshInterval)
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
newUrl := fmt.Sprintf("http://%s%s%s",
|
|
|
|
r.Host,
|
|
|
|
preparePath(filePath),
|
|
|
|
queryParams,
|
|
|
|
)
|
|
|
|
http.Redirect(w, r, newUrl, RedirectStatusCode)
|
|
|
|
}
|
|
|
|
}
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
func serveMedia(paths []string, Regexes *Regexes, index *FileIndex, registeredFormats *formats.SupportedFormats) httprouter.Handle {
|
2023-06-03 13:29:49 -05:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
filters := &Filters{
|
|
|
|
includes: splitQueryParams(r.URL.Query().Get("include"), Regexes),
|
|
|
|
excludes: splitQueryParams(r.URL.Query().Get("exclude"), Regexes),
|
|
|
|
}
|
2023-05-08 20:05:10 -05:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
sortOrder := SortOrder(r)
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-09-11 12:59:40 -05:00
|
|
|
filePath := strings.TrimPrefix(r.URL.Path, MediaPrefix)
|
2023-05-08 13:12:51 -05:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
if runtime.GOOS == "windows" {
|
2023-09-11 12:59:40 -05:00
|
|
|
filePath = strings.TrimPrefix(filePath, "/")
|
2023-06-03 13:29:49 -05:00
|
|
|
}
|
2023-05-08 13:12:51 -05:00
|
|
|
|
2023-09-11 12:59:40 -05:00
|
|
|
exists, err := fileExists(filePath)
|
2023-06-03 13:29:49 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2023-06-03 18:37:06 -05:00
|
|
|
|
2023-08-05 21:38:28 -05:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 10:24:12 -05:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !exists {
|
2023-09-11 12:59:40 -05:00
|
|
|
notFound(w, r, filePath)
|
2023-06-03 13:29:49 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-05-08 13:12:51 -05:00
|
|
|
|
2023-09-11 12:59:40 -05:00
|
|
|
registered, fileType, mimeType, err := formats.FileType(filePath, registeredFormats)
|
2023-06-03 13:29:49 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2023-06-03 18:37:06 -05:00
|
|
|
|
2023-08-05 21:38:28 -05:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 10:24:12 -05:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
return
|
|
|
|
}
|
2023-05-08 13:12:51 -05:00
|
|
|
|
2023-09-11 12:09:08 -05:00
|
|
|
if !registered {
|
2023-09-11 12:59:40 -05:00
|
|
|
notFound(w, r, filePath)
|
2023-05-08 13:12:51 -05:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-11 12:59:40 -05:00
|
|
|
fileUri := generateFileUri(filePath)
|
2023-06-03 13:29:49 -05:00
|
|
|
|
2023-09-11 12:59:40 -05:00
|
|
|
fileName := filepath.Base(filePath)
|
2023-06-03 13:29:49 -05:00
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
refreshTimer, refreshInterval := refreshInterval(r)
|
2023-06-03 13:29:49 -05:00
|
|
|
|
|
|
|
queryParams := generateQueryParams(filters, sortOrder, refreshInterval)
|
|
|
|
|
|
|
|
var htmlBody strings.Builder
|
|
|
|
htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
|
2023-09-06 11:53:19 -05:00
|
|
|
htmlBody.WriteString(FaviconHtml)
|
2023-06-03 13:29:49 -05:00
|
|
|
htmlBody.WriteString(`<style>html,body{margin:0;padding:0;height:100%;}`)
|
2023-09-11 19:11:45 -05:00
|
|
|
htmlBody.WriteString(`a{color:inherit;display:block;height:100%;width:100%;text-decoration:none;}`)
|
2023-06-03 13:29:49 -05:00
|
|
|
htmlBody.WriteString(`img{margin:auto;display:block;max-width:97%;max-height:97%;object-fit:scale-down;`)
|
2023-09-11 19:11:45 -05:00
|
|
|
htmlBody.WriteString(`position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}`)
|
2023-09-11 19:43:18 -05:00
|
|
|
htmlBody.WriteString(fileType.Css)
|
2023-09-11 19:11:45 -05:00
|
|
|
htmlBody.WriteString(`</style>`)
|
2023-09-11 12:59:40 -05:00
|
|
|
htmlBody.WriteString((fileType.Title(queryParams, fileUri, filePath, fileName, mimeType)))
|
2023-06-03 13:29:49 -05:00
|
|
|
htmlBody.WriteString(`</head><body>`)
|
|
|
|
if refreshInterval != "0ms" {
|
|
|
|
htmlBody.WriteString(fmt.Sprintf("<script>window.onload = function(){setInterval(function(){window.location.href = '/%s';}, %d);};</script>",
|
|
|
|
queryParams,
|
|
|
|
refreshTimer))
|
|
|
|
}
|
2023-09-11 12:59:40 -05:00
|
|
|
htmlBody.WriteString((fileType.Body(queryParams, fileUri, filePath, fileName, mimeType)))
|
2023-09-10 21:02:03 -05:00
|
|
|
htmlBody.WriteString(`</body></html>`)
|
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
_, err = io.WriteString(w, gohtml.Format(htmlBody.String()))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2023-06-03 18:37:06 -05:00
|
|
|
|
2023-08-05 21:38:28 -05:00
|
|
|
serverError(w, r, nil)
|
2023-06-20 10:24:12 -05:00
|
|
|
|
2023-06-03 13:29:49 -05:00
|
|
|
return
|
|
|
|
}
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
|
|
|
}
|
2023-06-03 13:29:49 -05:00
|
|
|
|
2023-06-03 15:51:08 -05:00
|
|
|
func serveVersion() httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
2023-09-12 13:06:45 -05:00
|
|
|
data := []byte(fmt.Sprintf("roulette v%s\n", ReleaseVersion))
|
2023-06-03 15:51:08 -05:00
|
|
|
|
|
|
|
w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data))))
|
2023-06-03 18:37:06 -05:00
|
|
|
|
2023-06-03 15:51:08 -05:00
|
|
|
w.Write(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:06:15 -06:00
|
|
|
func ServePage(args []string) error {
|
2023-07-17 07:41:32 -05:00
|
|
|
timeZone := os.Getenv("TZ")
|
|
|
|
if timeZone != "" {
|
|
|
|
var err error
|
|
|
|
time.Local, err = time.LoadLocation(timeZone)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
bindHost, err := net.LookupHost(Bind)
|
2023-05-08 11:45:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bindAddr := net.ParseIP(bindHost[0])
|
|
|
|
if bindAddr == nil {
|
2023-05-31 13:01:24 -05:00
|
|
|
return errors.New("invalid bind address provided")
|
2023-05-08 11:45:57 -05:00
|
|
|
}
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
mux := httprouter.New()
|
|
|
|
|
|
|
|
registeredFormats := &formats.SupportedFormats{
|
|
|
|
Extensions: make(map[string]*formats.SupportedFormat),
|
|
|
|
MimeTypes: make(map[string]*formats.SupportedFormat),
|
|
|
|
}
|
2023-09-11 10:43:09 -05:00
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Audio || All {
|
2023-09-11 12:09:08 -05:00
|
|
|
registeredFormats.Add(formats.RegisterAudioFormats())
|
2023-09-11 10:43:09 -05:00
|
|
|
}
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Flash || All {
|
|
|
|
registeredFormats.Add(formats.RegisterFlashFormats())
|
|
|
|
}
|
|
|
|
|
|
|
|
if Images || All {
|
2023-09-11 12:09:08 -05:00
|
|
|
registeredFormats.Add(formats.RegisterImageFormats())
|
2023-09-11 10:43:09 -05:00
|
|
|
}
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Text || All {
|
2023-09-11 19:11:45 -05:00
|
|
|
registeredFormats.Add(formats.RegisterTextFormats())
|
|
|
|
}
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Videos || All {
|
2023-09-11 12:09:08 -05:00
|
|
|
registeredFormats.Add(formats.RegisterVideoFormats())
|
2023-09-11 10:43:09 -05:00
|
|
|
}
|
|
|
|
|
2023-09-11 12:09:08 -05:00
|
|
|
paths, err := normalizePaths(args, registeredFormats)
|
2023-01-24 12:03:26 -06:00
|
|
|
if err != nil {
|
2023-01-24 19:06:15 -06:00
|
|
|
return err
|
2023-01-19 20:31:02 -06:00
|
|
|
}
|
|
|
|
|
2023-04-11 04:44:18 -05:00
|
|
|
if len(paths) == 0 {
|
2023-09-10 21:08:51 -05:00
|
|
|
return ErrNoMediaFound
|
2023-04-11 04:44:18 -05:00
|
|
|
}
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Russian {
|
2023-09-09 21:00:58 -05:00
|
|
|
fmt.Printf("WARNING! Files *will* be deleted after serving!\n\n")
|
|
|
|
}
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
index := &FileIndex{
|
2023-09-06 17:31:05 -05:00
|
|
|
mutex: sync.RWMutex{},
|
|
|
|
list: []string{},
|
|
|
|
}
|
|
|
|
|
2023-09-10 11:27:55 -05:00
|
|
|
regexes := &Regexes{
|
2023-01-27 10:06:10 -06:00
|
|
|
filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`),
|
2023-09-07 16:20:48 -05:00
|
|
|
alphanumeric: regexp.MustCompile(`^[A-z0-9]*$`),
|
2022-09-16 14:45:54 -05:00
|
|
|
}
|
2022-09-26 12:31:45 -05:00
|
|
|
|
2023-09-06 17:31:05 -05:00
|
|
|
srv := &http.Server{
|
2023-09-12 13:06:45 -05:00
|
|
|
Addr: net.JoinHostPort(Bind, strconv.Itoa(int(Port))),
|
2023-09-06 17:31:05 -05:00
|
|
|
Handler: mux,
|
|
|
|
IdleTimeout: 10 * time.Minute,
|
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
WriteTimeout: 5 * time.Minute,
|
|
|
|
}
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-09-06 17:31:05 -05:00
|
|
|
stats := &ServeStats{
|
2023-01-27 10:06:10 -06:00
|
|
|
mutex: sync.RWMutex{},
|
|
|
|
list: []string{},
|
2023-09-06 17:31:05 -05:00
|
|
|
count: make(map[string]uint32),
|
|
|
|
size: make(map[string]string),
|
|
|
|
times: make(map[string][]string),
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
|
|
|
|
2023-06-20 10:24:12 -05:00
|
|
|
mux.PanicHandler = serverErrorHandler()
|
2023-05-30 18:47:26 -05:00
|
|
|
|
2023-09-11 12:09:08 -05:00
|
|
|
mux.GET("/", serveRoot(paths, regexes, index, registeredFormats))
|
2023-09-06 17:31:05 -05:00
|
|
|
|
|
|
|
mux.GET("/favicons/*favicon", serveFavicons())
|
|
|
|
|
|
|
|
mux.GET("/favicon.ico", serveFavicons())
|
|
|
|
|
2023-09-11 12:09:08 -05:00
|
|
|
mux.GET(MediaPrefix+"/*media", serveMedia(paths, regexes, index, registeredFormats))
|
2023-09-06 17:31:05 -05:00
|
|
|
|
2023-09-09 21:17:17 -05:00
|
|
|
mux.GET(SourcePrefix+"/*static", serveStaticFile(paths, stats, index))
|
2023-09-06 17:31:05 -05:00
|
|
|
|
|
|
|
mux.GET("/version", serveVersion())
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Cache {
|
2023-02-04 23:44:31 -06:00
|
|
|
skipIndex := false
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if CacheFile != "" {
|
|
|
|
err := index.Import(CacheFile)
|
2023-02-08 07:50:40 -06:00
|
|
|
if err == nil {
|
2023-02-04 23:44:31 -06:00
|
|
|
skipIndex = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !skipIndex {
|
2023-09-11 12:09:08 -05:00
|
|
|
index.generateCache(args, registeredFormats)
|
2023-02-04 23:44:31 -06:00
|
|
|
}
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-09-11 12:09:08 -05:00
|
|
|
mux.GET("/clear_cache", serveCacheClear(args, index, registeredFormats))
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Index {
|
|
|
|
mux.GET("/html/", serveIndexHtml(args, index, false))
|
|
|
|
if PageLength != 0 {
|
|
|
|
mux.GET("/html/:page", serveIndexHtml(args, index, true))
|
2023-09-09 00:06:28 -05:00
|
|
|
}
|
2023-06-03 13:29:49 -05:00
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
mux.GET("/json", serveIndexJson(args, index))
|
|
|
|
if PageLength != 0 {
|
|
|
|
mux.GET("/json/:page", serveIndexJson(args, index))
|
2023-09-09 00:06:28 -05:00
|
|
|
}
|
2023-04-28 13:19:07 -05:00
|
|
|
}
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Profile {
|
2023-09-06 10:15:11 -05:00
|
|
|
mux.HandlerFunc("GET", "/debug/pprof/", pprof.Index)
|
|
|
|
mux.HandlerFunc("GET", "/debug/pprof/cmdline", pprof.Cmdline)
|
|
|
|
mux.HandlerFunc("GET", "/debug/pprof/profile", pprof.Profile)
|
|
|
|
mux.HandlerFunc("GET", "/debug/pprof/symbol", pprof.Symbol)
|
|
|
|
mux.HandlerFunc("GET", "/debug/pprof/trace", pprof.Trace)
|
|
|
|
}
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Statistics {
|
|
|
|
if StatisticsFile != "" {
|
|
|
|
stats.Import(StatisticsFile)
|
2023-09-06 17:31:05 -05:00
|
|
|
|
|
|
|
gracefulShutdown := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-gracefulShutdown
|
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
stats.Export(StatisticsFile)
|
2023-09-06 17:31:05 -05:00
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
mux.GET("/stats", serveStats(args, stats))
|
2023-09-12 13:06:45 -05:00
|
|
|
if PageLength != 0 {
|
2023-09-09 00:06:28 -05:00
|
|
|
mux.GET("/stats/:page", serveStats(args, stats))
|
|
|
|
}
|
2023-05-31 12:43:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = srv.ListenAndServe()
|
|
|
|
if !errors.Is(err, http.ErrServerClosed) {
|
2023-01-24 19:06:15 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-09-08 10:12:06 -05:00
|
|
|
}
|