-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (68 loc) · 1.88 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
74
75
76
77
78
79
80
81
82
83
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"cloud.google.com/go/datastore"
"github.com/gorilla/mux"
"github.com/oklog/run"
)
func main() {
router := mux.NewRouter()
// API endpoint
api := router.PathPrefix("/v1").Subrouter()
// Authority Management
api.HandleFunc("/authorities", NotImplemented).Methods(http.MethodGet)
api.HandleFunc("/authorities/{id}", NotImplemented).Methods(http.MethodGet)
api.HandleFunc("/authorities", NotImplemented).Methods(http.MethodPost)
api.HandleFunc("/authorities/{id}", NotImplemented).Methods(http.MethodDelete)
// Certificate Management
api.HandleFunc("/authorities/{id}/", NotImplemented).Methods(http.MethodGet)
api.HandleFunc("/authorities/{id}/sign", NotImplemented).Methods(http.MethodPost)
api.HandleFunc("/certificate/{serial}/revoke", NotImplemented).Methods(http.MethodPost)
// Determine port for HTTP service.
port := os.Getenv("PORT")
if _, err := strconv.ParseInt(port, 10, 64); err != nil {
port = "8080"
}
srv := http.Server{
Addr: ":" + port,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
MaxHeaderBytes: 1 << 20,
Handler: router,
}
var g run.Group
ctx, cancel := context.WithCancel(context.Background())
// HTTP server
{
g.Add(func() error { return srv.ListenAndServe() }, func(error) { srv.Shutdown(ctx) })
}
// Signal Handler
{
g.Add(
func() error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
select {
case <-ctx.Done():
return nil
case <-c:
cancel()
log.Printf("Got to go.")
return nil
}
}, func(err error) {})
}
_ = g.Run()
}
func Datastore(ctx context.Context) (*datastore.Client, error) {
return datastore.NewClient(ctx, os.Getenv("GOOGLE_CLOUD_PROJECT"))
}
func NotImplemented(w http.ResponseWriter, r *http.Request) {
}