-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
47 lines (37 loc) · 960 Bytes
/
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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gofor-little/env"
"github.com/integrationninjas/go-app/handlers"
)
func main() {
// load .env file
// godotenv package
port := goDotEnvVariable("PORT")
if port == "" {
port = "8080" // Default port
}
// Initialize logging
log.SetOutput(os.Stdout)
log.SetFlags(log.LstdFlags | log.Lshortfile)
// Register API handlers
http.HandleFunc("/", handlers.HelloHandler)
http.HandleFunc("/items", handlers.ItemsHandler)
http.HandleFunc("/randomuser", handlers.GetRandomUser)
http.HandleFunc("/gemini", handlers.GeminiHandler)
fmt.Println("Server listening on port", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
// use godot package to load/read the .env file and
// return the value of the key
func goDotEnvVariable(key string) string {
// load .env file
err := env.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
return os.Getenv(key)
}