-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
73 lines (60 loc) · 1.46 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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"gopkg.in/yaml.v2"
)
type Config struct {
Server Server
Redirects map[string]Redirect
}
type Server struct {
Address string `yaml:"address"`
Port int `yaml:"port"`
}
type Redirect struct {
To string `yaml:"to"`
With int `yaml:"with"`
RetainPath bool `yaml:"retain_path"`
}
func main() {
// Read in config file
c, err := ioutil.ReadFile("config.yaml")
if err != nil {
log.Fatal("Error reading config: ", err)
}
// Unmarshal YAML
config := Config{}
err = yaml.Unmarshal(c, &config)
if err != nil {
log.Fatal("Error marshalling YAML: ", err)
}
// Setup handler for redirects
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, found := config.Redirects[r.Host]
if !found {
log.Printf("Client %s %s -> Not Found (404)", r.RemoteAddr, r.Host)
http.NotFound(w, r)
return
}
redirect := config.Redirects[r.Host]
To := redirect.To
if redirect.RetainPath {
To += r.URL.RequestURI()
}
With := redirect.With
log.Printf("Client: %s %s -> %s (%d)\n", r.RemoteAddr, r.Host, To, With)
http.Redirect(w, r, To, With)
return
})
// Start HTTP Server
ServerAddress := config.Server.Address
ServerPort := config.Server.Port
log.Printf("Listening on %v:%v\n", ServerAddress, ServerPort)
err = http.ListenAndServe(fmt.Sprintf("%v:%v", ServerAddress, ServerPort), nil)
if err != nil {
log.Fatal("Error starting server: ", err)
}
}