Return to Level1Techs.com

Revamping My Portfolio

devember2k18

#1

I, AdminDev, will participate in the next Devember. My Devember will be rebuilding my online portfolio. I promise I will program for my Devember for at least an hour, every day of the next December. I will also write a daily public devlog and will make the produced code publicly available on the internet. No matter what, I will keep my promise.

Edit

This project is no longer being worked on. Sorry to call DOA within the first week, but a lot of things came up. I’m no longer pursing this project or goal.


The original post

Hey, everyone, how’s it going?

I currently have a lackluster portfolio on the Internet somewheres. It’s written in Node and Express, and has links to my other sites (GitLab, LinkedIn, GitHub, The rollBak). I’ve always liked the home page, but didn’t care for the other pages, which is basically a hurried About Me section and a Work History section, which is a hurried resume.

I figured it’s time to give it an overhaul. I’m going to take out the Node and Express components and use Golang. I’ve been practicing some CSS black magicks lately, so I think the front end won’t need to be much more than that. I had the VueJS bug, but we’ll see how that goes. My main goal is to have the backend up in Golang. Secondary goals are make the other pages more pleasurable and enticing, not just a hero pic with black text on white background.

Bonus Objective: Web crawler/bot, also written in Golang, that checks for 200 status every five minutes, sends notification if 4XX/5XX are returned.

Infrastructure: Linode VPC with CentOS 7
Language: Golang, HTML, CSS, JavaScript (maybe)
Development Environment: Windows 10 Professional, Debian Stretch, OS X Mojave


#2

Day 1

Write out the bot and tested it. Used native Go packages and “borrowed” some code to clean up the mail function.

Source Code here
package main

import (
	"crypto/tls"
	"fmt"
	"log"
	"net/http"
	"net/smtp"
	"strings"
	"time"
)

type Mail struct {
	senderId string
	toIds    []string
	subject  string
	body     string
}

type SmtpServer struct {
	host string
	port string
}

func (s *SmtpServer) ServerName() string {
	return s.host + ":" + s.port
}

func (mail *Mail) BuildMessage() string {
	message := ""
	message += fmt.Sprintf("From: %s\r\n", mail.senderId)
	if len(mail.toIds) > 0 {
		message += fmt.Sprintf("To: %s\r\n", strings.Join(mail.toIds, ";"))
	}

	message += fmt.Sprintf("Subject: %s\r\n", mail.subject)
	message += "\r\n" + mail.body

	return message
}

func main() {
	sites := []string{
            "myWebSites"
	}

	c := make(chan string)

	for _, site := range sites {
		go checkSite(site, c)
	}

	for s := range c {
		go func(site string) {
			time.Sleep(30 * time.Second)
			checkSite(site, c)
		}(s)
	}
}

func checkSite(site string, c chan string) {
	_, err := http.Get(site)
	if err != nil {
		c <- site
		sendMail()
		return
	}

	fmt.Println(time.Now(), site, "is online")
	c <- site

}

func sendMail() {

	mail := Mail{}
	mail.senderId = "myAdminEmail"
	mail.toIds = []string{"listOfEmails"}
	mail.subject = "Web Server Offline"
	mail.body = "Website might be down"

	messageBody := mail.BuildMessage()

	smtpServer := SmtpServer{host: "smtp.gmail.com", port: "465"}

	log.Println(smtpServer.host)
	//build an auth
	auth := smtp.PlainAuth("", mail.senderId, "pw", smtpServer.host)

	// Gmail will reject connection if it's not secure
	// TLS config
	tlsconfig := &tls.Config{
		InsecureSkipVerify: true,
		ServerName:         smtpServer.host,
	}

	conn, err := tls.Dial("tcp", smtpServer.ServerName(), tlsconfig)
	if err != nil {
		log.Panic(err)
	}

	client, err := smtp.NewClient(conn, smtpServer.host)
	if err != nil {
		log.Panic(err)
	}

	// step 1: Use Auth
	if err = client.Auth(auth); err != nil {
		log.Panic(err)
	}

	// step 2: add all from and to
	if err = client.Mail(mail.senderId); err != nil {
		log.Panic(err)
	}
	for _, k := range mail.toIds {
		if err = client.Rcpt(k); err != nil {
			log.Panic(err)
		}
	}

	// Data
	w, err := client.Data()
	if err != nil {
		log.Panic(err)
	}

	_, err = w.Write([]byte(messageBody))
	if err != nil {
		log.Panic(err)
	}

	err = w.Close()
	if err != nil {
		log.Panic(err)
	}

	client.Quit()

	log.Println("Mail sent successfully")

}

