Created
June 11, 2018 04:09
-
-
Save rrgarciach/0ea36f2ecb3e2211f42f75787ab9ec47 to your computer and use it in GitHub Desktop.
Go HTTP server example
This file contains 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
version: '2' | |
services: | |
my-golang-app-run: | |
container_name: my-golang-app-run | |
build: . | |
command: go run main.go | |
volumes: | |
- ./src:/go/src/app | |
working_dir: /go/src/app | |
ports: | |
- "3030:3000" | |
environment: | |
PORT: 3001 |
This file contains 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
FROM golang:alpine | |
ADD ./src /go/src/app | |
WORKDIR /go/src/app | |
ENV PORT=3001 | |
CMD ["go", "run", "main.go"] |
This file contains 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 ( | |
"fmt" | |
"net/http" | |
) | |
func main() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "This is a website server by a Go HTTP server.") | |
}) | |
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello World! I'm a HTTP server!") | |
}) | |
fs := http.FileServer(http.Dir("static/")) | |
http.Handle("/static/", http.StripPrefix("/static/", fs)) | |
http.ListenAndServe(":3001", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment