-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
41 lines (36 loc) · 900 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
package main
import (
"database/sql"
"log"
"net/http"
_ "github.com/go-sql-driver/mysql"
)
func main() {
// load database configuration file
log.Println("Reading config file...")
cfg, err := loadDbConfig("db-config.json")
if err != nil {
log.Fatalln(err)
}
log.Println("Config file OK.")
// open database and test connection
log.Println("Setting up database...")
dataSourceName := cfg.getDataSourceName()
db, err := sql.Open("mysql", dataSourceName)
if err != nil {
log.Fatalln(err)
}
defer db.Close()
log.Println("Pinging database...")
err = db.Ping()
if err != nil {
log.Fatalln(err)
}
log.Println("Database OK.")
// start server
log.Println("Starting server...")
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
http.Handle("/", &app{db})
log.Print("Serving HTTP...")
log.Fatalln(http.ListenAndServe(":80", nil))
}