/
api.go
116 lines (93 loc) · 2.71 KB
/
api.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package api
import (
"context"
"fmt"
"net"
"net/http"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var ShutdownCh chan bool
var server *http.Server
func SetupRoutes(r *mux.Router) {
r.HandleFunc("/api/agent/{ip}", GetAgent).Methods("GET")
}
func init() {
viper.SetDefault("api.server.address", "127.0.0.1")
viper.SetDefault("api.server.port", 9000)
viper.SetDefault("api.server.ssl-key", "key.pem")
viper.SetDefault("api.server.ssl-cert", "cert.pem")
viper.SetDefault("api.server.https", false)
}
func Run() {
var listen string = fmt.Sprintf("%s:%d", viper.GetString("api.server.address"), viper.GetInt("api.server.port"))
if apiRunning() {
log.Info("api: already running on ", listen)
return
}
log.Info("api: not running on ", listen, "; starting")
ShutdownCh = make(chan bool)
r := mux.NewRouter()
r.Use(loggingMiddleware)
SetupRoutes(r)
headersOk := handlers.AllowedHeaders([]string{"X-Requested-With"})
originsOk := handlers.AllowedOrigins([]string{"*"})
//originsOk := handlers.AllowedOrigins([]string{os.Getenv("ORIGIN_ALLOWED")})
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS", "DELETE"})
server = &http.Server{
Addr: listen,
Handler: handlers.CORS(originsOk, headersOk, methodsOk)(r),
}
go func() {
if viper.GetBool("api.server.https") == true {
if err := server.ListenAndServeTLS(
viper.GetString("api.server.ssl-cert"),
viper.GetString("api.server.ssl-key"),
); err != http.ErrServerClosed {
log.Fatalf("api: %s", err)
}
} else {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("api: %s", err)
}
}
}()
}
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do stuff here
log.Println(r.RequestURI)
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r)
})
}
func apiRunning() bool {
var address string = fmt.Sprintf("%s:%d", viper.GetString("api.server.address"), viper.GetInt("api.server.port"))
conn, err := net.Dial("tcp", address)
if err != nil {
return false
}
conn.Close()
return true
}
func Shutdown() {
if server != nil {
log.Info("api: [shutdown] shutting down")
if err := server.Shutdown(context.TODO()); err != nil {
log.Panic("api: ", err)
}
server = nil
} else {
log.Info("api: [shutdown] server == nil in this process")
}
if ShutdownCh != nil {
log.Info("api: [shutdown] signaling shutdown channel")
ShutdownCh <- true
close(ShutdownCh)
ShutdownCh = nil
} else {
log.Info("api: [shutdown] ShutdownCh == nil in this process")
}
}