-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (61 loc) · 1.35 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
package main
import (
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"github.com/Jonss/heroku-go-embed-template/pkg/config"
"github.com/Jonss/heroku-go-embed-template/pkg/db"
"github.com/Jonss/heroku-go-embed-template/pkg/server"
"github.com/gorilla/mux"
)
//go:embed ui/build
var embeddedFiles embed.FS
var cfg config.Config
func main() {
var err error
cfg, err = config.LoadConfig(".")
if err != nil {
log.Fatalf("error getting config: error=(%v)", err)
}
port := os.Getenv("PORT")
if port == "" {
port = "9000" // Default port if not specified
}
conn, err := db.NewConnection(cfg.DBURL)
if err != nil {
log.Fatal(err)
}
err = db.Migrate(conn, cfg.DBName)
if err != nil {
log.Fatal(err)
}
validator, err := server.NewValidator()
if err != nil {
log.Fatal(err)
}
router := mux.NewRouter()
srv := server.NewServer(
router,
cfg,
validator,
)
srv.Routes()
fmt.Printf("Starting Server... env: [%s].\n", cfg.Env)
router.PathPrefix("/").Handler(http.FileServer(getFileSystem()))
// http.Handle("/", )
fmt.Println("Server started!")
http.ListenAndServe(":"+port, router)
}
func getFileSystem() http.FileSystem {
// Get the build subdirectory as the
// root directory so that it can be passed
// to the http.FileServer
fsys, err := fs.Sub(embeddedFiles, "ui/build")
if err != nil {
panic(err)
}
return http.FS(fsys)
}