forked from TechBowl-japan/go-stations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (65 loc) · 1.7 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
77
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/TechBowl-japan/go-stations/db"
"github.com/TechBowl-japan/go-stations/handler"
"github.com/TechBowl-japan/go-stations/handler/router"
)
func main() {
err := realMain()
if err != nil {
log.Fatalln("main: failed to exit successfully, err =", err)
}
}
func realMain() error {
// config values
const (
defaultPort = ":8080"
defaultDBPath = ".sqlite3/todo.db"
)
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
dbPath := os.Getenv("DB_PATH")
if dbPath == "" {
dbPath = defaultDBPath
}
// set time zone
var err error
time.Local, err = time.LoadLocation("Asia/Tokyo")
if err != nil {
return err
}
// set up sqlite3
todoDB, err := db.NewDB(dbPath)
if err != nil {
return err
}
defer todoDB.Close()
// NOTE: 新しいエンドポイントの登録はrouter.NewRouterの内部で行うようにする
mux := router.NewRouter(todoDB)
healthzHandler := handler.NewHealthzHandler()
mux.HandleFunc("/healthz", healthzHandler.ServeHTTP)
// mux.Handle("/healthz", HealthzHandler)
//todoHandler := handler.NewTODOHandler(todoDB)
// mux.HandleFunc("/todos", todoHandler.Create)
//var svc *service.TODOService = service.NewTODOService(todoDB)
//mux.HandleFunc("/todos", todoHandler.ServeHTTP)
http.HandleFunc("/", hello)
//http.ListenAndServe(":8000", nil)
// TODO: サーバーをlistenする
// サーバーをポート8080で起動
http.ListenAndServe(defaultPort, mux)
//http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "Hello World")
// })
return nil
}
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, Gopher!")
}