Sunteți pe pagina 1din 4

5/16/2017 HowshouldIstructurepackagesforamultiplebinarywebapplication?

GettingHelpGoForum

HowshouldIstructurepackagesforamultiplebinarywebapplication?

steveivy Sep'15
Sep2015
So,newbietoGohere,andI'mworkingonportingasmallwebserviceIwroteinPythontoGo.ThepythonversionconsistedofasingleFlaskapp,with
1/17
handlersforaRESTAPIthatindexeddatainElasticsearch,andadashboardthatprovidessystemstatusandbasicdata/searchviews.
Sep2015
MyfirstattemptinGosortofreplicatedthatmodel:oneflatpackage("main")writtenwithGinandelastigo.Thepackageconsistsofahalfdozenfiles,and
implementstheRESTapiandthedashboardhandlers,alongwithsomebackendabstractions.

NowI'dliketoseparatethewebservicefromthedashboardservicesothatIcanworkonscalingtherestAPI,butrunthemanagementdashboardbyitself,
andI'mnotsurehowtodoit.Eachbinarywillneedit'sown"main"package,butIwantallthecodeinonerepositoryasthereiscodethatshouldbeshared.
Anytips?

NateFinch Sep'15

it'sperfectlyfinetoputthemeachintheirownsubdirectory,solike

github.com/steve/project/restcmd
github.com/steve/project/webcmd
Dec2015
Thenputreadmeetcintherootofthedir.

