2022-09-08 15:12:06 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2022 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-09-10 23:03:04 +00:00
|
|
|
"fmt"
|
2022-09-08 15:12:06 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2022-09-08 20:30:51 +00:00
|
|
|
"net/url"
|
2022-09-08 15:57:59 +00:00
|
|
|
"os"
|
2022-09-16 18:45:33 +00:00
|
|
|
"path/filepath"
|
2022-09-08 15:12:06 +00:00
|
|
|
"strconv"
|
2022-09-08 20:57:52 +00:00
|
|
|
"strings"
|
2022-09-10 23:03:04 +00:00
|
|
|
"time"
|
2022-09-08 15:12:06 +00:00
|
|
|
)
|
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
const LOGDATE string = "2006-01-02T15:04:05.000-07:00"
|
|
|
|
const PREFIX string = "/src"
|
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
type Filters struct {
|
|
|
|
Includes []string
|
|
|
|
Excludes []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filters) IsEmpty() bool {
|
2022-10-20 19:22:01 +00:00
|
|
|
if !(f.HasIncludes() && f.HasExcludes()) {
|
2022-10-20 00:27:11 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
func (f *Filters) HasIncludes() bool {
|
|
|
|
if len(f.Includes) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
func (f *Filters) GetIncludes() string {
|
|
|
|
return strings.Join(f.Includes, ",")
|
|
|
|
}
|
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
func (f *Filters) HasExcludes() bool {
|
|
|
|
if len(f.Excludes) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
func (f *Filters) GetExcludes() string {
|
|
|
|
return strings.Join(f.Excludes, ",")
|
|
|
|
}
|
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
type appHandler func(http.ResponseWriter, *http.Request) error
|
2022-09-10 23:03:04 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := fn(w, r); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 01:37:12 +00:00
|
|
|
func splitQueryParams(query string) []string {
|
|
|
|
if query == "" {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
params := strings.Split(query, ",")
|
|
|
|
|
|
|
|
for i := 0; i < len(params); i++ {
|
|
|
|
params[i] = strings.ToLower(params[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return params
|
2022-10-20 01:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateQueryParams(filters *Filters, sort string) string {
|
|
|
|
switch {
|
|
|
|
case Filter && !Sort:
|
|
|
|
return fmt.Sprintf("?include=%v&exclude=%v",
|
|
|
|
filters.GetIncludes(),
|
|
|
|
filters.GetExcludes(),
|
|
|
|
)
|
|
|
|
case !Filter && Sort:
|
|
|
|
return fmt.Sprintf("?sort=%v", sort)
|
|
|
|
case Filter && Sort:
|
|
|
|
return fmt.Sprintf("?include=%v&exclude=%v&sort=%v",
|
|
|
|
filters.GetIncludes(),
|
|
|
|
filters.GetExcludes(),
|
|
|
|
sort,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func stripQueryParams(inUrl string) (string, error) {
|
2022-09-26 17:31:45 +00:00
|
|
|
url, err := url.Parse(inUrl)
|
2022-09-24 23:59:10 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return "", err
|
2022-09-24 23:59:10 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
|
|
|
|
url.RawQuery = ""
|
|
|
|
|
|
|
|
return url.String(), nil
|
2022-09-24 23:59:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-17 16:07:37 +00:00
|
|
|
func refererToUri(referer string) string {
|
|
|
|
parts := strings.SplitAfterN(referer, "/", 4)
|
|
|
|
|
|
|
|
if len(parts) < 4 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return "/" + parts[3]
|
|
|
|
}
|
|
|
|
|
2022-10-04 02:35:02 +00:00
|
|
|
func serveHtml(w http.ResponseWriter, r *http.Request, filePath string) error {
|
2022-09-16 18:45:33 +00:00
|
|
|
fileName := filepath.Base(filePath)
|
2022-09-08 20:57:52 +00:00
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
|
|
|
|
htmlBody := `<html lang="en">
|
2022-09-08 15:12:06 +00:00
|
|
|
<head>
|
2022-09-08 20:30:51 +00:00
|
|
|
<style>img{max-width:100%;max-height:97vh;height:auto;}</style>
|
|
|
|
<title>`
|
2022-09-08 20:57:52 +00:00
|
|
|
htmlBody += fileName
|
|
|
|
htmlBody += `</title>
|
2022-09-08 15:12:06 +00:00
|
|
|
</head>
|
2022-09-24 23:59:10 +00:00
|
|
|
<body>`
|
2022-10-20 01:37:12 +00:00
|
|
|
switch {
|
|
|
|
case Filter && Sort:
|
|
|
|
htmlBody += fmt.Sprintf(`<a href="/?include=%v&exclude=%v&sort=%v"><img src="`,
|
|
|
|
r.URL.Query().Get("include"),
|
2022-10-20 15:38:32 +00:00
|
|
|
r.URL.Query().Get("exclude"),
|
|
|
|
r.URL.Query().Get("sort"),
|
2022-10-20 01:37:12 +00:00
|
|
|
)
|
|
|
|
case Filter && !Sort:
|
|
|
|
htmlBody += fmt.Sprintf(`<a href="/?include=%v&exclude=%v"><img src="`,
|
|
|
|
r.URL.Query().Get("include"),
|
|
|
|
r.URL.Query().Get("exclude"),
|
|
|
|
)
|
|
|
|
case !Filter && Sort:
|
|
|
|
htmlBody += fmt.Sprintf(`<a href="/?sort=%v"><img src="`,
|
|
|
|
r.URL.Query().Get("sort"),
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
htmlBody += `<a href="/"><img src="`
|
|
|
|
}
|
2022-09-16 18:45:33 +00:00
|
|
|
htmlBody += PREFIX + filePath
|
2022-09-08 20:57:52 +00:00
|
|
|
htmlBody += `"></img></a>
|
2022-09-09 00:11:07 +00:00
|
|
|
</body>
|
|
|
|
</html>`
|
|
|
|
|
2022-09-16 18:45:33 +00:00
|
|
|
_, err := io.WriteString(w, htmlBody)
|
2022-09-08 20:57:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-08 15:12:06 +00:00
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
return nil
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 02:35:02 +00:00
|
|
|
func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) error {
|
2022-10-20 01:37:12 +00:00
|
|
|
strippedUrl, err := stripQueryParams(r.URL.Path)
|
2022-09-08 15:12:06 +00:00
|
|
|
if err != nil {
|
2022-09-08 20:57:52 +00:00
|
|
|
return err
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
prefixedFilePath, err := url.QueryUnescape(strippedUrl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-08 20:57:52 +00:00
|
|
|
}
|
2022-09-10 21:17:55 +00:00
|
|
|
|
2022-10-18 22:30:04 +00:00
|
|
|
filePath := filepath.Clean(strings.TrimPrefix(prefixedFilePath, PREFIX))
|
2022-09-08 20:57:52 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
if !pathIsValid(filePath, paths) {
|
2022-10-04 02:35:02 +00:00
|
|
|
http.NotFound(w, r)
|
2022-09-08 20:57:52 +00:00
|
|
|
}
|
2022-09-08 15:12:06 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
exists, err := fileExists(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-10 21:17:55 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
if !exists {
|
2022-10-04 02:35:02 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
|
|
|
|
return nil
|
2022-09-08 20:57:52 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 00:01:56 +00:00
|
|
|
var startTime time.Time
|
2022-09-10 23:03:04 +00:00
|
|
|
if Verbose {
|
2022-09-12 00:01:56 +00:00
|
|
|
startTime = time.Now()
|
2022-09-12 01:05:55 +00:00
|
|
|
fmt.Printf("%v Serving file: %v", startTime.Format(LOGDATE), filePath)
|
2022-09-10 23:03:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
buf, err := os.ReadFile(filePath)
|
2022-09-08 15:12:06 +00:00
|
|
|
if err != nil {
|
2022-09-08 20:57:52 +00:00
|
|
|
return err
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
2022-09-08 20:57:52 +00:00
|
|
|
|
|
|
|
w.Write(buf)
|
|
|
|
|
2022-09-10 21:17:55 +00:00
|
|
|
if Verbose {
|
2022-09-22 02:24:05 +00:00
|
|
|
fmt.Printf(" (Finished in %v)\n", time.Since(startTime).Round(time.Microsecond))
|
2022-09-10 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
return nil
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
func serveStaticFileHandler(paths []string) appHandler {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-10-04 02:35:02 +00:00
|
|
|
err := serveStaticFile(w, r, paths)
|
2022-09-16 18:45:33 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-16 18:45:33 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
|
|
|
|
return nil
|
2022-09-16 18:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
func serveHtmlHandler(paths []string) appHandler {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-10-20 01:37:12 +00:00
|
|
|
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
|
2022-09-26 17:31:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-24 23:59:10 +00:00
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
filters := Filters{}
|
2022-10-20 01:39:57 +00:00
|
|
|
if Filter {
|
|
|
|
filters.Includes = splitQueryParams(r.URL.Query().Get("include"))
|
|
|
|
filters.Excludes = splitQueryParams(r.URL.Query().Get("exclude"))
|
2022-10-20 15:38:32 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println("Filters disabled")
|
2022-10-20 01:39:57 +00:00
|
|
|
}
|
2022-10-20 00:41:42 +00:00
|
|
|
|
2022-10-20 01:39:57 +00:00
|
|
|
sortOrder := ""
|
|
|
|
if Sort {
|
|
|
|
sortOrder = r.URL.Query().Get("sort")
|
|
|
|
}
|
2022-09-18 18:07:46 +00:00
|
|
|
|
|
|
|
switch {
|
2022-10-20 01:37:12 +00:00
|
|
|
case r.URL.Path == "/" && sortOrder == "asc" && refererUri != "":
|
2022-09-18 18:17:29 +00:00
|
|
|
query, err := url.QueryUnescape(refererUri)
|
2022-09-18 18:07:46 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-18 18:07:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
path, err := splitPath(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath, err := getNextFile(path)
|
2022-09-18 18:07:46 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-17 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if filePath == "" {
|
2022-10-20 01:37:12 +00:00
|
|
|
filePath, err = pickFile(paths, &filters, sortOrder)
|
2022-09-24 23:59:10 +00:00
|
|
|
switch {
|
|
|
|
case err != nil && err == ErrNoImagesFound:
|
|
|
|
http.NotFound(w, r)
|
2022-10-04 02:35:02 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
return nil
|
2022-09-24 23:59:10 +00:00
|
|
|
case err != nil:
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-17 16:07:37 +00:00
|
|
|
}
|
2022-09-18 18:07:46 +00:00
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
path, err := splitPath(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath, err = getFirstFile(path)
|
2022-09-18 18:07:46 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-18 18:07:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 01:37:12 +00:00
|
|
|
newUrl := fmt.Sprintf("%v%v%v",
|
|
|
|
r.URL.Host,
|
|
|
|
filePath,
|
|
|
|
generateQueryParams(&filters, sortOrder),
|
|
|
|
)
|
2022-10-20 15:38:32 +00:00
|
|
|
http.Redirect(w, r, newUrl, http.StatusTemporaryRedirect)
|
2022-10-20 01:37:12 +00:00
|
|
|
case r.URL.Path == "/" && sortOrder == "asc" && refererUri == "":
|
|
|
|
filePath, err := pickFile(paths, &filters, sortOrder)
|
2022-09-26 17:31:45 +00:00
|
|
|
if err != nil && err == ErrNoImagesFound {
|
2022-09-24 23:59:10 +00:00
|
|
|
http.NotFound(w, r)
|
2022-10-04 02:35:02 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
2022-09-18 18:07:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
path, err := splitPath(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath, err = getFirstFile(path)
|
2022-09-18 18:07:46 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-18 18:07:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 01:37:12 +00:00
|
|
|
newUrl := fmt.Sprintf("%v%v%v",
|
|
|
|
r.URL.Host,
|
|
|
|
filePath,
|
|
|
|
generateQueryParams(&filters, sortOrder),
|
|
|
|
)
|
2022-10-20 15:38:32 +00:00
|
|
|
http.Redirect(w, r, newUrl, http.StatusTemporaryRedirect)
|
2022-10-20 01:37:12 +00:00
|
|
|
case r.URL.Path == "/" && sortOrder == "desc" && refererUri != "":
|
2022-10-18 18:42:32 +00:00
|
|
|
query, err := url.QueryUnescape(refererUri)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
path, err := splitPath(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath, err := getPreviousFile(path)
|
2022-10-18 18:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if filePath == "" {
|
2022-10-20 01:37:12 +00:00
|
|
|
filePath, err = pickFile(paths, &filters, sortOrder)
|
2022-10-18 18:42:32 +00:00
|
|
|
switch {
|
|
|
|
case err != nil && err == ErrNoImagesFound:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
path, err := splitPath(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath, err = getLastFile(path)
|
2022-10-18 18:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 01:37:12 +00:00
|
|
|
newUrl := fmt.Sprintf("%v%v%v",
|
|
|
|
r.URL.Host,
|
|
|
|
filePath,
|
|
|
|
generateQueryParams(&filters, sortOrder),
|
|
|
|
)
|
2022-10-20 15:38:32 +00:00
|
|
|
http.Redirect(w, r, newUrl, http.StatusTemporaryRedirect)
|
2022-10-20 01:37:12 +00:00
|
|
|
case r.URL.Path == "/" && sortOrder == "desc" && refererUri == "":
|
|
|
|
filePath, err := pickFile(paths, &filters, sortOrder)
|
2022-10-18 18:42:32 +00:00
|
|
|
if err != nil && err == ErrNoImagesFound {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-18 21:40:13 +00:00
|
|
|
path, err := splitPath(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath, err = getLastFile(path)
|
2022-10-18 18:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-20 01:37:12 +00:00
|
|
|
newUrl := fmt.Sprintf("%v%v%v",
|
|
|
|
r.URL.Host,
|
|
|
|
filePath,
|
|
|
|
generateQueryParams(&filters, sortOrder),
|
|
|
|
)
|
2022-10-20 15:38:32 +00:00
|
|
|
http.Redirect(w, r, newUrl, http.StatusTemporaryRedirect)
|
2022-09-24 23:59:10 +00:00
|
|
|
case r.URL.Path == "/":
|
2022-10-20 01:37:12 +00:00
|
|
|
filePath, err := pickFile(paths, &filters, sortOrder)
|
2022-09-26 17:31:45 +00:00
|
|
|
if err != nil && err == ErrNoImagesFound {
|
2022-09-24 23:59:10 +00:00
|
|
|
http.NotFound(w, r)
|
2022-10-04 02:35:02 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
2022-09-16 18:45:33 +00:00
|
|
|
|
2022-10-20 01:37:12 +00:00
|
|
|
newUrl := fmt.Sprintf("%v%v%v",
|
|
|
|
r.URL.Host,
|
|
|
|
filePath,
|
|
|
|
generateQueryParams(&filters, sortOrder),
|
|
|
|
)
|
2022-10-20 15:38:32 +00:00
|
|
|
http.Redirect(w, r, newUrl, http.StatusTemporaryRedirect)
|
2022-09-18 18:07:46 +00:00
|
|
|
default:
|
2022-09-24 23:59:10 +00:00
|
|
|
filePath := r.URL.Path
|
2022-09-16 18:45:33 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
image, err := isImage(filePath)
|
2022-09-16 18:45:33 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-18 18:17:29 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
if !image {
|
2022-09-16 18:45:33 +00:00
|
|
|
http.NotFound(w, r)
|
2022-10-04 02:35:02 +00:00
|
|
|
|
|
|
|
return nil
|
2022-09-16 18:45:33 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 02:35:02 +00:00
|
|
|
err = serveHtml(w, r, filePath)
|
2022-09-18 18:17:29 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-16 18:45:33 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
|
|
|
|
return nil
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doNothing(http.ResponseWriter, *http.Request) {}
|
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
func ServePage(args []string) error {
|
2022-09-09 19:28:57 +00:00
|
|
|
paths, err := normalizePaths(args)
|
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-09 19:28:57 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
|
2022-09-16 18:45:33 +00:00
|
|
|
for _, i := range paths {
|
|
|
|
fmt.Println("Paths: " + i)
|
|
|
|
}
|
2022-09-18 18:32:58 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
http.Handle("/", serveHtmlHandler(paths))
|
2022-09-16 18:45:33 +00:00
|
|
|
http.Handle(PREFIX+"/", http.StripPrefix(PREFIX, serveStaticFileHandler(paths)))
|
2022-09-08 15:12:06 +00:00
|
|
|
http.HandleFunc("/favicon.ico", doNothing)
|
|
|
|
|
2022-10-18 21:54:01 +00:00
|
|
|
err = http.ListenAndServe(":"+strconv.FormatInt(int64(Port), 10), nil)
|
2022-09-16 19:45:54 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-16 19:45:54 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
|
|
|
|
return nil
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|