From 67d0a83039f5a722b126060b7794d811354c0039 Mon Sep 17 00:00:00 2001 From: "Quentin McGaw (desktop)" Date: Sat, 11 Jan 2020 03:54:17 +0000 Subject: [PATCH] Reworked Go code --- go.mod | 3 +- go.sum | 82 ++++++++++++++++++++ main.go | 237 +++++++++++++++++++++++--------------------------------- 3 files changed, 180 insertions(+), 142 deletions(-) diff --git a/go.mod b/go.mod index 43ddaf0..07927f9 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,9 @@ module github.com/qdm12/stackedit-docker require ( - github.com/julienschmidt/httprouter v1.2.0 + github.com/julienschmidt/httprouter v1.3.0 github.com/kyokomi/emoji v1.5.1 + github.com/qdm12/golibs v0.0.0-20200108212314-82caecb70123 ) go 1.13 diff --git a/go.sum b/go.sum index 95270e1..f441891 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,86 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= +github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= +github.com/go-openapi/analysis v0.0.0-20180825180245-b006789cd277/go.mod h1:k70tL6pCuVxPJOHXQ+wIac1FUrvNkHolPie/cLEU6hI= +github.com/go-openapi/analysis v0.17.0/go.mod h1:IowGgpVeD0vNm45So8nr+IcQ3pxVtpRoBWb8PVZO0ik= +github.com/go-openapi/errors v0.17.0/go.mod h1:LcZQpmvG4wyF5j4IhA73wkLFQg+QJXOQHVjmcZxhka0= +github.com/go-openapi/errors v0.17.2/go.mod h1:LcZQpmvG4wyF5j4IhA73wkLFQg+QJXOQHVjmcZxhka0= +github.com/go-openapi/jsonpointer v0.17.0/go.mod h1:cOnomiV+CVVwFLk0A/MExoFMjwdsUdVpsRhURCKh+3M= +github.com/go-openapi/jsonreference v0.17.0/go.mod h1:g4xxGn04lDIRh0GJb5QlpE3HfopLOL6uZrK/VgnsK9I= +github.com/go-openapi/loads v0.17.0/go.mod h1:72tmFy5wsWx89uEVddd0RjRWPZm92WRLhf7AC+0+OOU= +github.com/go-openapi/runtime v0.0.0-20180920151709-4f900dc2ade9/go.mod h1:6v9a6LTXWQCdL8k1AO3cvqx5OtZY/Y9wKTgaoP6YRfA= +github.com/go-openapi/runtime v0.17.2/go.mod h1:QO936ZXeisByFmZEO1IS1Dqhtf4QV1sYYFtIq6Ld86Q= +github.com/go-openapi/spec v0.17.0/go.mod h1:XkF/MOi14NmjsfZ8VtAKf8pIlbZzyoTvZsdfssdxcBI= +github.com/go-openapi/strfmt v0.17.0/go.mod h1:P82hnJI0CXkErkXi8IKjPbNBM6lV6+5pLP5l494TcyU= +github.com/go-openapi/swag v0.17.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg= +github.com/go-openapi/validate v0.17.0/go.mod h1:Uh4HdOzKt19xGIGm1qHf/ofbX1YQ4Y+MYsct2VUrAJ4= +github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gotify/go-api-client/v2 v2.0.4/go.mod h1:VKiah/UK20bXsr0JObE1eBVLW44zbBouzjuri9iwjFU= github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kyokomi/emoji v1.5.1 h1:qp9dub1mW7C4MlvoRENH6EAENb9skEFOvIEbp1Waj38= github.com/kyokomi/emoji v1.5.1/go.mod h1:mZ6aGCD7yk8j6QY6KICwnZ2pxoszVseX1DNoGtU2tBA= +github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= +github.com/phayes/permbits v0.0.0-20190612203442-39d7c581d2ee/go.mod h1:3uODdxMgOaPYeWU7RzZLxVtJHZ/x1f/iHkBZuKJDzuY= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/qdm12/golibs v0.0.0-20200108212314-82caecb70123 h1:YxvlGvUJ+IO1EwaLoZlMrMT6GfuwQ56k0p0YM4rTVZc= +github.com/qdm12/golibs v0.0.0-20200108212314-82caecb70123/go.mod h1:oNw3Ie4T0wyeBk9fK8vOe5tKdWG3bEcII6RlQjw7JIs= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/atomic v1.5.1 h1:rsqfU5vBkVknbhUGbAUwQKR2H4ItV8tjJ+6kJX4cxHM= +go.uber.org/atomic v1.5.1/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/multierr v1.4.0 h1:f3WCSC2KzAcBXGATIxAB1E2XuCpNU255wNKZ505qi3E= +go.uber.org/multierr v1.4.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= +go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= +go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/net v0.0.0-20181005035420-146acd28ed58/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= diff --git a/main.go b/main.go index 9bcedce..5e470ec 100644 --- a/main.go +++ b/main.go @@ -1,141 +1,96 @@ -package main - -import ( - "fmt" - "log" - "net/http" - "os" - "strconv" - "strings" - "time" - - "github.com/kyokomi/emoji" -) - -const ( - defaultListeningPort = "8000" - defaultConf = `{"dropboxAppKey":"","dropboxAppKeyFull":"","githubClientId":"","googleClientId":"","googleApiKey":"","wordpressClientId":"","allowSponsorship":true}` -) - -func parseEnv() (listeningPort string) { - listeningPort = os.Getenv("LISTENINGPORT") - if len(listeningPort) == 0 { - listeningPort = defaultListeningPort - } else { - value, err := strconv.Atoi(listeningPort) - if err != nil { - log.Fatal(emoji.Sprint(":x:") + " LISTENINGPORT environment variable '" + listeningPort + - "' is not a valid integer") - } - if value < 1024 { - if os.Geteuid() == 0 { - log.Println(emoji.Sprint(":warning:") + "LISTENINGPORT environment variable '" + listeningPort + - "' allowed to be in the reserved system ports range as you are running as root.") - } else if os.Geteuid() == -1 { - log.Println(emoji.Sprint(":warning:") + "LISTENINGPORT environment variable '" + listeningPort + - "' allowed to be in the reserved system ports range as you are running in Windows.") - } else { - log.Fatal(emoji.Sprint(":x:") + " LISTENINGPORT environment variable '" + listeningPort + - "' can't be in the reserved system ports range (1 to 1023) when running without root.") - } - } - if value > 65535 { - log.Fatal(emoji.Sprint(":x:") + " LISTENINGPORT environment variable '" + listeningPort + - "' can't be higher than 65535") - } - if value > 49151 { - // dynamic and/or private ports. - log.Println(emoji.Sprint(":warning:") + "LISTENINGPORT environment variable '" + listeningPort + - "' is in the dynamic/private ports range (above 49151)") - } - } - return listeningPort -} - -func healthcheckMode() bool { - args := os.Args - if len(args) > 1 { - if len(args) > 2 { - log.Fatal(emoji.Sprint(":x:") + " Too many arguments provided") - } - if args[1] == "healthcheck" { - return true - } - log.Fatal(emoji.Sprint(":x:") + " Argument 1 can only be 'healthcheck', not " + args[1]) - } - return false -} - -func healthcheck(listeningPort string) { - request, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:"+listeningPort+"/healthcheck", nil) - if err != nil { - fmt.Println("Can't build HTTP request") - os.Exit(1) - } - client := &http.Client{Timeout: 1 * time.Second} - response, err := client.Do(request) - if err != nil { - fmt.Println("Can't execute HTTP request") - os.Exit(1) - } - if response.StatusCode != 200 { - fmt.Println("Status code is " + response.Status) - os.Exit(1) - } - os.Exit(0) -} - -func main() { - listeningPort := parseEnv() - if healthcheckMode() { - healthcheck(listeningPort) - } - fmt.Println("#####################################") - fmt.Println("########## StackEdit Server #########") - fmt.Println("########## by Quentin McGaw #########") - fmt.Println("########## Give some " + emoji.Sprint(":heart:") + "at ##########") - fmt.Println("# github.com/qdm12/stackedit-docker #") - fmt.Print("#####################################\n\n") - http.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - }) - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - - switch { - case r.URL.Path == "/conf" || r.URL.Path == "/app/conf": - // TODO: read from `docker config` or ENVIROMENT or File - fmt.Fprintf(w, "%s", defaultConf) - return; - - case r.URL.Path == "/": - r.URL.Path = "/static/landing/" - - case strings.HasPrefix(r.URL.Path, "/static/landing/"): - r.URL.Path = r.URL.Path - - case r.URL.Path == "/sitemap.xml": - r.URL.Path = "/static/sitemap.xml" - - case r.URL.Path == "/oauth2/callback": - r.URL.Path = "/static/oauth2/callback.html" - - case r.URL.Path == "/app" || r.URL.Path == "/app/": - r.URL.Path = "/dist/" - - case strings.HasPrefix(r.URL.Path, "/app/"): - r.URL.Path = "/dist/" + r.URL.Path[4:] - - default: - if strings.HasPrefix(r.URL.Path, "/") { - r.URL.Path = "/dist" + r.URL.Path - } else { - r.URL.Path = "/dist/" + r.URL.Path - } - } - - http.ServeFile(w, r, "/html" + r.URL.Path) - }); - - log.Println("Web UI listening on 0.0.0.0:" + listeningPort + emoji.Sprint(" :ear:")) - log.Fatal(http.ListenAndServe("0.0.0.0:"+listeningPort, nil)) -} +package main + +import ( + "fmt" + "net/http" + "os" + "strings" + + "github.com/julienschmidt/httprouter" + "github.com/kyokomi/emoji" + "github.com/qdm12/golibs/healthcheck" + "github.com/qdm12/golibs/logging" + "github.com/qdm12/golibs/params" + "github.com/qdm12/golibs/server" +) + +const ( + defaultConf = `{"dropboxAppKey":"","dropboxAppKeyFull":"","githubClientId":"","googleClientId":"","googleApiKey":"","wordpressClientId":"","allowSponsorship":true}` +) + +func main() { + if healthcheck.Mode(os.Args) { + if err := healthcheck.Query(); err != nil { + logging.Err(err) + os.Exit(1) + } + os.Exit(0) + } + fmt.Println("#####################################") + fmt.Println("########## StackEdit Server #########") + fmt.Println("########## by Quentin McGaw #########") + fmt.Println("########## Give some " + emoji.Sprint(":heart:") + " at ##########") + fmt.Println("# github.com/qdm12/stackedit-docker #") + fmt.Print("#####################################\n\n") + envParams := params.NewEnvParams() + logging.InitLogger("console", logging.InfoLevel, 0) + listeningPort, err := envParams.GetListeningPort(params.Default("8000")) + if err != nil { + logging.Err(err) + os.Exit(1) + } + logging.Infof("Using internal listening port %s", listeningPort) + rootURL, err := envParams.GetRootURL(params.Default("/")) + if err != nil { + logging.Err(err) + os.Exit(1) + } + logging.Infof("Using root URL %q", rootURL) + + handler := func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + switch path { + case rootURL + "/conf", rootURL + "/app/conf": + // TODO: read from `docker config` or ENVIROMENT or File + fmt.Fprintf(w, "%s", defaultConf) + return + case rootURL + "/": + path = rootURL + "/static/landing/" + case rootURL + "/sitemap.xml": + path = rootURL + "/static/sitemap.xml" + case rootURL + "/oauth2/callback": + path = rootURL + "/static/oauth2/callback.html" + case rootURL + "/app", rootURL + "/app/": + path = rootURL + "/dist/" + default: + switch { + case strings.HasPrefix(path, rootURL+"/app/"): + path = rootURL + "/dist/" + path[len(rootURL)+4:] + case strings.HasPrefix(path, rootURL+"/"): + path = rootURL + "/dist" + path[len(rootURL):] + default: + path = rootURL + "/dist/" + path + } + } + http.ServeFile(w, r, "/html"+path) + } + + logging.Info("Web UI listening on 0.0.0.0:" + listeningPort + emoji.Sprint(" :ear:")) + + productionRouter := httprouter.New() + productionRouter.HandlerFunc(http.MethodGet, rootURL+"/", handler) + healthcheckRouter := healthcheck.CreateRouter(func() error { return nil }) + serverErrs := server.RunServers( + server.Settings{Name: "production", Addr: "0.0.0.0:" + listeningPort, Handler: productionRouter}, + server.Settings{Name: "healthcheck", Addr: "127.0.0.1:9999", Handler: healthcheckRouter}, + ) + for _, err := range serverErrs { + if err != nil { + logging.Err(err) + } + } + if len(serverErrs) > 0 { + logging.Errorf("%v", serverErrs) + os.Exit(1) + } +}