Skip to content
Permalink
Browse files

create project

  • Loading branch information...
sunface committed Aug 29, 2019
1 parent 6ab91b7 commit 123f177fb3c1160dce4dcf7962b57675da22d32f
Showing with 13,847 additions and 3 deletions.
  1. +0 −3 README.md
  2. +62 −0 cmd/root.go
  3. +12 −0 conf.yaml
  4. +12 −0 internal/api_handler.go
  5. +7 −0 internal/ecode/ecode.go
  6. +69 −0 internal/internal.go
  7. +36 −0 internal/misc/config.go
  8. +10 −0 internal/misc/http.go
  9. +40 −0 internal/misc/log.go
  10. +20 −0 internal/misc/var.go
  11. +55 −0 internal/session/session.go
  12. +21 −0 main.go
  13. +15 −0 quick-start/cql/init.cql
  14. +12 −0 ui/.babelrc
  15. +9 −0 ui/.editorconfig
  16. +14 −0 ui/.gitignore
  17. +10 −0 ui/.postcssrc.js
  18. +21 −0 ui/README.md
  19. +41 −0 ui/build/build.js
  20. +54 −0 ui/build/check-versions.js
  21. BIN ui/build/logo.png
  22. +101 −0 ui/build/utils.js
  23. +22 −0 ui/build/vue-loader.conf.js
  24. +86 −0 ui/build/webpack.base.conf.js
  25. +95 −0 ui/build/webpack.dev.conf.js
  26. +145 −0 ui/build/webpack.prod.conf.js
  27. +8 −0 ui/config/dev.env.js
  28. +69 −0 ui/config/index.js
  29. +4 −0 ui/config/prod.env.js
  30. +12 −0 ui/index.html
  31. +11,708 −0 ui/package-lock.json
  32. +71 −0 ui/package.json
  33. +21 −0 ui/src/App.vue
  34. BIN ui/src/assets/401_images/401.gif
  35. BIN ui/src/assets/404_images/404.png
  36. BIN ui/src/assets/404_images/404_cloud.png
  37. BIN ui/src/assets/login.png
  38. BIN ui/src/assets/logo.png
  39. +11 −0 ui/src/lang/en.js
  40. +33 −0 ui/src/lang/index.js
  41. +11 −0 ui/src/lang/zh.js
  42. +43 −0 ui/src/main.js
  43. +20 −0 ui/src/router/index.js
  44. +7 −0 ui/src/store/getters.js
  45. +18 −0 ui/src/store/index.js
  46. +28 −0 ui/src/store/modules/misc.js
  47. +62 −0 ui/src/store/modules/user.js
  48. +165 −0 ui/src/theme/common_layout.less
  49. 0 ui/src/theme/dark/layout.less
  50. 0 ui/src/theme/dark/style.less
  51. 0 ui/src/theme/layout.less
  52. 0 ui/src/theme/light/layout.less
  53. +3 −0 ui/src/theme/light/style.less
  54. +8 −0 ui/src/theme/light/var.less
  55. +6 −0 ui/src/theme/style.less
  56. +15 −0 ui/src/utils/auth.js
  57. +63 −0 ui/src/utils/request.js
  58. +68 −0 ui/src/views/errorPage/page404.vue
  59. +103 −0 ui/src/views/home.vue
  60. +253 −0 ui/src/views/nav.vue
  61. +68 −0 ui/src/views/template.vue
  62. 0 ui/static/.gitkeep
@@ -1,6 +1,4 @@
<div align="center">
<br>
<img alt="DEV" src="https://thepracticaldev.s3.amazonaws.com/i/ro3538by3b2fupbs63sr.png" width="500px">
<h1>DEV Community 👩‍💻👨‍💻</h1>
</div>

@@ -28,7 +26,6 @@ Like many open source projects, we require that contributors provide us with a C

Our version of the CLA was adapted from the Microsoft Contributor License Agreement, which they generously made available to the public domain under Creative Commons CC0 1.0 Universal.

Any questions, please refer to our [license FAQ](https://docs.dev.to/licensing/) doc or email yo@dev.to

<br>

@@ -0,0 +1,62 @@
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/thinkindev/im.dev/internal"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "im.dev",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {
internal.Start("conf.yaml")
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.im.dev1.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
@@ -0,0 +1,12 @@
version: 0.1.0
log_level: debug

listen_addr: ":10086"

cql:
cluster:
- "10.77.64.46:9042"
- "10.77.64.47:9042"
- "10.77.64.48:9042"
connection_num: 10
keyspace: "im_dev"
@@ -0,0 +1,12 @@
package internal

import (
"github.com/labstack/echo"
"github.com/thinkindev/im.dev/internal/session"
)

func apiHandler(e *echo.Echo) {
// sign-in apis
e.POST("/web/signIn", session.SignIn)
e.POST("/web/signOut", session.SignOut)
}
@@ -0,0 +1,7 @@
package ecode

// account
const (
NotSignIn = 1001
NotSignInMsg = "user need sign in"
)
@@ -0,0 +1,69 @@
package internal

import (
"time"

"go.uber.org/zap"

"github.com/gocql/gocql"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/thinkindev/im.dev/internal/misc"
)

// Start web server for im.dev ui
func Start(confPath string) {
// init config
misc.InitConf(confPath)

// init logger
misc.InitLog(misc.Conf.LogLevel)

misc.Log.Info("config init ok", zap.Any("config", misc.Conf))

err := connectDatabase()
if err != nil {
if err != nil {
misc.Log.Fatal("connect to cql cluster error", zap.String("error", err.Error()))
}
}

e := echo.New()
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{Level: 5}))
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowHeaders: append([]string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, "token"}),
AllowCredentials: true,
}))

