Skip to content

Commit

Permalink
test: improve project coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Jun 12, 2022
1 parent 7be5c0f commit 599e9ab
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 76 deletions.
42 changes: 2 additions & 40 deletions apt-proxy.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,10 @@
package main

import (
"flag"

"github.com/soulteary/apt-proxy/cli"
"github.com/soulteary/apt-proxy/linux"
)

const (
DEFAULT_HOST = "0.0.0.0"
DEFAULT_PORT = "3142"
DEFAULT_CACHE_DIR = "./.aptcache"
DEFAULT_MIRROR = "" // "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/"
DEFAULT_TYPE = linux.LINUX_DISTROS_UBUNTU
DEFAULT_DEBUG = false
)

var version string

func parseFlags() (appFlags cli.AppFlags) {
var (
host string
port string
types string
)
flag.StringVar(&host, "host", DEFAULT_HOST, "the host to bind to")
flag.StringVar(&port, "port", DEFAULT_PORT, "the port to bind to")
flag.StringVar(&types, "type", DEFAULT_TYPE, "select the type of system to cache: ubuntu/debian")
flag.BoolVar(&appFlags.Debug, "debug", DEFAULT_DEBUG, "whether to output debugging logging")
flag.StringVar(&appFlags.CacheDir, "cachedir", DEFAULT_CACHE_DIR, "the dir to store cache data in")
flag.StringVar(&appFlags.Mirror, "mirror", DEFAULT_MIRROR, "the mirror for fetching packages")
flag.Parse()

if types != linux.LINUX_DISTROS_UBUNTU && types != linux.LINUX_DISTROS_DEBIAN {
types = linux.LINUX_DISTROS_UBUNTU
}

appFlags.Types = types
appFlags.Listen = host + ":" + port
appFlags.Version = version

return appFlags
}

func main() {
cli.Parse(parseFlags())
flags := cli.ParseFlags()
cli.Daemon(&flags)
}
66 changes: 32 additions & 34 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
package cli

import (
"log"
"net/http"
"flag"

"github.com/soulteary/apt-proxy/pkgs/httpcache"
"github.com/soulteary/apt-proxy/pkgs/httplog"
"github.com/soulteary/apt-proxy/proxy"
"github.com/soulteary/apt-proxy/linux"
)

type AppFlags struct {
Debug bool
Version string
CacheDir string
Mirror string
Types string
Listen string
}

func Parse(appFlags AppFlags) {
log.Printf("running apt-proxy %s", appFlags.Version)

if appFlags.Debug {
log.Printf("enable debug: true")
httpcache.DebugLogging = true
}
const (
DEFAULT_HOST = "0.0.0.0"
DEFAULT_PORT = "3142"
DEFAULT_CACHE_DIR = "./.aptcache"
DEFAULT_MIRROR = "" // "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/"
DEFAULT_TYPE = linux.LINUX_DISTROS_UBUNTU
DEFAULT_DEBUG = false
)

cache, err := httpcache.NewDiskCache(appFlags.CacheDir)
if err != nil {
log.Fatal(err)
var version string

func ParseFlags() (appFlags AppFlags) {
var (
host string
port string
types string
)
flag.StringVar(&host, "host", DEFAULT_HOST, "the host to bind to")
flag.StringVar(&port, "port", DEFAULT_PORT, "the port to bind to")
flag.StringVar(&types, "type", DEFAULT_TYPE, "select the type of system to cache: ubuntu/debian")
flag.BoolVar(&appFlags.Debug, "debug", DEFAULT_DEBUG, "whether to output debugging logging")
flag.StringVar(&appFlags.CacheDir, "cachedir", DEFAULT_CACHE_DIR, "the dir to store cache data in")
flag.StringVar(&appFlags.Mirror, "mirror", DEFAULT_MIRROR, "the mirror for fetching packages")
flag.Parse()

if types != linux.LINUX_DISTROS_UBUNTU && types != linux.LINUX_DISTROS_DEBIAN {
types = linux.LINUX_DISTROS_UBUNTU
}

ap := proxy.NewAptProxyFromDefaults(appFlags.Mirror, appFlags.Types)
ap.Handler = httpcache.NewHandler(cache, ap.Handler)

logger := httplog.NewResponseLogger(ap.Handler)
logger.DumpRequests = appFlags.Debug
logger.DumpResponses = appFlags.Debug
logger.DumpErrors = appFlags.Debug
ap.Handler = logger
appFlags.Types = types
appFlags.Listen = host + ":" + port
appFlags.Version = version

log.Printf("proxy listening on %s", appFlags.Listen)
log.Fatal(http.ListenAndServe(appFlags.Listen, ap))
return appFlags
}
4 changes: 2 additions & 2 deletions apt-proxy_test.go → cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
"os"
Expand All @@ -7,7 +7,7 @@ import (

func TestParseFlags(t *testing.T) {
os.Args = append(os.Args, "--type=not-support-os")
flags := parseFlags()
flags := ParseFlags()

if flags.Debug != DEFAULT_DEBUG {
t.Fatal("Default option `Debug` value mismatch")
Expand Down
65 changes: 65 additions & 0 deletions cli/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cli

import (
"log"
"net/http"

"github.com/soulteary/apt-proxy/pkgs/httpcache"
"github.com/soulteary/apt-proxy/pkgs/httplog"
"github.com/soulteary/apt-proxy/proxy"
)

type AppFlags struct {
Debug bool
Version string
CacheDir string
Mirror string
Types string
Listen string
}

func initStore(appFlags AppFlags) (cache httpcache.Cache, err error) {
cache, err = httpcache.NewDiskCache(appFlags.CacheDir)
if err != nil {
return cache, err
}
return cache, nil
}

func initProxy(appFlags AppFlags, cache httpcache.Cache) (ap *proxy.AptProxy) {
ap = proxy.NewAptProxyFromDefaults(appFlags.Mirror, appFlags.Types)
ap.Handler = httpcache.NewHandler(cache, ap.Handler)
return ap
}

func initLogger(appFlags AppFlags, ap *proxy.AptProxy) {
if appFlags.Debug {
log.Printf("enable debug: true")
httpcache.DebugLogging = true
}
logger := httplog.NewResponseLogger(ap.Handler)
logger.DumpRequests = appFlags.Debug
logger.DumpResponses = appFlags.Debug
logger.DumpErrors = appFlags.Debug
ap.Handler = logger
}

func StartServer(appFlags *AppFlags, ap *proxy.AptProxy) {
log.Printf("proxy listening on %s", appFlags.Listen)
log.Fatal(http.ListenAndServe(appFlags.Listen, ap))
}

func Daemon(appFlags *AppFlags) {
log.Printf("running apt-proxy %s", appFlags.Version)

cache, err := initStore(*appFlags)
if err != nil {
log.Fatal(err)
}

ap := initProxy(*appFlags, cache)

initLogger(*appFlags, ap)

StartServer(&*appFlags, ap)
}
17 changes: 17 additions & 0 deletions cli/daemon_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cli

import (
"testing"
)

func TestInit(t *testing.T) {
flags := ParseFlags()

cache, err := initStore(flags)
if err != nil {
t.Fatal("Init Store Failed")
}

ap := initProxy(flags, cache)
initLogger(flags, ap)
}

0 comments on commit 599e9ab

Please sign in to comment.