Просмотр исходного кода

Added the ability to push a version in during build.

Kelly Norton 10 лет назад
Родитель
Сommit
20c733551d
3 измененных файлов с 17 добавлено и 5 удалено
  1. 10 1
      main.go
  2. 6 3
      web/web.go
  3. 1 1
      web/web_test.go

+ 10 - 1
main.go

@@ -8,6 +8,15 @@ import (
 	"github.com/kellegous/go/web"
 	"github.com/kellegous/go/web"
 )
 )
 
 
+var version string
+
+func getVersion() string {
+	if version == "" {
+		return "none"
+	}
+	return version
+}
+
 func main() {
 func main() {
 	flagData := flag.String("data", "data",
 	flagData := flag.String("data", "data",
 		"The location to use for the data store")
 		"The location to use for the data store")
@@ -20,5 +29,5 @@ func main() {
 		log.Panic(err)
 		log.Panic(err)
 	}
 	}
 
 
-	log.Panic(web.ListenAndServe(*flagAddr, ctx))
+	log.Panic(web.ListenAndServe(*flagAddr, getVersion(), ctx))
 }
 }

+ 6 - 3
web/web.go

@@ -279,7 +279,7 @@ func (h *defaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 }
 }
 
 
 // Setup a Mux with all web routes.
 // Setup a Mux with all web routes.
-func allRoutes(ctx *context.Context) *http.ServeMux {
+func allRoutes(ctx *context.Context, version string) *http.ServeMux {
 	mux := http.NewServeMux()
 	mux := http.NewServeMux()
 	mux.Handle("/", &defaultHandler{ctx})
 	mux.Handle("/", &defaultHandler{ctx})
 	mux.Handle("/api/url/", &apiHandler{ctx})
 	mux.Handle("/api/url/", &apiHandler{ctx})
@@ -289,11 +289,14 @@ func allRoutes(ctx *context.Context) *http.ServeMux {
 	mux.HandleFunc("/s/", func(w http.ResponseWriter, r *http.Request) {
 	mux.HandleFunc("/s/", func(w http.ResponseWriter, r *http.Request) {
 		serveAsset(w, r, r.URL.Path[len("/s/"):])
 		serveAsset(w, r, r.URL.Path[len("/s/"):])
 	})
 	})
+	mux.HandleFunc("/:version", func(w http.ResponseWriter, r *http.Request) {
+		fmt.Fprintln(w, version)
+	})
 	return mux
 	return mux
 }
 }
 
 
 // ListenAndServe sets up all web routes, binds the port and handles incoming
 // ListenAndServe sets up all web routes, binds the port and handles incoming
 // web requests.
 // web requests.
-func ListenAndServe(addr string, ctx *context.Context) error {
-	return http.ListenAndServe(addr, allRoutes(ctx))
+func ListenAndServe(addr, version string, ctx *context.Context) error {
+	return http.ListenAndServe(addr, allRoutes(ctx, version))
 }
 }

+ 1 - 1
web/web_test.go

@@ -75,7 +75,7 @@ func newEnv() (*env, error) {
 	}
 	}
 
 
 	return &env{
 	return &env{
-		mux: allRoutes(ctx),
+		mux: allRoutes(ctx, ""),
 		dir: dir,
 		dir: dir,
 		ctx: ctx,
 		ctx: ctx,
 	}, nil
 	}, nil