/
config.go
61 lines (52 loc) · 1.87 KB
/
config.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
54
55
56
57
58
59
60
61
package server
import (
"fmt"
"strings"
)
// Configuration for server behavior.
type Config struct {
// HTTP service address for the server.
// The format is `[host]:[port]`, e.g. localhost:8080.
HTTP string
// Prefix of URI for the chat API.
// e.g. given ChatAPIPrefix = `/api` and chat API `/chat/rooms`,
// the prefixed chat API is `/api/chat/rooms`.
ChatAPIPrefix string
// Prefix of URI for the static file server.
//
// Example: given local html file `/www/index.html`,
// StaticHandlerPrefix = "/www" and StaticHandlerPrefix = "/static",
// the requesting the server with URI `/static/index.html` responds
// the html content of `/www/index.html`.
StaticHandlerPrefix string
// root directory to serve static files.
StaticFileDir string
// indicates whether serving static files is enable.
// if false, StaticHandlerPrefix and StaticFileDir do not
// affect the server.
EnableServeStaticFile bool
// show all of URI routes at server starts.
ShowRoutes bool
}
// DefaultConfig is default configuration for the server.
var DefaultConfig = Config{
HTTP: "localhost:8080",
ChatAPIPrefix: "",
StaticHandlerPrefix: "",
StaticFileDir: "", // current directory
EnableServeStaticFile: true,
ShowRoutes: true,
}
// Validate checks whether the all of field values are correct format.
func (c *Config) Validate() error {
if ss := strings.Split(c.HTTP, ":"); len(ss) != 2 {
return fmt.Errorf("config: HTTP should be [host]:[port], but %v", c.HTTP)
}
if len(c.ChatAPIPrefix) > 0 && !strings.HasPrefix(c.ChatAPIPrefix, "/") {
return fmt.Errorf("config: ChatAPIPrefix should start with \"/\" but %v", c.ChatAPIPrefix)
}
if len(c.StaticHandlerPrefix) > 0 && !strings.HasPrefix(c.StaticHandlerPrefix, "/") {
return fmt.Errorf("config: StaticHandlerPrefix should start with \"/\" but %v", c.StaticHandlerPrefix)
}
return nil
}