forked from danryan/hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
51 lines (42 loc) · 1.11 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
package hal
import (
"fmt"
"net/http"
"time"
"github.com/danryan/env"
"github.com/sirupsen/logrus"
)
// Config struct
type config struct {
Name string `env:"key=HAL_NAME default=hal"`
Alias string `env:"key=HAL_ALIAS"`
AdapterName string `env:"key=HAL_ADAPTER default=shell"`
StoreName string `env:"key=HAL_STORE default=memory"`
Port int `env:"key=PORT default=9000"`
LogLevel string `env:"key=HAL_LOG_LEVEL default=info"`
}
func newConfig() *config {
c := &config{}
env.MustProcess(c)
return c
}
func newLogger() *logrus.Logger {
level, err := logrus.ParseLevel(Config.LogLevel)
if err != nil {
panic(err)
}
logger := logrus.New()
logger.Level = level
return logger
}
// newRouter initializes a new http.ServeMux and sets up several default routes
func newRouter() *http.ServeMux {
router := http.NewServeMux()
router.HandleFunc("/hal/ping", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "PONG")
})
router.HandleFunc("/hal/time", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Server time is: %s\n", time.Now().UTC())
})
return router
}