Skip to content

Commit

Permalink
go serve
Browse files Browse the repository at this point in the history
  • Loading branch information
kingster committed Apr 13, 2022
1 parent 6732482 commit 085aae1
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 18 deletions.
8 changes: 8 additions & 0 deletions cmd/dkvsrv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import (
"os"
"os/signal"
"path"
"runtime"
"strings"
"syscall"

"github.com/flipkart-incubator/dkv/internal"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -520,6 +523,11 @@ func setupHttpServer() {
router.HandleFunc("/debug/pprof/trace", pprof.Trace)
}

//Admin UI
liveMode := runtime.GOOS == "dawin"
fileSystem := internal.GetWWWFileSystem(liveMode)
router.PathPrefix("/admin/").Handler(http.StripPrefix("/admin/", internal.NoCache(http.FileServer(fileSystem))))

http.Handle("/", router)
http.ListenAndServe(config.HttpListenAddr, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/flipkart-incubator/dkv

go 1.15
go 1.16

require (
github.com/Jille/grpc-multi-resolver v1.0.0
Expand Down
88 changes: 88 additions & 0 deletions internal/www.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package internal

import (
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
)

//go:embed www
var embededFiles embed.FS

func getFileDir() string {
_, file, _, ok := runtime.Caller(0)
if !ok {
file = "./"
} else {
file = filepath.Dir(file)
}

dir := fmt.Sprintf("%s/www", file)
return dir
}

func GetWWWFileSystem(develMode bool) http.FileSystem {

if develMode {
dir := getFileDir()
log.Print("using live mode with Dir: ", dir)
return http.FS(os.DirFS(dir))
}

log.Println("using embed mode")
fSys, err := fs.Sub(embededFiles, "www")
if err != nil {
panic(err)
}

//fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error {
// fmt.Println(p)
// return nil
//})

return http.FS(fSys)
}

var epoch = time.Unix(0, 0).Format(time.RFC1123)

var noCacheHeaders = map[string]string{
"Expires": epoch,
"Cache-Control": "no-cache, private, max-age=0",
"Pragma": "no-cache",
"X-Accel-Expires": "0",
}

var etagHeaders = []string{
"ETag",
"If-Modified-Since",
"If-Match",
"If-None-Match",
"If-Range",
"If-Unmodified-Since",
}

func NoCache(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
// Delete any ETag headers that may have been set
for _, v := range etagHeaders {
if r.Header.Get(v) != "" {
r.Header.Del(v)
}
}

// Set our NoCache headers
for k, v := range noCacheHeaders {
w.Header().Set(k, v)
}

h.ServeHTTP(w, r)
}

return http.HandlerFunc(fn)
}
14 changes: 7 additions & 7 deletions internal/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
<link href="//cdnjs.cloudflare.com/ajax/libs/c3/0.4.15/c3.css" rel="stylesheet" type="text/css">

<!-- monitoring -->
<link rel="stylesheet" href="/css/main.css">
<script type="text/javascript" src="/js/main.js"></script>
<script type="text/javascript" src="/js/config.js"></script>
<script type="text/javascript" src="/js/controllers.js"></script>
<script type="text/javascript" src="/js/services.js"></script>
<script type="text/javascript" src="/js/directives.js"></script>
<script type="text/javascript" src="/js/lib.js"></script>
<link rel="stylesheet" href="/admin/css/main.css">
<script type="text/javascript" src="/admin/js/main.js"></script>
<script type="text/javascript" src="/admin/js/config.js"></script>
<script type="text/javascript" src="/admin/js/controllers.js"></script>
<script type="text/javascript" src="/admin/js/services.js"></script>
<script type="text/javascript" src="/admin/js/directives.js"></script>
<script type="text/javascript" src="/admin/js/lib.js"></script>

<!-- css-->
<style>
Expand Down
4 changes: 2 additions & 2 deletions internal/www/js/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CONFIG={
"router": {
"Home":{
"path":"/",
"path":"/admin/",
"controller":"HomeCtrl",
"templateUrl": "home.html",
"templateUrl": "/admin/home.html",
},
},
};
6 changes: 3 additions & 3 deletions internal/www/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@
function($location, $scope, $rootScope ,$timeout, $sce, DKVService) {
/* discover master nodes from here*/
$scope.masters = {
"m1":"http://127.0.0.1:7081",
"m2":"http://127.0.0.1:7082",
"m3":"http://127.0.0.1:7083",
"m1":"http://127.0.0.1:8081",
"m2":"http://127.0.0.1:8082",
"m3":"http://127.0.0.1:8083",
}

$scope.througputParser = througputParser
Expand Down
2 changes: 1 addition & 1 deletion internal/www/js/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.directive("headerNav", function() {
return {
restrict: "E",
templateUrl: "header-nav.html",
templateUrl: "/admin/header-nav.html",
controller: function($scope, $rootScope, $location, $sce) {
$scope.config = CONFIG;
}
Expand Down
2 changes: 1 addition & 1 deletion internal/www/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
});
}
$routeProvider.otherwise({
redirectTo: "/"
redirectTo: "/admin/"
});
$locationProvider.html5Mode(true);
})
Expand Down
3 changes: 0 additions & 3 deletions internal/www/run.sh

This file was deleted.

0 comments on commit 085aae1

Please sign in to comment.