Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Mansfield committed Jun 9, 2019
0 parents commit 91a4cdf
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/Makefile
@@ -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
24 changes: 24 additions & 0 deletions cmd/server/server.go
@@ -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())
}
}
18 changes: 18 additions & 0 deletions cmd/version.go
@@ -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)
}
31 changes: 31 additions & 0 deletions server.go
@@ -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>")
}

0 comments on commit 91a4cdf

Please sign in to comment.