github.com/acornpublishing/functional-programming-go@v0.0.0-20220401005601-c3bd3786d5a1/Chapter06/04_onion/main.go (about)

     1  package main
     2  
     3  import (
     4  	. "utils"
     5  	. "interfaces"
     6  	"os"
     7  	"io/ioutil"
     8  	"infrastructure"
     9  	"github.com/pkg/errors"
    10  	"net/http"
    11  )
    12  
    13  const defaultFileName = "eventset1.jsonl"
    14  var	fileName	string
    15  var wsh WebserviceHandler
    16  
    17  func init() {
    18  	GetOptions()
    19  	if Config.LogDebugInfo {
    20  		InitLog("trace-debug-log.txt", os.Stdout, os.Stdout, os.Stderr)
    21  	} else {
    22  		InitLog("trace-log.txt", ioutil.Discard, os.Stdout, os.Stderr)
    23  	}
    24  	// use a filename in a downloads subdirectory
    25  	fileName = os.Getenv("TEST_FILENAME")
    26  	if len(fileName) == 0 {
    27  		fileName = defaultFileName  // CloudflareLogFilename(time.Now())
    28  	}
    29  	Debug.Printf("ProjectRoot: %s", PadRight(Config.ProjectRoot, " ", 20))
    30  	Debug.Printf("AppEnv: %s", PadRight(Config.AppEnv, " ", 20))
    31  	Debug.Printf("GcpSourceKeyFile: %s", PadRight(Config.GcpSourceKeyFile, " ", 20))
    32  	Debug.Printf("GcpSinkKeyFile: %s", PadRight(Config.GcpSinkKeyFile, " ", 20))
    33  	Debug.Printf("LogDebugInfo: %v", Config.LogDebugInfo)
    34  	HandlePanic(os.Chdir(Config.ProjectRoot))
    35  }
    36  
    37  type endpoint struct {
    38  	Api
    39  	uriExample	 string
    40  }
    41  
    42  func printApiExample(url, uriExample string) {
    43  	if len(uriExample) == 0 {
    44  		Info.Printf("http://localhost:%s%s", Config.ApiPort, url)
    45  	} else {
    46  		Info.Printf("http://localhost:%s%s?%s", Config.ApiPort, url, uriExample)
    47  	}
    48  }
    49  
    50  func main() {
    51  	gcpi, err := infrastructure.GetGcpInteractor()
    52  	HandlePanic(errors.Wrap(err, "unable to get gcp interactor"))
    53  	li, err := infrastructure.GetLocalInteractor()
    54  	HandlePanic(errors.Wrap(err, "unable to get local interactor"))
    55  
    56  	wsh = WebserviceHandler{}
    57  	wsh.GcpInteractor = gcpi
    58  	wsh.LocalInteractor = li
    59  
    60  	var endpoints = []endpoint{
    61  		{Api{wsh.Health, "/health"}, ""},
    62  		{Api{wsh.ListSourceBuckets, "/list-source-buckets"}, "projectId="+Config.GcpSourceProjectId},
    63  		{Api{wsh.ListSinkBuckets, "/list-sink-buckets"}, "projectId="+Config.GcpSinkProjectId},
    64  		{Api{wsh.SourceFileExists, "/source-file-exists"}, "fileName="+fileName},
    65  		{Api{wsh.DownloadFile, "/download-file"}, "fileName="+fileName},
    66  		{Api{wsh.UploadFile, "/upload-file"}, "fileName="+fileName},
    67  		{Api{wsh.LocalFileExists, "/local-file-exists"}, "fileName="+fileName},
    68  	}
    69  	Info.Println("Example API endpoints:")
    70  	{
    71  		for _, ep := range endpoints {
    72  			http.HandleFunc(ep.Api.Url, ep.Api.Handler)
    73  			printApiExample(ep.Api.Url, ep.uriExample)
    74  		}
    75  	}
    76  	http.ListenAndServe(":"+Config.ApiPort, nil)
    77  }