2023-05-18 20:04:55 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2024-11-03 17:01:48 -06:00
|
|
|
"goth.stack/api"
|
|
|
|
"goth.stack/lib"
|
|
|
|
"goth.stack/pages"
|
2023-05-18 20:04:55 -06:00
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2024-10-20 16:16:50 -06:00
|
|
|
echoSwagger "github.com/swaggo/echo-swagger"
|
2024-11-03 17:01:48 -06:00
|
|
|
_ "goth.stack/docs"
|
2023-05-18 20:04:55 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed public/*
|
|
|
|
var PublicFS embed.FS
|
|
|
|
|
2024-11-03 17:01:48 -06:00
|
|
|
// @title GOTH Stack API
|
2024-10-20 16:16:50 -06:00
|
|
|
// @version 1.0
|
2024-11-03 17:01:48 -06:00
|
|
|
// @description This is the API for GOTH Stack - Go + HTMX + Tailwind
|
2024-10-20 16:16:50 -06:00
|
|
|
// @host localhost:3000
|
|
|
|
// @BasePath /api
|
2023-05-18 20:04:55 -06:00
|
|
|
func main() {
|
|
|
|
// Load environment variables
|
|
|
|
godotenv.Load(".env")
|
|
|
|
|
|
|
|
// Initialize Echo router
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
// Middleware
|
|
|
|
e.Use(middleware.Logger())
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
e.Pre(middleware.RemoveTrailingSlash())
|
|
|
|
e.Use(middleware.RequestID())
|
|
|
|
e.Use(middleware.Secure())
|
|
|
|
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
|
|
|
|
Level: 5,
|
|
|
|
}))
|
|
|
|
e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(50)))
|
|
|
|
|
|
|
|
// Static server
|
|
|
|
fs := http.FS(PublicFS)
|
2024-02-21 01:46:30 -07:00
|
|
|
e.GET("/public/*", echo.WrapHandler(http.FileServer(fs)))
|
2023-05-18 20:04:55 -06:00
|
|
|
|
|
|
|
// Page routes
|
|
|
|
e.GET("/", pages.Home)
|
2024-08-21 12:59:22 -06:00
|
|
|
e.GET("/posts", pages.Posts)
|
|
|
|
e.GET("/posts/:post", pages.Post)
|
2024-02-22 13:12:47 -07:00
|
|
|
e.GET("/tools", pages.Tools)
|
|
|
|
e.GET("/tools/resize", pages.Resize)
|
2024-03-27 14:52:28 -06:00
|
|
|
e.GET("/tools/ssedemo", pages.SSEDemo)
|
2023-05-18 20:04:55 -06:00
|
|
|
|
|
|
|
// API Routes:
|
|
|
|
apiGroup := e.Group("/api")
|
2024-10-20 16:16:50 -06:00
|
|
|
// Swagger endpoint
|
|
|
|
apiGroup.GET("/swagger/*", echoSwagger.WrapHandler)
|
2023-05-18 20:04:55 -06:00
|
|
|
apiGroup.GET("/ping", api.Ping)
|
2024-04-16 00:09:09 -06:00
|
|
|
apiGroup.GET("/rss", api.RSSFeedHandler)
|
2023-05-18 20:04:55 -06:00
|
|
|
apiGroup.GET("/post/copy", api.PostCopy)
|
|
|
|
|
|
|
|
apiGroup.GET("/sse", func(c echo.Context) error {
|
2024-06-04 15:40:29 -06:00
|
|
|
return api.SSE(c)
|
2023-05-18 20:04:55 -06:00
|
|
|
})
|
|
|
|
|
2024-03-27 14:52:28 -06:00
|
|
|
apiGroup.POST("/tools/sendsse", func(c echo.Context) error {
|
2024-06-04 15:40:29 -06:00
|
|
|
return api.SSEDemoSend(c)
|
2023-05-18 20:04:55 -06:00
|
|
|
})
|
|
|
|
|
2024-03-27 14:52:28 -06:00
|
|
|
apiGroup.POST("/tools/resize", api.ResizeHandler)
|
2023-05-18 20:04:55 -06:00
|
|
|
|
|
|
|
// Parse command-line arguments for IP and port
|
|
|
|
ip := flag.String("ip", "", "IP address to bind the server to")
|
|
|
|
port := flag.String("port", "3000", "Port to bind the server to")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// Start server with HTTP/2 support
|
|
|
|
s := &http.Server{
|
|
|
|
Addr: fmt.Sprintf("%s:%s", *ip, *port),
|
|
|
|
Handler: e,
|
|
|
|
}
|
|
|
|
e.Logger.Fatal(e.StartServer(s))
|
2024-05-08 14:43:57 -06:00
|
|
|
lib.LogSuccess.Println("Server started on port", *port)
|
2023-05-18 20:04:55 -06:00
|
|
|
}
|