How to run a golang program on https


(Agha) #1

package main

import ( “fmt”
“log”
“net/http”
“encoding/json”
“io/ioutil”
“bytes”
github.com/gorilla/mux
“strings”
)
func main() {
r := mux.NewRouter()

	httpsMux := http.NewServeMux()


	httpsMux.Handle("/", http.HandlerFunc(infoTeam))

	
	go http.ListenAndServeTLS(":8488", "cert.pem", 

“key.pem”, httpsMux)

	http.Handle("/", r)

	fmt.Println("Main Page")

}

//--------------------------------------------------------------------------------------------

func infoTeam(rw http.ResponseWriter, r *http.Request) {

	fmt.Println("Main Page")
	rw.Header().Set(" Server in Golang")
	rw.Write([]byte("Service (Golang Server)\nTeam Members:\n Agha Assad\n"))

}

this is my code, can any one guide me regarding this


(Julien Heger) #2
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/gorilla/mux"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
)

func main() {
	r := mux.NewRouter()

	httpsMux := http.NewServeMux()

	httpsMux.Handle("/", http.HandlerFunc(infoTeam))

	go http.ListenAndServeTLS(":8488", "cert.pem",
		"key.pem", httpsMux)

	http.Handle("/", r)

	fmt.Println("Main Page")
}

//--------------------------------------------------------------------------------------------

func infoTeam(rw http.ResponseWriter, r *http.Request) {

	fmt.Println("Main Page")
	rw.Header().Set(" Server in Golang")
	rw.Write([]byte("Service (Golang Server)\nTeam Members:\n Agha Assad\n"))
}


Please have a look at the sticky thread, that helps enormously to better be able to help you:

What exactly is your question / what problem do you want to solve?
Apart from guessing that your code example is incomplete, I can’t see anything particular that the go compiler wouldn’t tell you already.

If your request is for detailed feedback on your code, I can give that, but I’d suggest trying to improve it yourself first, based on the compiler errors, that’s good practice :slight_smile:


(Agha) #3

thanku @Jaytn for guiding me, I have solved this issue, in future if I want help from this forum I’ll explain my question clearly.


(Agha) #4

package main

import ( “fmt”
“log”
“net/http”
“encoding/json”
“io/ioutil”
“bytes”
github.com/gorilla/mux

)

func main() {
r := mux.NewRouter()

	httpsMux := http.NewServeMux()

	// Check if the cert files are available.



	httpsMux.Handle("/", http.HandlerFunc(infoTeam))


	http.ListenAndServeTLS(":8488", "cert.pem", "key.pem", httpsMux)
	http.Handle("/", r)

}

func infoTeam(rw http.ResponseWriter, r *http.Request) {

fmt.Println("Main Page")
rw.Header().Set(" Server in Golang")
rw.Write([]byte("Service (Golang Server)\nTeam Members:\n Agha Assad\n"))

}

This is the code for running the golang program on Https, i just used gorilla/mux library for this. the code is runnig now.


(system) #5

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.