his: Split up ui and api interface to prevent certificate popup in ui

master
Bas Kloosterman 2 years ago
parent 7a3db7c9b2
commit 91eb3c1850
  1. 2
      docker-compose.yaml
  2. 41
      his/main.go
  3. 8
      his/openapiclient.go
  4. 72
      his/srv.go

@ -5,7 +5,7 @@ services:
container_name: okapi_his container_name: okapi_his
restart: always restart: always
environment: environment:
EXT_ADDR: "https://okapi_his:8084" EXT_ADDR: "https://okapi_his"
volumes: volumes:
- "./his/data:/data" - "./his/data:/data"
- "./his/certs:/certs" - "./his/certs:/certs"

@ -2,41 +2,62 @@ package main
import ( import (
"context" "context"
"fmt"
"log" "log"
"net/http"
"os" "os"
"os/signal" "os/signal"
"sync" "sync"
) )
var localAddr = "0.0.0.0:8084" var apiPort = "8083"
var externalAddr = "https://localhost:8084"
var apiAddr = "0.0.0.0:" + apiPort
var uiAddr = "0.0.0.0:8084"
var externalApiAddr = "https://localhost:" + apiPort
func main() { func main() {
stop := make(chan os.Signal, 1) stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt) signal.Notify(stop, os.Interrupt)
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
if os.Getenv("LOCAL_ADDR") != "" { if os.Getenv("UI_ADDR") != "" {
localAddr = os.Getenv("LOCAL_ADDR") uiAddr = os.Getenv("UI_ADDR")
} }
if os.Getenv("EXT_ADDR") != "" { if os.Getenv("EXT_ADDR") != "" {
externalAddr = os.Getenv("EXT_ADDR") externalApiAddr = "https://" + os.Getenv("EXT_ADDR") + ":" + apiPort
} }
srv := NewServer(localAddr) srv := NewServer(apiAddr, uiAddr)
srv.LoadData("./data/data.db") srv.LoadData("./data/data.db")
wg.Add(1)
go func() { go func() {
wg.Add(1) if err := srv.ListenAndServeUI(); err != nil && err != http.ErrServerClosed {
srv.ListenAndServe() panic(fmt.Errorf("err listenAndServe ui: %v", err))
}
}()
wg.Add(1)
go func() {
if err := srv.ListenAndServeAPI(); err != nil && err != http.ErrServerClosed {
panic(fmt.Errorf("err listenAndServe api: %v", err))
}
}() }()
<-stop <-stop
go func() { go func() {
log.Println("Shutdown server...") log.Println("Shutdown api server...")
srv.Shutdown(context.Background()) srv.ShutdownAPI(context.Background())
wg.Done()
log.Println("Server.shutdown...")
}()
go func() {
log.Println("Shutdown ui server...")
srv.ShutdownUI(context.Background())
wg.Done() wg.Done()
log.Println("Server.shutdown...") log.Println("Server.shutdown...")
}() }()

@ -240,7 +240,7 @@ func (srv *HISServer) enableService(serviceProvider *model.ServiceProvider, serv
Fetch: &okapi.CallbackConfiguration{ Fetch: &okapi.CallbackConfiguration{
Protocol: "https://whiteboxsystems.nl/protospecs/whitebox-fetch/http", Protocol: "https://whiteboxsystems.nl/protospecs/whitebox-fetch/http",
Configuration: toStruct(map[string]interface{}{ Configuration: toStruct(map[string]interface{}{
"url": externalAddr + "/external/api", "url": externalApiAddr + "/external/api",
}), }),
Auth: &okapi.ProtocolAuthConfiguration{ Auth: &okapi.ProtocolAuthConfiguration{
Method: sharedmodel.AuthMethodDecozoMTLS, Method: sharedmodel.AuthMethodDecozoMTLS,
@ -249,7 +249,7 @@ func (srv *HISServer) enableService(serviceProvider *model.ServiceProvider, serv
Push: &okapi.CallbackConfiguration{ Push: &okapi.CallbackConfiguration{
Protocol: "https://whiteboxsystems.nl/protospecs/whitebox-push/http", Protocol: "https://whiteboxsystems.nl/protospecs/whitebox-push/http",
Configuration: toStruct(map[string]interface{}{ Configuration: toStruct(map[string]interface{}{
"url": externalAddr + "/external/api", "url": externalApiAddr + "/external/api",
}), }),
Auth: &okapi.ProtocolAuthConfiguration{ Auth: &okapi.ProtocolAuthConfiguration{
Method: sharedmodel.AuthMethodDecozoMTLS, Method: sharedmodel.AuthMethodDecozoMTLS,
@ -262,7 +262,7 @@ func (srv *HISServer) enableService(serviceProvider *model.ServiceProvider, serv
Fetch: &okapi.CallbackConfiguration{ Fetch: &okapi.CallbackConfiguration{
Protocol: "https://hl7.org/fhir", Protocol: "https://hl7.org/fhir",
Configuration: toStruct(map[string]interface{}{ Configuration: toStruct(map[string]interface{}{
"url": externalAddr + "/external/fhir/Patient", "url": externalApiAddr + "/external/fhir/Patient",
}), }),
Auth: &okapi.ProtocolAuthConfiguration{ Auth: &okapi.ProtocolAuthConfiguration{
Method: sharedmodel.AuthMethodDecozoBearerToken, Method: sharedmodel.AuthMethodDecozoBearerToken,
@ -271,7 +271,7 @@ func (srv *HISServer) enableService(serviceProvider *model.ServiceProvider, serv
Push: &okapi.CallbackConfiguration{ Push: &okapi.CallbackConfiguration{
Protocol: "https://hl7.org/fhir", Protocol: "https://hl7.org/fhir",
Configuration: toStruct(map[string]interface{}{ Configuration: toStruct(map[string]interface{}{
"url": externalAddr + "/external/fhir/Patient", "url": externalApiAddr + "/external/fhir/Patient",
}), }),
Auth: &okapi.ProtocolAuthConfiguration{ Auth: &okapi.ProtocolAuthConfiguration{
Method: sharedmodel.AuthMethodDecozoBearerToken, Method: sharedmodel.AuthMethodDecozoBearerToken,

@ -66,8 +66,10 @@ func loadKeyPair() credentials.TransportCredentials {
} }
type HISServer struct { type HISServer struct {
srv *http.Server apiSrv *http.Server
inited bool apiInited bool
uiSrv *http.Server
uiInited bool
data *gorm.DB data *gorm.DB
stopTasks chan struct{} stopTasks chan struct{}
clientCert tls.Certificate clientCert tls.Certificate
@ -80,27 +82,51 @@ func (srv *HISServer) LoadData(location string) error {
} }
func (srv *HISServer) Addr() string { func (srv *HISServer) Addr() string {
if srv.srv == nil { if srv.apiSrv == nil {
return "" return ""
} }
return srv.srv.Addr return srv.apiSrv.Addr
} }
func (srv *HISServer) ListenAndServe() { func (srv *HISServer) ListenAndServeAPI() error {
if !srv.inited {
srv.init() if !srv.apiInited {
srv.initAPI()
}
log.Printf("API Listening on %v\n", srv.apiSrv.Addr)
return srv.apiSrv.ListenAndServeTLS("", "")
}
func (srv *HISServer) ListenAndServeUI() error {
if !srv.uiInited {
srv.initUI()
} }
log.Println("Listening on %v", srv.srv.Addr) log.Printf("UI Listening on %v\n", srv.uiSrv.Addr)
srv.srv.ListenAndServeTLS("", "") return srv.uiSrv.ListenAndServeTLS("", "")
}
func (srv *HISServer) ShutdownAPI(ctx context.Context) (error, error) {
return srv.apiSrv.Shutdown(ctx), srv.uiSrv.Shutdown(ctx)
}
func (srv *HISServer) ShutdownUI(ctx context.Context) (error, error) {
return srv.uiSrv.Shutdown(ctx), srv.uiSrv.Shutdown(ctx)
} }
func (srv *HISServer) Shutdown(ctx context.Context) error { func (srv *HISServer) initAPI() {
return srv.srv.Shutdown(ctx) srv.apiInited = true
r := srv.apiSrv.Handler.(*gin.Engine)
r.Use(srv.Authenticate)
r.GET("/external/api/patients/:id", srv.GetPatient)
r.GET("/external/fhir/Patient", srv.GetFHIRPatient)
} }
func (srv *HISServer) init() { func (srv *HISServer) initUI() {
r := srv.srv.Handler.(*gin.Engine) srv.uiInited = true
r := srv.uiSrv.Handler.(*gin.Engine)
r.LoadHTMLGlob("templates/*") r.LoadHTMLGlob("templates/*")
r.Static("/assets", "./assets") r.Static("/assets", "./assets")
@ -121,11 +147,6 @@ func (srv *HISServer) init() {
r.GET("/api/services", srv.GetServices) r.GET("/api/services", srv.GetServices)
r.POST("/api/services/:id/subscriptions", srv.UpdateSubscription) r.POST("/api/services/:id/subscriptions", srv.UpdateSubscription)
r.Use(srv.Authenticate)
r.GET("/external/api/patients/:id", srv.GetPatient)
r.GET("/external/fhir/Patient", srv.GetFHIRPatient)
srv.inited = true
ticker := time.NewTicker(30 * time.Second) ticker := time.NewTicker(30 * time.Second)
srv.stopTasks = make(chan struct{}) srv.stopTasks = make(chan struct{})
srv.TaskSyncPatients() srv.TaskSyncPatients()
@ -612,11 +633,11 @@ func (srv *HISServer) GetFHIRPatient(c *gin.Context) {
c.JSON(404, nil) c.JSON(404, nil)
} }
func NewServer(addr string) *HISServer { func NewServer(apiAddr, uiAddr string) *HISServer {
cert := loadCert() cert := loadCert()
srv := &HISServer{ srv := &HISServer{
srv: &http.Server{ apiSrv: &http.Server{
Addr: addr, Addr: apiAddr,
Handler: gin.Default(), Handler: gin.Default(),
TLSConfig: &tls.Config{ TLSConfig: &tls.Config{
ClientAuth: tls.RequestClientCert, ClientAuth: tls.RequestClientCert,
@ -625,6 +646,15 @@ func NewServer(addr string) *HISServer {
}, },
}, },
}, },
uiSrv: &http.Server{
Addr: uiAddr,
Handler: gin.Default(),
TLSConfig: &tls.Config{
GetCertificate: func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
return cert, nil
},
},
},
clientCert: *cert, clientCert: *cert,
} }

Loading…
Cancel
Save