<commit_before>// Package service implements the gohstd web service, providing remote access
// to the command history supplied by gohst clients
package service

import (
	"github.com/gorilla/mux"
	gohst "github.com/warreq/gohstd/common"
	"net/http"
	"path/filepath"
)

// Route represents a URI route for the server to support. A definition of a
// Route declares support for an HTTP endpoint and maps that URI to the
// appropriate HTTP handler.
type Route struct {
	Name        string
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

type Routes []Route

var userRepo gohst.UserRepo
var commandRepo gohst.CommandRepo
var postRepo PsqlPostRepo

// NewRouter constructs a *mux.Router based on the routes defined in this
// package, which can then be passed to the net/http server.
func NewRouter(cmd gohst.CommandRepo, user gohst.UserRepo, post PsqlPostRepo) *mux.Router {
	userRepo = user
	commandRepo = cmd
	postRepo = post
	router := mux.NewRouter().StrictSlash(true)

	for _, route := range routes {
		var handler http.Handler

		// Note that handler func wrapping like this means that the last one applied
		// is the outer-most wrapper and executes first
		handler = route.HandlerFunc
		handler = StandardHeader(handler)
		handler = Auth(handler, route.Name)
		handler = Logger(handler, route.Name)

		router.
			Methods(route.Method).
			Path(route.Pattern).
			Name(route.Name).
			Handler(handler)
	}

	// Serve the web application on the root path
	router.PathPrefix("/").Handler(http.FileServer(http.Dir(filepath.FromSlash("./webapp"))))
	return router
}

var routes = Routes{
	Route{
		"GetPosts",
		"GET",
		"/api/posts",
		GetPosts,
	},
	Route{
		"SendPost",
		"POST",
		"/api/users/{username}/posts",
		SendPost,
	},
	Route{
		"UserRegister",
		"POST",
		"/api/users/register",
		UserRegister,
	},
	Route{
		"UserLogin",
		"GET",
		"/api/users/login",
		UserLogin,
	},
	Route{
		"UserShow",
		"GET",
		"/api/users/{username}",
		UserShow,
	},
	Route{
		"CommandIndex",
		"GET",
		"/api/users/{username}/commands",
		CommandIndex,
	},
	Route{
		"CommandCreate",
		"POST",
		"/api/users/{username}/commands",
		CommandCreate,
	},
	Route{
		"CommandTagCreate",
		"POST",
		"/api/users/{username}/commands/{commandId}/tags",
		CommandTagCreate,
	},
	Route{
		"UserTagShow",
		"GET",
		"/api/users/{username}/tags",
		UserTagShow,
	},
	Route{
		"UserTagRename",
		"PUT",
		"/api/users/{username}/tags/{tag}",
		UserTagRename,
	},
	Route{
		"UserTagDelete",
		"DELETE",
		"/api/users/{username}/tags/{tag}",
		UserTagDelete,
	},
	Route{
		"CommandTagDelete",
		"DELETE",
		"/api/users/{username}/commands/{commandId/tags/{tag}",
		CommandTagDelete,
	},
	Route{
		"CommandDelete",
		"DELETE",
		"/api/users/{username}/commands/{commandId}",
		CommandDelete,
	},
}
<commit_msg>also load webapp relative to executable's dir<commit_after>// Package service implements the gohstd web service, providing remote access
// to the command history supplied by gohst clients
package service

import (
	"github.com/gorilla/mux"
	"github.com/kardianos/osext"
	gohst "github.com/warreq/gohstd/common"
	"log"
	"net/http"
	"path"
	"path/filepath"
)

// Route represents a URI route for the server to support. A definition of a
// Route declares support for an HTTP endpoint and maps that URI to the
// appropriate HTTP handler.
type Route struct {
	Name        string
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

type Routes []Route

var userRepo gohst.UserRepo
var commandRepo gohst.CommandRepo
var postRepo PsqlPostRepo

// NewRouter constructs a *mux.Router based on the routes defined in this
// package, which can then be passed to the net/http server.
func NewRouter(cmd gohst.CommandRepo, user gohst.UserRepo, post PsqlPostRepo) *mux.Router {
	userRepo = user
	commandRepo = cmd
	postRepo = post
	router := mux.NewRouter().StrictSlash(true)

	for _, route := range routes {
		var handler http.Handler

		// Note that handler func wrapping like this means that the last one applied
		// is the outer-most wrapper and executes first
		handler = route.HandlerFunc
		handler = StandardHeader(handler)
		handler = Auth(handler, route.Name)
		handler = Logger(handler, route.Name)

		router.
			Methods(route.Method).
			Path(route.Pattern).
			Name(route.Name).
			Handler(handler)
	}

	// Serve the web application on the root path
	execPath, err := osext.ExecutableFolder()
	if err != nil {
		log.Fatal(err)
	}

	appPath := filepath.FromSlash(path.Join(execPath, "webapp"))

	router.PathPrefix("/").Handler(http.FileServer(http.Dir(appPath)))
	return router
}

var routes = Routes{
	Route{
		"GetPosts",
		"GET",
		"/api/posts",
		GetPosts,
	},
	Route{
		"SendPost",
		"POST",
		"/api/users/{username}/posts",
		SendPost,
	},
	Route{
		"UserRegister",
		"POST",
		"/api/users/register",
		UserRegister,
	},
	Route{
		"UserLogin",
		"GET",
		"/api/users/login",
		UserLogin,
	},
	Route{
		"UserShow",
		"GET",
		"/api/users/{username}",
		UserShow,
	},
	Route{
		"CommandIndex",
		"GET",
		"/api/users/{username}/commands",
		CommandIndex,
	},
	Route{
		"CommandCreate",
		"POST",
		"/api/users/{username}/commands",
		CommandCreate,
	},
	Route{
		"CommandTagCreate",
		"POST",
		"/api/users/{username}/commands/{commandId}/tags",
		CommandTagCreate,
	},
	Route{
		"UserTagShow",
		"GET",
		"/api/users/{username}/tags",
		UserTagShow,
	},
	Route{
		"UserTagRename",
		"PUT",
		"/api/users/{username}/tags/{tag}",
		UserTagRename,
	},
	Route{
		"UserTagDelete",
		"DELETE",
		"/api/users/{username}/tags/{tag}",
		UserTagDelete,
	},
	Route{
		"CommandTagDelete",
		"DELETE",
		"/api/users/{username}/commands/{commandId/tags/{tag}",
		CommandTagDelete,
	},
	Route{
		"CommandDelete",
		"DELETE",
		"/api/users/{username}/commands/{commandId}",
		CommandDelete,
	},
}

{'fraction_non_alphanumeric': 0.140474552957359, 'fraction_numerical': 0.0, 'mean_word_length': 3.7063106796116503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1828262', 'n_tokens_mistral': 2149, 'n_tokens_neox': 1982, 'n_words': 495}