/
server.go
143 lines (121 loc) · 3.57 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
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
135
136
137
138
139
140
141
142
143
package main
import (
"crypto/tls"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"path"
"github.com/gorilla/mux"
"github.com/jprobinson/go-utils/utils"
"github.com/jprobinson/go-utils/web"
"golang.org/x/crypto/acme"
"golang.org/x/crypto/acme/autocert"
)
const (
serverLog = "/var/log/goserver/server.log"
accessLog = "/var/log/goserver/access.log"
)
func main() {
router := mux.NewRouter()
// add subway redirects to server
subway(router, "subway.jprbnsn.com")
subway(router, "wheresthel.com")
subway(router, "www.wheresthel.com")
// add the countdown
static(router, "countdown.jprbnsn.com", "/opt/jp/www/thecountdown")
// add colin
static(router, "colinjhiggins.com", "/opt/jp/www/colinjhiggins")
static(router, "www.colinjhiggins.com", "/opt/jp/www/colinjhiggins")
// add personal site
static(router, "jprbnsn.com", "/opt/jp/www/jprbnsn")
static(router, "www.jprbnsn.com", "/opt/jp/www/jprbnsn")
// join xword demo
joinGame(router)
// redirect to new newshound
router.Host("newshound.jprbnsn.com").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
target := "https://newshound.email" + r.URL.Path
if len(r.URL.RawQuery) > 0 {
target += "?" + r.URL.RawQuery
}
http.Redirect(w, r, target, http.StatusMovedPermanently)
})
handler := web.AccessLogHandler(accessLog, router)
logSetup := utils.NewDefaultLogSetup(serverLog)
logSetup.SetupLogging()
go utils.ListenForLogSignal(logSetup)
// https
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(
"jprbnsn.com", "www.jprbnsn.com",
"newshound.jprbnsn.com",
"colinjhiggins.com", "www.colinjhiggins.com",
"countdown.jprbnsn.com",
"wheresthel.com", "www.wheresthel.com",
"subway.jprbnsn.com",
"join.jprbnsn.com",
),
Cache: autocert.DirCache("certs"),
}
server := &http.Server{
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
NextProtos: []string{acme.ALPNProto},
},
Handler: handler,
Addr: ":https",
}
go func() {
log.Println("starting https...")
log.Fatal(server.ListenAndServeTLS("", ""))
}()
// http
log.Println("starting http...")
log.Fatal(http.ListenAndServe(":http", http.HandlerFunc(https)))
}
func joinGame(router *mux.Router) {
router.Host("join.jprbnsn.com").Handler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
code := path.Base(r.URL.Path)
http.Redirect(w, r, "https://www.nytimes.com/svc/crosswords/v6/multiplayer/join/"+code,
http.StatusMovedPermanently)
}),
)
}
func subway(router *mux.Router, host string) {
router.Host(host).Handler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "http://wheresthetrain.nyc", http.StatusMovedPermanently)
}),
)
}
func static(router *mux.Router, host, dir string) {
srouter := router.Host(host).Subrouter()
srouter.PathPrefix("/").Handler(http.FileServer(http.Dir(dir)))
}
func https(w http.ResponseWriter, r *http.Request) {
target := "https://" + r.Host + r.URL.Path
if len(r.URL.RawQuery) > 0 {
target += "?" + r.URL.RawQuery
}
http.Redirect(w, r, target, http.StatusMovedPermanently)
}
type config struct {
DBURL string `json:"db-url"`
DBUser string `json:"db-user"`
DBPassword string `json:"db-pw"`
}
func NewConfig(filename string) *config {
config := config{}
readBytes, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("Cannot read config file: %s %s", filename, err)
}
err = json.Unmarshal(readBytes, &config)
if err != nil {
log.Fatalf("Cannot parse JSON in config file: %s %s", filename, err)
}
return &config
}