Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jason Mansfield
committed
Jun 9, 2019
0 parents
commit 91a4cdf
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
VERSION := $(shell git tag | grep ^v | sort -V | tail -n 1) | ||
LDFLAGS = -ldflags "-X versserv/cmd.Version=${VERSION}" | ||
TIMESTAMP := $(shell date +%Y%m%d-%H%M) | ||
DEVLDFLAGS = -ldflags "-X versserv/cmd.Version=dev-${TIMESTAMP}" | ||
|
||
SERVBIN=/tmp/versserv | ||
|
||
dev-serv: server/* | ||
go build ${DEVLDFLAGS} -o ${SERVBIN} server/*.go | ||
|
||
serv: server/* | ||
go build ${LDFLAGS} -o ${SERVBIN} server/*.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
|
||
"versserv" | ||
"versserv/cmd" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
if *cmd.FVersion { | ||
cmd.ShowVersion() | ||
} | ||
|
||
s, err := versserv.NewServer() | ||
if err != nil { | ||
panic("creating server: " + err.Error()) | ||
} | ||
|
||
if err := s.ListenAndServe(); err != nil { | ||
panic("listening: " + err.Error()) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cmd | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
var ( | ||
Version = "" // set at compile time with -ldflags "-X versserv/cmd.Version=x.y.yz" | ||
|
||
FVersion = flag.Bool("version", false, "show version and exit") | ||
) | ||
|
||
func ShowVersion() { | ||
fmt.Println("version:", Version) | ||
os.Exit(0) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package versserv | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"versserv/cmd" | ||
) | ||
|
||
type Server struct { | ||
// server components | ||
} | ||
|
||
func NewServer() (*Server, error) { | ||
// make your own Server, Mux | ||
s := &Server{} | ||
http.HandleFunc("/", s.root) | ||
return s, nil | ||
} | ||
|
||
func (s *Server) ListenAndServe() error { | ||
log.Print("starting server version ", cmd.Version) | ||
return http.ListenAndServe("localhost:8000", nil) | ||
} | ||
|
||
func (s *Server) root(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("X-Version", cmd.Version) | ||
w.Header().Set("Content-Type", "text/html") | ||
fmt.Fprintln(w, "<html><body><h1>Hello World!</h1></body></html>") | ||
} |