Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added nodedb restapi #629

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

added nodedb restapi #629

wants to merge 4 commits into from

Conversation

PkayJava
Copy link

No description provided.

@PkayJava PkayJava requested a review from sophoah September 12, 2022 02:18
Copy link
Contributor

@diego1q2w diego1q2w left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, but there is a lot of repetitive code and global variables, some functions are to big and hard to read. In future that will make it harder to maintain and for anyone else to implement changes.
Left few comments that may help you to strive to that direction, let me know if there is any questions :D

Comment on lines 60 to 62
if accessKey != "" {
_accessKey = accessKey
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to remove all those global variables you can do something like:

Suggested change
if accessKey != "" {
_accessKey = accessKey
}
if accessKey != "" {
accessKey = os.Getenv("AWS_ACCESS_KEY_ID")
}

the same with other vars.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, this could be remove, since it is not being used.

Comment on lines 33 to 35
var accessKey string
var secretAccessKey string
var sessionToken string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is not a good practice to have global variables since someone may mistakenly overwritten them, try to allocate them within a function, and if they are used across many functions is a good idea to use structs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, this could be remove, since it is not being used.

if serverMtls != "" {
mtlsCertificate, errorReadFile := os.ReadFile(serverMtls)
if errorReadFile != nil {
log.Println(errorReadFile.Error())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use a log.Fatal method, that will terminate the program with a log, so the user can fix the issue

Suggested change
log.Println(errorReadFile.Error())
log.Fatal(errorReadFile.Error())

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like program to could continue to run. I will not change to log.Fatal then.

if serverCertificate != "" && serverPrivateKey != "" {
var config *tls.Config
if serverMtls != "" {
mtlsCertificate, errorReadFile := os.ReadFile(serverMtls)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we need the certificate?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mtlsCertificate, it is for mTLS(mutual tls) configuration. however it is optional

serverCertificate and serverPrivateKey, it is for https. however it is optional

Comment on lines 221 to 250
if errorDescribeInstances == nil {
for _, reservation := range _describeInstancesOutput.Reservations {
for _, instance := range reservation.Instances {
_instanceId := *instance.InstanceId
_publicIpAddress := ""
_privateIpAddress := ""
_tagName := ""
if instance.PublicIpAddress != nil {
_publicIpAddress = *instance.PublicIpAddress
}
if instance.PrivateIpAddress != nil {
_privateIpAddress = *instance.PrivateIpAddress
}
for _, tag := range instance.Tags {
if tag.Key != nil && *tag.Key == "Name" && tag.Value != nil {
_tagName = *tag.Value
}
}
_ec2s = append(_ec2s, common.Ec2Dto{
TagName: _tagName,
InstanceId: _instanceId,
PublicIP: _publicIpAddress,
PrivateIP: _privateIpAddress,
Region: *region.RegionName,
})
}
}
} else {
println(errorDescribeInstances.Error())
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all this can go to it's own method, it'll be easier to read and maintatin in the future

var _accessToken string

func main() {
flag.StringVar(&serverCertificate, "server-certificate", "", "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a lot of repetetive code across different cloud providers, this is a good example to introduce clean architecure concepts.
I work as follows

  • cmd/ cmd is just for calling methods from pkg, the main file should be no longer than a few lines since is just calling methods
    • aws/main
    • do/main
    • hetzner/main
  • pkg/ here you put all the bussines logic, that way you can reuse components
    • nodedb/ domain or app name
      • infra/ folder where you run all the requests to the cloud providers
      • adapter/ folder where you build the api or access point (in this case the cli)
      • logic files this is where you put all the bussines logic this is not a folder but a bunch of files, this is where you would put all the common structs

@@ -0,0 +1,7 @@
package common

type ConfigurationDto struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all this structs could go inside the pkg/nodedb/dto.go file

os.Exit(0)
}

func Home(w http.ResponseWriter, r *http.Request) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you move this to the pkg directory?

@PkayJava PkayJava requested a review from diego1q2w September 15, 2022 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants