Skip to content

Commit

Permalink
Merge PR #32: Implement flags and config
Browse files Browse the repository at this point in the history
  • Loading branch information
Kissaki committed Jan 17, 2019
2 parents da57ec5 + b79ab16 commit ec30cc3
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 51 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ all: public mumble-www package
public: $(shell find hugo/ -type f)
(cd hugo/; hugo)

mumble-www: src/mumble-www.go src/githubcache.go src/snapshotcache.go
go build -o $@ $^
mumble-www: mumble-www.go githubcache.go snapshotcache.go config.go downloads.go programflags.go
(cd src/; GO111MODULE=on go build -o ../$@ $^)

package: public mumble-www
7z a mumble-www.7z public mumble-www
21 changes: 21 additions & 0 deletions build-go.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@echo off
SETLOCAL

PUSHD %~dp0

set GO111MODULE=on

PUSHD src

SET SRCFILES=mumble-www.go githubcache.go snapshotcache.go config.go downloads.go programflags.go

go build -o ..\mumble-www.exe %SRCFILES%
IF ERRORLEVEL 1 (EXIT /B 1)

SET GOOS=linux
go build -o ..\mumble-www %SRCFILES%
IF ERRORLEVEL 1 (EXIT /B 1)

POPD

POPD
10 changes: 3 additions & 7 deletions build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ PUSHD %~dp0

PUSHD hugo
hugo
IF ERRORLEVEL 1 (PAUSE && EXIT /B 1)
IF ERRORLEVEL 1 (EXIT /B 1)
POPD

go build src/mumble-www.go src/githubcache.go src/snapshotcache.go
IF ERRORLEVEL 1 (PAUSE && EXIT /B 1)

SET GOOS=linux
go build src/mumble-www.go src/githubcache.go src/snapshotcache.go
IF ERRORLEVEL 1 (PAUSE && EXIT /B 1)
call build-go.bat
IF ERRORLEVEL 1 (EXIT /B 1)

DEL mumble-www.7z 1>NUL
7z.exe a mumble-www.7z public mumble-www
Expand Down
33 changes: 33 additions & 0 deletions src/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"io/ioutil"

yaml "gopkg.in/yaml.v2"
)

type Config struct {
Host string `yaml:host`
Port int `yaml:port`
PublicPath string `yaml:publicpath`
GitHubCacheTimeMins int `yaml:githubcachetimemins`
}

func NewConfig() *Config {
return &Config{}
}

func ReadConfig(path string) (c *Config, err error) {
c = NewConfig()
err = ReadConfigInto(path, c)
return
}

func ReadConfigInto(path string, c *Config) (err error) {
yamlFile, err := ioutil.ReadFile(path)
if err != nil {
return
}
err = yaml.Unmarshal(yamlFile, c)
return
}
33 changes: 33 additions & 0 deletions src/downloads.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"net/http"
"time"
)

func setupDownloadsStable(mux *http.ServeMux) {
stable := map[string]string{
"windows-32": "https://github.com/mumble-voip/mumble/releases/download/1.2.19/mumble-1.2.19.msi",
"osx": "https://github.com/mumble-voip/mumble/releases/download/1.2.19/Mumble-1.2.19.dmg",
"osx-universal": "https://github.com/mumble-voip/mumble/releases/download/1.2.10/Mumble-Universal-1.2.10.dmg",
"ios": "http://itunes.apple.com/us/app/mumble/id443472808?ls=1&mt=8",
"ubuntu": "https://launchpad.net/~mumble/+archive/release",
"linux-static-server": "https://github.com/mumble-voip/mumble/releases/download/1.2.19/murmur-static_x86-1.2.19.tar.bz2",
"osx-static-server": "https://github.com/mumble-voip/mumble/releases/download/1.2.19/Murmur-OSX-Static-1.2.19.tar.bz2",
}

for source, target := range stable {
mux.Handle("/downloads/"+source, http.RedirectHandler(target, http.StatusTemporaryRedirect))
}
}

func setupDownloadsSnapshot(mux *http.ServeMux) {
snapshotCache := NewSnapshotCache(time.Hour)
for source := range snapshotRegexps {
mux.Handle("/downloads/"+source+"/snapshot", snapshotCache.Handle(source))
}
mux.Handle(
"/downloads/ubuntu/snapshot",
http.RedirectHandler("https://launchpad.net/~mumble/+archive/snapshot", http.StatusTemporaryRedirect),
)
}
3 changes: 2 additions & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module "https: //github.com/mumble-voip/mumble-www"
module github.com/mumble-voip/mumble-www

require (
github.com/PuerkitoBio/goquery v1.5.0
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
gopkg.in/yaml.v2 v2.2.2
)
3 changes: 3 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a h1:gOpx8G595UYyvj8UK4+OFyY4rx037g3fmfhe5SasG3U=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
54 changes: 13 additions & 41 deletions src/mumble-www.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,41 @@
package main

import (
"flag"
"fmt"
"log"
"net/http"
"time"
)

