-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
99 lines (95 loc) · 2.96 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/joho/godotenv"
"github.com/mvrilo/go-redoc"
"github.com/onlytunesradio/go-api-template/src/api"
config "github.com/onlytunesradio/go-api-template/src/db"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func main() {
// Redoc Environment
// ================
doc := redoc.Redoc{
DocsPath: "/docs",
// Change SpecPath && SpecFile to ./static/swagger.json when developing locally!
SpecPath: "/srv/static/swagger.json",
SpecFile: "/srv/static/swagger.json",
Title: "OnlyTunes API Template",
Description: "API Documentation for OnlyTunes API Template",
}
// =================
// Declaring Environment variables
// =================
var DBHost, DBUser, DBPass, DBName, DBPort string
err := godotenv.Load(".env")
if err != nil {
log.Printf("Error loading .env file, Using container variables: ERR: %v", err)
}
DBHost = os.Getenv("DB_HOST")
DBUser = os.Getenv("DB_USER")
DBPass = os.Getenv("DB_PASS")
DBName = os.Getenv("DB_NAME")
DBPort = os.Getenv("DB_PORT")
// =================
// Declaring Database Connection
// =================
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", DBHost, DBPort, DBUser, DBPass, DBName)
config.DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
// Uncomment the line below to disable the logging Gorm does by default
//Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
log.Printf("Error connecting to the database: ERR: %v", err)
}
// =================
// Initialize Router and WebServer
// =================
r := chi.NewRouter()
// Uncomment these during development / debugging
//r.Use(middleware.Logger)
//r.Use(middleware.RealIP)
//r.Use(middleware.RequestID)
r.Use(middleware.Recoverer)
r.Use(middleware.CleanPath)
// =================
// Set Custom status messages for 404 && 405
// =================
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte("404 - Page not found - Check the API Docs for more info"))
})
r.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(405)
w.Write([]byte("405 - Method not allowed - Check the API Docs for more info"))
})
// =================
// Initialize API Middleware
// =================
r.Use(middleware.Heartbeat("/ping"))
r.Use(middleware.NoCache)
// =================
// Initialize API Routes
// =================
r.Mount("/test", api.TestRouter())
// =================
// Initialize API Documentation
// =================
// Change the http.Dir to ./static for local development!
r.Handle("/static/*", http.StripPrefix("/static", http.FileServer(http.Dir("/srv/static"))))
r.Handle("/docs", doc.Handler())
// =================
// Start WebServer
// =================
fmt.Printf("Starting Server on port 4000\n")
err = http.ListenAndServe(":4000", r)
if err != nil {
log.Fatal(err)
}
}