Files
hetty/cmd/main.go

101 lines
2.6 KiB
Go
Raw Normal View History

2019-11-17 09:54:15 +01:00
package main
import (
"crypto/tls"
2019-11-24 00:14:49 +01:00
"crypto/x509"
"flag"
2019-11-17 09:54:15 +01:00
"log"
2019-12-01 14:07:12 +01:00
"net"
2019-11-17 15:09:37 +01:00
"net/http"
2019-11-30 12:43:55 +01:00
"net/http/httputil"
"net/url"
2019-12-01 14:07:12 +01:00
"os"
"strings"
2019-11-24 17:30:39 +01:00
2019-12-01 14:47:25 +01:00
"github.com/dstotijn/gurp/pkg/api"
2019-11-30 12:43:55 +01:00
"github.com/dstotijn/gurp/pkg/proxy"
2019-12-01 14:07:12 +01:00
"github.com/dstotijn/gurp/pkg/reqlog"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
2019-11-30 12:43:55 +01:00
"github.com/gorilla/mux"
2019-11-17 09:54:15 +01:00
)
2019-11-24 00:14:49 +01:00
var (
caCertFile = flag.String("cert", "", "CA certificate file path")
caKeyFile = flag.String("key", "", "CA private key file path")
2019-11-30 12:43:55 +01:00
dev = flag.Bool("dev", false, "Run in development mode")
adminPath = flag.String("adminPath", "", "File path to admin build")
2019-11-24 00:14:49 +01:00
)
2019-11-17 09:54:15 +01:00
func main() {
2019-11-24 00:14:49 +01:00
flag.Parse()
tlsCA, err := tls.LoadX509KeyPair(*caCertFile, *caKeyFile)
if err != nil {
log.Fatalf("[FATAL] Could not load CA key pair: %v", err)
}
caCert, err := x509.ParseCertificate(tlsCA.Certificate[0])
if err != nil {
log.Fatalf("[FATAL] Could not parse CA: %v", err)
}
reqLogService := reqlog.NewService()
2019-12-01 14:07:12 +01:00
p, err := proxy.NewProxy(caCert, tlsCA.PrivateKey)
2019-11-24 00:14:49 +01:00
if err != nil {
log.Fatalf("[FATAL] Could not create Proxy: %v", err)
}
2019-11-17 09:54:15 +01:00
p.UseRequestModifier(reqLogService.RequestModifier)
p.UseResponseModifier(reqLogService.ResponseModifier)
2019-12-01 14:07:12 +01:00
var adminHandler http.Handler
2019-11-30 12:43:55 +01:00
if *dev {
adminURL, err := url.Parse("http://localhost:3000")
if err != nil {
log.Fatalf("[FATAL] Invalid admin URL: %v", err)
}
2019-12-01 14:07:12 +01:00
adminHandler = httputil.NewSingleHostReverseProxy(adminURL)
2019-11-30 12:43:55 +01:00
} else {
if *adminPath == "" {
log.Fatal("[FATAL] `adminPath` must be set")
}
2019-12-01 14:07:12 +01:00
adminHandler = http.FileServer(http.Dir(*adminPath))
2019-11-30 12:43:55 +01:00
}
2019-12-01 14:07:12 +01:00
router := mux.NewRouter().SkipClean(true)
adminRouter := router.MatcherFunc(func(req *http.Request, match *mux.RouteMatch) bool {
hostname, _ := os.Hostname()
host, _, _ := net.SplitHostPort(req.Host)
return strings.EqualFold(host, hostname) || req.Host == "gurp.proxy"
2019-12-01 14:47:25 +01:00
}).Subrouter()
2019-12-01 14:07:12 +01:00
// GraphQL server.
adminRouter.Path("/api/playground").Handler(playground.Handler("GraphQL Playground", "/api/graphql"))
adminRouter.Path("/api/graphql").Handler(handler.NewDefaultServer(api.NewExecutableSchema(api.Config{Resolvers: &api.Resolver{
RequestLogService: &reqLogService,
}})))
2019-12-01 14:07:12 +01:00
// Admin interface.
2019-12-01 14:47:25 +01:00
adminRouter.PathPrefix("").Handler(adminHandler)
2019-12-01 14:07:12 +01:00
// Fallback (default) is the Proxy handler.
2019-11-30 12:43:55 +01:00
router.PathPrefix("").Handler(p)
2019-11-17 15:09:37 +01:00
s := &http.Server{
Addr: ":8080",
2019-11-30 12:43:55 +01:00
Handler: router,
2019-11-17 15:09:37 +01:00
TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){}, // Disable HTTP/2
2019-11-17 09:54:15 +01:00
}
log.Println("[INFO] Running server on :8080 ...")
2019-11-24 00:14:49 +01:00
err = s.ListenAndServe()
2019-11-17 15:09:37 +01:00
if err != nil && err != http.ErrServerClosed {
2019-11-24 00:14:49 +01:00
log.Fatalf("[FATAL] HTTP server closed: %v", err)
2019-11-17 09:54:15 +01:00
}
}