Skip to content
This repository was archived by the owner on Sep 22, 2022. It is now read-only.

Commit 99d877d

Browse files
committed
Добавлена загрузка порта из конфиг-файла
1 parent 3edc435 commit 99d877d

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.exe
22
posts/*.[mM][Dd]
33
public/uploads/
4+
mdserver.yaml

config.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
6+
"gopkg.in/yaml.v2"
7+
)
8+
9+
// Config - структура для считывания конфигурационного файла
10+
type Config struct {
11+
Listen string `yaml:"listen"`
12+
}
13+
14+
func readConfig(ConfigName string) (x *Config, err error) {
15+
var file []byte
16+
if file, err = ioutil.ReadFile(ConfigName); err != nil {
17+
return nil, err
18+
}
19+
x = new(Config)
20+
if err = yaml.Unmarshal(file, x); err != nil {
21+
return nil, err
22+
}
23+
return x, nil
24+
}

mdserver.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
"github.com/bmizerany/pat"
1111
)
1212

13+
const (
14+
configFileName = "mdserver.yaml"
15+
)
16+
1317
var (
1418
// компилируем шаблоны, если не удалось, то выходим
1519
postTemplate = template.Must(template.ParseFiles(path.Join("templates", "layout.html"), path.Join("templates", "post.html")))
@@ -18,6 +22,10 @@ var (
1822
)
1923

2024
func main() {
25+
cfg, err := readConfig(configFileName)
26+
if err != nil {
27+
log.Fatalln(err)
28+
}
2129
// для отдачи сервером статичных файлов из папки public/static
2230
fs := noDirListing(http.FileServer(http.Dir("./public/static")))
2331
http.Handle("/static/", http.StripPrefix("/static/", fs))
@@ -31,8 +39,8 @@ func main() {
3139
mux.Get("/", http.HandlerFunc(postHandler))
3240

3341
http.Handle("/", mux)
34-
log.Println("Listening...")
35-
http.ListenAndServe(":8890", nil)
42+
log.Printf("Listening %s...", cfg.Listen)
43+
log.Fatalln(http.ListenAndServe(cfg.Listen, nil))
3644
}
3745

3846
func postHandler(w http.ResponseWriter, r *http.Request) {

mdserver.yaml.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
listen: 0.0.0.0:8890

0 commit comments

Comments
 (0)