initial core commit
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/kristjank/lambda-iot/core/internal/handler"
|
||||
"github.com/kristjank/lambda-iot/core/internal/middleware"
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr := ":8080"
|
||||
|
||||
// Gin setup
|
||||
r := gin.New()
|
||||
r.Use(gin.Recovery())
|
||||
r.Use(middleware.GinLogger())
|
||||
|
||||
// Public routes
|
||||
r.GET("/health", handler.Health)
|
||||
r.GET("/hello", handler.Hello)
|
||||
r.POST("/login", handler.Login)
|
||||
|
||||
// Protected routes
|
||||
auth := r.Group("/")
|
||||
auth.Use(middleware.AuthMiddleware(getJWTSecret()))
|
||||
auth.GET("/protected", handler.Protected)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: r,
|
||||
}
|
||||
|
||||
go func() {
|
||||
log.Printf("starting server on %s", addr)
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("listen: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
|
||||
<-quit
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(ctx); err != nil {
|
||||
log.Fatalf("server forced to shutdown: %v", err)
|
||||
}
|
||||
log.Println("server exiting")
|
||||
}
|
||||
|
||||
func getJWTSecret() string {
|
||||
if s := os.Getenv("JWT_SECRET"); s != "" {
|
||||
return s
|
||||
}
|
||||
return "secret"
|
||||
}
|
||||
Reference in New Issue
Block a user