github.com/l3x/learn-fp-go@v0.0.0-20171228022418-7639825d0b71/2-design-patterns/ch06-onion-arch/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  	fileName = os.Getenv("TEST_FILENAME")
    25  	if len(fileName) == 0 {
    26  		fileName = defaultFileName
    27  	}
    28  	Debug.Printf("ProjectRoot: %s", PadRight(Config.ProjectRoot, " ", 20))
    29  	Debug.Printf("AppEnv: %s", PadRight(Config.AppEnv, " ", 20))
    30  	Debug.Printf("GcpSourceKeyFile: %s", PadRight(Config.GcpSourceKeyFile, " ", 20))
    31  	Debug.Printf("GcpSinkKeyFile: %s", PadRight(Config.GcpSinkKeyFile, " ", 20))
    32  	Debug.Printf("LogDebugInfo: %v", Config.LogDebugInfo)
    33  	HandlePanic(os.Chdir(Config.ProjectRoot))
    34  }
    35  
    36  type endpoint struct {
    37  	Api
    38  	uriExample	 string
    39  }
    40  
    41  func printApiExample(url, uriExample string) {
    42  	if len(uriExample) == 0 {
    43  		Info.Printf("http://localhost:%s%s", Config.ApiPort, url)
    44  	} else {
    45  		Info.Printf("http://localhost:%s%s?%s", Config.ApiPort, url, uriExample)
    46  	}
    47  }
    48  
    49  func main() {
    50  	gcpi, err := infrastructure.GetGcpInteractor()
    51  	HandlePanic(errors.Wrap(err, "unable to get gcp interactor"))
    52  	li, err := infrastructure.GetLocalInteractor()
    53  	HandlePanic(errors.Wrap(err, "unable to get local interactor"))
    54  
    55  	wsh = WebserviceHandler{}
    56  	wsh.GcpInteractor = gcpi
    57  	wsh.LocalInteractor = li
    58  
    59  	var endpoints = []endpoint{
    60  		{Api{wsh.Health, "/health"}, ""},
    61  		{Api{wsh.ListSourceBuckets, "/list-source-buckets"}, "projectId="+ Config.GcpSourceProjectId},
    62  		{Api{wsh.ListSinkBuckets, "/list-sink-buckets"}, "projectId="+ Config.GcpSinkProjectId},
    63  		{Api{wsh.SourceFileExists, "/source-file-exists"}, "fileName="+fileName},
    64  		{Api{wsh.DownloadFile, "/download-file"}, "fileName="+fileName},
    65  		{Api{wsh.UploadFile, "/upload-file"}, "fileName="+fileName},
    66  		{Api{wsh.LocalFileExists, "/local-file-exists"}, "fileName="+fileName},
    67  	}
    68  	Info.Println("Example API endpoints:")
    69  	{
    70  		for _, ep := range endpoints {
    71  			http.HandleFunc(ep.Api.Url, ep.Api.Handler)
    72  			printApiExample(ep.Api.Url, ep.uriExample)
    73  		}
    74  	}
    75  	http.ListenAndServe(":"+Config.ApiPort, nil)
    76  }