1
1
package main
2
2
3
3
import (
4
- "context"
5
- "database/sql"
6
4
"flag"
7
- "fmt"
8
5
"log"
9
- "net/http"
10
6
"os"
11
- "os/signal"
12
7
"time"
13
8
14
9
"github.com/alexedwards/scs"
15
10
16
11
_ "github.com/mattn/go-sqlite3"
17
- "snippetbox.org/pkg/models"
18
12
)
19
13
20
- func existDir (path string ) bool {
21
- if _ , err := os .Stat (path ); os .IsNotExist (err ) {
14
+ func existDir (path * string ) bool {
15
+ if _ , err := os .Stat (* path ); os .IsNotExist (err ) {
22
16
return false
23
17
}
24
18
return true
@@ -33,105 +27,40 @@ func main() {
33
27
flag .StringVar (& app .htmlDir , "html-dir" , "./ui/html" , "Path to html templates" )
34
28
flag .StringVar (& app .databaseFile , "db-file" , "./info.db" , "Path to database file" )
35
29
flag .StringVar (& app .secret , "secret" , "8sB9ozuKkqWtN3b6lEiInd1dSISxPWogpaGV5HG4wKs=" , "Secret key for cookies encryption" )
36
- tlsCert := flag .String ( "tls-cert" , "./tls/cert.pem" , "TLS certificate" )
37
- tlsKey := flag .String ( "tls-key" , "./tls/key.pem" , "TLS private-key" )
30
+ flag .StringVar ( & app . tlsCert , "tls-cert" , "./tls/cert.pem" , "TLS certificate" )
31
+ flag .StringVar ( & app . tlsKey , "tls-key" , "./tls/key.pem" , "TLS private-key" )
38
32
flag .Parse ()
39
33
40
- if ! existDir (app .staticDir ) {
34
+ if ! existDir (& app .staticDir ) {
41
35
log .Fatal ("Folder for static-dir was not found" )
42
36
}
43
37
44
- if ! existDir (app .htmlDir ) {
38
+ if ! existDir (& app .htmlDir ) {
45
39
log .Fatal ("Folder for html-dir was not found" )
46
40
}
47
41
48
- if ! existDir (* tlsCert ) {
42
+ if ! existDir (& app . tlsCert ) {
49
43
log .Fatal ("TLS certificate was not found" )
50
44
}
51
45
52
- if ! existDir (* tlsKey ) {
46
+ if ! existDir (& app . tlsKey ) {
53
47
log .Fatal ("TLS key was not found" )
54
48
}
55
49
56
- if err := app .connectDb (); err != nil {
50
+ if err := app .ConnectDb (); err != nil {
57
51
log .Fatal ("Failed to establish database connection" )
58
52
}
59
- defer func () {
60
- log .Println ("Closing database connection" )
61
- app .closeDB ()
62
- }()
53
+ defer app .CloseDB ()
63
54
64
55
sessionManager := scs .NewCookieManager (app .secret )
65
56
sessionManager .Lifetime (12 * time .Hour )
66
57
sessionManager .Persist (true )
67
58
sessionManager .Secure (true )
68
59
app .sessions = sessionManager
69
60
70
- server := & http.Server {
71
- Addr : app .addr ,
72
- Handler : app .Routes (),
73
- WriteTimeout : 15 * time .Second ,
74
- ReadTimeout : 15 * time .Second ,
75
- IdleTimeout : 60 * time .Second ,
76
- }
77
-
78
- c := make (chan os.Signal , 1 )
79
- signal .Notify (c , os .Interrupt )
80
- go func () {
81
- for sig := range c {
82
- log .Printf ("Terminating (signal caught - %s)\n " , sig )
83
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
84
- defer func () {
85
- log .Println ("Closing context" )
86
- cancel ()
87
- }()
88
- server .Shutdown (ctx )
89
- }
90
- }()
91
-
92
- log .Printf ("Listening on %s\n " , app .addr )
93
-
94
- if err := server .ListenAndServeTLS (* tlsCert , * tlsKey ); err != nil {
95
- if err != http .ErrServerClosed {
96
- log .Println ("The error below raised after shutdown:" )
97
- log .Println (err )
98
- }
99
- }
100
- }
101
-
102
- func (app * App ) connectDb () error {
103
- initDb := ! existDir (app .databaseFile )
104
-
105
- dsn := fmt .Sprintf ("file:%s?cache=shared&_loc=auto" , app .databaseFile )
106
- db , err := sql .Open ("sqlite3" , dsn )
107
- if err != nil {
108
- return err
109
- }
61
+ app .InitServer ()
110
62
111
- defer func () {
112
- if err != nil {
113
- db .Close ()
114
- }
115
- }()
63
+ app .MonitorInterrupts ()
116
64
117
- if err = db .Ping (); err != nil {
118
- return err
119
- }
120
-
121
- app .database = & models.Database {DB : db }
122
-
123
- if initDb {
124
- log .Println ("Initializing database..." )
125
- if err := app .database .InitializeDb (); err != nil {
126
- return err
127
- }
128
- }
129
-
130
- return nil
131
- }
132
-
133
- func (app * App ) closeDB () {
134
- if app .database != nil {
135
- app .database .Close ()
136
- }
65
+ app .RunServer ()
137
66
}
0 commit comments