Last active
July 12, 2020 16:04
-
-
Save kenng/691fa4c1f8e93b24c5afd37f61f751f5 to your computer and use it in GitHub Desktop.
golang HTTPS server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
) | |
// usage: | |
// with default parameter | |
// <program> | |
// | |
// to specify different path and filename | |
// <program> -d <ssl-directory> -c <cert-crt-filename> -k <cert-key-filename> | |
func main() { | |
homeDir, err := os.UserHomeDir() | |
if err != nil { | |
log.Fatal("error in getting home directory") | |
} | |
sslCertDirArg := flag.String("d", homeDir+"/ssl-local-cert", "the directory of SSL cert") | |
sslCrtNameArg := flag.String("c", "test.iw.com.pem", "the filename of SSL cert") | |
sslKeyNameArg := flag.String("k", "test.iw.com-key.pem", "the filename of SSL key") | |
flag.Parse() | |
if string((*sslCrtNameArg)[0]) != "/" { | |
*sslCrtNameArg = "/" + *sslCrtNameArg | |
} | |
if string((*sslKeyNameArg)[0]) != "/" { | |
*sslKeyNameArg = "/" + *sslKeyNameArg | |
} | |
sslCertCrtPath := *sslCertDirArg + *sslCrtNameArg | |
sslCertKeyPath := *sslCertDirArg + *sslKeyNameArg | |
fmt.Println(sslCertCrtPath) | |
fmt.Println(sslCertKeyPath) | |
// create file server handler serving current directory | |
fs := http.FileServer(http.Dir(".")) | |
// start HTTP server with `fs` as the default handler | |
fmt.Println("server run with :9000 and :443") | |
log.Fatal(http.ListenAndServeTLS(":443", sslCertCrtPath, sslCertKeyPath, fs)) | |
log.Fatal(http.ListenAndServe(":9000", fs)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment