Qr code generator


you can type your input then you will see QR code result to the output screen

QRCode.js javascript library provides Qr output

Generating xml sitemap with Golang

sitemaps are xml based files shared for search engines. Main purpose of sitemap is making websites easy indexable

golang library encoding/xml contains functions for marshalling & unmarshalling xml data format

Sitemap xml structure

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
      <loc>http://33m.org/</loc>
      <lastmod>2024-12-01</lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.9</priority>
   </url>
</urlset> 

Urlset tag is root tag of the sitemap file that we can add more than one url's under.

Url tag contains :

loc tag indicates page url (required)

lastmod tag indicates last modification date

changefreq indicates page update frequency

priority tag indicates page priority

only loc tag is required, other tags are optional

Sitemap Index file

if we have multiple sitemap, we can add all sitemaps to the one sitemap index file

sitemap index file structure
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <sitemap>
      <loc>http://33m.org/sitemap.xml</loc>
      <lastmod>2024-12-12T18:23:17+00:00</lastmod>
   </sitemap>
   <sitemap>
      <loc>http://33m.org/sitemap2.xml</loc>
      <lastmod>2024-12-22</lastmod>
   </sitemap>
</sitemapindex>

tags under the sitemap tag have same function with url tag.

only we need to add sitemap url

Golang encoding/xml library

encoding/xml library supports Marshalling & Unmarshalling xml

lets create a example sitemap with Golang

Declaration of object structures for sitemap
type UrlSet struct {
	XMLName           xml.Name `xml:"urlset"`
	Url               []Url    `xml:"url"`
	Xsi               string   `xml:"xmlns:xsi,attr"`
	XsiSchemaLocation string   `xml:"xsi:schemaLocation,attr"`
	Xmlns             string   `xml:"xmlns,attr"`
}

type Url struct {
	Loc     string  `xml:"loc"`
	Lastmod *string `xml:"lastmod"`
}
Creating Instance of Urlset Object
locs := []Url{}
locs = append(locs, Url{Loc: "http://33m.org/golang", Lastmod: "2024-12-12"})
locs = append(locs, Url{Loc: "http://33m.org/java", Lastmod: "2024-11-12"})
locs = append(locs, Url{Loc: "http://33m.org/linux", Lastmod: "2024-11-12"})

urlSet := UrlSet{
		Url:               locs,
		Xmlns:             "http://www.sitemaps.org/schemas/sitemap/0.9",
		XsiSchemaLocation: "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd",
		Xsi:               "http://www.w3.org/2001/XMLSchema-instance",
	}

