2021-03-26 00:17:52 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2021-07-22 06:55:19 +08:00
|
|
|
"context"
|
2021-12-07 04:31:08 +08:00
|
|
|
"fmt"
|
2021-07-22 06:55:19 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
2021-12-07 04:31:08 +08:00
|
|
|
"time"
|
2021-10-09 03:07:12 +08:00
|
|
|
|
2021-07-22 06:55:19 +08:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
2021-12-07 04:31:08 +08:00
|
|
|
"github.com/gravitl/netmaker/logger"
|
2021-07-22 06:55:19 +08:00
|
|
|
"github.com/gravitl/netmaker/servercfg"
|
2021-03-26 00:17:52 +08:00
|
|
|
)
|
|
|
|
|
2022-01-26 00:00:56 +08:00
|
|
|
// HttpHandlers - handler functions for REST interactions
|
2021-12-09 05:52:32 +08:00
|
|
|
var HttpHandlers = []interface{}{
|
|
|
|
nodeHandlers,
|
|
|
|
userHandlers,
|
|
|
|
networkHandlers,
|
|
|
|
dnsHandlers,
|
|
|
|
fileHandlers,
|
|
|
|
serverHandlers,
|
|
|
|
extClientHandlers,
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// HandleRESTRequests - handles the rest requests
|
2021-03-26 00:17:52 +08:00
|
|
|
func HandleRESTRequests(wg *sync.WaitGroup) {
|
2021-07-22 06:55:19 +08:00
|
|
|
defer wg.Done()
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-07-22 06:55:19 +08:00
|
|
|
r := mux.NewRouter()
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-07-22 06:55:19 +08:00
|
|
|
// Currently allowed dev origin is all. Should change in prod
|
|
|
|
// should consider analyzing the allowed methods further
|
|
|
|
headersOk := handlers.AllowedHeaders([]string{"Access-Control-Allow-Origin", "X-Requested-With", "Content-Type", "authorization"})
|
|
|
|
originsOk := handlers.AllowedOrigins([]string{servercfg.GetAllowedOrigin()})
|
|
|
|
methodsOk := handlers.AllowedMethods([]string{"GET", "PUT", "POST", "DELETE"})
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-12-09 05:52:32 +08:00
|
|
|
for _, handler := range HttpHandlers {
|
|
|
|
handler.(func(*mux.Router))(r)
|
|
|
|
}
|
2021-10-09 03:07:12 +08:00
|
|
|
|
2021-07-22 06:55:19 +08:00
|
|
|
port := servercfg.GetAPIPort()
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-07-22 06:55:19 +08:00
|
|
|
srv := &http.Server{Addr: ":" + port, Handler: handlers.CORS(originsOk, headersOk, methodsOk)(r)}
|
|
|
|
go func() {
|
2021-03-26 00:17:52 +08:00
|
|
|
err := srv.ListenAndServe()
|
|
|
|
if err != nil {
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(0, err.Error())
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
2021-07-22 06:55:19 +08:00
|
|
|
}()
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(0, "REST Server successfully started on port ", port, " (REST)")
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-07-22 06:55:19 +08:00
|
|
|
// Relay os.Interrupt to our channel (os.Interrupt = CTRL+C)
|
|
|
|
// Ignore other incoming signals
|
2022-01-06 23:57:05 +08:00
|
|
|
ctx, stop := signal.NotifyContext(context.TODO(), os.Interrupt)
|
|
|
|
defer stop()
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-07-22 06:55:19 +08:00
|
|
|
// Block main routine until a signal is received
|
|
|
|
// As long as user doesn't press CTRL+C a message is not passed and our main routine keeps running
|
2022-01-06 23:57:05 +08:00
|
|
|
<-ctx.Done()
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-07-22 06:55:19 +08:00
|
|
|
// After receiving CTRL+C Properly stop the server
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(0, "Stopping the REST server...")
|
|
|
|
logger.Log(0, "REST Server closed.")
|
|
|
|
logger.DumpFile(fmt.Sprintf("data/netmaker.log.%s", time.Now().Format(logger.TimeFormatDay)))
|
2022-01-26 00:00:56 +08:00
|
|
|
srv.Shutdown(context.TODO())
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|