2020-11-23 13:58:19 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
|
|
_ "github.com/joho/godotenv/autoload"
|
|
|
|
"github.com/rs/cors"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"gosrc.io/xmpp"
|
|
|
|
|
|
|
|
"github.com/sour-is/keyproofs/pkg/cache"
|
|
|
|
"github.com/sour-is/keyproofs/pkg/config"
|
2020-11-23 20:49:53 -07:00
|
|
|
"github.com/sour-is/keyproofs/pkg/graceful"
|
2020-12-04 11:36:24 -07:00
|
|
|
"github.com/sour-is/keyproofs/pkg/httpsrv"
|
|
|
|
|
|
|
|
app_avatar "github.com/sour-is/keyproofs/pkg/app/avatar"
|
|
|
|
app_dns "github.com/sour-is/keyproofs/pkg/app/dns"
|
|
|
|
app_keyproofs "github.com/sour-is/keyproofs/pkg/app/keyproofs"
|
|
|
|
app_vcard "github.com/sour-is/keyproofs/pkg/app/vcard"
|
|
|
|
app_wkd "github.com/sour-is/keyproofs/pkg/app/wkd"
|
2020-11-23 13:58:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-12-04 11:36:24 -07:00
|
|
|
// AppName Application Name
|
|
|
|
AppName string = "KeyProofs"
|
|
|
|
|
2020-11-23 13:58:19 -07:00
|
|
|
// AppVersion Application Version Number
|
|
|
|
AppVersion string
|
|
|
|
|
|
|
|
// AppBuild Application Build Hash
|
|
|
|
BuildHash string
|
|
|
|
|
|
|
|
// AppDate Application Build Date
|
|
|
|
BuildDate string
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-11-23 20:49:53 -07:00
|
|
|
log := zerolog.New(zerolog.NewConsoleWriter()).
|
|
|
|
With().
|
|
|
|
Timestamp().
|
|
|
|
Caller().
|
|
|
|
Logger()
|
2020-11-23 13:58:19 -07:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = log.WithContext(ctx)
|
2020-11-23 20:49:53 -07:00
|
|
|
ctx = graceful.WithInterupt(ctx)
|
|
|
|
ctx, _ = graceful.WithWaitGroup(ctx)
|
2020-11-23 13:58:19 -07:00
|
|
|
|
|
|
|
cfg := config.New()
|
2020-12-04 11:36:24 -07:00
|
|
|
cfg.Set("app-name", AppName)
|
2020-11-23 13:58:19 -07:00
|
|
|
cfg.Set("app-version", AppVersion)
|
|
|
|
cfg.Set("build-hash", BuildHash)
|
|
|
|
cfg.Set("build-date", BuildDate)
|
|
|
|
ctx = cfg.Apply(ctx)
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
log.Info().
|
|
|
|
Str("app", AppName).
|
|
|
|
Str("version", AppVersion).
|
|
|
|
Str("build-hash", BuildHash).
|
|
|
|
Str("build-date", BuildDate).
|
|
|
|
Msg("startup...")
|
|
|
|
|
2020-11-23 13:58:19 -07:00
|
|
|
if err := run(ctx); err != nil {
|
2020-12-04 11:36:24 -07:00
|
|
|
log.Error().Err(err).Msg("Application Failed")
|
2020-11-23 13:58:19 -07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(ctx context.Context) error {
|
|
|
|
log := log.Ctx(ctx)
|
2020-11-23 20:49:53 -07:00
|
|
|
wg := graceful.WaitGroup(ctx)
|
2020-12-04 11:36:24 -07:00
|
|
|
cfg := config.FromContext(ctx)
|
2020-11-23 13:58:19 -07:00
|
|
|
|
|
|
|
// derive baseURL from listener options
|
|
|
|
listen := env("HTTP_LISTEN", ":9061")
|
|
|
|
host, _ := os.Hostname()
|
|
|
|
if strings.HasPrefix(listen, ":") {
|
|
|
|
host += listen
|
|
|
|
}
|
|
|
|
baseURL := fmt.Sprintf("http://%s", host)
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
// Setup router
|
|
|
|
cors := cors.New(cors.Options{
|
|
|
|
AllowCredentials: true,
|
|
|
|
AllowedMethods: strings.Fields(env("CORS_METHODS", "GET")),
|
|
|
|
AllowedOrigins: strings.Fields(env("CORS_ORIGIN", "*")),
|
2020-11-23 13:58:19 -07:00
|
|
|
})
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
logFmt := &middleware.DefaultLogFormatter{Logger: accessLog(log.Info)}
|
|
|
|
|
2020-11-23 13:58:19 -07:00
|
|
|
mux := chi.NewRouter()
|
|
|
|
mux.Use(
|
|
|
|
middleware.RequestID,
|
|
|
|
middleware.RealIP,
|
|
|
|
middleware.Recoverer,
|
2020-12-04 11:36:24 -07:00
|
|
|
middleware.RequestLogger(logFmt),
|
|
|
|
secHeaders,
|
|
|
|
cors.Handler,
|
|
|
|
addLogger(log),
|
|
|
|
cfg.ApplyHTTP,
|
2020-11-23 13:58:19 -07:00
|
|
|
)
|
|
|
|
|
2020-11-28 15:11:49 -07:00
|
|
|
if env("DISABLE_KEYPROOF", "false") == "false" {
|
2020-12-04 11:36:24 -07:00
|
|
|
// Set config values
|
|
|
|
cfg.Set("base-url", env("BASE_URL", baseURL))
|
|
|
|
cfg.Set("dns-url", env("DNS_URL", baseURL))
|
|
|
|
cfg.Set("xmpp-url", env("XMPP_URL", baseURL))
|
|
|
|
|
|
|
|
cfg.Set("reddit.api-key", os.Getenv("REDDIT_APIKEY"))
|
|
|
|
cfg.Set("reddit.secret", os.Getenv("REDDIT_SECRET"))
|
|
|
|
cfg.Set("github.secret", os.Getenv("GITHUB_SECRET"))
|
|
|
|
|
2020-11-28 15:11:49 -07:00
|
|
|
// Create cache for promise engine
|
|
|
|
arc, _ := lru.NewARC(4096)
|
|
|
|
c := cache.New(arc)
|
2020-12-04 11:36:24 -07:00
|
|
|
app_keyproofs.NewKeyProofApp(ctx, c).Routes(mux)
|
2020-11-28 15:11:49 -07:00
|
|
|
}
|
2020-11-23 20:49:53 -07:00
|
|
|
|
2020-11-28 15:11:49 -07:00
|
|
|
if env("DISABLE_DNS", "false") == "false" {
|
2020-12-04 11:36:24 -07:00
|
|
|
app_dns.New(ctx).Routes(mux)
|
2020-11-23 13:58:19 -07:00
|
|
|
}
|
|
|
|
|
2020-11-28 15:11:49 -07:00
|
|
|
if env("DISABLE_AVATAR", "false") == "false" {
|
2020-12-04 11:36:24 -07:00
|
|
|
app, err := app_avatar.New(ctx, env("AVATAR_PATH", "pub"))
|
2020-11-28 15:11:49 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
app.Routes(mux)
|
2020-11-28 15:11:49 -07:00
|
|
|
}
|
|
|
|
|
2020-12-03 12:32:24 -07:00
|
|
|
if env("DISABLE_WKD", "false") == "false" {
|
2020-12-04 11:36:24 -07:00
|
|
|
app, err := app_wkd.New(ctx, env("WKD_PATH", "pub"), env("WKD_DOMAIN", "pub"))
|
2020-12-03 12:32:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
app.Routes(mux)
|
2020-12-03 12:32:24 -07:00
|
|
|
}
|
|
|
|
|
2020-11-28 15:11:49 -07:00
|
|
|
if env("DISABLE_VCARD", "false") == "false" {
|
2020-12-04 11:36:24 -07:00
|
|
|
app, err := app_vcard.New(ctx, &xmpp.Config{
|
|
|
|
Jid: os.Getenv("XMPP_USERNAME"),
|
|
|
|
Credential: xmpp.Password(os.Getenv("XMPP_PASSWORD")),
|
|
|
|
})
|
2020-11-28 15:11:49 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-04 11:36:24 -07:00
|
|
|
app.Routes(mux)
|
2020-11-28 15:11:49 -07:00
|
|
|
}
|
2020-11-23 13:58:19 -07:00
|
|
|
|
|
|
|
log.Info().
|
|
|
|
Str("listen", listen).
|
2020-12-03 12:32:24 -07:00
|
|
|
Int("user", os.Geteuid()).
|
|
|
|
Int("group", os.Getgid()).
|
2020-12-04 11:36:24 -07:00
|
|
|
Msg("running")
|
2020-11-23 13:58:19 -07:00
|
|
|
|
2020-12-04 11:36:24 -07:00
|
|
|
err := httpsrv.New(&http.Server{
|
2020-11-23 13:58:19 -07:00
|
|
|
Addr: listen,
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
Handler: mux,
|
|
|
|
}).Run(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-23 20:49:53 -07:00
|
|
|
return wg.Wait(5 * time.Second)
|
2020-11-23 13:58:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func env(name, defaultValue string) string {
|
|
|
|
if value := os.Getenv(name); value != "" {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
2020-11-23 20:49:53 -07:00
|
|
|
func secHeaders(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("X-XSS-Protection", "1; mode=block")
|
|
|
|
w.Header().Set("X-Frame-Options", "DENY")
|
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
w.Header().Set("Content-Security-Policy", "font-src https://pagecdn.io")
|
2020-11-23 13:58:19 -07:00
|
|
|
|
2020-11-23 20:49:53 -07:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2020-11-23 13:58:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type accessLog func() *zerolog.Event
|
|
|
|
|
|
|
|
func (a accessLog) Print(v ...interface{}) {
|
|
|
|
a().Msg(fmt.Sprint(v...))
|
|
|
|
}
|
2020-12-04 11:36:24 -07:00
|
|
|
|
|
|
|
func addLogger(log *zerolog.Logger) func(next http.Handler) http.Handler {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r = r.WithContext(log.WithContext(r.Context()))
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|