remove appconfig, add -m to set UDS chmod for convenience - twitch-go - twitch.tv web application in Go
(HTM) git clone git://git.codemadness.org/twitch-go
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit a037179b9b094c06054fbe82354e252e813cc25d
(DIR) parent 139769a621f12341f563609d14c98cf5a67083d8
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sun, 17 May 2015 15:14:02 +0200
remove appconfig, add -m to set UDS chmod for convenience
Diffstat:
M main.go | 66 ++++++++++++++++---------------
1 file changed, 34 insertions(+), 32 deletions(-)
---
(DIR) diff --git a/main.go b/main.go
@@ -17,17 +17,6 @@ import (
"time"
)
-// config
-type Config struct {
- Addr string
- AddrType string
- Datadir string
- TemplateThemeDir string
- TemplatePageDir string
- StaticContentDir string
- Pidfile string
-}
-
type TwitchHandler func(http.ResponseWriter, *http.Request) error
type Templates struct {
@@ -35,7 +24,6 @@ type Templates struct {
Themes map[string]*template.Template
}
-var appconfig Config
var templates *Templates
func NewTemplates() *Templates {
@@ -142,36 +130,50 @@ func usage() {
}
func main() {
- appconfig = Config{
- TemplateThemeDir: "templates/themes/default",
- TemplatePageDir: "templates/pages/",
- StaticContentDir: "static/",
- }
- flag.StringVar(&appconfig.Datadir, "d", "", "Chdir to data directory")
- flag.StringVar(&appconfig.Pidfile, "f", "", "PID file")
- flag.StringVar(&appconfig.Addr, "l", "127.0.0.1:8080", "listen address")
- flag.StringVar(&appconfig.AddrType, "t", "tcp4", `listen type: "tcp", "tcp4", "tcp6", "unix" or "unixpacket"`)
+ // config
+ var config_addr string
+ var config_addrtype string
+ var config_chmod uint
+ var config_datadir string
+ var config_templatethemedir string = "templates/themes/default/"
+ var config_templatepagedir string = "templates/pages/"
+ var config_staticcontentdir string = "static/"
+ var config_pidfile string
+
+ flag.StringVar(&config_datadir, "d", "", "Chdir to data directory")
+ flag.StringVar(&config_pidfile, "f", "", "PID file")
+ flag.StringVar(&config_addr, "l", "127.0.0.1:8080", "listen address")
+ flag.UintVar(&config_chmod, "m", 0755, "Permission for unix domain socket")
+ flag.StringVar(&config_addrtype, "t", "tcp4", `listen type: "tcp", "tcp4", "tcp6", "unix" or "unixpacket"`)
flag.Parse()
// Remove previous UDS if it exists.
- if appconfig.AddrType == "unix" {
- os.Remove(appconfig.Addr)
+ if config_addrtype == "unix" {
+ os.Remove(config_addr)
}
- l, err := net.Listen(appconfig.AddrType, appconfig.Addr)
+ l, err := net.Listen(config_addrtype, config_addr)
if err != nil {
panic(err)
}
- err = os.Chdir(appconfig.Datadir)
+ // Set permission on UDS.
+ if config_addrtype == "unix" {
+ err = os.Chmod(config_addr, os.FileMode(config_chmod))
+ if err != nil {
+ panic(err)
+ }
+ }
+
+ err = os.Chdir(config_datadir)
if err != nil {
panic(err)
}
// Write PID to pid file.
- if appconfig.Pidfile != "" {
+ if config_pidfile != "" {
pid := os.Getpid()
- if err = ioutil.WriteFile(appconfig.Pidfile, []byte(fmt.Sprintf("%d", pid)), 0644); err != nil {
+ if err = ioutil.WriteFile(config_pidfile, []byte(fmt.Sprintf("%d", pid)), 0644); err != nil {
panic(err)
}
}
@@ -183,8 +185,8 @@ func main() {
return
}
Cleaned = true
- if appconfig.Pidfile != "" {
- os.Remove(appconfig.Pidfile)
+ if config_pidfile != "" {
+ os.Remove(config_pidfile)
}
}
@@ -202,11 +204,11 @@ func main() {
templates = NewTemplates()
// Parse templates and keep in-memory.
- err = templates.LoadPages(appconfig.TemplatePageDir)
+ err = templates.LoadPages(config_templatepagedir)
if err != nil {
panic(err)
}
- err = templates.LoadThemes(appconfig.TemplateThemeDir)
+ err = templates.LoadThemes(config_templatethemedir)
if err != nil {
panic(err)
}
@@ -217,7 +219,7 @@ func main() {
http.HandleFunc("/game", MakeHandler(GameHandler))
http.HandleFunc("/links", MakeHandler(LinksHandler))
- fileserv := http.FileServer(http.Dir(appconfig.StaticContentDir)).ServeHTTP
+ fileserv := http.FileServer(http.Dir(config_staticcontentdir)).ServeHTTP
http.HandleFunc("/", MakeHandler(func(w http.ResponseWriter, r *http.Request) error {
if r.URL.Path == "/" {
return FeaturedHandler(w, r)