mirror of
https://github.com/foomo/contentserver.git
synced 2025-10-16 12:25:44 +00:00
fixed initialization and moved content_server
This commit is contained in:
parent
985e0d8e5f
commit
dc8619e131
70
content_server.go
Normal file
70
content_server.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"github.com/foomo/contentserver/server"
|
||||||
|
"github.com/foomo/contentserver/server/log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
PROTOCOL_TCP = "tcp"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ExitCode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
EXIT_CODE_OK = 0
|
||||||
|
EXIT_CODE_INSUFFICIENT_ARGS = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
var contentServer string
|
||||||
|
|
||||||
|
var uniqushPushVersion = "content-server 1.2.0"
|
||||||
|
|
||||||
|
var showVersionFlag = flag.Bool("version", false, "Version info")
|
||||||
|
var protocol = flag.String("protocol", PROTOCOL_TCP, "what protocol to server for")
|
||||||
|
var address = flag.String("address", "127.0.0.1:8081", "address to bind host:port")
|
||||||
|
var varDir = flag.String("vardir", "127.0.0.1:8081", "where to put my data")
|
||||||
|
var logLevelOptions = []string{
|
||||||
|
log.LOG_LEVEL_NAME_ERROR,
|
||||||
|
log.LOG_LEVEL_NAME_RECORD,
|
||||||
|
log.LOG_LEVEL_NAME_WARNING,
|
||||||
|
log.LOG_LEVEL_NAME_NOTICE,
|
||||||
|
log.LOG_LEVEL_NAME_DEBUG}
|
||||||
|
|
||||||
|
var logLevel = flag.String(
|
||||||
|
"logLevel",
|
||||||
|
log.LOG_LEVEL_NAME_RECORD,
|
||||||
|
fmt.Sprintf(
|
||||||
|
"one of %s",
|
||||||
|
strings.Join(logLevelOptions, ", ")))
|
||||||
|
|
||||||
|
func exitUsage(code int) {
|
||||||
|
fmt.Printf("Usage: %s http(s)://your-content-server/path/to/content.json\n", os.Args[0])
|
||||||
|
flag.PrintDefaults()
|
||||||
|
os.Exit(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
if *showVersionFlag {
|
||||||
|
fmt.Printf("%v\n", uniqushPushVersion)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(flag.Args()) == 1 {
|
||||||
|
fmt.Println(*protocol, *address, flag.Arg(0))
|
||||||
|
log.SetLogLevel(log.GetLogLevelByName(*logLevel))
|
||||||
|
switch *protocol {
|
||||||
|
case PROTOCOL_TCP:
|
||||||
|
server.RunSocketServer(flag.Arg(0), *address, *varDir)
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
exitUsage(EXIT_CODE_INSUFFICIENT_ARGS)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
exitUsage(EXIT_CODE_INSUFFICIENT_ARGS)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -147,7 +147,6 @@ func handleConnection(conn net.Conn) {
|
|||||||
func RunSocketServer(server string, address string, varDir string) {
|
func RunSocketServer(server string, address string, varDir string) {
|
||||||
log.Record("building repo with content from " + server)
|
log.Record("building repo with content from " + server)
|
||||||
contentRepo = repo.NewRepo(server, varDir)
|
contentRepo = repo.NewRepo(server, varDir)
|
||||||
contentRepo.Update()
|
|
||||||
ln, err := net.Listen("tcp", address)
|
ln, err := net.Listen("tcp", address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// failed to create socket
|
// failed to create socket
|
||||||
@ -155,6 +154,9 @@ func RunSocketServer(server string, address string, varDir string) {
|
|||||||
} else {
|
} else {
|
||||||
// there we go
|
// there we go
|
||||||
log.Record("RunSocketServer: started to listen on " + address)
|
log.Record("RunSocketServer: started to listen on " + address)
|
||||||
|
if len(contentRepo.Directory) == 0 {
|
||||||
|
contentRepo.Update()
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
conn, err := ln.Accept() // this blocks until connection or error
|
conn, err := ln.Accept() // this blocks until connection or error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user