S SmartDocs
Serie: Go go 60 righe · Aggiornato 2026-04-18

main.go

Go/Baasid/cmd/server/main.go

// Package main bootstraps the Baasid HTTP API server.
//
//	@title						Baasid Backend API
//	@version					1.1
//	@description				用戶登入成功後,Server 產生 JWT;之後商品 CRUD 請求皆須於 **Header** 帶入 `Authorization: Bearer <token>`。
//	@termsOfService				http://swagger.io/terms/
//
//	@contact.name				API Support
//	@contact.url				https://github.com/example/baasid
//	@contact.email				support@example.com
//
//	@license.name				MIT
//	@license.url				https://opensource.org/licenses/MIT
//
//	@host						localhost:8080
//	@BasePath					/
//
//	@securityDefinitions.apikey	BearerAuth
//	@in							header
//	@name						Authorization
//	@description				填入 `Bearer ` 後接 JWT,例如:`Bearer eyJhbGciOiJIUzI1NiIs...`
package main

import (
	"log"

	"baasid/internal/config"
	"baasid/internal/database"
	"baasid/internal/router"

	// Generated OpenAPI docs (run `make swagger` or `swag init` after changing annotations).
	_ "baasid/docs"

	"github.com/joho/godotenv"
)

func main() {
	// Load key=value pairs from a local .env file into the process environment.
	// This call is intentionally silent when .env is missing so production
	// can rely purely on real environment variables.
	if err := godotenv.Load(); err != nil {
		log.Printf("main: no .env loaded (%v) — using OS environment only", err)
	}

	cfg, err := config.Load()
	if err != nil {
		log.Fatalf("config: %v", err)
	}

	db, err := database.Connect(cfg.DatabaseURL)
	if err != nil {
		log.Fatalf("database: %v", err)
	}

	e := router.NewEcho(cfg, db)
	log.Printf("HTTP listening on %s (Swagger: http://127.0.0.1%s/swagger/index.html)\n", cfg.ServerAddr, cfg.ServerAddr)
	if err := e.Start(cfg.ServerAddr); err != nil {
		log.Fatalf("server: %v", err)
	}
}

Articoli correlati