Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Go HTTP server example
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
FROM golang:alpine
ADD ./src /go/src/app
WORKDIR /go/src/app
ENV PORT=3001
CMD ["go", "run", "main.go"]
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