I censored some stuff like my e-mails, websites (for now), and, obviously, my password.

:wink:


#3

Hey, not fair… We should start at Dec.1…


#4

Whoops.

Well hey, at least I’m being honest :grin:

If you never follow the rules, you never lose fam :wink:


#5

Remember its not a contest :wink:

You’ll get your badge as long as you do your project.


#6

Is that why you started 4 days earlier and reminded me, that it all starts at December 1-st? :stuck_out_tongue:


#7

Starting is fine. This is the planning phase.


#8

Made any commits? :stuck_out_tongue:


#9

Nah, I put developments on hold this week. When I completely and utterly decimate the competition, I don’t want to hear anything about starting early :wink:


#10

CAPTAIN’S LOG

Star Date 237*47-45

Already regretting this decision. Go is weird.

Found an easy way to parse files though.

package main

import (
    "log"
    "os"
    "html/template"
)

var glb *template.Template

func init() {
    glb = template.Must(template.ParseGlob("templates/*"))
}

func main() {
    err := glb.ExecuteTemplate(os.Stdout, nil)
    if err != nil {
        log.Fatalln(err)
    }
    
    err = glb.ExecuteTemplate(os.Stdout, "index.gohtml", nil) 
    if err != nil {
        log.Fatalln(err)
    }
}

Lol at Go avoiding callback hell by just having single error check after EVERY execution of a Template.

Maybe I just misread the documentation.

Brb scrapping project and going PHP 7.2

u mad?


#11

Another Update

Made edits tovim and code to use 8 spaces for tabs.

Seems to be the Google standard… I like it. More than I thought I would. I absolutely HATE when Ruby, Python, and JavaScript programmers use 2 spaces for tabs.

That is all.


#12

This is what I’d call a “prober”. We’re you thinking of instrumenting it for Prometheus?


#13

That’s not a bad idea. I just recently spun up a Prometheus server. Playing with that and Grafana.


#14

Lol that was fast.

IMO you should’ve used Rust.


#15

RANT TIME

So, a few months ago, I was on the Level1Techs Devember Discord Channel. I was starting to get back into C++ and asked if anyone had some fun exercises. I was told to use Go or Rust instead, because you can write systems with them, and they’re more modern, blah, blah, blah.

I decided to go with Go, which spawned my deep love and infatuation with it.

Six months later

I linked the Devember Challenge 2018 thread into the Discord thread and mentioned my revamp with Go…

Apparently, Go sucks now, so does Rust, so does JavaScript, so does PHP, so does OS X, so does Ubuntu, so does Debian, so does Linux Mint, so does INSERT THING HERE.

No one is ever happy with anything lol. Including me :sweat_smile:

I’m sticking with Go. It’s different, but I like it. Just released a video, actually:

Ahhhh SHAMELESS PLUG @BGL @sgtawesomesauce @Eden @anotherriddle @Goalkeeper @Novasty more juicy content like I sent earlier :wink: Plz respond to main DM with feedback if you feel so inclined.

Or here. Whatevs. Thanks!

DEVEMBER 2K18 CHAMP


#16

The only reason I suggested it is because I have seem lots of examples of people using Rust for game development, as well as working examples of HTTP servers.

Take a look at this one.

But by all means keep at it with Go! I just hate to see you give up so soon :frowning:
Gotta push past the difficulties! :muscle:


#17

you suck confirmed.


#18

:point_up:


#19

Dude, if you need peoples validation of your distro/language choice, you’re never going to get something done. :stuck_out_tongue:

Everyone has a complaint with something. Just use what works and the rest will fall into place.


#20

I have low self esteem, like most sysadmins lol :sob: