Repository: markcheno/go-vue-starter
Branch: master
Commit: e4f64e150983
Files: 45
Total size: 45.9 KB
Directory structure:
gitextract_tjk8vlva/
├── .gitignore
├── .vscode/
│ └── launch.json
├── Godeps/
│ ├── Godeps.json
│ └── Readme
├── LICENSE
├── README.md
├── api/
│ ├── api.go
│ ├── quotes.go
│ └── users.go
├── auth/
│ └── auth.go
├── client/
│ ├── .babelrc
│ ├── .editorconfig
│ ├── .eslintignore
│ ├── .eslintrc.js
│ ├── .gitignore
│ ├── .postcssrc.js
│ ├── README.md
│ ├── build/
│ │ ├── build.js
│ │ ├── check-versions.js
│ │ ├── dev-client.js
│ │ ├── dev-server.js
│ │ ├── utils.js
│ │ ├── vue-loader.conf.js
│ │ ├── webpack.base.conf.js
│ │ ├── webpack.dev.conf.js
│ │ └── webpack.prod.conf.js
│ ├── config/
│ │ ├── dev.env.js
│ │ ├── index.js
│ │ └── prod.env.js
│ ├── index.html
│ ├── package.json
│ ├── src/
│ │ ├── App.vue
│ │ ├── auth/
│ │ │ └── index.js
│ │ ├── components/
│ │ │ ├── Home.vue
│ │ │ ├── Login.vue
│ │ │ ├── SecretQuote.vue
│ │ │ ├── Signup.vue
│ │ │ └── UserInfo.vue
│ │ └── main.js
│ └── static/
│ └── .gitkeep
├── models/
│ ├── db.go
│ ├── quotes.go
│ └── users.go
├── routes/
│ └── routes.go
└── server.go
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
# file types
*.o
*.a
*.so
*.exe
*.test
*.prof
*.db
*.DS_Store
# folders
vendor
node_modules
tmp
================================================
FILE: .vscode/launch.json
================================================
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {},
"args": [],
"showLog": true
}
]
}
================================================
FILE: Godeps/Godeps.json
================================================
{
"ImportPath": "github.com/markcheno/go-vue-starter",
"GoVersion": "go1.8",
"GodepVersion": "v79",
"Deps": [
{
"ImportPath": "github.com/auth0/go-jwt-middleware",
"Rev": "f3f7de3b9e394e3af3b88e1b9457f6f71d1ae0ac"
},
{
"ImportPath": "github.com/dgrijalva/jwt-go",
"Comment": "v3.0.0-17-g2268707",
"Rev": "2268707a8f0843315e2004ee4f1d021dc08baedf"
},
{
"ImportPath": "github.com/gorilla/mux",
"Comment": "v1.3.0-5-g599cba5",
"Rev": "599cba5e7b6137d46ddf58fb1765f5d928e69604"
},
{
"ImportPath": "github.com/jinzhu/gorm",
"Comment": "v1.0-138-g45ccb13",
"Rev": "45ccb134373e7d9fa76d5987a6fed6cd5a5adfd4"
},
{
"ImportPath": "github.com/jinzhu/gorm/dialects/postgres",
"Comment": "v1.0-138-g45ccb13",
"Rev": "45ccb134373e7d9fa76d5987a6fed6cd5a5adfd4"
},
{
"ImportPath": "github.com/jinzhu/gorm/dialects/sqlite",
"Comment": "v1.0-138-g45ccb13",
"Rev": "45ccb134373e7d9fa76d5987a6fed6cd5a5adfd4"
},
{
"ImportPath": "github.com/jinzhu/inflection",
"Rev": "1c35d901db3da928c72a72d8458480cc9ade058f"
},
{
"ImportPath": "github.com/lib/pq",
"Comment": "go1.0-cutoff-166-g2704adc",
"Rev": "2704adc878c21e1329f46f6e56a1c387d788ff94"
},
{
"ImportPath": "github.com/lib/pq/hstore",
"Comment": "go1.0-cutoff-166-g2704adc",
"Rev": "2704adc878c21e1329f46f6e56a1c387d788ff94"
},
{
"ImportPath": "github.com/lib/pq/oid",
"Comment": "go1.0-cutoff-166-g2704adc",
"Rev": "2704adc878c21e1329f46f6e56a1c387d788ff94"
},
{
"ImportPath": "github.com/mattn/go-sqlite3",
"Comment": "v1.2.0-80-gcf7286f",
"Rev": "cf7286f069c3ef596efcc87781a4653a2e7607bd"
},
{
"ImportPath": "github.com/satori/go.uuid",
"Comment": "v1.1.0-8-g5bf94b6",
"Rev": "5bf94b69c6b68ee1b541973bb8e1144db23a194b"
},
{
"ImportPath": "github.com/urfave/negroni",
"Comment": "v0.2.0-104-gc0db5fe",
"Rev": "c0db5feaa33826cd5117930c8f4ee5c0f565eec6"
},
{
"ImportPath": "golang.org/x/crypto/bcrypt",
"Rev": "9ef620b9ca2f82b55030ffd4f41327fa9e77a92c"
},
{
"ImportPath": "golang.org/x/crypto/blowfish",
"Rev": "9ef620b9ca2f82b55030ffd4f41327fa9e77a92c"
},
{
"ImportPath": "golang.org/x/net/context",
"Rev": "d1e1b351919c6738fdeb9893d5c998b161464f0c"
}
]
}
================================================
FILE: Godeps/Readme
================================================
This directory tree is generated automatically by godep.
Please do not edit.
See https://github.com/tools/godep for more information.
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2017 Mark Chenoweth
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# go-vue-starter
Copyright 2017 Mark Chenoweth
## Golang Starter project with Vue.js single page client
### Work in progress...
### Features:
- Middleware: [Negroni](https://github.com/urfave/negroni)
- Router: [Gorilla](https://github.com/gorilla/mux)
- Orm: [Gorm](https://github.com/jinzhu/gorm) (sqlite or postgres)
- Jwt authentication: [jwt-go](https://github.com/dgrijalva/jwt-go) and [go-jwt-middleware](https://github.com/auth0/go-jwt-middleware)
- [Vue.js](https://vuejs.org/) spa client with webpack
- User management
### TODO:
- config from file
- email confirmation
- logrus
- letsencrypt tls
### To get started:
``` bash
# clone repository
go get github.com/markcheno/go-vue-starter
cd $GOPATH/src/github.com/markcheno/go-vue-starter
# install Go dependencies (and make sure ports 3000/8080 are open)
go get -u ./...
go run server.go
# open a new terminal and change to the client dir
cd client
# install dependencies
npm install
# serve with hot reload at localhost:8080
npm run dev
# build for production with minification
npm run build
```
### License
MIT License - see LICENSE for more details
================================================
FILE: api/api.go
================================================
package api
import (
"github.com/markcheno/go-vue-starter/models"
)
// API -
type API struct {
users *models.UserManager
quotes *models.QuoteManager
}
// NewAPI -
func NewAPI(db *models.DB) *API {
usermgr, _ := models.NewUserManager(db)
quotemgr, _ := models.NewQuoteManager(db)
return &API{
users: usermgr,
quotes: quotemgr,
}
}
================================================
FILE: api/quotes.go
================================================
package api
import "net/http"
// Quote -
func (api *API) Quote(w http.ResponseWriter, req *http.Request) {
quote := api.quotes.RandomQuote()
w.Write([]byte(quote.Text))
}
// SecretQuote -
func (api *API) SecretQuote(w http.ResponseWriter, req *http.Request) {
quote := api.quotes.RandomQuote()
w.Write([]byte(quote.Text))
}
================================================
FILE: api/users.go
================================================
package api
import (
"encoding/json"
"net/http"
"github.com/markcheno/go-vue-starter/auth"
"github.com/markcheno/go-vue-starter/models"
)
// UserJSON - json data expected for login/signup
type UserJSON struct {
Username string `json:"username"`
Password string `json:"password"`
}
// UserSignup -
func (api *API) UserSignup(w http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
jsondata := UserJSON{}
err := decoder.Decode(&jsondata)
if err != nil || jsondata.Username == "" || jsondata.Password == "" {
http.Error(w, "Missing username or password", http.StatusBadRequest)
return
}
if api.users.HasUser(jsondata.Username) {
http.Error(w, "username already exists", http.StatusBadRequest)
return
}
user := api.users.AddUser(jsondata.Username, jsondata.Password)
jsontoken := auth.GetJSONToken(user)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(jsontoken))
}
// UserLogin -
func (api *API) UserLogin(w http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
jsondata := UserJSON{}
err := decoder.Decode(&jsondata)
if err != nil || jsondata.Username == "" || jsondata.Password == "" {
http.Error(w, "Missing username or password", http.StatusBadRequest)
return
}
user := api.users.FindUser(jsondata.Username)
if user.Username == "" {
http.Error(w, "username not found", http.StatusBadRequest)
return
}
if !api.users.CheckPassword(user.Password, jsondata.Password) {
http.Error(w, "bad password", http.StatusBadRequest)
return
}
jsontoken := auth.GetJSONToken(user)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(jsontoken))
}
// GetUserFromContext - return User reference from header token
func (api *API) GetUserFromContext(req *http.Request) *models.User {
userclaims := auth.GetUserClaimsFromContext(req)
user := api.users.FindUserByUUID(userclaims["uuid"].(string))
return user
}
// UserInfo - example to get
func (api *API) UserInfo(w http.ResponseWriter, req *http.Request) {
user := api.GetUserFromContext(req)
js, _ := json.Marshal(user)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
================================================
FILE: auth/auth.go
================================================
package auth
import (
"net/http"
"time"
jwtmiddleware "github.com/auth0/go-jwt-middleware"
jwt "github.com/dgrijalva/jwt-go"
"github.com/markcheno/go-vue-starter/models"
)
// signingKey set up a global string for our secret
var signingKey = []byte("knrjkevdckjh")
// JwtMiddleware handler for jwt tokens
var JwtMiddleware = jwtmiddleware.New(jwtmiddleware.Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
return signingKey, nil
},
UserProperty: "user",
Debug: false,
SigningMethod: jwt.SigningMethodHS256,
})
// GetToken create a jwt token with user claims
func GetToken(user *models.User) string {
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims)
claims["uuid"] = user.UUID
claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
signedToken, _ := token.SignedString(signingKey)
return signedToken
}
// GetJSONToken create a JSON token string
func GetJSONToken(user *models.User) string {
token := GetToken(user)
jsontoken := "{\"id_token\": \"" + token + "\"}"
return jsontoken
}
// GetUserClaimsFromContext return "user" claims as a map from request
func GetUserClaimsFromContext(req *http.Request) map[string]interface{} {
//claims := context.Get(req, "user").(*jwt.Token).Claims.(jwt.MapClaims)
claims := req.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
return claims
}
================================================
FILE: client/.babelrc
================================================
{
"presets": [
["env", { "modules": false }],
"stage-2"
],
"plugins": ["transform-runtime"],
"comments": false,
"env": {
"test": {
"presets": ["env", "stage-2"],
"plugins": [ "istanbul" ]
}
}
}
================================================
FILE: client/.editorconfig
================================================
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
================================================
FILE: client/.eslintignore
================================================
build/*.js
config/*.js
================================================
FILE: client/.eslintrc.js
================================================
// http://eslint.org/docs/user-guide/configuring
module.exports = {
root: true,
parser: 'babel-eslint',
parserOptions: {
sourceType: 'module'
},
env: {
browser: true,
},
// https://github.com/feross/standard/blob/master/RULES.md#javascript-standard-style
extends: 'standard',
// required to lint *.vue files
plugins: [
'html'
],
// add your custom rules here
'rules': {
// allow paren-less arrow functions
'arrow-parens': 0,
// allow async-await
'generator-star-spacing': 0,
// allow debugger during development
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0
}
}
================================================
FILE: client/.gitignore
================================================
.DS_Store
node_modules/
dist/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
test/unit/coverage
test/e2e/reports
selenium-debug.log
================================================
FILE: client/.postcssrc.js
================================================
// https://github.com/michael-ciniawsky/postcss-load-config
module.exports = {
"plugins": {
// to edit target browsers: use "browserlist" field in package.json
"autoprefixer": {}
}
}
================================================
FILE: client/README.md
================================================
# starter
> Go/Vue starter project
## Build Setup
``` bash
# install dependencies
npm install
# serve with hot reload at localhost:8080
npm run dev
# build for production with minification
npm run build
# build for production and view the bundle analyzer report
npm run build --report
# run unit tests
npm run unit
# run e2e tests
npm run e2e
# run all tests
npm test
```
For detailed explanation on how things work, checkout the [guide](http://vuejs-templates.github.io/webpack/) and [docs for vue-loader](http://vuejs.github.io/vue-loader).
================================================
FILE: client/build/build.js
================================================
require('./check-versions')()
process.env.NODE_ENV = 'production'
var ora = require('ora')
var rm = require('rimraf')
var path = require('path')
var chalk = require('chalk')
var webpack = require('webpack')
var config = require('../config')
var webpackConfig = require('./webpack.prod.conf')
var spinner = ora('building for production...')
spinner.start()
rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
if (err) throw err
webpack(webpackConfig, function (err, stats) {
spinner.stop()
if (err) throw err
process.stdout.write(stats.toString({
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false
}) + '\n\n')
console.log(chalk.cyan(' Build complete.\n'))
console.log(chalk.yellow(
' Tip: built files are meant to be served over an HTTP server.\n' +
' Opening index.html over file:// won\'t work.\n'
))
})
})
================================================
FILE: client/build/check-versions.js
================================================
var chalk = require('chalk')
var semver = require('semver')
var packageConfig = require('../package.json')
var shell = require('shelljs')
function exec (cmd) {
return require('child_process').execSync(cmd).toString().trim()
}
var versionRequirements = [
{
name: 'node',
currentVersion: semver.clean(process.version),
versionRequirement: packageConfig.engines.node
},
]
if (shell.which('npm')) {
versionRequirements.push({
name: 'npm',
currentVersion: exec('npm --version'),
versionRequirement: packageConfig.engines.npm
})
}
module.exports = function () {
var warnings = []
for (var i = 0; i < versionRequirements.length; i++) {
var mod = versionRequirements[i]
if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
warnings.push(mod.name + ': ' +
chalk.red(mod.currentVersion) + ' should be ' +
chalk.green(mod.versionRequirement)
)
}
}
if (warnings.length) {
console.log('')
console.log(chalk.yellow('To use this template, you must update following to modules:'))
console.log()
for (var i = 0; i < warnings.length; i++) {
var warning = warnings[i]
console.log(' ' + warning)
}
console.log()
process.exit(1)
}
}
================================================
FILE: client/build/dev-client.js
================================================
/* eslint-disable */
require('eventsource-polyfill')
var hotClient = require('webpack-hot-middleware/client?noInfo=true&reload=true')
hotClient.subscribe(function (event) {
if (event.action === 'reload') {
window.location.reload()
}
})
================================================
FILE: client/build/dev-server.js
================================================
require('./check-versions')()
var config = require('../config')
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
}
var opn = require('opn')
var path = require('path')
var express = require('express')
var webpack = require('webpack')
var proxyMiddleware = require('http-proxy-middleware')
var webpackConfig = process.env.NODE_ENV === 'testing'
? require('./webpack.prod.conf')
: require('./webpack.dev.conf')
// default port where dev server listens for incoming traffic
var port = process.env.PORT || config.dev.port
// automatically open browser, if not set will be false
var autoOpenBrowser = !!config.dev.autoOpenBrowser
// Define HTTP proxies to your custom API backend
// https://github.com/chimurai/http-proxy-middleware
var proxyTable = config.dev.proxyTable
var app = express()
var compiler = webpack(webpackConfig)
var devMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
quiet: true
})
var hotMiddleware = require('webpack-hot-middleware')(compiler, {
log: () => {}
})
// force page reload when html-webpack-plugin template changes
compiler.plugin('compilation', function (compilation) {
compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
hotMiddleware.publish({ action: 'reload' })
cb()
})
})
// proxy api requests
Object.keys(proxyTable).forEach(function (context) {
var options = proxyTable[context]
if (typeof options === 'string') {
options = { target: options }
}
app.use(proxyMiddleware(options.filter || context, options))
})
// handle fallback for HTML5 history API
app.use(require('connect-history-api-fallback')())
// serve webpack bundle output
app.use(devMiddleware)
// enable hot-reload and state-preserving
// compilation error display
app.use(hotMiddleware)
// serve pure static assets
var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
app.use(staticPath, express.static('./static'))
var uri = 'http://localhost:' + port
var _resolve
var readyPromise = new Promise(resolve => {
_resolve = resolve
})
console.log('> Starting dev server...')
devMiddleware.waitUntilValid(() => {
console.log('> Listening at ' + uri + '\n')
// when env is testing, don't need open it
if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
opn(uri)
}
_resolve()
})
var server = app.listen(port)
module.exports = {
mounted: readyPromise,
close: () => {
server.close()
}
}
================================================
FILE: client/build/utils.js
================================================
var path = require('path')
var config = require('../config')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
exports.assetsPath = function (_path) {
var assetsSubDirectory = process.env.NODE_ENV === 'production'
? config.build.assetsSubDirectory
: config.dev.assetsSubDirectory
return path.posix.join(assetsSubDirectory, _path)
}
exports.cssLoaders = function (options) {
options = options || {}
var cssLoader = {
loader: 'css-loader',
options: {
minimize: process.env.NODE_ENV === 'production',
sourceMap: options.sourceMap
}
}
// generate loader string to be used with extract text plugin
function generateLoaders (loader, loaderOptions) {
var loaders = [cssLoader]
if (loader) {
loaders.push({
loader: loader + '-loader',
options: Object.assign({}, loaderOptions, {
sourceMap: options.sourceMap
})
})
}
// Extract CSS when that option is specified
// (which is the case during production build)
if (options.extract) {
return ExtractTextPlugin.extract({
use: loaders,
fallback: 'vue-style-loader'
})
} else {
return ['vue-style-loader'].concat(loaders)
}
}
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
return {
css: generateLoaders(),
postcss: generateLoaders(),
less: generateLoaders('less'),
sass: generateLoaders('sass', { indentedSyntax: true }),
scss: generateLoaders('sass'),
stylus: generateLoaders('stylus'),
styl: generateLoaders('stylus')
}
}
// Generate loaders for standalone style files (outside of .vue)
exports.styleLoaders = function (options) {
var output = []
var loaders = exports.cssLoaders(options)
for (var extension in loaders) {
var loader = loaders[extension]
output.push({
test: new RegExp('\\.' + extension + '$'),
use: loader
})
}
return output
}
================================================
FILE: client/build/vue-loader.conf.js
================================================
var utils = require('./utils')
var config = require('../config')
var isProduction = process.env.NODE_ENV === 'production'
module.exports = {
loaders: utils.cssLoaders({
sourceMap: isProduction
? config.build.productionSourceMap
: config.dev.cssSourceMap,
extract: isProduction
})
}
================================================
FILE: client/build/webpack.base.conf.js
================================================
var path = require('path')
var utils = require('./utils')
var config = require('../config')
var vueLoaderConfig = require('./vue-loader.conf')
function resolve (dir) {
return path.join(__dirname, '..', dir)
}
module.exports = {
entry: {
app: './src/main.js'
},
output: {
path: config.build.assetsRoot,
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'production'
? config.build.assetsPublicPath
: config.dev.assetsPublicPath
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src')
}
},
module: {
rules: [
{
test: /\.(js|vue)$/,
loader: 'eslint-loader',
enforce: 'pre',
include: [resolve('src'), resolve('test')],
options: {
formatter: require('eslint-friendly-formatter')
}
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test')]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
}
}
================================================
FILE: client/build/webpack.dev.conf.js
================================================
var utils = require('./utils')
var webpack = require('webpack')
var config = require('../config')
var merge = require('webpack-merge')
var baseWebpackConfig = require('./webpack.base.conf')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
// add hot-reload related code to entry chunks
Object.keys(baseWebpackConfig.entry).forEach(function (name) {
baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name])
})
module.exports = merge(baseWebpackConfig, {
module: {
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
},
// cheap-module-eval-source-map is faster for development
devtool: '#cheap-module-eval-source-map',
plugins: [
new webpack.DefinePlugin({
'process.env': config.dev.env
}),
// https://github.com/glenjamin/webpack-hot-middleware#installation--usage
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
// https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true
}),
new FriendlyErrorsPlugin()
]
})
================================================
FILE: client/build/webpack.prod.conf.js
================================================
var path = require('path')
var utils = require('./utils')
var webpack = require('webpack')
var config = require('../config')
var merge = require('webpack-merge')
var baseWebpackConfig = require('./webpack.base.conf')
var CopyWebpackPlugin = require('copy-webpack-plugin')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
var env = process.env.NODE_ENV === 'testing'
? require('../config/test.env')
: config.build.env
var webpackConfig = merge(baseWebpackConfig, {
module: {
rules: utils.styleLoaders({
sourceMap: config.build.productionSourceMap,
extract: true
})
},
devtool: config.build.productionSourceMap ? '#source-map' : false,
output: {
path: config.build.assetsRoot,
filename: utils.assetsPath('js/[name].[chunkhash].js'),
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
},
plugins: [
// http://vuejs.github.io/vue-loader/en/workflow/production.html
new webpack.DefinePlugin({
'process.env': env
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
sourceMap: true
}),
// extract css into its own file
new ExtractTextPlugin({
filename: utils.assetsPath('css/[name].[contenthash].css')
}),
// Compress extracted CSS. We are using this plugin so that possible
// duplicated CSS from different components can be deduped.
new OptimizeCSSPlugin({
cssProcessorOptions: {
safe: true
}
}),
// generate dist index.html with correct asset hash for caching.
// you can customize output by editing /index.html
// see https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: process.env.NODE_ENV === 'testing'
? 'index.html'
: config.build.index,
template: 'index.html',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency'
}),
// split vendor js into its own file
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module, count) {
// any required modules inside node_modules are extracted to vendor
return (
module.resource &&
/\.js$/.test(module.resource) &&
module.resource.indexOf(
path.join(__dirname, '../node_modules')
) === 0
)
}
}),
// extract webpack runtime and module manifest to its own file in order to
// prevent vendor hash from being updated whenever app bundle is updated
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
chunks: ['vendor']
}),
// copy custom static assets
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.build.assetsSubDirectory,
ignore: ['.*']
}
])
]
})
if (config.build.productionGzip) {
var CompressionWebpackPlugin = require('compression-webpack-plugin')
webpackConfig.plugins.push(
new CompressionWebpackPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp(
'\\.(' +
config.build.productionGzipExtensions.join('|') +
')$'
),
threshold: 10240,
minRatio: 0.8
})
)
}
if (config.build.bundleAnalyzerReport) {
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
webpackConfig.plugins.push(new BundleAnalyzerPlugin())
}
module.exports = webpackConfig
================================================
FILE: client/config/dev.env.js
================================================
var merge = require('webpack-merge')
var prodEnv = require('./prod.env')
module.exports = merge(prodEnv, {
NODE_ENV: '"development"'
})
================================================
FILE: client/config/index.js
================================================
// see http://vuejs-templates.github.io/webpack for documentation.
var path = require('path')
module.exports = {
build: {
env: require('./prod.env'),
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/',
productionSourceMap: false,
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
},
dev: {
env: require('./dev.env'),
port: 8080,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
// proxyTable: {},
proxyTable: {
'/api': {
target: 'http://localhost:3000/api',
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
},
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false
}
}
================================================
FILE: client/config/prod.env.js
================================================
module.exports = {
NODE_ENV: '"production"'
}
================================================
FILE: client/index.html
================================================
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Quote Server</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div id="app"></div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>
================================================
FILE: client/package.json
================================================
{
"name": "starter",
"version": "1.0.0",
"description": "Go/Vue starter project",
"author": "Mark Chenoweth",
"private": true,
"scripts": {
"dev": "node build/dev-server.js",
"start": "node build/dev-server.js",
"build": "node build/build.js",
"lint": "eslint --ext .js,.vue src"
},
"dependencies": {
"hoek": "^5.0.3",
"mime": "^2.3.1",
"npm": "^6.14.6",
"tough-cookie": "^2.4.3",
"vue": "^2.5.16",
"vue-resource": "^1.5.0",
"vue-router": "^2.8.1"
},
"devDependencies": {
"autoprefixer": "^6.7.2",
"babel-core": "^6.26.2",
"babel-eslint": "^7.2.3",
"babel-loader": "^6.2.10",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.26.0",
"chalk": "^1.1.3",
"chromedriver": "^2.38.2",
"compression-webpack-plugin": "^2.0.0",
"connect-history-api-fallback": "^1.5.0",
"copy-webpack-plugin": "^4.5.1",
"css-loader": "^2.1.1",
"eslint": "^4.19.1",
"eslint-config-standard": "^6.2.1",
"eslint-friendly-formatter": "^2.0.7",
"eslint-loader": "^1.9.0",
"eslint-plugin-html": "^4.0.1",
"eslint-plugin-promise": "^3.7.0",
"eslint-plugin-standard": "^2.0.1",
"eventsource-polyfill": "^0.9.6",
"express": "^4.18.2",
"extract-text-webpack-plugin": "^2.1.2",
"file-loader": "^0.10.0",
"friendly-errors-webpack-plugin": "^1.7.0",
"html-webpack-plugin": "^2.30.1",
"http-proxy-middleware": "^0.19.1",
"opn": "^4.0.2",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"ora": "^1.4.0",
"rimraf": "^2.6.2",
"semver": "^5.5.0",
"shelljs": "^0.8.5",
"url-loader": "^1.1.2",
"vue-loader": "^11.1.4",
"vue-style-loader": "^2.0.0",
"vue-template-compiler": "^2.5.16",
"webpack": "^2.7.0",
"webpack-bundle-analyzer": "^4.8.0",
"webpack-dev-middleware": "^1.12.2",
"webpack-hot-middleware": "^2.25.3",
"webpack-merge": "^2.6.1"
},
"engines": {
"node": ">= 4.0.0",
"npm": ">= 3.0.0"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}
================================================
FILE: client/src/App.vue
================================================
<template>
<div id="app">
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><router-link to="home">Home</router-link></li>
<li v-if="!isAuthenticated()"><router-link to="login">Login</router-link></li>
<li v-if="!isAuthenticated()"><router-link to="signup">Sign Up</router-link></li>
<li v-if="isAuthenticated()"><router-link to="secretquote">Secret Quote</router-link></li>
<li v-if="isAuthenticated()"><router-link to="userinfo">User Info</router-link></li>
<li v-if="isAuthenticated()"><router-link to="logout" @click.native="logout()">Logout</router-link></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<router-view></router-view>
</div>
</div>
</template>
<script>
import auth from './auth'
export default {
data () {
return {
}
},
methods: {
logout () {
auth.logout(this)
},
isAuthenticated () {
return auth.isAuthenticated()
}
}
}
</script>
================================================
FILE: client/src/auth/index.js
================================================
const API_URL = '/api/user/'
const LOGIN_URL = API_URL + 'login'
const SIGNUP_URL = API_URL + 'signup'
export default {
login (context, creds, redirect) {
context.$http.post(LOGIN_URL, creds).then(response => {
localStorage.setItem('id_token', response.body.id_token)
if (redirect) {
context.$router.replace(redirect)
}
}, response => {
context.error = response.statusText
})
},
signup (context, creds, redirect) {
context.$http.post(SIGNUP_URL, creds).then(response => {
localStorage.setItem('id_token', response.body.id_token)
if (redirect) {
context.$router.replace(redirect)
}
}, response => {
context.error = response.statusText
})
},
logout (context) {
localStorage.removeItem('id_token')
context.$router.replace('/home')
},
isAuthenticated () {
var jwt = localStorage.getItem('id_token')
if (jwt) {
return true
}
return false
},
getAuthHeader () {
return {
'Authorization': 'Bearer ' + localStorage.getItem('id_token')
}
}
}
================================================
FILE: client/src/components/Home.vue
================================================
<template>
<div class="col-sm-6 col-sm-offset-3">
<h1>Get a public quote!</h1>
<button class="btn btn-primary" v-on:click="getQuote()">Get a Quote</button>
<div class="quote-area" v-if="quote">
<h2><blockquote>{{ quote }}</blockquote></h2>
</div>
</div>
</template>
<script>
export default {
data () {
return {
quote: ''
}
},
methods: {
getQuote () {
this.$http.get('/api/quote/random').then(response => {
this.quote = response.body
}, response => {
console.log(response)
})
}
}
}
</script>
================================================
FILE: client/src/components/Login.vue
================================================
<template>
<div class="col-sm-4 col-sm-offset-4">
<h2>Log In</h2>
<div class="alert alert-danger" v-if="error">
<p>{{ error }}</p>
</div>
<div class="form-group">
<input
type="text"
class="form-control"
placeholder="Enter your username"
v-model="credentials.username"
v-on:keyup.enter="submit"
>
</div>
<div class="form-group">
<input
type="password"
class="form-control"
placeholder="Enter your password"
v-model="credentials.password"
v-on:keyup.enter="submit"
>
</div>
<button class="btn btn-primary" @click="submit">Login</button>
</div>
</template>
<script>
import auth from '../auth'
export default {
data () {
return {
credentials: {
username: '',
password: ''
},
error: ''
}
},
methods: {
submit () {
var credentials = {
username: this.credentials.username,
password: this.credentials.password
}
auth.login(this, credentials, 'secretquote')
}
}
}
</script>
================================================
FILE: client/src/components/SecretQuote.vue
================================================
<template>
<div class="col-sm-6 col-sm-offset-3">
<h1>Get a secret quote!</h1>
<button class="btn btn-warning" v-on:click="getQuote()">Get a Quote</button>
<div class="quote-area" v-if="quote">
<h2>
<blockquote>{{ quote }}</blockquote>
</h2>
</div>
</div>
</template>
<script>
import auth from '../auth'
export default {
data () {
return {
quote: ''
}
},
methods: {
getQuote () {
this.$http
.get('/api/quote/protected/random', { headers: auth.getAuthHeader() })
.then(
response => {
this.quote = response.body
},
response => {
if (response.status === 401) {
auth.logout(this)
}
console.log(response)
}
)
}
}
}
</script>
================================================
FILE: client/src/components/Signup.vue
================================================
<template>
<div class="col-sm-4 col-sm-offset-4">
<h2>Sign Up</h2>
<div class="alert alert-danger" v-if="error">
<p>{{ error }}</p>
</div>
<div class="form-group">
<input
type="text"
class="form-control"
placeholder="Enter your username"
v-model="credentials.username"
v-on:keyup.enter="submit"
>
</div>
<div class="form-group">
<input
type="password"
class="form-control"
placeholder="Enter your password"
v-model="credentials.password"
v-on:keyup.enter="submit"
>
</div>
<button class="btn btn-primary" @click="submit">Signup</button>
</div>
</template>
<script>
import auth from '../auth'
export default {
data () {
return {
credentials: {
username: '',
password: ''
},
error: ''
}
},
methods: {
submit () {
var credentials = {
username: this.credentials.username,
password: this.credentials.password
}
// console.log('signup: credentials=', credentials)
auth.signup(this, credentials, 'secretquote')
}
}
}
</script>
================================================
FILE: client/src/components/UserInfo.vue
================================================
<template>
<div class="col-sm-6 col-sm-offset-3">
<h1>User Info</h1>
<pre>{{ userinfo }}</pre>
</div>
</template>
<script>
import auth from '../auth'
export default {
data () {
return {
userinfo: ''
}
},
mounted () {
this.getUserInfo()
},
methods: {
getUserInfo () {
this.$http.get('/api/user/info', { headers: auth.getAuthHeader() }).then(
response => {
this.userinfo = response.body
},
response => {
if (response.status === 401) {
auth.logout(this)
}
console.log(response)
}
)
}
}
}
</script>
================================================
FILE: client/src/main.js
================================================
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import Home from '@/components/Home'
import Login from '@/components/Login'
import Signup from '@/components/Signup'
import SecretQuote from '@/components/SecretQuote'
import UserInfo from '@/components/UserInfo'
import VueResource from 'vue-resource'
Vue.use(VueResource)
import VueRouter from 'vue-router'
Vue.use(VueRouter)
Vue.config.productionTip = true
import auth from './auth'
function requireAuth (to, from, next) {
if (!auth.isAuthenticated()) {
this.$router.replace('/login')
} else {
next()
}
}
const router = new VueRouter({
mode: 'history',
// base: __dirname,
routes: [
{
path: '/',
component: Home
},
{
path: '/home',
name: 'home',
component: Home
},
{
path: '/login',
name: 'login',
component: Login
},
{
path: '/signup',
name: 'signup',
component: Signup
},
{
path: '/secretquote',
name: 'secretquote',
component: SecretQuote,
beforeEnter: requireAuth
},
{
path: '/userinfo',
name: 'userinfo',
component: UserInfo,
beforeEnter: requireAuth
}
]
})
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
template: '<App/>',
components: { App }
})
================================================
FILE: client/static/.gitkeep
================================================
================================================
FILE: models/db.go
================================================
package models
import (
"github.com/jinzhu/gorm"
// postgress db driver
_ "github.com/jinzhu/gorm/dialects/postgres"
// sqlite db driver
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
// DB abstraction
type DB struct {
*gorm.DB
}
// NewPostgresDB - postgres database
func NewPostgresDB(dataSourceName string) *DB {
db, err := gorm.Open("postgres", dataSourceName)
if err != nil {
panic(err)
}
if err = db.DB().Ping(); err != nil {
panic(err)
}
//db.LogMode(true)
return &DB{db}
}
// NewSqliteDB - sqlite database
func NewSqliteDB(databaseName string) *DB {
db, err := gorm.Open("sqlite3", databaseName)
if err != nil {
panic(err)
}
if err = db.DB().Ping(); err != nil {
panic(err)
}
//db.LogMode(true)
return &DB{db}
}
================================================
FILE: models/quotes.go
================================================
package models
import (
"math/rand"
"github.com/jinzhu/gorm"
// postgress db driver
_ "github.com/jinzhu/gorm/dialects/postgres"
// import sqlite3 driver
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
// Quote struct
type Quote struct {
gorm.Model
Text string
}
// QuoteManager struct
type QuoteManager struct {
db *DB
}
// NewQuoteManager - Create a quote manager that can be used for retrieving quotes
func NewQuoteManager(db *DB) (*QuoteManager, error) {
db.AutoMigrate(&Quote{})
quotemgr := QuoteManager{}
quotemgr.db = db
return "emgr, nil
}
// RandomQuote - return a random quote
func (qm *QuoteManager) RandomQuote() *Quote {
quote := Quote{
Text: quotes[rand.Intn(len(quotes))],
}
return "e
}
var quotes = []string{
"Chuck Norris doesn't call the wrong number. You answer the wrong phone.",
"Chuck Norris has already been to Mars; that's why there are no signs of life.",
"Chuck Norris and Superman once fought each other on a bet. The loser had to start wearing his underwear on the outside of his pants.",
"Some magicans can walk on water, Chuck Norris can swim through land.",
"Chuck Norris once urinated in a semi truck's gas tank as a joke....that truck is now known as Optimus Prime.",
"Chuck Norris doesn't flush the toilet, he scares the sh*t out of it",
"Chuck Norris counted to infinity - twice.",
"Chuck Norris can cut through a hot knife with butter",
"Chuck Norris is the reason why Waldo is hiding.",
"Death once had a near-Chuck Norris experience",
"When the Boogeyman goes to sleep every night, he checks his closet for Chuck Norris.",
"Chuck Norris can slam a revolving door.",
"Chuck Norris once kicked a horse in the chin. Its decendants are known today as Giraffes.",
"Chuck Norris will never have a heart attack. His heart isn't nearly foolish enough to attack him.",
"Chuck Norris once got bit by a rattle snake........ After three days of pain and agony ..................the rattle snake died",
"Chuck Norris can win a game of Connect Four in only three moves.",
"When Chuck Norris does a pushup, he isn't lifting himself up, he's pushing the Earth down.",
"There is no theory of evolution. Just a list of animals Chuck Norris allows to live.",
"Chuck Norris can light a fire by rubbing two ice-cubes together.",
"Chuck Norris doesn’t wear a watch. HE decides what time it is.",
"The original title for Alien vs. Predator was Alien and Predator vs Chuck Norris.",
"The film was cancelled shortly after going into preproduction. No one would pay nine dollars to see a movie fourteen seconds long.",
"Chuck Norris doesn't read books. He stares them down until he gets the information he wants.",
"Chuck Norris made a Happy Meal cry.",
"Outer space exists because it's afraid to be on the same planet with Chuck Norris.",
"If you spell Chuck Norris in Scrabble, you win. Forever.",
"Chuck Norris can make snow angels on a concrete slab.",
"Chuck Norris destroyed the periodic table, because Chuck Norris only recognizes the element of surprise.",
"Chuck Norris has to use a stunt double when he does crying scenes.",
"Chuck Norris' hand is the only hand that can beat a Royal Flush.",
"There is no theory of evolution. Just a list of creatures Chuck Norris has allowed to live.",
"Chuck Norris does not sleep. He waits.",
"Chuck Norris tells a GPS which way to go.",
"Some people wear Superman pajamas. Superman wears Chuck Norris pajamas.",
"Chuck Norris's tears cure cancer ..... to bad he has never cried",
"Chuck Norris doesn't breathe, he holds air hostage.",
"Chuck Norris had a staring contest with Medusa, and won.",
"When life hands Chuck Norris lemons, he makes orange juice.",
"When Chuck Norris goes on a picnic, the ants bring him food.",
"Chuck Norris gives Freddy Krueger nightmares.",
"They once made a Chuck Norris toilet paper, but there was a problem: It wouldn't take shit from anybody.",
"Chuck Norris can punch a cyclops between the eyes.",
"Chuck Norris doesn't mow his lawn, he stands on the porch and dares it to grow",
"Chuck Norris put out a forest fire. using only gasoline",
"Chuck Norris CAN believe it's not butter.",
"Custom t-shirts provided by Spreadshirt",
}
================================================
FILE: models/users.go
================================================
package models
import (
"github.com/jinzhu/gorm"
// postgress db driver
_ "github.com/jinzhu/gorm/dialects/postgres"
// import sqlite3 driver
_ "github.com/jinzhu/gorm/dialects/sqlite"
uuid "github.com/satori/go.uuid"
"golang.org/x/crypto/bcrypt"
)
// User struct
type User struct {
gorm.Model `json:"-"`
Username string `gorm:"not null;unique" json:"username"`
Password string `gorm:"not null" json:"-"`
UUID string `gorm:"not null;unique" json:"uuid"`
}
// UserManager struct
type UserManager struct {
db *DB
}
// NewUserManager - Create a new *UserManager that can be used for managing users.
func NewUserManager(db *DB) (*UserManager, error) {
db.AutoMigrate(&User{})
usermgr := UserManager{}
usermgr.db = db
return &usermgr, nil
}
// HasUser - Check if the given username exists.
func (state *UserManager) HasUser(username string) bool {
if err := state.db.Where("username=?", username).Find(&User{}).Error; err != nil {
return false
}
return true
}
// FindUser -
func (state *UserManager) FindUser(username string) *User {
user := User{}
state.db.Where("username=?", username).Find(&user)
return &user
}
// FindUserByUUID -
func (state *UserManager) FindUserByUUID(uuid string) *User {
user := User{}
state.db.Where("uuid=?", uuid).Find(&user)
return &user
}
// AddUser - Creates a user and hashes the password
func (state *UserManager) AddUser(username, password string) *User {
passwordHash := state.HashPassword(username, password)
guid, _ := uuid.NewV4()
user := &User{
Username: username,
Password: passwordHash,
UUID: guid.String(),
}
state.db.Create(&user)
return user
}
// HashPassword - Hash the password (takes a username as well, it can be used for salting).
func (state *UserManager) HashPassword(username, password string) string {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
panic("Permissions: bcrypt password hashing unsuccessful")
}
return string(hash)
}
// CheckPassword - compare a hashed password with a possible plaintext equivalent
func (state *UserManager) CheckPassword(hashedPassword, password string) bool {
if bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) != nil {
return false
}
return true
}
================================================
FILE: routes/routes.go
================================================
package routes
import (
"net/http"
"github.com/gorilla/mux"
"github.com/markcheno/go-vue-starter/api"
"github.com/markcheno/go-vue-starter/auth"
"github.com/urfave/negroni"
)
// NewRoutes builds the routes for the api
func NewRoutes(api *api.API) *mux.Router {
mux := mux.NewRouter()
// client static files
mux.Handle("/", http.FileServer(http.Dir("./client/dist/"))).Methods("GET")
mux.PathPrefix("/static/js").Handler(http.StripPrefix("/static/js/", http.FileServer(http.Dir("./client/dist/static/js/"))))
// api
a := mux.PathPrefix("/api").Subrouter()
// users
u := a.PathPrefix("/user").Subrouter()
u.HandleFunc("/signup", api.UserSignup).Methods("POST")
u.HandleFunc("/login", api.UserLogin).Methods("POST")
u.Handle("/info", negroni.New(
negroni.HandlerFunc(auth.JwtMiddleware.HandlerWithNext),
negroni.Wrap(http.HandlerFunc(api.UserInfo)),
))
// quotes
q := a.PathPrefix("/quote").Subrouter()
q.HandleFunc("/random", api.Quote).Methods("GET")
q.Handle("/protected/random", negroni.New(
negroni.HandlerFunc(auth.JwtMiddleware.HandlerWithNext),
negroni.Wrap(http.HandlerFunc(api.SecretQuote)),
))
return mux
}
================================================
FILE: server.go
================================================
package main
import (
"github.com/markcheno/go-vue-starter/api"
"github.com/markcheno/go-vue-starter/models"
"github.com/markcheno/go-vue-starter/routes"
"github.com/urfave/negroni"
)
func main() {
db := models.NewSqliteDB("data.db")
api := api.NewAPI(db)
routes := routes.NewRoutes(api)
n := negroni.Classic()
n.UseHandler(routes)
n.Run(":3000")
}
gitextract_tjk8vlva/ ├── .gitignore ├── .vscode/ │ └── launch.json ├── Godeps/ │ ├── Godeps.json │ └── Readme ├── LICENSE ├── README.md ├── api/ │ ├── api.go │ ├── quotes.go │ └── users.go ├── auth/ │ └── auth.go ├── client/ │ ├── .babelrc │ ├── .editorconfig │ ├── .eslintignore │ ├── .eslintrc.js │ ├── .gitignore │ ├── .postcssrc.js │ ├── README.md │ ├── build/ │ │ ├── build.js │ │ ├── check-versions.js │ │ ├── dev-client.js │ │ ├── dev-server.js │ │ ├── utils.js │ │ ├── vue-loader.conf.js │ │ ├── webpack.base.conf.js │ │ ├── webpack.dev.conf.js │ │ └── webpack.prod.conf.js │ ├── config/ │ │ ├── dev.env.js │ │ ├── index.js │ │ └── prod.env.js │ ├── index.html │ ├── package.json │ ├── src/ │ │ ├── App.vue │ │ ├── auth/ │ │ │ └── index.js │ │ ├── components/ │ │ │ ├── Home.vue │ │ │ ├── Login.vue │ │ │ ├── SecretQuote.vue │ │ │ ├── Signup.vue │ │ │ └── UserInfo.vue │ │ └── main.js │ └── static/ │ └── .gitkeep ├── models/ │ ├── db.go │ ├── quotes.go │ └── users.go ├── routes/ │ └── routes.go └── server.go
SYMBOL INDEX (42 symbols across 14 files)
FILE: api/api.go
type API (line 8) | type API struct
function NewAPI (line 14) | func NewAPI(db *models.DB) *API {
FILE: api/quotes.go
method Quote (line 6) | func (api *API) Quote(w http.ResponseWriter, req *http.Request) {
method SecretQuote (line 12) | func (api *API) SecretQuote(w http.ResponseWriter, req *http.Request) {
FILE: api/users.go
type UserJSON (line 12) | type UserJSON struct
method UserSignup (line 18) | func (api *API) UserSignup(w http.ResponseWriter, req *http.Request) {
method UserLogin (line 43) | func (api *API) UserLogin(w http.ResponseWriter, req *http.Request) {
method GetUserFromContext (line 73) | func (api *API) GetUserFromContext(req *http.Request) *models.User {
method UserInfo (line 80) | func (api *API) UserInfo(w http.ResponseWriter, req *http.Request) {
FILE: auth/auth.go
function GetToken (line 26) | func GetToken(user *models.User) string {
function GetJSONToken (line 36) | func GetJSONToken(user *models.User) string {
function GetUserClaimsFromContext (line 43) | func GetUserClaimsFromContext(req *http.Request) map[string]interface{} {
FILE: client/build/check-versions.js
function exec (line 5) | function exec (cmd) {
FILE: client/build/utils.js
function generateLoaders (line 24) | function generateLoaders (loader, loaderOptions) {
FILE: client/build/webpack.base.conf.js
function resolve (line 6) | function resolve (dir) {
FILE: client/src/auth/index.js
constant API_URL (line 1) | const API_URL = '/api/user/'
constant LOGIN_URL (line 2) | const LOGIN_URL = API_URL + 'login'
constant SIGNUP_URL (line 3) | const SIGNUP_URL = API_URL + 'signup'
method login (line 7) | login (context, creds, redirect) {
method signup (line 18) | signup (context, creds, redirect) {
method logout (line 29) | logout (context) {
method isAuthenticated (line 34) | isAuthenticated () {
method getAuthHeader (line 42) | getAuthHeader () {
FILE: client/src/main.js
function requireAuth (line 21) | function requireAuth (to, from, next) {
FILE: models/db.go
type DB (line 12) | type DB struct
function NewPostgresDB (line 17) | func NewPostgresDB(dataSourceName string) *DB {
function NewSqliteDB (line 34) | func NewSqliteDB(databaseName string) *DB {
FILE: models/quotes.go
type Quote (line 14) | type Quote struct
type QuoteManager (line 20) | type QuoteManager struct
method RandomQuote (line 37) | func (qm *QuoteManager) RandomQuote() *Quote {
function NewQuoteManager (line 25) | func NewQuoteManager(db *DB) (*QuoteManager, error) {
FILE: models/users.go
type User (line 14) | type User struct
type UserManager (line 22) | type UserManager struct
method HasUser (line 39) | func (state *UserManager) HasUser(username string) bool {
method FindUser (line 47) | func (state *UserManager) FindUser(username string) *User {
method FindUserByUUID (line 54) | func (state *UserManager) FindUserByUUID(uuid string) *User {
method AddUser (line 61) | func (state *UserManager) AddUser(username, password string) *User {
method HashPassword (line 74) | func (state *UserManager) HashPassword(username, password string) stri...
method CheckPassword (line 83) | func (state *UserManager) CheckPassword(hashedPassword, password strin...
function NewUserManager (line 27) | func NewUserManager(db *DB) (*UserManager, error) {
FILE: routes/routes.go
function NewRoutes (line 13) | func NewRoutes(api *api.API) *mux.Router {
FILE: server.go
function main (line 10) | func main() {
Condensed preview — 45 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (52K chars).
[
{
"path": ".gitignore",
"chars": 97,
"preview": "# file types\n*.o\n*.a\n*.so\n*.exe\n*.test\n*.prof\n*.db\n*.DS_Store\n\n# folders\nvendor\nnode_modules\ntmp\n"
},
{
"path": ".vscode/launch.json",
"chars": 320,
"preview": "{\n \"version\": \"0.2.0\",\n \"configurations\": [\n {\n \"name\": \"Launch\",\n \"type\": \"go\",\n \"request\": \"launch"
},
{
"path": "Godeps/Godeps.json",
"chars": 2287,
"preview": "{\n\t\"ImportPath\": \"github.com/markcheno/go-vue-starter\",\n\t\"GoVersion\": \"go1.8\",\n\t\"GodepVersion\": \"v79\",\n\t\"Deps\": [\n\t\t{\n\t\t"
},
{
"path": "Godeps/Readme",
"chars": 136,
"preview": "This directory tree is generated automatically by godep.\n\nPlease do not edit.\n\nSee https://github.com/tools/godep for mo"
},
{
"path": "LICENSE",
"chars": 1071,
"preview": "MIT License\n\nCopyright (c) 2017 Mark Chenoweth\n\nPermission is hereby granted, free of charge, to any person obtaining a "
},
{
"path": "README.md",
"chars": 1135,
"preview": "# go-vue-starter\n\nCopyright 2017 Mark Chenoweth\n\n## Golang Starter project with Vue.js single page client\n\n### Work in p"
},
{
"path": "api/api.go",
"chars": 348,
"preview": "package api\n\nimport (\n\t\"github.com/markcheno/go-vue-starter/models\"\n)\n\n// API -\ntype API struct {\n\tusers *models.UserMa"
},
{
"path": "api/quotes.go",
"chars": 331,
"preview": "package api\n\nimport \"net/http\"\n\n// Quote -\nfunc (api *API) Quote(w http.ResponseWriter, req *http.Request) {\n\tquote := a"
},
{
"path": "api/users.go",
"chars": 2176,
"preview": "package api\n\nimport (\n\t\"encoding/json\"\n\t\"net/http\"\n\n\t\"github.com/markcheno/go-vue-starter/auth\"\n\t\"github.com/markcheno/g"
},
{
"path": "auth/auth.go",
"chars": 1386,
"preview": "package auth\n\nimport (\n\t\"net/http\"\n\t\"time\"\n\n\tjwtmiddleware \"github.com/auth0/go-jwt-middleware\"\n\tjwt \"github.com/dgrijal"
},
{
"path": "client/.babelrc",
"chars": 234,
"preview": "{\n \"presets\": [\n [\"env\", { \"modules\": false }],\n \"stage-2\"\n ],\n \"plugins\": [\"transform-runtime\"],\n \"comments\":"
},
{
"path": "client/.editorconfig",
"chars": 147,
"preview": "root = true\n\n[*]\ncharset = utf-8\nindent_style = space\nindent_size = 2\nend_of_line = lf\ninsert_final_newline = true\ntrim_"
},
{
"path": "client/.eslintignore",
"chars": 23,
"preview": "build/*.js\nconfig/*.js\n"
},
{
"path": "client/.eslintrc.js",
"chars": 642,
"preview": "// http://eslint.org/docs/user-guide/configuring\n\nmodule.exports = {\n root: true,\n parser: 'babel-eslint',\n parserOpt"
},
{
"path": "client/.gitignore",
"chars": 132,
"preview": ".DS_Store\nnode_modules/\ndist/\nnpm-debug.log*\nyarn-debug.log*\nyarn-error.log*\ntest/unit/coverage\ntest/e2e/reports\nseleniu"
},
{
"path": "client/.postcssrc.js",
"chars": 196,
"preview": "// https://github.com/michael-ciniawsky/postcss-load-config\n\nmodule.exports = {\n \"plugins\": {\n // to edit target bro"
},
{
"path": "client/README.md",
"chars": 553,
"preview": "# starter\n\n> Go/Vue starter project\n\n## Build Setup\n\n``` bash\n# install dependencies\nnpm install\n\n# serve with hot reloa"
},
{
"path": "client/build/build.js",
"chars": 953,
"preview": "require('./check-versions')()\n\nprocess.env.NODE_ENV = 'production'\n\nvar ora = require('ora')\nvar rm = require('rimraf')\n"
},
{
"path": "client/build/check-versions.js",
"chars": 1257,
"preview": "var chalk = require('chalk')\nvar semver = require('semver')\nvar packageConfig = require('../package.json')\nvar shell = r"
},
{
"path": "client/build/dev-client.js",
"chars": 245,
"preview": "/* eslint-disable */\nrequire('eventsource-polyfill')\nvar hotClient = require('webpack-hot-middleware/client?noInfo=true&"
},
{
"path": "client/build/dev-server.js",
"chars": 2520,
"preview": "require('./check-versions')()\n\nvar config = require('../config')\nif (!process.env.NODE_ENV) {\n process.env.NODE_ENV = J"
},
{
"path": "client/build/utils.js",
"chars": 1949,
"preview": "var path = require('path')\nvar config = require('../config')\nvar ExtractTextPlugin = require('extract-text-webpack-plugi"
},
{
"path": "client/build/vue-loader.conf.js",
"chars": 307,
"preview": "var utils = require('./utils')\nvar config = require('../config')\nvar isProduction = process.env.NODE_ENV === 'production"
},
{
"path": "client/build/webpack.base.conf.js",
"chars": 1548,
"preview": "var path = require('path')\nvar utils = require('./utils')\nvar config = require('../config')\nvar vueLoaderConfig = requir"
},
{
"path": "client/build/webpack.dev.conf.js",
"chars": 1225,
"preview": "var utils = require('./utils')\nvar webpack = require('webpack')\nvar config = require('../config')\nvar merge = require('w"
},
{
"path": "client/build/webpack.prod.conf.js",
"chars": 3883,
"preview": "var path = require('path')\nvar utils = require('./utils')\nvar webpack = require('webpack')\nvar config = require('../conf"
},
{
"path": "client/config/dev.env.js",
"chars": 139,
"preview": "var merge = require('webpack-merge')\nvar prodEnv = require('./prod.env')\n\nmodule.exports = merge(prodEnv, {\n NODE_ENV: "
},
{
"path": "client/config/index.js",
"chars": 1626,
"preview": "// see http://vuejs-templates.github.io/webpack for documentation.\nvar path = require('path')\n\nmodule.exports = {\n buil"
},
{
"path": "client/config/prod.env.js",
"chars": 48,
"preview": "module.exports = {\n NODE_ENV: '\"production\"'\n}\n"
},
{
"path": "client/index.html",
"chars": 735,
"preview": "<!DOCTYPE html>\n<html>\n <head>\n <meta charset=\"utf-8\">\n <title>Quote Server</title>\n <link rel=\"stylesheet\" hr"
},
{
"path": "client/package.json",
"chars": 2165,
"preview": "{\n \"name\": \"starter\",\n \"version\": \"1.0.0\",\n \"description\": \"Go/Vue starter project\",\n \"author\": \"Mark Chenoweth\",\n "
},
{
"path": "client/src/App.vue",
"chars": 1575,
"preview": "<template>\n<div id=\"app\">\n\n <nav class=\"navbar navbar-default navbar-static-top\">\n <div class=\"container\">\n <di"
},
{
"path": "client/src/auth/index.js",
"chars": 1090,
"preview": "const API_URL = '/api/user/'\nconst LOGIN_URL = API_URL + 'login'\nconst SIGNUP_URL = API_URL + 'signup'\n\nexport default {"
},
{
"path": "client/src/components/Home.vue",
"chars": 582,
"preview": "<template>\n <div class=\"col-sm-6 col-sm-offset-3\">\n <h1>Get a public quote!</h1>\n <button class=\"btn btn-primary\""
},
{
"path": "client/src/components/Login.vue",
"chars": 1105,
"preview": "<template>\n <div class=\"col-sm-4 col-sm-offset-4\">\n <h2>Log In</h2>\n <div class=\"alert alert-danger\" v-if=\"error\""
},
{
"path": "client/src/components/SecretQuote.vue",
"chars": 824,
"preview": "<template>\n <div class=\"col-sm-6 col-sm-offset-3\">\n <h1>Get a secret quote!</h1>\n <button class=\"btn btn-warning\""
},
{
"path": "client/src/components/Signup.vue",
"chars": 1167,
"preview": "<template>\n <div class=\"col-sm-4 col-sm-offset-4\">\n <h2>Sign Up</h2>\n <div class=\"alert alert-danger\" v-if=\"error"
},
{
"path": "client/src/components/UserInfo.vue",
"chars": 643,
"preview": "<template>\n <div class=\"col-sm-6 col-sm-offset-3\">\n <h1>User Info</h1>\n <pre>{{ userinfo }}</pre>\n </div>\n</temp"
},
{
"path": "client/src/main.js",
"chars": 1451,
"preview": "// The Vue build version to load with the `import` command\n// (runtime-only or standalone) has been set in webpack.base."
},
{
"path": "client/static/.gitkeep",
"chars": 0,
"preview": ""
},
{
"path": "models/db.go",
"chars": 757,
"preview": "package models\n\nimport (\n\t\"github.com/jinzhu/gorm\"\n\t// postgress db driver\n\t_ \"github.com/jinzhu/gorm/dialects/postgres\""
},
{
"path": "models/quotes.go",
"chars": 4204,
"preview": "package models\n\nimport (\n\t\"math/rand\"\n\n\t\"github.com/jinzhu/gorm\"\n\t// postgress db driver\n\t_ \"github.com/jinzhu/gorm/dial"
},
{
"path": "models/users.go",
"chars": 2282,
"preview": "package models\n\nimport (\n\t\"github.com/jinzhu/gorm\"\n\t// postgress db driver\n\t_ \"github.com/jinzhu/gorm/dialects/postgres\""
},
{
"path": "routes/routes.go",
"chars": 1155,
"preview": "package routes\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/gorilla/mux\"\n\t\"github.com/markcheno/go-vue-starter/api\"\n\t\"github.com/"
},
{
"path": "server.go",
"chars": 361,
"preview": "package main\n\nimport (\n\t\"github.com/markcheno/go-vue-starter/api\"\n\t\"github.com/markcheno/go-vue-starter/models\"\n\t\"github"
}
]
About this extraction
This page contains the full source code of the markcheno/go-vue-starter GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 45 files (45.9 KB), approximately 13.6k tokens, and a symbol index with 42 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.