2026-03-16 22:21:17 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-03-18 09:39:02 +00:00
|
|
|
"forge.arimelody.space/ari/indir/log"
|
|
|
|
|
"forge.arimelody.space/ari/indir/web"
|
2026-03-16 22:21:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
if len(os.Args) < 2 { printHelp() }
|
|
|
|
|
|
|
|
|
|
host := "127.0.0.1"
|
|
|
|
|
port := 8080
|
|
|
|
|
root := "/"
|
|
|
|
|
|
|
|
|
|
filesDir := ""
|
|
|
|
|
i := 1
|
|
|
|
|
for {
|
|
|
|
|
if i >= len(os.Args) { break }
|
|
|
|
|
switch os.Args[i] {
|
|
|
|
|
case "-h":
|
|
|
|
|
fallthrough
|
|
|
|
|
case "--help":
|
|
|
|
|
printHelp()
|
|
|
|
|
|
|
|
|
|
case "--host":
|
|
|
|
|
if i + 1 >= len(os.Args) {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "fatal: --host argument cannot be empty\n")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
i++
|
|
|
|
|
host = os.Args[i]
|
2025-06-08 02:09:00 +01:00
|
|
|
|
2026-03-16 22:21:17 +01:00
|
|
|
case "--port":
|
|
|
|
|
if i + 1 >= len(os.Args) {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "fatal: --port argument cannot be empty\n")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
i++
|
|
|
|
|
var err error
|
|
|
|
|
port, err = strconv.Atoi(os.Args[i])
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "fatal: failed to parse port %s: %v\n", os.Args[i], err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2025-06-08 02:09:00 +01:00
|
|
|
|
2026-03-16 22:21:17 +01:00
|
|
|
case "--root":
|
|
|
|
|
if i + 1 >= len(os.Args) {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "fatal: --root argument cannot be empty\n")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
i++
|
|
|
|
|
root = os.Args[i]
|
|
|
|
|
if !strings.HasPrefix(root, "/") { root = "/" + root }
|
|
|
|
|
if !strings.HasSuffix(root, "/") { root += "/" }
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if len(filesDir) > 0 {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "unsupported argument: %s\n", os.Args[i])
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
filesDir = os.Args[i]
|
2026-03-16 20:41:16 +01:00
|
|
|
}
|
2026-03-16 22:21:17 +01:00
|
|
|
i++
|
|
|
|
|
}
|
2025-06-08 02:09:00 +01:00
|
|
|
|
2026-03-16 22:21:17 +01:00
|
|
|
if len(filesDir) == 0 {
|
|
|
|
|
filesDir = "."
|
|
|
|
|
}
|
2025-06-08 02:09:00 +01:00
|
|
|
|
2026-03-16 22:21:17 +01:00
|
|
|
ignoredFiles := []string{
|
|
|
|
|
".",
|
|
|
|
|
".DS_Store",
|
|
|
|
|
}
|
2025-06-08 02:09:00 +01:00
|
|
|
|
2026-03-16 22:21:17 +01:00
|
|
|
fmt.Printf("Now hosting \"%s\" at http://%s:%d", filesDir, host, port)
|
|
|
|
|
if root != "/" { fmt.Printf("%s", root) }
|
|
|
|
|
fmt.Println(".")
|
2025-06-08 02:09:00 +01:00
|
|
|
|
2026-03-18 09:39:02 +00:00
|
|
|
app := web.AppState{
|
|
|
|
|
Root: root,
|
|
|
|
|
FilesDir: filesDir,
|
|
|
|
|
IgnoredFiles: ignoredFiles,
|
|
|
|
|
}
|
2026-03-16 22:21:17 +01:00
|
|
|
|
2026-03-18 09:39:02 +00:00
|
|
|
http.ListenAndServe(
|
|
|
|
|
fmt.Sprintf("%s:%d", host, port),
|
|
|
|
|
log.HTTPLog(web.Handler(&app)),
|
|
|
|
|
)
|
2026-03-16 22:21:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func printHelp() {
|
|
|
|
|
fmt.Printf(
|
2026-03-18 09:39:02 +00:00
|
|
|
`indir [--host address] [--port port] [--root http_root] directory
|
2026-03-16 22:21:17 +01:00
|
|
|
|
|
|
|
|
--help shows this help message
|
|
|
|
|
--host address hosts on the specified address
|
|
|
|
|
--port port hosts on the specified port
|
|
|
|
|
--root http_root hosts on the specified subdirectory, i.e. `+"`/files/`\n",
|
|
|
|
|
)
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|