Skip to content

Commit ddd338b

Browse files
committed
Server options moved to separate struct
1 parent fcf8c3f commit ddd338b

File tree

4 files changed

+39
-41
lines changed

4 files changed

+39
-41
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- CSRF protection for main form.
1111
- Health check for Docker.
1212
- Heroku application manifest.
13+
14+
### Changed
15+
- Server options moved to separate struct.

cmd/tornote/main.go

+7-22
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@
1515
// You should have received a copy of the GNU Affero General Public License
1616
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1717

18-
//import (
19-
//
20-
// sw "github.com/osminogin/tornote/go"
21-
//)
22-
//
23-
//
24-
//func main() {
25-
//
26-
// router := sw.NewRouter()
27-
//
28-
// log.Fatal(http.ListenAndServe(":8080", router))
29-
//}
30-
3118
package main
3219

3320
import (
@@ -42,6 +29,9 @@ var (
4229
)
4330

4431
func main() {
32+
var srv *tornote.Server
33+
var opts tornote.ServerOpts
34+
4535
// Set default settings
4636
v := viper.New()
4737
v.SetDefault("PORT", 8000)
@@ -57,17 +47,12 @@ func main() {
5747
v.AutomaticEnv()
5848

5949
// Server initialization and start
60-
var srv *tornote.Server
61-
var opts tornote.ServerOpts
62-
50+
opts.Port = v.GetUint64("PORT")
51+
opts.DSN = v.GetString("DATABASE_URL")
52+
opts.Secret = v.GetString("SECRET_KEY")
6353
opts.HTTPSOnly = v.GetBool("HTTPS_ONLY")
6454

65-
srv = tornote.NewServer(
66-
v.GetUint64("PORT"),
67-
v.GetString("DATABASE_URL"),
68-
v.GetString("SECRET_KEY"),
69-
opts,
70-
)
55+
srv = tornote.NewServer(opts)
7156
srv.Init()
7257
log.Fatal(srv.Listen())
7358
}

server.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,43 @@ import (
3232
)
3333

3434
type Server struct {
35-
// Listen port
36-
Port uint64
37-
// Data source name
38-
DSN string
3935
// Server options
40-
Opts ServerOpts
36+
opts ServerOpts
4137
// PostgreSQL connection
4238
db *pg.DB
43-
// Server secret key
44-
secret string
4539
// Mux router
4640
router *mux.Router
4741
// Compiled templates
4842
templates map[string]*template.Template
4943
}
5044

5145
type ServerOpts struct {
46+
// Listen port
47+
Port uint64
48+
// Data source name
49+
DSN string
50+
// HTTPS only traffic allowed
5251
HTTPSOnly bool
52+
// Server Secret key
53+
Secret string
5354
}
5455

5556
// Constructor for new Server.
56-
func NewServer(port uint64, dsn string, secret string, opts ServerOpts) *Server {
57-
_, err := pg.ParseURL(dsn)
57+
func NewServer(opts ServerOpts) *Server {
58+
_, err := pg.ParseURL(opts.DSN)
5859
if err != nil {
5960
panic(err)
6061
}
61-
return &Server{Port: port, DSN: dsn, secret: secret, Opts: opts}
62+
return &Server{opts: opts}
6263
}
6364

6465
// Open and check database connection.
6566
func (s *Server) connectDB() error {
66-
opt, err := pg.ParseURL(s.DSN)
67+
o, err := pg.ParseURL(s.opts.DSN)
6768
if err != nil {
6869
return err
6970
}
70-
s.db = pg.Connect(opt)
71+
s.db = pg.Connect(o)
7172

7273
// XXX: Ping postgres connection
7374
//if err = s.db.Ping(); err != nil {
@@ -90,7 +91,7 @@ func (s *Server) createSchema() error {
9091
// Generates a hash with a static length suitable for CSRF middleware.
9192
func (s *Server) genHashFromSecret() []byte {
9293
h := sha256.New()
93-
h.Write([]byte(s.secret))
94+
h.Write([]byte(s.opts.Secret))
9495
return h.Sum(nil)
9596
}
9697

@@ -141,10 +142,10 @@ func (s *Server) Init() {
141142
csrfMiddleware := csrf.Protect(
142143
s.genHashFromSecret(),
143144
csrf.FieldName("csrf_token"),
144-
csrf.Secure(s.Opts.HTTPSOnly),
145+
csrf.Secure(s.opts.HTTPSOnly),
145146
)
146147
s.router.Use(csrfMiddleware)
147-
if s.Opts.HTTPSOnly {
148+
if s.opts.HTTPSOnly {
148149
s.router.Use(RedirectToHTTPSMiddleware)
149150
}
150151

@@ -175,10 +176,10 @@ func (s *Server) Listen() error {
175176
}
176177

177178
// Start the server
178-
if s.Opts.HTTPSOnly {
179+
if s.opts.HTTPSOnly {
179180
log.Println("HTTPS only traffic allowed")
180181
}
181-
log.Printf("Starting server on :%d", s.Port)
182-
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", s.Port), s.router))
182+
log.Printf("Starting server on :%d", s.opts.Port)
183+
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", s.opts.Port), s.router))
183184
return nil
184185
}

server_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,20 @@ const (
2424
)
2525

2626
func stubServer() *Server {
27-
srv := NewServer(TestPort, TestDSN, TestSecret, ServerOpts{})
27+
srv := NewServer(ServerOpts{})
2828
srv.Init()
2929
return srv
3030
}
3131

32+
func testServerOpts() ServerOpts {
33+
return ServerOpts{
34+
Port: 8000,
35+
DSN: "postgres://postgres:postgres@postgres/postgres",
36+
HTTPSOnly: false,
37+
Secret: "4tests0nly",
38+
}
39+
}
40+
3241
//func TestNewServer(t *testing.T) {
3342
// s := stubServer()
3443
// if s.DSN != TestDSN && s.Port != TestPort {

0 commit comments

Comments
 (0)