-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
134 lines (123 loc) · 3.32 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2023-2024 Buf Technologies, Inc.
//
// 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
//
// http://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 (
"bytes"
"context"
"flag"
"html/template"
"io/fs"
"log"
"mime"
"net/http"
"os"
"path/filepath"
"connectrpc.com/connect"
"connectrpc.com/vanguard"
testv1 "connectrpc.com/vanguard/internal/gen/vanguard/test/v1"
"connectrpc.com/vanguard/internal/gen/vanguard/test/v1/testv1connect"
"google.golang.org/genproto/googleapis/api/httpbody"
)
func main() {
flagset := flag.NewFlagSet("fileserver", flag.ExitOnError)
port := flagset.String("p", "8100", "port to serve on")
directory := flagset.String("d", ".", "the directory of static file to host")
if err := flagset.Parse(os.Args[1:]); err != nil {
log.Fatal(err)
}
// Create Connect handler.
serviceHandler := &ContentService{
FS: os.DirFS(*directory),
}
// And wrap it with Vanguard.
service := vanguard.NewService(testv1connect.NewContentServiceHandler(serviceHandler))
handler, err := vanguard.NewTranscoder([]*vanguard.Service{service})
if err != nil {
log.Fatal(err)
}
// Now handler also supports REST requests, translated to Connect
// using the HTTP annotations on the ContentService definition.
log.Printf("Serving %s on HTTP port: %s\n", *directory, *port)
log.Fatal(http.ListenAndServe(":"+*port, handler))
}
var indexHTMLTemplate = template.Must(template.New("http").Parse(`
<html>
<head>
<meta charset="UTF-8">
<title>{{.Title}}</title>
</head>
<body>
<pre>
{{- if ne .Title "."}}
<a href="/">..</a>
{{- end}}
{{- range $path, $name := .Files}}
<a href="/{{$path}}">{{$name}}</a>
{{- end}}
</pre>
</body>
</html>
`))
type ContentService struct {
testv1connect.UnimplementedContentServiceHandler
fs.FS
}
func (c *ContentService) Index(_ context.Context, req *connect.Request[testv1.IndexRequest]) (*connect.Response[httpbody.HttpBody], error) {
name := req.Msg.GetPage()
log.Printf("Index: %v", name)
if name == "/" || name == "" {
name = "."
}
file, err := c.Open(name)
if err != nil {
return nil, err
}
stat, err := file.Stat()
if err != nil {
return nil, err
}
contentType := "text/html"
var data []byte
if !stat.IsDir() {
contentType = mime.TypeByExtension(filepath.Ext(name))
data, err = fs.ReadFile(c.FS, name)
if err != nil {
return nil, err
}
} else {
tmplData := struct {
Title string
Files map[string]string
}{
Title: name,
Files: make(map[string]string),
}
entries, err := fs.ReadDir(c.FS, name)
if err != nil {
return nil, err
}
for _, entry := range entries {
tmplData.Files[filepath.Join(name, entry.Name())] = entry.Name()
}
var buf bytes.Buffer
if err := indexHTMLTemplate.Execute(&buf, tmplData); err != nil {
return nil, err
}
data = buf.Bytes()
}
return connect.NewResponse(&httpbody.HttpBody{
ContentType: contentType,
Data: data,
}), nil
}