aboutsummaryrefslogtreecommitdiff
path: root/route.go
blob: 26754ac852732d39aee0a87eb22ac96dbb3ff7f2 (plain)
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
package goxy

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
)

// Routes defines a set of routes including correspondent TLS certificates
type Routes map[string]Route

type Route struct {
	Host, Upstream string
	Cert, Key      []byte
}

func (r Route) String() string {
	return fmt.Sprintf("%v → %v", r.Host, r.Upstream)
}

func (r Routes) Save(fname string) error {
	fd, err := os.Create(fname)
	if err != nil {
		return err
	}
	defer fd.Close()
	return json.NewEncoder(fd).Encode(r)
}

func (r *Routes) Load(fname string) error {
	fd, err := os.Open(fname)
	if err != nil {
		return err
	}
	defer fd.Close()
	return json.NewDecoder(fd).Decode(r)
}

func (r Routes) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
	for _, v := range r {
		fmt.Fprintln(w, v)
	}
}