-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (81 loc) · 2.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2022 Jacques Supcik <jacques@supcik.net>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"archive/zip"
"bufio"
"errors"
"fmt"
"log"
"net/http"
"os"
"strings"
"sync"
"github.com/pkg/browser"
flag "github.com/spf13/pflag"
"golang.org/x/tools/godoc/vfs/httpfs"
"golang.org/x/tools/godoc/vfs/zipfs"
)
func main() {
flag.ErrHelp = errors.New("ZIPFILE is the archive containing the web site")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s ZIPFILE:\n", os.Args[0])
flag.PrintDefaults()
}
var prefix string
port := flag.Int("port", 8080, "Port Number")
flag.StringVar(&prefix, "prefix", "", "Path prefix")
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
os.Exit(0)
}
zipfile := flag.Arg(0)
z, err := zip.OpenReader(zipfile)
zfs := zipfs.New(z, "content")
if err != nil {
log.Fatal(err)
}
defer z.Close()
if prefix == "" {
f, err := zfs.Open("/.prefix")
if err == nil {
s := bufio.NewScanner(f)
if s.Scan() {
prefix = s.Text()
}
}
}
// Make sure that the prefix starts with a "/" and also ends
// with a "/"
if !strings.HasPrefix(prefix, "/") {
prefix = "/" + prefix
}
if !strings.HasSuffix(prefix, "/") {
prefix = prefix + "/"
}
wg := new(sync.WaitGroup)
wg.Add(1)
httpfs := httpfs.New(zfs)
http.Handle(prefix, http.StripPrefix(prefix, http.FileServer(httpfs)))
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
wg.Done()
}()
err = browser.OpenURL(fmt.Sprintf("http://localhost:%d/%s", *port, prefix))
if err != nil {
log.Fatal(err)
}
wg.Wait()
}