-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdls.go
57 lines (50 loc) · 2.14 KB
/
dls.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
package main
import (
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
authv1 "github.com/sjdaws/dls/api/auth"
leasingv1 "github.com/sjdaws/dls/api/leasing"
"github.com/sjdaws/dls/client/health"
"github.com/sjdaws/dls/client/token"
"github.com/sjdaws/dls/internal/global"
"github.com/sjdaws/dls/internal/web"
)
func main() {
auth := authv1.New()
leasing := leasingv1.New()
router := mux.NewRouter()
router.HandleFunc("/auth/v1/code", auth.CodeChallenge).Methods("POST")
router.HandleFunc("/auth/v1/origin", auth.FindOrCreateOrigin).Methods("POST")
router.HandleFunc("/auth/v1/origin/update", auth.FindOrCreateOrigin).Methods("POST")
router.HandleFunc("/auth/v1/token", auth.TokenExchange).Methods("POST")
router.HandleFunc("/health", health.GetHealth).Methods("GET")
router.HandleFunc("/leasing/v1/lease/{id}", leasing.DeleteLease).Methods("DELETE")
router.HandleFunc("/leasing/v1/lease/{id}", leasing.UpdateLease).Methods("PUT")
router.HandleFunc("/leasing/v1/lessor", leasing.CreateOriginLease).Methods("POST")
router.HandleFunc("/leasing/v1/lessor/leases", leasing.DeleteOriginLeases).Methods("DELETE")
router.HandleFunc("/leasing/v1/lessor/leases", leasing.GetOriginLeases).Methods("GET")
router.HandleFunc("/token/download", token.Download).Methods("GET")
router.MethodNotAllowedHandler = http.HandlerFunc(invalidRoute)
router.NotFoundHandler = http.HandlerFunc(invalidRoute)
http.Handle("/", router)
errs := make(chan error, 1)
go serveHTTP(router, errs)
log.Fatal(<-errs)
}
func invalidRoute(response http.ResponseWriter, request *http.Request) {
web.Error(request, response, "route requested but not found", errors.New("invalid route"), nil)
}
func serveHTTP(router *mux.Router, errs chan<- error) {
server := &http.Server{
Addr: fmt.Sprintf(":%d", global.ContainerPort),
Handler: router,
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
}
log.Printf("Listening for HTTP connections on port %d", global.ContainerPort)
errs <- server.ListenAndServe()
}