func setup(mux *http.ServeMux) {
func setup(mux *http.ServeMux, config *Config) {
setupDownloadsStable(mux)
setupDownloadsSnapshot(mux)
setupGithubCache(mux)
setupStaticFiles(mux)
setupGithubCache(mux, config.GitHubCacheTimeMins)
setupStaticFiles(mux, config.PublicPath)
}

func setupDownloadsStable(mux *http.ServeMux) {
stable := map[string]string{
"windows-32": "https://github.com/mumble-voip/mumble/releases/download/1.2.19/mumble-1.2.19.msi",
"osx": "https://github.com/mumble-voip/mumble/releases/download/1.2.19/Mumble-1.2.19.dmg",
"osx-universal": "https://github.com/mumble-voip/mumble/releases/download/1.2.10/Mumble-Universal-1.2.10.dmg",
"ios": "http://itunes.apple.com/us/app/mumble/id443472808?ls=1&mt=8",
"ubuntu": "https://launchpad.net/~mumble/+archive/release",
"linux-static-server": "https://github.com/mumble-voip/mumble/releases/download/1.2.19/murmur-static_x86-1.2.19.tar.bz2",
"osx-static-server": "https://github.com/mumble-voip/mumble/releases/download/1.2.19/Murmur-OSX-Static-1.2.19.tar.bz2",
}

for source, target := range stable {
mux.Handle("/downloads/"+source, http.RedirectHandler(target, http.StatusTemporaryRedirect))
}
}

func setupDownloadsSnapshot(mux *http.ServeMux) {
snapshotCache := NewSnapshotCache(time.Hour)
for source := range snapshotRegexps {
mux.Handle("/downloads/"+source+"/snapshot", snapshotCache.Handle(source))
}
mux.Handle(
"/downloads/ubuntu/snapshot",
http.RedirectHandler("https://launchpad.net/~mumble/+archive/snapshot", http.StatusTemporaryRedirect),
)
}

func setupGithubCache(mux *http.ServeMux) {
githubCache := NewGithubCache(time.Minute * 10)
func setupGithubCache(mux *http.ServeMux, cacheTimeMins int) {
githubCache := NewGithubCache(time.Minute * time.Duration(cacheTimeMins))
mux.Handle("/LICENSE", githubCache.Handle("/LICENSE"))
mux.Handle("/AUTHORS", githubCache.Handle("/AUTHORS"))
}

func setupStaticFiles(mux *http.ServeMux) {
public := flag.String("public", "public", "public directory")
fs := http.FileServer(http.Dir(*public))
func setupStaticFiles(mux *http.ServeMux, path string) {
fs := http.FileServer(http.Dir(path))
mux.Handle("/", fs)
}

func main() {
addr := flag.String("addr", "127.0.0.1:8081", "bind address")
flag.Parse()
config := ParseInit()
addr := fmt.Sprintf("%s:%d", config.Host, config.Port)

log.Printf("Starting server on %s\n", *addr)
log.Printf("Starting server on %s\n", addr)

mux := http.NewServeMux()

setup(mux)
setup(mux, config)

if err := http.ListenAndServe(*addr, mux); err != nil {
if err := http.ListenAndServe(addr, mux); err != nil {
log.Fatal(err)
}
}
4 changes: 4 additions & 0 deletions src/mumble-www.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
host: 127.0.0.1
port: 8081
publicpath: public
githubcachetimemins: 10
60 changes: 60 additions & 0 deletions src/programflags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"flag"
"log"
)

type Flags struct {
ConfigPath string
Host string
Port int
PublicPath string
GitHubCacheTimeMins int
}

func NewFlags() *Flags {
return &Flags{}
}

func ParseFlags() (f *Flags) {
f = NewFlags()
flag.StringVar(&f.ConfigPath, "config", "", "path to yaml config file")
flag.StringVar(&f.Host, "host", "", "bind host address")
flag.IntVar(&f.Port, "port", 0, "bind port number")
flag.StringVar(&f.PublicPath, "publicpath", "", "path to public web files folder")
flag.IntVar(&f.GitHubCacheTimeMins, "githubcachetimemins", 0, "Cache time in minutes to cache GitHub content")
flag.Parse()
return
}

func ParseInit() (config *Config) {
flags := ParseFlags()

if flags.ConfigPath == "" && (flags.Host == "" || flags.Port == 0 || flags.PublicPath == "" || flags.GitHubCacheTimeMins == 0) {
flag.Usage()
log.Fatalln("Missing parameter config or complete configuration parameters")
}

config = NewConfig()
if flags.ConfigPath != "" {
err := ReadConfigInto(flags.ConfigPath, config)
if err != nil {
log.Fatalf("Failed to read config file at %s. Error: %s", flags.ConfigPath, err)
}
}

if flags.Host != "" {
config.Host = flags.Host
}
if flags.Port != 0 {
config.Port = flags.Port
}
if flags.PublicPath != "" {
config.PublicPath = flags.PublicPath
}
if flags.GitHubCacheTimeMins != 0 {
config.GitHubCacheTimeMins = flags.GitHubCacheTimeMins
}
return
}

0 comments on commit ec30cc3

Please sign in to comment.