-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
53 lines (43 loc) · 1009 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
48
49
50
51
52
53
package main
import (
"database/sql"
"fmt"
"io"
"net/http"
"os"
"time"
"go-web-boilerplate/config"
"github.com/gorilla/handlers"
_ "github.com/lib/pq"
)
var db *sql.DB
func index(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}
func main() {
conn, _ := sql.Open("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
config.Cfg.DBUsername,
config.Cfg.DBPassword,
config.Cfg.DBName,
config.Cfg.DBSSLMode,
))
db = conn
defer conn.Close()
if err := conn.Ping(); err != nil {
panic(err)
}
mux := http.NewServeMux()
mux.HandleFunc("/", index)
file, _ := os.OpenFile("server.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
outs := io.MultiWriter(file, os.Stdout)
logged := handlers.LoggingHandler(outs, mux)
server := &http.Server{
Addr: config.Cfg.HTTPAddr,
Handler: logged,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}