parent
66ae17527f
commit
424b3bb932
@ -1,9 +1,8 @@
|
|||||||
module github.com/qdm12/stackedit-docker
|
module github.com/qdm12/stackedit-docker
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/julienschmidt/httprouter v1.3.0
|
github.com/kyokomi/emoji v2.2.4+incompatible
|
||||||
github.com/kyokomi/emoji v1.5.1
|
github.com/qdm12/golibs v0.0.0-20200528010515-765b7cd4f0db
|
||||||
github.com/qdm12/golibs v0.0.0-20200108212314-82caecb70123
|
|
||||||
)
|
)
|
||||||
|
|
||||||
go 1.13
|
go 1.14
|
||||||
|
@ -1,121 +1,157 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"context"
|
||||||
"fmt"
|
"encoding/json"
|
||||||
"net/http"
|
"fmt"
|
||||||
"os"
|
"net/http"
|
||||||
"strings"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"github.com/kyokomi/emoji"
|
"strings"
|
||||||
"github.com/qdm12/golibs/healthcheck"
|
"syscall"
|
||||||
"github.com/qdm12/golibs/logging"
|
|
||||||
"github.com/qdm12/golibs/params"
|
"github.com/kyokomi/emoji"
|
||||||
"github.com/qdm12/golibs/server"
|
"github.com/qdm12/golibs/healthcheck"
|
||||||
)
|
"github.com/qdm12/golibs/logging"
|
||||||
|
"github.com/qdm12/golibs/params"
|
||||||
const (
|
"github.com/qdm12/golibs/server"
|
||||||
defaultConf = `{"dropboxAppKey":"","dropboxAppKeyFull":"","githubClientId":"","googleClientId":"","googleApiKey":"","wordpressClientId":"","allowSponsorship":true}`
|
)
|
||||||
)
|
|
||||||
|
func main() {
|
||||||
func main() {
|
ctx := context.Background()
|
||||||
if healthcheck.Mode(os.Args) {
|
os.Exit(_main(ctx, os.Args))
|
||||||
if err := healthcheck.Query(); err != nil {
|
}
|
||||||
logging.Err(err)
|
|
||||||
os.Exit(1)
|
func _main(ctx context.Context, args []string) int {
|
||||||
}
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
os.Exit(0)
|
defer cancel()
|
||||||
}
|
|
||||||
fmt.Println("#####################################")
|
if healthcheck.Mode(args) {
|
||||||
fmt.Println("########## StackEdit Server #########")
|
if err := healthcheck.Query(); err != nil {
|
||||||
fmt.Println("########## by Quentin McGaw #########")
|
fmt.Println(err)
|
||||||
fmt.Println("########## Give some " + emoji.Sprint(":heart:") + " at ##########")
|
return 1
|
||||||
fmt.Println("# github.com/qdm12/stackedit-docker #")
|
}
|
||||||
fmt.Print("#####################################\n\n")
|
return 0
|
||||||
envParams := params.NewEnvParams()
|
}
|
||||||
logging.InitLogger("console", logging.InfoLevel, 0)
|
|
||||||
listeningPort, err := envParams.GetListeningPort(params.Default("8000"))
|
fmt.Println("#####################################")
|
||||||
if err != nil {
|
fmt.Println("########## StackEdit Server #########")
|
||||||
logging.Err(err)
|
fmt.Println("########## by Quentin McGaw #########")
|
||||||
os.Exit(1)
|
fmt.Println("########## Give some " + emoji.Sprint(":heart:") + " at ##########")
|
||||||
}
|
fmt.Println("# github.com/qdm12/stackedit-docker #")
|
||||||
logging.Infof("Using internal listening port %s", listeningPort)
|
fmt.Print("#####################################\n\n")
|
||||||
rootURL, err := envParams.GetRootURL(params.Default("/"))
|
envParams := params.NewEnvParams()
|
||||||
if err != nil {
|
logger, err := logging.NewLogger(logging.ConsoleEncoding, logging.InfoLevel, -1)
|
||||||
logging.Err(err)
|
if err != nil {
|
||||||
os.Exit(1)
|
fmt.Println(err)
|
||||||
}
|
return 1
|
||||||
logging.Infof("Using root URL %q", rootURL)
|
}
|
||||||
|
|
||||||
productionRouter := http.NewServeMux()
|
listeningPort, warning, err := envParams.GetListeningPort(params.Default("8000"))
|
||||||
productionRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
if len(warning) > 0 {
|
||||||
urlStackeditPath := strings.TrimPrefix(r.URL.Path, rootURL)
|
logger.Warn(warning)
|
||||||
filepath := "/dist/" + urlStackeditPath
|
}
|
||||||
switch urlStackeditPath {
|
if err != nil {
|
||||||
case "/conf", "/app/conf":
|
logger.Error(err)
|
||||||
w.Write(getAllStackeditEnv())
|
return 1
|
||||||
return
|
}
|
||||||
case "/":
|
logger.Info("Using internal listening port %s", listeningPort)
|
||||||
filepath = "/static/landing/"
|
|
||||||
case "/sitemap.xml":
|
rootURL, err := envParams.GetRootURL(params.Default("/"))
|
||||||
filepath = "/static/sitemap.xml"
|
if err != nil {
|
||||||
case "/oauth2/callback":
|
logger.Error(err)
|
||||||
filepath = "/static/oauth2/callback.html"
|
return 1
|
||||||
case "/app", "/app/":
|
}
|
||||||
filepath = "/dist/index.html"
|
logger.Info("Using root URL %q", rootURL)
|
||||||
default:
|
|
||||||
switch {
|
productionRouter := http.NewServeMux()
|
||||||
case strings.HasPrefix(urlStackeditPath, "/static/css/static/fonts/"):
|
productionRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
filepath = "/dist/" + strings.TrimPrefix(urlStackeditPath, "/static/css/")
|
urlStackeditPath := strings.TrimPrefix(r.URL.Path, rootURL)
|
||||||
case strings.HasPrefix(urlStackeditPath, "/app/static/css/static/fonts/"):
|
filepath := "/dist/" + urlStackeditPath
|
||||||
filepath = "/dist/" + strings.TrimPrefix(urlStackeditPath, "/app/static/css/")
|
switch urlStackeditPath {
|
||||||
case strings.HasPrefix(urlStackeditPath, "/app/"):
|
case "/conf", "/app/conf":
|
||||||
filepath = "/dist/" + strings.TrimPrefix(urlStackeditPath, "/app/")
|
bytes := getAllStackeditEnv()
|
||||||
}
|
if _, err := w.Write(bytes); err != nil {
|
||||||
}
|
logger.Error(err)
|
||||||
http.ServeFile(w, r, "/html"+filepath)
|
}
|
||||||
})
|
return
|
||||||
healthcheckRouter := healthcheck.CreateRouter(func() error { return nil })
|
case "/":
|
||||||
serverErrs := server.RunServers(
|
filepath = "/static/landing/"
|
||||||
server.Settings{Name: "production", Addr: "0.0.0.0:" + listeningPort, Handler: productionRouter},
|
case "/sitemap.xml":
|
||||||
server.Settings{Name: "healthcheck", Addr: "127.0.0.1:9999", Handler: healthcheckRouter},
|
filepath = "/static/sitemap.xml"
|
||||||
)
|
case "/oauth2/callback":
|
||||||
for _, err := range serverErrs {
|
filepath = "/static/oauth2/callback.html"
|
||||||
if err != nil {
|
case "/app", "/app/":
|
||||||
logging.Err(err)
|
filepath = "/dist/index.html"
|
||||||
}
|
default:
|
||||||
}
|
switch {
|
||||||
if len(serverErrs) > 0 {
|
case strings.HasPrefix(urlStackeditPath, "/static/css/static/fonts/"):
|
||||||
logging.Errorf("%v", serverErrs)
|
filepath = "/dist/" + strings.TrimPrefix(urlStackeditPath, "/static/css/")
|
||||||
os.Exit(1)
|
case strings.HasPrefix(urlStackeditPath, "/app/static/css/static/fonts/"):
|
||||||
}
|
filepath = "/dist/" + strings.TrimPrefix(urlStackeditPath, "/app/static/css/")
|
||||||
}
|
case strings.HasPrefix(urlStackeditPath, "/app/"):
|
||||||
|
filepath = "/dist/" + strings.TrimPrefix(urlStackeditPath, "/app/")
|
||||||
// Returns all stackedit env values as JSON
|
}
|
||||||
func getAllStackeditEnv() []byte {
|
}
|
||||||
env := struct {
|
http.ServeFile(w, r, "/html"+filepath)
|
||||||
DropboxAppKey string `json:"dropboxAppKey"`
|
})
|
||||||
DropboxAppKeyFull string `json:"dropboxAppKeyFull"`
|
healthcheckHandlerFunc := healthcheck.GetHandler(func() error { return nil })
|
||||||
GithubClientID string `json:"githubClientId"`
|
serverErrors := make(chan []error)
|
||||||
GithubClientSecret string `json:"githubClientSecret"`
|
go func() {
|
||||||
GoogleClientID string `json:"googleClientId"`
|
serverErrors <- server.RunServers(ctx,
|
||||||
GoogleAPIKey string `json:"googleApiKey"`
|
server.Settings{Name: "production", Addr: "0.0.0.0:" + listeningPort, Handler: productionRouter},
|
||||||
WordpressClientID string `json:"wordpressClientId"`
|
server.Settings{Name: "healthcheck", Addr: "127.0.0.1:9999", Handler: healthcheckHandlerFunc},
|
||||||
PaypalReceiverEmail string `json:"paypalReceiverEmail"`
|
)
|
||||||
AllowSponsorship bool `json:"allowSponsorship"`
|
}()
|
||||||
}{}
|
|
||||||
envParams := params.NewEnvParams()
|
osSignals := make(chan os.Signal, 1)
|
||||||
env.DropboxAppKey, _ = envParams.GetEnv("DROPBOX_APP_KEY")
|
signal.Notify(osSignals,
|
||||||
env.DropboxAppKeyFull, _ = envParams.GetEnv("DROPBOX_APP_KEY_FULL")
|
syscall.SIGINT,
|
||||||
env.GithubClientID, _ = envParams.GetEnv("GITHUB_CLIENT_ID")
|
syscall.SIGTERM,
|
||||||
env.GithubClientSecret, _ = envParams.GetEnv("GITHUB_CLIENT_SECRET")
|
os.Interrupt,
|
||||||
env.GoogleClientID, _ = envParams.GetEnv("GOOGLE_CLIENT_ID")
|
)
|
||||||
env.GoogleAPIKey, _ = envParams.GetEnv("GOOGLE_API_KEY")
|
select {
|
||||||
env.WordpressClientID, _ = envParams.GetEnv("WORDPRESS_CLIENT_ID")
|
case errors := <-serverErrors:
|
||||||
env.PaypalReceiverEmail, _ = envParams.GetEnv("PAYPAL_RECEIVER_EMAIL")
|
for _, err := range errors {
|
||||||
if len(env.PaypalReceiverEmail) > 0 {
|
logger.Error(err)
|
||||||
env.AllowSponsorship = true
|
}
|
||||||
}
|
return 1
|
||||||
b, _ := json.Marshal(env)
|
case signal := <-osSignals:
|
||||||
return b
|
message := fmt.Sprintf("Stopping program: caught OS signal %q", signal)
|
||||||
}
|
logger.Warn(message)
|
||||||
|
return 2
|
||||||
|
case <-ctx.Done():
|
||||||
|
message := fmt.Sprintf("Stopping program: %s", ctx.Err())
|
||||||
|
logger.Warn(message)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns all stackedit env values as JSON
|
||||||
|
func getAllStackeditEnv() []byte {
|
||||||
|
env := struct {
|
||||||
|
DropboxAppKey string `json:"dropboxAppKey"`
|
||||||
|
DropboxAppKeyFull string `json:"dropboxAppKeyFull"`
|
||||||
|
GithubClientID string `json:"githubClientId"`
|
||||||
|
GithubClientSecret string `json:"githubClientSecret"`
|
||||||
|
GoogleClientID string `json:"googleClientId"`
|
||||||
|
GoogleAPIKey string `json:"googleApiKey"`
|
||||||
|
WordpressClientID string `json:"wordpressClientId"`
|
||||||
|
PaypalReceiverEmail string `json:"paypalReceiverEmail"`
|
||||||
|
AllowSponsorship bool `json:"allowSponsorship"`
|
||||||
|
}{}
|
||||||
|
envParams := params.NewEnvParams()
|
||||||
|
env.DropboxAppKey, _ = envParams.GetEnv("DROPBOX_APP_KEY")
|
||||||
|
env.DropboxAppKeyFull, _ = envParams.GetEnv("DROPBOX_APP_KEY_FULL")
|
||||||
|
env.GithubClientID, _ = envParams.GetEnv("GITHUB_CLIENT_ID")
|
||||||
|
env.GithubClientSecret, _ = envParams.GetEnv("GITHUB_CLIENT_SECRET")
|
||||||
|
env.GoogleClientID, _ = envParams.GetEnv("GOOGLE_CLIENT_ID")
|
||||||
|
env.GoogleAPIKey, _ = envParams.GetEnv("GOOGLE_API_KEY")
|
||||||
|
env.WordpressClientID, _ = envParams.GetEnv("WORDPRESS_CLIENT_ID")
|
||||||
|
env.PaypalReceiverEmail, _ = envParams.GetEnv("PAYPAL_RECEIVER_EMAIL")
|
||||||
|
if len(env.PaypalReceiverEmail) > 0 {
|
||||||
|
env.AllowSponsorship = true
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(env)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
Loading…
Reference in new issue