aboutsummaryrefslogblamecommitdiff
path: root/config.go
blob: f8f575e7d5f22b8749355afcc4ccf46616ab12dc (plain) (tree)
1
2
3
4
5
6
7
8
9

            

               
                 
                    

              
 
                                         
                                                      

 
                   

                                                          



                                         
                   


                                         
                               
 

                                                                        
                                  
 
 

                                                                               












                                                                

                                                          





                                                                                
                                                       
                 




                                                            
                                                                                  




                                                                                           





                          
 







                                                                  
 
 
                                                                              
                                     



                                      
 
package main

import (
	"bufio"
	"context"
	"crypto/tls"
	"os"
	"sync"

	"github.com/jackc/pgx/v5/pgxpool"
	"go.lindenii.runxiyu.org/lindenii-common/scfg"
)

var config struct {
	Server_name string            `scfg:"server_name"`
	Routes      map[string]string `scfg:"routes"`
	TLS         struct {
		Cert string `scfg:"cert"`
		Key  string `scfg:"key"`
	} `scfg:"tls"`
	DB struct {
		Type string `scfg:"type"`
		Conn string `scfg:"conn"`
	} `scfg:"db"`
	_tls_config *tls.Config
}
var (
	config_mutex sync.RWMutex // covers things like the database too
	global_db    *pgxpool.Pool
)

// load_config loads the configuration file and sets up global things according
// to the configuration directives.
func load_config(path string) error {
	config_file, err := os.Open(path)
	if err != nil {
		return err
	}
	decoder := scfg.NewDecoder(bufio.NewReader(config_file))
	if func() error {
		config_mutex.Lock()
		defer config_mutex.Unlock()
		err = decoder.Decode(&config)
		if err != nil {
			return err
		}

		// TLS key loading and TLS config creation
		cer, err := tls.LoadX509KeyPair(config.TLS.Cert, config.TLS.Key)
		if err != nil {
			return err
		}
		config._tls_config = &tls.Config{
			Certificates: []tls.Certificate{cer},
			MinVersion:   tls.VersionTLS13,
		}

		// Database setup
		if config.DB.Type != "postgres" {
			return err_unsupported_database_type
		}
		global_db, err = pgxpool.New(context.Background(), config.DB.Conn)
		// BUG: Context-related leak: cancel context when the config is invalidated
		if err != nil {
			return err
		}

		return nil
	}() != nil {
		return err
	}
	return nil
}

// config_fetch_one fetches one value from the configuration.
//
// Consequtive calls do not guarantee a consistent snapshot of the
// configuration. Use config_consistent_run in these cases.
func config_fetch_one[T any](x *T) T {
	config_mutex.RLock()
	defer config_mutex.RUnlock()
	return *x
}

// config_consistent_run runs the supplied function with a consistent snapshot
// of values covered by config_mutex.
func config_consistent_run(f func()) {
	config_mutex.RLock()
	defer config_mutex.RUnlock()
	f()
}