This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
forked from hkjn/blockpress.me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
78 lines (69 loc) · 1.86 KB
/
server.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
// Fileserver is a minimal service for to serving contents from the file system over HTTP.
package main
import (
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"golang.org/x/crypto/acme/autocert"
)
var subdomains = []string{
"anton", "dana", "hkjn",
}
type multiHostHandler struct {
fileHandlers map[string]http.Handler
}
func (mhh multiHostHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Got request for https://%s%s", r.Host, r.URL.Path)
inner, ok := mhh.fileHandlers[r.Host]
if !ok {
log.Printf("No domain %s, serving 404.\n", r.Host)
http.NotFound(w, r)
return
}
inner.ServeHTTP(w, r)
}
func main() {
hostnames := []string{
"blockpress.me",
}
fh := map[string]http.Handler{
"blockpress.me": http.FileServer(http.Dir("/var/www/root")),
}
for _, name := range subdomains {
sd := fmt.Sprintf("%s.blockpress.me", name)
fp := fmt.Sprintf("/var/www/%s", name)
log.Printf("Serving %q from file path %q..\n", sd, fp)
fh[sd] = http.FileServer(http.Dir(fp))
hostnames = append(hostnames, sd)
}
http.Handle("/", multiHostHandler{fh})
addr := os.Getenv("BLOCKPRESS_ADDR")
if addr == "" {
addr = ":8080"
}
s := &http.Server{
Addr: addr,
}
if addr == ":443" {
fmt.Printf("Serving TLS as %q..\n", hostnames)
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache("/etc/secrets/acme/"),
HostPolicy: autocert.HostWhitelist(hostnames...),
}
// Configure extra tcp/80 server for http-01 challenge:
// https://godoc.org/golang.org/x/crypto/acme/autocert#Manager.HTTPHandler
httpServer := &http.Server{
Handler: m.HTTPHandler(nil),
Addr: ":80",
}
go httpServer.ListenAndServe()
s.TLSConfig = &tls.Config{GetCertificate: m.GetCertificate}
log.Fatal(s.ListenAndServeTLS("", ""))
} else {
fmt.Printf("Serving plaintext HTTP on %s..\n", addr)
log.Fatal(s.ListenAndServe())
}
}