42 lines
904 B
Go
42 lines
904 B
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
_ "goapi/docs"
|
|
|
|
"github.com/joho/godotenv"
|
|
httpSwagger "github.com/swaggo/http-swagger"
|
|
)
|
|
|
|
// @title GoApi
|
|
// @version 1.0
|
|
// @description A basic API with support for Swagger and Middleware
|
|
// @host localhost:8080
|
|
// @BasePath /api/v1
|
|
// @securityDefinitions.apikey ApiKeyAuth
|
|
// @in header
|
|
// @name X-API-Key
|
|
func main() {
|
|
// Load .env file
|
|
if err := godotenv.Load(); err != nil {
|
|
log.Println("No .env file found, using environment variables")
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
// API routes
|
|
mux.HandleFunc("/api/v1/health", GetHealth)
|
|
mux.HandleFunc("/api/v1/users", GetUsers)
|
|
|
|
// Swagger UI
|
|
mux.Handle("/swagger/", httpSwagger.WrapHandler)
|
|
|
|
// Apply middleware
|
|
handler := LoggingMiddleware(AuthMiddleware(mux))
|
|
|
|
log.Println("Server starting on :8080")
|
|
log.Fatal(http.ListenAndServe(":8080", handler))
|
|
}
|