mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-12 16:14:37 +08:00
setup controller, routes skeleton for turn server
This commit is contained in:
parent
b8904d1f3c
commit
7f0c147e80
4 changed files with 186 additions and 0 deletions
73
turnserver/config/config.go
Normal file
73
turnserver/config/config.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/gravitl/netmaker/config"
|
||||
)
|
||||
|
||||
var (
|
||||
Version = "dev"
|
||||
)
|
||||
|
||||
// GetAllowedOrigin - get the allowed origin
|
||||
func GetAllowedOrigin() string {
|
||||
allowedorigin := "*"
|
||||
if os.Getenv("CORS_ALLOWED_ORIGIN") != "" {
|
||||
allowedorigin = os.Getenv("CORS_ALLOWED_ORIGIN")
|
||||
} else if config.Config.Server.AllowedOrigin != "" {
|
||||
allowedorigin = config.Config.Server.AllowedOrigin
|
||||
}
|
||||
return allowedorigin
|
||||
}
|
||||
|
||||
// GetAPIPort - gets the api port
|
||||
func GetAPIPort() string {
|
||||
apiport := "8086"
|
||||
if os.Getenv("API_PORT") != "" {
|
||||
apiport = os.Getenv("API_PORT")
|
||||
} else if config.Config.Server.APIPort != "" {
|
||||
apiport = config.Config.Server.APIPort
|
||||
}
|
||||
return apiport
|
||||
}
|
||||
|
||||
// SetVersion - set version of netmaker
|
||||
func SetVersion(v string) {
|
||||
if v != "" {
|
||||
Version = v
|
||||
}
|
||||
}
|
||||
|
||||
// GetVersion - version of netmaker
|
||||
func GetVersion() string {
|
||||
return Version
|
||||
}
|
||||
|
||||
// IsDebugMode - gets the debug mode for the server
|
||||
func IsDebugMode() bool {
|
||||
debugMode := false
|
||||
if os.Getenv("DEBUG_MODE") == "on" {
|
||||
debugMode = true
|
||||
}
|
||||
return debugMode
|
||||
}
|
||||
|
||||
// GetVerbosity - get logger verbose level
|
||||
func GetVerbosity() int32 {
|
||||
var verbosity = 0
|
||||
var err error
|
||||
if os.Getenv("VERBOSITY") != "" {
|
||||
verbosity, err = strconv.Atoi(os.Getenv("VERBOSITY"))
|
||||
if err != nil {
|
||||
verbosity = 0
|
||||
}
|
||||
} else if config.Config.Server.Verbosity != 0 {
|
||||
verbosity = int(config.Config.Server.Verbosity)
|
||||
}
|
||||
if verbosity < 0 || verbosity > 4 {
|
||||
verbosity = 0
|
||||
}
|
||||
return int32(verbosity)
|
||||
}
|
81
turnserver/src/controller/controller.go
Normal file
81
turnserver/src/controller/controller.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/handlers"
|
||||
|
||||
"github.com/gravitl/netmaker/logger"
|
||||
"github.com/gravitl/netmaker/turnserver/config"
|
||||
"github.com/gravitl/netmaker/turnserver/src/middleware"
|
||||
"github.com/gravitl/netmaker/turnserver/src/routes"
|
||||
)
|
||||
|
||||
// HandleRESTRequests - handles the rest requests
|
||||
func HandleRESTRequests(ctx context.Context, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
// Set GIN MODE (debug or release)
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
if config.GetVersion() == "dev" || config.IsDebugMode() {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
}
|
||||
// Register gin router with default configuration
|
||||
// comes with default middleware and recovery handlers.
|
||||
router := gin.Default()
|
||||
// Intialize all routes
|
||||
routes.Init(router)
|
||||
// Attach custom logger to gin to print incoming requests to stdout.
|
||||
router.Use(ginLogger())
|
||||
// Attach rate limiter to middleware
|
||||
router.Use(middleware.RateLimiter())
|
||||
// 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{config.GetAllowedOrigin()})
|
||||
methodsOk := handlers.AllowedMethods([]string{"GET", "PUT", "POST", "DELETE"})
|
||||
|
||||
// get server port from config
|
||||
port := config.GetAPIPort()
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: ":" + port,
|
||||
Handler: handlers.CORS(originsOk, headersOk, methodsOk)(router),
|
||||
}
|
||||
go func() {
|
||||
// service connections
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("listen: %s\n", err)
|
||||
}
|
||||
}()
|
||||
logger.Log(0, fmt.Sprintf("REST Server (Version: %s) successfully started on port (%s) ", config.GetVersion(), port))
|
||||
<-ctx.Done()
|
||||
log.Println("Shutdown Server ...")
|
||||
if err := srv.Shutdown(ctx); err != nil {
|
||||
log.Fatal("Server Shutdown:", err)
|
||||
}
|
||||
logger.Log(0, "Server exiting...")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func ginLogger() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
// Get the client IP address
|
||||
clientIP := c.ClientIP()
|
||||
|
||||
// Get the current time
|
||||
now := time.Now()
|
||||
// Log the request
|
||||
log.Printf("[%s] %s %s %s", now.Format(time.RFC3339), c.Request.Method, c.Request.URL.Path, clientIP)
|
||||
|
||||
// Proceed to the next handler
|
||||
c.Next()
|
||||
}
|
||||
}
|
25
turnserver/src/middleware/middleware.go
Normal file
25
turnserver/src/middleware/middleware.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gravitl/netmaker/logger"
|
||||
"github.com/ulule/limiter/v3"
|
||||
mgin "github.com/ulule/limiter/v3/drivers/middleware/gin"
|
||||
"github.com/ulule/limiter/v3/drivers/store/memory"
|
||||
)
|
||||
|
||||
// RateLimiter - middleware handler to enforce rate limiting on requests
|
||||
func RateLimiter() gin.HandlerFunc {
|
||||
|
||||
rate, err := limiter.NewRateFromFormatted("1000-H")
|
||||
if err != nil {
|
||||
logger.FatalLog(err.Error())
|
||||
}
|
||||
store := memory.NewStore()
|
||||
|
||||
// Then, create the limiter instance which takes the store and the rate as arguments.
|
||||
// Now, you can add this instance to gin middleware.
|
||||
instance := limiter.New(store, rate)
|
||||
return mgin.NewMiddleware(instance)
|
||||
|
||||
}
|
7
turnserver/src/routes/routes.go
Normal file
7
turnserver/src/routes/routes.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package routes
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func Init(r *gin.Engine) *gin.Engine {
|
||||
return r
|
||||
}
|
Loading…
Add table
Reference in a new issue