Using different namespaces are possible, we use default sitemap schema (http://www.sitemaps.org/schemas/sitemap/0.9)

Marshalling
xmlBytes, err := xml.Marshal(urlSet)
if err != nil {
	panic(err)
}

fmt.Println(string(xmlBytes))
Result :
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>http://33m.org/golang</loc>
        <lastmod>2024-12-12</lastmod>
    </url>
    <url>
        <loc>http://33m.org/java</loc>
        <lastmod>2024-12-12</lastmod>
    </url>
    <url>
        <loc>http://33m.org/linux</loc>
        <lastmod>2024-12-12</lastmod>
    </url>
</urlset>
Declaration of object structures for sitemap index
type SitemapIndex struct {
	XMLName           xml.Name  `xml:"sitemapindex"`
	Sitemap           []Sitemap `xml:"sitemap"`
	Xsi               string    `xml:"xmlns:xsi,attr"`
	XsiSchemaLocation string    `xml:"xsi:schemaLocation,attr"`
	Xmlns             string    `xml:"xmlns,attr"`
}

type Sitemap struct {
	Loc     string  `xml:"loc"`
	Lastmod *string `xml:"lastmod"`
}
Creating instance of SitemapIndex Object
sitemaps := []Sitemap{}
sitemaps = append(sitemaps, Sitemap{Loc: "http://33m.org/sitemap1.xml"})
sitemaps = append(sitemaps, Sitemap{Loc: "http://33m.org/sitemap2.xml"})


sitemapIndex := SitemapIndex{
		Sitemap:           sitemaps,
		Xmlns:             "http://www.sitemaps.org/schemas/sitemap/0.9",
		XsiSchemaLocation: "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd",
		Xsi:               "http://www.w3.org/2001/XMLSchema-instance",
	}
Marshalling
xmlBytes, err := xml.Marshal(sitemapIndex)
if err != nil {
	panic(err)
}

fmt.Println(string(xmlBytes))
Result :
<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <sitemap>
        <loc>http://33m.org/sitemap1.xml</loc>
    </sitemap>
    <sitemap>
        <loc>http://33m.org/sitemap2.xml</loc>
    </sitemap>
</sitemapindex>

What is robots.txt, why we need it ?

During the first time search engine bots visiting website, they check domain root directory. Robots.txt file contains information which sub directories should be indexed or not

Robots.txt example
https://www.google.com/robots.txt
Robots.txt structure

Robots.txt has basic structure

User-agent: useragent1
Disallow: ...
Allow: ...
Sitemap: ...

line starts with User-agent informs search engine regarding to valid operations on below

for example
User-agent: *
Disallow: /search
Allow: /search/about

this block indicates that /search path is disallowed for all clients and /search/about path is allowed

Allowing all pages for all user agents
User-agent: *
Allow: *
Disallowing all pages for all user agents
User-agent: *
Disallow: *
Sitemap tag

sitemap tag shows where sitemap file located

Sitemap: https://www.google.com/sitemap.xml

there should be only one Sitemap tag in robots.txt file.

we recommend using sitemap index file in the multiple sitemap case

Letsencrypt adding new certificate with Certboot

What is Letsencrypt !

Letsencrypt is platform that providing free ssl certificate

What is Certbot

Certbot is open source command line tool for generating ssl certificate.

Adding new certificate
 sudo certbot certonly --standalone -d example.com
for nginx
sudo certbot --nginx
for apache
sudo certbot --apache

Unless you don't use additional --certonly parameter, certbot will replace nginx and apache configuration

Renewing certiticates

Letsencrypt certificates have default 3 months expire date. we need to renew certificates periodically, otherwise website might have availability problems due to expired certificate

sudo certbot renew 

List Certificates

 sudo certbot certificates

Golang redirect http to https

http -> https

package main

import (
    "net"
    "log"
    "net/http"
)

var httpAddr ":8080"
var httpsAddr ":8443"

func main() {
    srv := http.Server{
        Addr: httpsAddr,
    }

    _, tlsPort, err := net.SplitHostPort(httpsAddr)
    if err != nil {
        return err
    }
    go redirectToHTTPS(tlsPort)

    srv.ListenAndServeTLS("cert.pem", "key.pem")
}

func redirectToHTTPS(tlsPort string) {
    httpSrv := http.Server{
        Addr: httpAddr,
        Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
            var host string
		if strings.Contains(r.Host, ":") {
			var err error
			host, _, err = net.SplitHostPort(r.Host)
			if err != nil {
				panic(err)
			}
	      } else {
				host = r.Host
	      }
            u := r.URL
            u.Host = net.JoinHostPort(host, tlsPort)
            u.Scheme="https"
            log.Println(u.String())
            http.Redirect(w,r,u.String(), http.StatusMovedPermanently)
        }),
    }
    log.Println(httpSrv.ListenAndServe())
}

text size calculator

You can get text size from message !

Text size calculator calculate how many characters in the text

Mac os x sleep from command line

Mac has sleeping button on the apple icon -> sleep. Another effective way is sleeping from command line

pmset sleepnow

this command sleeps mac immediatelly

Sleeping delay
if we want to sleep after specific period, we can wait command awhile, and after system trigger command

for example sleeping after 10 seconds

sleep 10;pmset sleepnow  

Tar create archive on command line

Tar is a command for compressing data as tar format

tar -cvf target-21-Jan-2025.tar.gz target-folder

-c helps to compress folder

-v is optional, if you want to verbose, you can add

-f <filename> Location of archive

Other options

-b block size : default 512 byte

-w Interactive mode

-z, -j, -J, --lzma Compress algorithm : gzip, bzip2, xz, lzma

Golang server https certificate multi domain setup

loading certificate !

	rootMux := http.NewServeMux()
	cfg := &tls.Config{}

	cert, err := tls.LoadX509KeyPair("fullchain.pem", "/privkey.pem")
	if err != nil {
		panic(err)
	}

	cfg.Certificates = append(cfg.Certificates, cert)

	cert2, err2 := tls.LoadX509KeyPair("fullchain2.pem", "/privkey2.pem")
	if err2 != nil {
		panic(err2)
	}

	cfg.Certificates = append(cfg.Certificates, cert2)

	cfg.BuildNameToCertificate()

	server := http.Server{
		Addr:      ":443",
		Handler:   rootMux,
		TLSConfig: cfg,
	}

	err = server.ListenAndServeTLS("", "")
	if err != nil {
		panic(err)
	}