forked from lox/apt-proxy
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: optimize the program, improve the coverage
- Loading branch information
Showing
5 changed files
with
112 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
.aptcache | ||
cachedata | ||
apt-proxy | ||
last-cid | ||
coverage.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,48 @@ | ||
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.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.UBUNTU && types != linux.DEBIAN { | ||
types = linux.UBUNTU | ||
} | ||
|
||
appFlags.Types = types | ||
appFlags.Listen = host + ":" + port | ||
appFlags.Version = version | ||
|
||
return appFlags | ||
} | ||
|
||
func main() { | ||
cli.Parse() | ||
cli.Parse(parseFlags()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestParseFlags(t *testing.T) { | ||
flags := parseFlags() | ||
|
||
if flags.Debug != DEFAULT_DEBUG { | ||
t.Fatal("Default option `Debug` value mismatch") | ||
} | ||
|
||
if flags.Listen != (DEFAULT_HOST + ":" + DEFAULT_PORT) { | ||
t.Fatal("Default option `Listen` value mismatch") | ||
} | ||
|
||
if flags.Types != DEFAULT_TYPE { | ||
t.Fatal("Default option `Types` value mismatch") | ||
} | ||
|
||
if flags.Mirror != DEFAULT_MIRROR { | ||
t.Fatal("Default option `Mirror` value mismatch") | ||
} | ||
|
||
if flags.CacheDir != DEFAULT_CACHE_DIR { | ||
t.Fatal("Default option `CacheDir` value mismatch") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,45 @@ | ||
package cli | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/soulteary/apt-proxy/linux" | ||
"github.com/soulteary/apt-proxy/pkgs/httpcache" | ||
"github.com/soulteary/apt-proxy/pkgs/httplog" | ||
"github.com/soulteary/apt-proxy/proxy" | ||
) | ||
|
||
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.UBUNTU | ||
DEFAULT_DEBUG = false | ||
) | ||
|
||
var ( | ||
version string | ||
listen string | ||
mirror string | ||
types string | ||
cacheDir string | ||
debug bool | ||
) | ||
|
||
func init() { | ||
var ( | ||
host string | ||
port string | ||
) | ||
flag.StringVar(&host, "host", DEFAULT_HOST, "the host to bind to") | ||
flag.StringVar(&port, "port", DEFAULT_PORT, "the port to bind to") | ||
flag.BoolVar(&debug, "debug", DEFAULT_DEBUG, "whether to output debugging logging") | ||
flag.StringVar(&mirror, "mirror", DEFAULT_MIRROR, "the mirror for fetching packages") | ||
flag.StringVar(&types, "type", DEFAULT_TYPE, "select the type of system to cache: ubuntu/debian") | ||
flag.StringVar(&cacheDir, "cachedir", DEFAULT_CACHE_DIR, "the dir to store cache data in") | ||
flag.Parse() | ||
|
||
if types != linux.UBUNTU && types != linux.DEBIAN { | ||
types = linux.UBUNTU | ||
} | ||
|
||
listen = host + ":" + port | ||
type AppFlags struct { | ||
Debug bool | ||
Version string | ||
CacheDir string | ||
Mirror string | ||
Types string | ||
Listen string | ||
} | ||
|
||
func Parse() { | ||
|
||
log.Printf("running apt-proxy %s", version) | ||
func Parse(appFlags AppFlags) { | ||
log.Printf("running apt-proxy %s", appFlags.Version) | ||
|
||
if debug { | ||
if appFlags.Debug { | ||
log.Printf("enable debug: true") | ||
httpcache.DebugLogging = true | ||
} | ||
|
||
cache, err := httpcache.NewDiskCache(cacheDir) | ||
cache, err := httpcache.NewDiskCache(appFlags.CacheDir) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
ap := proxy.NewAptProxyFromDefaults(mirror, types) | ||
ap := proxy.NewAptProxyFromDefaults(appFlags.Mirror, appFlags.Types) | ||
ap.Handler = httpcache.NewHandler(cache, ap.Handler) | ||
|
||
logger := httplog.NewResponseLogger(ap.Handler) | ||
logger.DumpRequests = debug | ||
logger.DumpResponses = debug | ||
logger.DumpErrors = debug | ||
logger.DumpRequests = appFlags.Debug | ||
logger.DumpResponses = appFlags.Debug | ||
logger.DumpErrors = appFlags.Debug | ||
ap.Handler = logger | ||
|
||
log.Printf("proxy listening on %s", listen) | ||
log.Fatal(http.ListenAndServe(listen, ap)) | ||
log.Printf("proxy listening on %s", appFlags.Listen) | ||
log.Fatal(http.ListenAndServe(appFlags.Listen, ap)) | ||
} |