Youcanthengogeteverythingintheprojectbydoing`gogetgithub.com/steve/project/...andeverythingwillJustWork.

Sevein Sep'15
JessGarcaCrespo

Inadditionto @NateFinch 'scomment,Iseemanyprojectsputtingalltheirmainpackagestogetherinonedirectoryandit'sfrequentlycalled cmd .Two


examplesoffthetopofmyheadareKubernetesandCamlistore.

Also,I'veseenotherprojectsmappingalltheavailablecommandsinonesingleprogram,e.g.Hashicorpseemstofollowthispatternacrossalltheirprojects
(Consul,Terraform,etc...).

etcdtakesadifferentapproach.Therootpackageisacompleteprogrambutalsoitssubpackageetcdctl.

Sevein Sep'15
JessGarcaCrespo

Also,anicereadthatcoversthistopicisthearticleGo:BestPracticesforProductionEnvironmentswrittenby@peterbourgon(thecreatorofGokit).

steveivy Sep'15

Hey @Sevein ,I'veseenthatGoinProductionarticlebforeI'vegotitbookmarkedandsimplyforgotthatthisisaddressedthere.I'llreviewitagain,and


thanks @NateFinch aswell!

Makpoc Sep'15
BisserNedkov

Ihadasimilardilemmaandwentforthissuggestion.Idon'tknowifit'sthebestone,butitIthinkitcoverstheexactsameissue.Therewasanother
suggestionlatertokeepthecommandlineuiintherootoftheproject,butIhaveyettodecidewhethertodoitornot.

steveivy Sep'15

Thanks @Makpoc anotherusefulperspective.ComingfromPython,learningcodeorganizationinGohasbeenarealheadscratcher.

Sevein Sep'15
JessGarcaCrespo

I'veseensomeprojectsoptingforstoringalltheirpackagesundera pkg subdirectory.Ithinkthat'sagoodapproachifyoudon'twanttohaveamixofGo


packagesandotherthingsthatstillyouwanttomanageinthesamerepo,likedocs,theprojectwebsite,examples,provisioningstuff,integrationtests,etc...
CamlistoreorKubernetesdothis.

ButnothingstopsyoufromputtingGopackagesandotherdirectoriestogether,e.g.that'swhatPrometheus,Hekaandothersdo.

calmh Sep'15
JakobBorg

Thisissomethingwe'vebeenfightinganditeratingon...Currentlayoutonareasonablysizedprojectlookslikethis:

https://forum.golangbridge.org/t/howshouldistructurepackagesforamultiplebinarywebapplication/665 1/4
5/16/2017 HowshouldIstructurepackagesforamultiplebinarywebapplication?GettingHelpGoForum

Godeps/
...lotsofdependencies.Thiswouldprobablybevendor/ifwestartedagaintoday.
cmd/
...lotsofbinaries/mainpackages
etc/
...variousexampleconfigsandstuff
gui/
...awebappthatiscompiledintooneofthebinaries
lib/
...ourinternalpackages,somewithsubdirectoriesoftheirown.
...thiswas"internal"atsomepoint,butsincethisisnowadaysenforcedandwe
...actuallyhaveafewexternaluses,itbecame"lib"
script/
...variousbuildsupportingGoscripts
build.sh
build.go
README
AUTHORS
...etcstandardtoplevelstuff

Thereareafewmoretopleveldirectoriesforstufflikegraphicsassetsandsoonaswell,butnotrelevanttotheGosideofthings.SoalltheGocodelives
under cmd/ and lib/ ,apartfrombuildscripts.

ThisallbuildswithstandardGOPATH(plusaprependforGodeps),sointernalpackagesareseenas github.com/organization/project/lib/thepackage .

I'vebeenlookinginto gb aswell,butI'mnotentirelyconvincedyet.

steveivy Sep'15

Heyfolks,thanksforallthegoodreplies.Itriedastructureverymuchliketheonein"GoinProduction"article:

HOST:test_servicesivy$tree

.
README.md
health
health.go
health_test.go
test_service_api
api_instances.go
api_instances_test.go
main.go
test_service_dash
dashboard.go
dashboard_test.go
main.go
static
bootstrap3.3.2/...
dashboard/...
templates/...
storage
storage.go
storage_elasticsearch.go
storage_elasticsearch_test.go
storage_filesystem.go
storage_filesystem_test.go
util_test.go
5directories,17files

Packageandimportsinstorage/storage.go:

packagestorage

import(
health"github.com/sivy/test_service/health"
)

typeStorageProviderinterface{
//stuff...
StatusCheck()health.Healthcheck
}

Running goinstall ,however,givesme:

HOST:test_servicesivy$cdtest_api/
HOST:test_apisivy$goinstall
#github.com/sivy/test_service/storage
../storage/storage.go:22:undefined:health.Healthcheck
../storage/storage_elasticsearch.go:224:undefined:health.Healthcheck
../storage/storage_filesystem.go:165:undefined:health.Healthcheck
HOST:test_apisivy$

https://forum.golangbridge.org/t/howshouldistructurepackagesforamultiplebinarywebapplication/665 2/4
5/16/2017 HowshouldIstructurepackagesforamultiplebinarywebapplication?GettingHelpGoForum
Yeah,I'mconfused.

dlclark Sep'15
DougClark

Not100%sureontheissuehere.Doeshealth.godeclareits packagehealth ?

steveivy Sep'15

Yes,itdoes health/health.go :

//healthprovidesdatatypesforencapsulatinghealthcheckdata
//foranapplication.
packagehealth

import(
"os"
"os/exec"
"strings"
)

/*
Datatypes
*/
typeHealthCheckstruct{
Namestring
Functionfunc()interface{}//preferred
Commandstring//fallback
}

typeHealthCheckResultstruct{
Successbool`json:"success"`
Datainterface{}`json:"data"`
}

//RunperformsthedefinedchecksforthisHealthCheck
func(hHealthCheck)Run()interface{}{
varcheckOutputinterface{}
varerrerror

ifh.Function!=nil{
checkOutput=h.Function()
}elseifh.Command!=""{
output,err:=exec.Command("sh","c",h.Command).Output()
iferr!=nil{
checkOutput=err
}else{
checkOutput=string(output)
}
}

//packuptheresultsasaHealthCheckResult
varhcrHealthCheckResult
iferr!=nil{
hcr=HealthCheckResult{Success:false,Data:string(err.Error())}
}else{
hcr=HealthCheckResult{Success:true,Data:checkOutput}
}
returnhcr
}

dlclark Sep'15
DougClark

Irecreateditlocallyandfigureditout!Checkthecapson Healthcheck intheusagevsthe HealthCheck typename.

steveivy Sep'15

Ohfercryin'outloud.

Thanks @dlclark

shakeel Oct'15
ShakeelMahate

SameerAjmanihasablogabouthowtonamepackagesandwhatshouldbeinyourpackages,highlyrecommendreadingit.
https://blog.golang.org/packagenames

https://forum.golangbridge.org/t/howshouldistructurepackagesforamultiplebinarywebapplication/665 3/4
5/16/2017 HowshouldIstructurepackagesforamultiplebinarywebapplication?GettingHelpGoForum

steveivy Oct'15

That'sgreatstuff,thanks!Iwillberevisitingmycode(again!)withthisinmind.

CLOSEDDEC30,'15

Thistopicwasautomaticallyclosed90daysafterthelastreply.Newrepliesarenolongerallowed.

Reply

https://forum.golangbridge.org/t/howshouldistructurepackagesforamultiplebinarywebapplication/665 4/4

S-ar putea să vă placă și