Skip to content

Commit

Permalink
Merged in d3d/add-api-server (pull request #4)
Browse files Browse the repository at this point in the history
Add api server

Approved-by: Yo-An Lin <[email protected]>
  • Loading branch information
chechiachang authored and c9s committed May 31, 2018
2 parents 93dfded + a5edf68 commit 29fc45d
Show file tree
Hide file tree
Showing 14 changed files with 444 additions and 8 deletions.
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# go tool paths
GO_COVER := $(shell which cover)
GO_COBERTURA := $(shell which gocover-cobertura)
GO_VENDOR := $(shell which govendor)

GO_FILES := $(shell find src -type f -iname "*.go")

CONFIG_FILE = config/k8s.json

SHELL := /bin/bash

GCP_DOCKER_REGISTRY = asia.gcr.io

all:

test:

deps:
go get ./...

vortex:
go build ./src/cmd/vortex

run: vortex
./vortex -config config/local.json -port 7890

image:
docker build -f dockerfiles/Dockerfile .
51 changes: 43 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
Votex
Vortex
===

Vortex is a networking development platform based on Linker Aurora.
# Package sharing between Aurora

[TOC]
Define what package can and can't be shared between aurora, vortex, and 5g-vortex. Since the ownership of vortex source code will be transfered to ITRI or open source.

# TODOs
Stay private:
1. Core aurora service packages like jobserver, jobupdater, ...
2. Kubernetes yamls
3. All aurora API (handler files) contain business logic

Check [Our dropbox paper](https://paper.dropbox.com/doc/5G-NFV-Information-Architecture-WIGrgN3OhdjGwEHkTRFmZ) for more details.
Shared packages (will go public)
1. Interface package of public tools like DBs (mongo, influxdb, redis...), logger, json

# Installer
# Vortex server

Vortex comes with an ansible playbook to install Vortex platform to server(s).
Vortex share the same config and dependent services with aurora. Make sure dependent services are available before start vortex server.

For information about installer, check *installer/README.md*
- MongoDB
- InfluxDB
- Redis
- Gearmand

### GoBuild

Build
```
make deps vortex
```

Run
```
make run
```

### Docker build

```
make image
```

### Test vortex image

1. Start dependent services like mongo or influxdb
2. Use docker run with host network

```
docker run -it --network=host asia.gcr.io/linker-aurora/vortex:<git-branch> bash
// example
docker run -it --network=host asia.gcr.io/linker-aurora/vortex:develop bash
```
52 changes: 52 additions & 0 deletions config/k8s.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"redis": {
"host": "redis.default",
"port": 6379,
"pool": {
"maxIdle": 100,
"_": "do not limit maxActive connection",
"maxActive": 0,
"idleTimeout": 60
},
"public": {
"host": "35.189.177.37",
"port": 32199
}
},
"gearman": {
"host": "gearman-job-server.default",
"port": 4730,
"public": {
"host": "gearman-job-server.default",
"port": 4730
}
},
"mongo": {
"url": "mongodb://mongo-0.mongo:27017/aurora",
"public": {
"url": "mongodb://35.189.177.37:31717/aurora"
}
},
"influxdb": {
"url": "http://influxdb.default:8086",
"database": "lisa"
},
"logger":{
"dir": "./logs",
"level": "debug",
"maxAge": "720h",
"suffixPattern": ".%Y%m%d",
"linkName": "access_log"
},
"app": {
"brand": {
"name": "Aurora",
"identifier": "aurora",
"companyName": "Linker Networks Inc."
},
"enableAuthentication": false,
"dbVersion": "v1.0.0",
"version": "<GIT_COMMIT>",
"logFileName": "access_log"
}
}
40 changes: 40 additions & 0 deletions config/local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"redis": {
"host": "localhost",
"port": 6379,
"public": {
"host": "localhost"
}
},
"gearman": {
"host": "localhost",
"port": 4730,
"public": {
"host": "localhost"
}
},
"mongo": {
"url": "mongodb://localhost:27017/aurora"
},
"influxdb": {
"url": "http://localhost:8086",
"database": "lisa"
},
"logger":{
"dir": "./logs",
"level": "debug",
"maxAge": "720h",
"suffixPattern": ".%Y%m%d",
"linkName": "access_log"
},
"app": {
"brand": {
"name": "Aurora",
"identifier": "aurora",
"companyName": "Linker Networks Inc."
},
"dbVersion": "v1.0.0",
"version": "local",
"logFileName": "access_log"
}
}
44 changes: 44 additions & 0 deletions dockerfiles/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Build stage
FROM golang:1.10-alpine3.7
MAINTAINER David Chang <[email protected]>
# TODO fix path after move out
WORKDIR /go/src/bitbucket.org/linkernetworks/vortex

RUN apk add --no-cache protobuf ca-certificates git

COPY config /cv-tracker/config
# TODO fix path after move out
COPY . /go/src/bitbucket.org/linkernetworks/vortex/src
COPY Makefile /go/src/bitbucket.org/linkernetworks/vortex

