-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (48 loc) · 1.24 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
package main
import (
"log"
"net/http"
"os"
"strconv"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"github.com/rs/cors"
)
var (
host string
port int
user string
password string
dbname string
)
func init() {
err := godotenv.Load()
if err != nil {
log.Println("No .env file found")
}
host = os.Getenv("DB_HOST")
portStr := os.Getenv("DB_PORT")
user = os.Getenv("DB_USER")
password = os.Getenv("DB_PASSWORD")
dbname = os.Getenv("DB_NAME")
port, err = strconv.Atoi(portStr)
if err != nil {
log.Fatal("Invalid port number in environment variables")
}
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/users", UsersHandler)
r.HandleFunc("/readings", ReadingsHandler)
r.HandleFunc("/readings/{user_id}", UserReadingsHandler)
r.HandleFunc("/users/rfid/{rfid}", GetUserByRFIDHandler)
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"},
Debug: true,
})
handler := c.Handler(r)
log.Fatal(http.ListenAndServe(":8080", handler))
}