-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
74 lines (59 loc) · 1.68 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
package main
import (
"flag"
"log"
"net/http"
"os"
"github.com/guilycst/go-htmx/internal/adapters/handlers/fileserver"
"github.com/guilycst/go-htmx/internal/adapters/handlers/htmx"
_ "time/tzdata"
"github.com/guilycst/go-htmx/internal/core/services/todosrv"
"github.com/guilycst/go-htmx/pkg/loadenv"
"github.com/guilycst/go-htmx/pkg/repo"
)
func init() {
//Parse flags
env := flag.String("env", "", ".env file")
flag.Parse()
// Try to load .env file if any
loadenv.LoadEnv(env)
var (
storage repo.Storage = repo.StorageFromString(os.Getenv("STORAGE"))
connStr string = os.Getenv("CONN_STR")
)
//Create new repository
repository, err := repo.GetRepo(storage, connStr)
if err != nil {
log.Fatal(err)
}
//Create service
srv := todosrv.New(repository)
//Initialize http handlers
handler, err := htmx.NewHTMXHandler(srv)
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/", handler.IndexHandleFunc)
http.HandleFunc("/add", handler.AddHandleFunc)
http.HandleFunc("/list", handler.ListHandleFunc)
http.HandleFunc("/done/", handler.DoneHandleFunc(true))
http.HandleFunc("/undone/", handler.DoneHandleFunc(false))
http.HandleFunc("/delete/", handler.Delete)
http.HandleFunc("/edit/", handler.Edit)
http.HandleFunc("/update/", handler.Update)
distFsh, err := fileserver.NewFileServerHandler("./dist")
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/dist/", distFsh)
assetsFsh, err := fileserver.NewFileServerHandler("./assets")
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/assets/", assetsFsh)
}
func main() {
// Start the HTTP server
log.Println("Server started on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}