ENV CGO_ENABLED 0
ENV GOOS linux
ENV TIMEZONE "Asia/Shanghai"
ARG BUILD_MODE=release
ARG BRAND=aurora
RUN apk add --no-cache tzdata && \
cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && \
echo $TIMEZONE > /etc/timezone && \
apk del tzdata
# TODO test go install
RUN go install -tags "${BUILD_MODE} ${BRAND}" ./src/cmd/...

# the final image: vortex
FROM alpine:3.7
RUN apk add --no-cache ca-certificates rsync
WORKDIR /vortex

# copy the go binaries from the build image
COPY --from=0 /go/bin /go/bin

# copy the config files from the current working dir
COPY config /vortex/config

ARG BRAND=aurora

# select the config file for deployment
ARG CONFIG=config/k8s-develop.json
COPY ${CONFIG} config/k8s.json

EXPOSE 7890
ENTRYPOINT ["/go/bin/vortex", "-port", "7890", "-config", "/vortex/config/k8s.json"]
21 changes: 21 additions & 0 deletions src/cmd/vortex/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"bitbucket.org/linkernetworks/vortex/src/server"
"flag"
)

func main() {
var (
configPath string
host string
port string
)

flag.StringVar(&configPath, "config", "config/local.json", "config file path")
flag.StringVar(&host, "host", "0.0.0.0", "hostname")
flag.StringVar(&port, "port", "7890", "port")

a := server.App{}
a.LoadConfig(configPath).Start(host, port)
}
6 changes: 6 additions & 0 deletions src/entity/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package entity

type Response struct {
Status string `json:"status"`
Info interface{} `json:"info"`
}
43 changes: 43 additions & 0 deletions src/server/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package server

import (
"bitbucket.org/linkernetworks/vortex/src/serviceprovider"
"github.com/linkernetworks/config"
"github.com/linkernetworks/logger"
"log"
"net"
"net/http"
)

type App struct {
Config config.Config
ServiceProvider *serviceprovider.Container
}

// LoadConfig consumes a string of path to the json config file and read config file into Config.
func (a *App) LoadConfig(configPath string) *App {
if configPath == "" {
log.Fatal("-config option is required.")
}

a.Config = config.MustRead(configPath)
return a
}

// Start consumes two strings, host and port, invoke service initilization and serve on desired host:port
func (a *App) Start(host, port string) error {

a.InitilizeService()

bind := net.JoinHostPort(host, port)
logger.Debugf("Starting LISA on host: %s port: %s", host, port)

return http.ListenAndServe(bind, a.AppRoute())
}

// InitilizeService weavering services with global variables inside server package
func (a *App) InitilizeService() {
logger.Setup(a.Config.Logger)

a.ServiceProvider = serviceprovider.New(a.Config)
}
16 changes: 16 additions & 0 deletions src/server/autocert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package server

import (
"golang.org/x/crypto/acme/autocert"
)

// @see https://github.com/golang/crypto/blob/master/acme/autocert/autocert.go
// @see https://www.captaincodeman.com/2017/05/07/automatic-https-with-free-ssl-certificates-using-go-lets-encrypt
func NewLocalAutoCertManager(domains []string) *autocert.Manager {
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(domains...), //your domain here
Cache: autocert.DirCache("certs"), // folder for storing certificates
}
return &certManager
}
11 changes: 11 additions & 0 deletions src/server/handler_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package server

import (
"bitbucket.org/linkernetworks/vortex/src/serviceprovider"
"github.com/emicklei/go-restful"
)

func VersionHandler(sp *serviceprovider.Container) restful.RouteFunction {
return func(req *restful.Request, resp *restful.Response) {
}
}
41 changes: 41 additions & 0 deletions src/server/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package server

import (
"github.com/emicklei/go-restful"
"github.com/linkernetworks/logger"
oauth "github.com/linkernetworks/oauth/entity"
)

type ActionResponse struct {
Error bool `json:"error"`
Message string `json:"message"`
}

type SignInResponse struct {
Error bool `json:"error"`
AuthRequired bool `json:"authenRequired,omitempty"`
Message string `json:"message"`
SignInUrl string `json:"signInUrl,omitempty"`
Session SessionResponse `json:"session,omitempty"`
}

type SessionResponse struct {
ID string `json:"id,omitempty"`
Token string `json:"token,omitempty"`
ExpiredAt int64 `json:"expiredAt,omitempty"`
CurrentUser oauth.User `json:"currentUser,omitempty"`
}

func WriteResponse(r *restful.Response, httpStatus int, res ActionResponse) error {
return r.WriteHeaderAndEntity(httpStatus, res)
}

func responseErrorWithStatus(r *restful.Response, httpStatus int, msg string) error {
logger.Error(msg)
return r.WriteHeaderAndEntity(
httpStatus,
ActionResponse{
Error: true,
Message: msg,
})
}
Loading

0 comments on commit 29fc45d

Please sign in to comment.