-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
116 lines (100 loc) · 3.25 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
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 main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
gw "github.com/chirpstack/chirpstack-rest-api/api"
"github.com/chirpstack/chirpstack-rest-api/ui"
)
var (
server = flag.String("server", envString("SERVER", "localhost:8080"), "ChirpStack API endpoint")
bind = flag.String("bind", envString("BIND", "0.0.0.0:8090"), "REST API server bind")
insec = flag.Bool("insecure", envBool("INSECURE", false), "Use insecure (non-TLS) connection to server")
cors = flag.String("cors", envString("CORS", "0.0.0.0"), "Set AllowedOrigins Header for CORS")
)
func envString(key string, defVal string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return defVal
}
func envBool(key string, defVal bool) bool {
if _, ok := os.LookupEnv(key); ok {
return true
}
return defVal
}
func run() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
r := mux.NewRouter()
gatewayHandler, err := getGatewayHandler(ctx)
if err != nil {
return err
}
r.PathPrefix("/api/").Handler(gatewayHandler)
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data, err := ui.FS.ReadFile("index.html")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(data)
})
r.PathPrefix("/").Handler(http.FileServer(http.FS(ui.FS)))
corsObj := handlers.AllowedOrigins([]string{*cors})
corsMethods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"})
corsHeaders := handlers.AllowedHeaders([]string{"Content-Type", "Authorization"})
return http.ListenAndServe(*bind, handlers.CORS(corsObj, corsMethods, corsHeaders)(r))
}
func getGatewayHandler(ctx context.Context) (http.Handler, error) {
mux := runtime.NewServeMux()
opts := []grpc.DialOption{}
if *insec {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
}
if err := gw.RegisterApplicationServiceHandlerFromEndpoint(ctx, mux, *server, opts); err != nil {
return nil, err
}
if err := gw.RegisterDeviceServiceHandlerFromEndpoint(ctx, mux, *server, opts); err != nil {
return nil, err
}
if err := gw.RegisterDeviceProfileServiceHandlerFromEndpoint(ctx, mux, *server, opts); err != nil {
return nil, err
}
if err := gw.RegisterDeviceProfileTemplateServiceHandlerFromEndpoint(ctx, mux, *server, opts); err != nil {
return nil, err
}
if err := gw.RegisterGatewayServiceHandlerFromEndpoint(ctx, mux, *server, opts); err != nil {
return nil, err
}
if err := gw.RegisterMulticastGroupServiceHandlerFromEndpoint(ctx, mux, *server, opts); err != nil {
return nil, err
}
if err := gw.RegisterTenantServiceHandlerFromEndpoint(ctx, mux, *server, opts); err != nil {
return nil, err
}
if err := gw.RegisterUserServiceHandlerFromEndpoint(ctx, mux, *server, opts); err != nil {
return nil, err
}
return mux, nil
}
func main() {
fmt.Println("Starting ChirpStack REST API server")
flag.Parse()
if err := run(); err != nil {
panic(err)
}
}