-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (56 loc) · 1.52 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
package main
import (
"io"
"log"
"net"
"net/http"
"sync"
"github.com/coreos/go-systemd/v22/activation"
)
type icsAdapter struct {
Name string
Url string
Description string
WriteCalendar func(w io.Writer) error
}
func (a icsAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-type", "text/calendar")
if err := a.WriteCalendar(w); err != nil {
log.Printf("Error writing calendar %s: %v", a.Name, err)
w.WriteHeader(http.StatusInternalServerError)
}
}
func (a *icsAdapter) Path() string {
return "/" + a.Name + ".ics"
}
var adapters []icsAdapter
func RegisterAdapter(name, url, description string, writeCalendar func(w io.Writer) error) {
adapters = append(adapters, icsAdapter{name, url, description, writeCalendar})
}
func main() {
for _, adapter := range adapters {
http.Handle(adapter.Path(), adapter)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-type", "text/html")
if err := writeIndex(w); err != nil {
log.Printf("Error writing index: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
})
listeners, err := activation.Listeners()
if err == nil && len(listeners) >= 1 {
wg := new(sync.WaitGroup)
wg.Add(len(listeners))
for _, l := range listeners {
go func(listener net.Listener) {
log.Println("Listening on systemd activated socket ...")
log.Fatal(http.Serve(listener, nil))
wg.Done()
}(l)
}
wg.Wait()
} else {
log.Fatal(http.ListenAndServe(":8083", nil))
}
}