roulette/cmd/web.go

138 lines
2.4 KiB
Go
Raw Normal View History

2022-09-08 15:12:06 +00:00
/*
Copyright © 2022 Seednode <seednode@seedno.de>
*/
package cmd
import (
"errors"
2022-09-08 15:12:06 +00:00
"fmt"
"io"
"log"
"net/http"
2022-09-08 20:30:51 +00:00
"net/url"
2022-09-08 15:57:59 +00:00
"os"
2022-09-08 15:12:06 +00:00
"strconv"
"strings"
2022-09-08 15:12:06 +00:00
)
func generatePageHtml(w http.ResponseWriter, paths []string) error {
fileList, err := getFileList(paths)
if err != nil {
return err
}
fileName, filePath := pickFile(fileList)
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>`
htmlBody += fileName
htmlBody += `</title>
2022-09-08 15:12:06 +00:00
</head>
<body>
<a href="/"><img src="`
htmlBody += filePath
htmlBody += `"></img></a>
2022-09-08 15:12:06 +00:00
`
_, err = io.WriteString(w, htmlBody)
if err != nil {
return err
}
2022-09-08 15:12:06 +00:00
return nil
2022-09-08 15:12:06 +00:00
}
func statusNotFound(w http.ResponseWriter, filePath string) error {
fmt.Println("Client requested non-existent file " + filePath + ".")
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "txt/plain")
htmlBody := "File not found."
_, err := io.WriteString(w, htmlBody)
if err != nil {
return err
}
2022-09-08 15:12:06 +00:00
return nil
2022-09-08 15:12:06 +00:00
}
func serveStaticFile(w http.ResponseWriter, request string, paths []string) error {
filePath, err := url.QueryUnescape(request)
2022-09-08 15:12:06 +00:00
if err != nil {
return err
2022-09-08 15:12:06 +00:00
}
var matchesPrefix = false
for i := 0; i < len(paths); i++ {
if strings.HasPrefix(filePath, paths[i]) {
matchesPrefix = true
}
}
if matchesPrefix == false {
err := statusNotFound(w, filePath)
if err != nil {
return err
}
return nil
}
2022-09-08 15:12:06 +00:00
_, err = os.Stat(filePath)
if errors.Is(err, os.ErrNotExist) {
err := statusNotFound(w, filePath)
if err != nil {
return err
}
return nil
} else if !errors.Is(err, os.ErrNotExist) && err != nil {
return err
}
buf, err := os.ReadFile(filePath)
2022-09-08 15:12:06 +00:00
if err != nil {
return err
2022-09-08 15:12:06 +00:00
}
w.Write(buf)
return nil
2022-09-08 15:12:06 +00:00
}
2022-09-08 20:30:51 +00:00
func servePageHandler(paths []string) http.HandlerFunc {
2022-09-08 15:12:06 +00:00
return func(w http.ResponseWriter, r *http.Request) {
2022-09-08 20:30:51 +00:00
if r.RequestURI == "/" {
err := generatePageHtml(w, paths)
2022-09-08 20:30:51 +00:00
if err != nil {
log.Fatal(err)
}
} else {
err := serveStaticFile(w, r.RequestURI, paths)
2022-09-08 20:30:51 +00:00
if err != nil {
log.Fatal(err)
2022-09-08 20:30:51 +00:00
}
}
2022-09-08 15:12:06 +00:00
}
}
func doNothing(http.ResponseWriter, *http.Request) {}
2022-09-08 15:57:59 +00:00
func ServePage(args []string) {
2022-09-08 15:12:06 +00:00
defer HandleExit()
2022-09-08 20:30:51 +00:00
paths := normalizePaths(args)
http.HandleFunc("/", servePageHandler(paths))
2022-09-08 15:12:06 +00:00
http.HandleFunc("/favicon.ico", doNothing)
port := strconv.Itoa(Port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}