How to run the Go website on URL without port on windows server?


(Saddam Ansari) #1

Below Is my main.go file

package main

import (
“html/template”
“log”
“net/http”
“database/sql”
“fmt”

)
const (
host = “localhost”
port = 5433
user = “postgres”
password = “9docfocus9”
dbname = “DFI”
)
var tpl *template.Template

type pageData struct {
Title string
FirstName string
}

func init() {
tpl = template.Must(template.ParseGlob(“templates/*.gohtml”))
}

func main() {
http.HandleFunc("/", idx)
http.HandleFunc("/products", prd)
http.HandleFunc("/contacts", cntct)
http.HandleFunc("/company", cmp)
http.HandleFunc("/solutions", sol)
http.HandleFunc("/benefits", bft)
http.HandleFunc("/careers", car)
http.HandleFunc("/document-management-overview", doc)
http.HandleFunc("/downloads", dwl)
http.HandleFunc("/downloads2", dwl2)
http.HandleFunc("/enterprise", ent)
http.HandleFunc("/features", fet)
http.HandleFunc("/workgroup", wg)
http.HandleFunc("/admin", adm)
http.HandleFunc("/admin-function", admfun)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir(“public”))))
http.ListenAndServe(":8086", nil)
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
“password=%s dbname=%s sslmode=disable”,
host, port, user, password, dbname)
db, err := sql.Open(“postgres”, psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()

err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println(“Successfully connected!”)
}

func idx(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Index Page",
}

err := tpl.ExecuteTemplate(w, "index.gohtml", pd)

if err != nil {
	log.Println("LOGGED", err)
	http.Error(w, "Internal serverrrrrr error", http.StatusInternalServerError)
	return
}

}

func captchae(w http.ResponseWriter, req *http.Request) {

//pd := captcha.New()
 //Must set a font, Other settings have default values
//pd.SetFont("comic.ttf")
//img, str := pd.Create(4, captcha.NUM)

//err := tpl.ExecuteTemplate(w, "admin.gohtml", pd)
//if err != nil {
	//log.Println(err)
	//http.Error(w, "Internal server error", http.StatusInternalServerError)
//	return
//}

}

func prd(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Product Page",
}

err := tpl.ExecuteTemplate(w, "products.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}

}

func cntct(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Contact Page",
}

err := tpl.ExecuteTemplate(w, "contacts.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}

}

func cmp(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Company Page",
}

err := tpl.ExecuteTemplate(w, "company.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}

}

func sol(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Solution Page",
}

err := tpl.ExecuteTemplate(w, "solutions.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}

}

func bft(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Benefits Page",
}

err := tpl.ExecuteTemplate(w, "benefits.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}

}

func car(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Careers Page",
}

err := tpl.ExecuteTemplate(w, "careers.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}

}

func doc(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Custom Document Management Page",
}

err := tpl.ExecuteTemplate(w, "document-management-overview.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return

}

}

func dwl(w http.ResponseWriter, req *http.Request) {

// var (
// 	title string
// 	pdf string
// )
// rows, err := db.Query("select title, pdf from document")
// if err != nil {
// 	log.Fatal(err)
// }
// defer rows.Close()
// for rows.Next() {
// 	err := rows.Scan(&id, &name)
// 	if err != nil {
// 		log.Fatal(err)
// 	}
// 	log.Println(id, name)
// }
// err = rows.Err()
// if err != nil {
// 	log.Fatal(err)
// }



pd := pageData{
	Title: "Downloads Page",
}

err := tpl.ExecuteTemplate(w, "downloads.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return

}

}

func dwl2(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Downloads2 Page",
}

err := tpl.ExecuteTemplate(w, "downloads2.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return

}

}

func ent(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Enterprise Page",
}

err := tpl.ExecuteTemplate(w, "enterprise.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return

}

}

func fet(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Features Page",
}

err := tpl.ExecuteTemplate(w, "features.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return

}

}

func wg(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Workgroup Page",
}

err := tpl.ExecuteTemplate(w, "workgroup.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return

}

}
func adm(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Admin Page",
}

err := tpl.ExecuteTemplate(w, "admin.gohtml", pd)
if err != nil {
	log.Println("LOGGED", err)
	http.Error(w, "Internal serverrrrrr error", http.StatusInternalServerError)
	return
}

}

func admfun(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Admin Page",
}

err := tpl.ExecuteTemplate(w, "admin-function.gohtml", pd)
if err != nil {
	log.Println("LOGGED", err)
	http.Error(w, "Internal serverrrrrr error", http.StatusInternalServerError)
	return
}

}


(Norbert Melzer) #2

WHat do you mean by this?


(Saddam Ansari) #3

Thanks for your reply.

here is the url- http://docfocus5.ca:8086/

I want to run this without showing the port but I am not able to do this.

I have tried a lot.

I am using windows server 2012. Open the port 8086 .

Please help.


(Norbert Melzer) #4

Start it on port 80. Thats the default for HTTP.

On linux systems you need administrative rights to be able to bind that port, I’m not sure how its on windows.


(Saddam Ansari) #5

On port 80 its not working I tried.


(Norbert Melzer) #6

Do you see any error when you start? Can you connect to your application from localhost?


(Saddam Ansari) #7

No when I change to port 80 its not working


(Norbert Melzer) #8

Please answer all questions from my previous post.

“is not working” answers only one, and I have to guess which one of them…


(Saddam Ansari) #9

Sorry.

When I tried to connect using port 8086 its working but when I tried using port 80 its not working.

Not able to connect my application.


(George Calianu) #10

Check your firewall for inbound rule on port 80 tcp. Make one if needed.


(Saddam Ansari) #11

Port 80 is open I checked the firewall setting.


(Norbert Melzer) #12

I ask you a last time:

When you start your server, do you see any error message?

PS: To properly answer my question, you might need to check the actual error value returned by this line:

http.ListenAndServe(":8086", nil)

(Saddam Ansari) #13

No error i m seeing


(Norbert Melzer) #14

Have you changed the line I pointed you to, to print the return value?


(Johan Dahl) #15

And of course change the port to 80 like this:

log.Fatalln(http.ListenAndServe(":80", nil))

(Ali Koyuncu) #16

80 is a special port, you might need to run your application as administrator, and make sure there is no application which uses it already. When you set port 80 do you see any error?