-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (47 loc) · 1.19 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
package main
import (
"log"
"net/http"
"os"
"time"
dotenv "github.com/joho/godotenv"
_ "github.com/lib/pq"
"github.com/ellielle/rss-aggregator/internal/database"
)
type apiConfig struct {
DB *database.Queries
}
func main() {
// interval to retrieve RSS Feed data
// interval is multiplied by time.Second
const FEED_INTERVAL = 60
// Retrive port from environment
err := dotenv.Load()
if err != nil {
log.Fatal("error loading .env file")
}
port := os.Getenv("PORT")
// Get database connection
db := getDatabase()
// Shove database queries into a config struct
dbQueries := database.New(db)
// Fill config struct
apiCfg := apiConfig{DB: dbQueries}
// Create a new request mux
mux := http.NewServeMux()
// Attach endpoints to request mux
apiCfg.createRouter(mux)
// Add CORS headers
corsMux := middlewareCors(mux)
// Start Feed refreshing worker
// Run in a goroutine so it can be concurrently processed
// while handling http requests
go updateFeedData(&apiCfg, FEED_INTERVAL*time.Second)
// Configure server and start
server := &http.Server{
Addr: ":" + port,
Handler: corsMux,
}
log.Printf("Serving on port: %v", port)
log.Fatal(server.ListenAndServe())
}