// route the request to corresponding api
apiHandler(e)

// start dashboard api server
e.Logger.Fatal(e.Start(misc.Conf.ListenAddr))
}

const (
// ConnectTimeout specify the timeout interval for connecting to cassandra
ConnectTimeout = 30
// ReconnectInterval specify the reconnect interval for connecting to cassandra
ReconnectInterval = 500
)

// connect to cassandra/scyllaDB cluster
func connectDatabase() error {
c := gocql.NewCluster(misc.Conf.CQL.Cluster...)
c.Timeout = ConnectTimeout * time.Second
c.ReconnectInterval = ReconnectInterval * time.Millisecond

c.Keyspace = misc.Conf.CQL.Keyspace
c.NumConns = misc.Conf.CQL.ConnectionNum

session, err := c.CreateSession()
if err != nil {
return err
}

misc.CQL = session
return nil
}
@@ -0,0 +1,36 @@
package misc

import (
"io/ioutil"
"log"

"gopkg.in/yaml.v2"
)

// Config for dashboardå
type Config struct {
Version string `yaml:"version"`
LogLevel string `yaml:"log_level"`
ListenAddr string `yaml:"listen_addr"`
CQL struct {
Cluster []string `yaml:"cluster"`
ConnectionNum int `yaml:"connection_num"`
Keyspace string
}
}

// InitConf init the config for dashboard
func InitConf(p string) {
conf := &Config{}
data, err := ioutil.ReadFile(p)
if err != nil {
log.Fatal("load config from file error,", err)
}

err = yaml.Unmarshal(data, &conf)
if err != nil {
log.Fatal("decode config error,", err)
}

Conf = conf
}
@@ -0,0 +1,10 @@
package misc

// HTTPResp is the structure for http response
type HTTPResp struct {
ErrCode int `json:"err_code"`

Message string `json:"message"`

Data interface{} `json:"data"`
}
@@ -0,0 +1,40 @@
package misc

import (
"os"
"strings"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// InitLog the logger interface
func InitLog(level string) {
var lv zapcore.Level
switch strings.ToLower(level) {
case "debug":
lv = zap.DebugLevel
case "info":
lv = zap.InfoLevel
case "warn":
lv = zap.WarnLevel
case "error":
lv = zap.ErrorLevel
}

atom := zap.NewAtomicLevel()

// To keep the example deterministic, disable timestamps in the output.
encoderCfg := zap.NewProductionEncoderConfig()
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
l := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderCfg),

zapcore.Lock(os.Stdout),
atom,
), zap.AddCaller())

atom.SetLevel(lv)

Log = l
}
@@ -0,0 +1,20 @@
package misc

import (
"encoding/base64"

"github.com/gocql/gocql"
"go.uber.org/zap"
)

// Conf is the global var for config
var Conf *Config

// Log is the global var for log
var Log *zap.Logger

// Base64 is the base64 handler
var Base64 = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")

// CQL is the cql session for access cassandra
var CQL *gocql.Session
@@ -0,0 +1,55 @@
package session

import (
"net/http"
"strconv"
"sync"
"time"

"github.com/thinkindev/im.dev/internal/ecode"

"github.com/labstack/echo"
"github.com/thinkindev/im.dev/internal/misc"
)

// UserInfo contains user's info
type UserInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Token string `json:"token"`
}

var sessions = &sync.Map{}

// SignIn is user's sign-in action
func SignIn(c echo.Context) error {
ui := &UserInfo{"13269", "Sunface", "https://avatars2.githubusercontent.com/u/7036754?s=460&v=4", strconv.FormatInt(time.Now().UnixNano(), 10)}
sessions.Store(ui.Token, ui)
return c.JSON(http.StatusOK, misc.HTTPResp{
Data: ui,
})
}

// SignOut is user's sign-out action
func SignOut(c echo.Context) error {
token := c.Request().Header.Get("token")
sessions.Delete(token)
return c.JSON(http.StatusOK, misc.HTTPResp{})
}

// CheckSignIn checks whether user has signed in
func CheckSignIn(f echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
token := c.Request().Header.Get("token")
_, ok := sessions.Load(token)
if !ok {
return c.JSON(http.StatusUnauthorized, misc.HTTPResp{
ErrCode: ecode.NotSignIn,
Message: ecode.NotSignInMsg,
})
}

return f(c)
}
}
21 main.go
@@ -0,0 +1,21 @@
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import "github.com/thinkindev/im.dev/cmd"

func main() {
cmd.Execute()
}
@@ -0,0 +1,15 @@
CREATE KEYSPACE im_dev WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}
AND durable_writes = false;

USE im_dev;

CREATE TABLE IF NOT EXISTS user (
id text,
name text,
PRIMARY KEY (id)
) WITH gc_grace_seconds = 10800;

CREATE TABLE IF NOT EXISTS user_activity (
id text,
PRIMARY KEY (id)
) WITH gc_grace_seconds = 10800;

0 comments on commit 123f177

Please sign in to comment.
You can’t perform that action at this time.