Skip to content
Snippets Groups Projects
Select Git revision
  • 23e363c2c9f0e5d6aee2d18c59c026a8f9217adb
  • ballinvoher default protected
  • client-http-server-for-token
  • master
  • gitlab-auth-issue
  • windows
  • microsoft
  • message
  • azure_auth
  • prometheus
  • permission-templates
  • no-datastore
  • save-public-keys
  • gitlab-group-level-start
  • v1.1.0
  • v1.0.0
  • v0.1
17 results

server.go

Blame
  • server.go 7.60 KiB
    package server
    
    import (
    	"bytes"
    	"crypto/tls"
    	"encoding/base64"
    	"encoding/json"
    	"fmt"
    	"log"
    	"net"
    	"net/http"
    	"os"
    	"time"
    
    	"github.com/gorilla/csrf"
    
    	"github.com/gobuffalo/packr"
    	"github.com/gorilla/handlers"
    	"github.com/prometheus/client_golang/prometheus/promhttp"
    
    	"github.com/gorilla/mux"
    	"github.com/gorilla/sessions"
    	"github.com/pkg/errors"
    
    	"go4.org/wkfs"
    	"golang.org/x/crypto/acme/autocert"
    	"golang.org/x/oauth2"
    
    	wkfscache "github.com/nsheridan/autocert-wkfs-cache"
    	"github.com/nsheridan/cashier/lib"
    	"github.com/nsheridan/cashier/server/auth"
    	"github.com/nsheridan/cashier/server/auth/github"
    	"github.com/nsheridan/cashier/server/auth/gitlab"
    	"github.com/nsheridan/cashier/server/auth/google"
    	"github.com/nsheridan/cashier/server/auth/microsoft"
    	"github.com/nsheridan/cashier/server/config"
    	"github.com/nsheridan/cashier/server/metrics"
    	"github.com/nsheridan/cashier/server/signer"
    	"github.com/nsheridan/cashier/server/store"
    	"github.com/sid77/drop"
    )
    
    func loadCerts(certFile, keyFile string) (tls.Certificate, error) {
    	key, err := wkfs.ReadFile(keyFile)
    	if err != nil {
    		return tls.Certificate{}, errors.Wrap(err, "error reading TLS private key")
    	}
    	cert, err := wkfs.ReadFile(certFile)
    	if err != nil {
    		return tls.Certificate{}, errors.Wrap(err, "error reading TLS certificate")
    	}
    	return tls.X509KeyPair(cert, key)
    }
    
    // Run the server.
    func Run(conf *config.Config) {
    	var err error
    
    	laddr := fmt.Sprintf("%s:%d", conf.Server.Addr, conf.Server.Port)
    	l, err := net.Listen("tcp", laddr)
    	if err != nil {
    		log.Fatal(errors.Wrapf(err, "unable to listen on %s:%d", conf.Server.Addr, conf.Server.Port))
    	}
    
    	tlsConfig := &tls.Config{}
    	if conf.Server.UseTLS {
    		if conf.Server.LetsEncryptServername != "" {
    			m := autocert.Manager{
    				Prompt:     autocert.AcceptTOS,
    				HostPolicy: autocert.HostWhitelist(conf.Server.LetsEncryptServername),