You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.5 KiB
73 lines
1.5 KiB
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/apex/gateway"
|
|
)
|
|
|
|
var (
|
|
port = flag.Int("port", -1, "specify a port")
|
|
)
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
listen := gateway.ListenAndServe
|
|
|
|
addr := "n/a"
|
|
if *port != -1 {
|
|
addr = fmt.Sprintf(":%d", *port)
|
|
listen = http.ListenAndServe
|
|
}
|
|
|
|
listen(addr, http.HandlerFunc(serve))
|
|
}
|
|
|
|
func serve(w http.ResponseWriter, r *http.Request) {
|
|
parts := strings.Split(r.RequestURI, "/")[1:]
|
|
|
|
w.Header().Set("content-type", "text/html")
|
|
|
|
if len(parts) != 1 {
|
|
w.Write(notfound())
|
|
} else {
|
|
w.Write(module(parts[0]))
|
|
}
|
|
}
|
|
|
|
func notfound() []byte {
|
|
return []byte(`<!doctype html>
|
|
<html lang="en-US">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width" />
|
|
<title>zestack.dev</title>
|
|
</head>
|
|
<body>
|
|
<a href="https://slim.zestack.dev">https://slim.zestack.dev</a>
|
|
</body>
|
|
</html>`)
|
|
}
|
|
|
|
func module(name string) []byte {
|
|
bts := []byte(`<!doctype html>
|
|
<html lang="en-US">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width" />
|
|
<meta name="go-import" content="zestack.dev/{} git https://github.com/zestack/{}">
|
|
<meta name="go-source" content="zestack.dev/{} https://github.com/zestack/{} https://github.com/zestack/{}/tree/master{/dir} https://github.com/zestack/{}/blob/master{/dir}/{file}#L{line}">
|
|
<title>{} - zestack.dev</title>
|
|
</head>
|
|
<body>
|
|
<a href="https://slim.zestack.dev">https://slim.zestack.dev</a>
|
|
</body>
|
|
</html>`)
|
|
|
|
return bytes.ReplaceAll(bts, []byte("{}"), []byte(name))
|
|
}
|
|
|